MIMPC++
A fast typesafe Mixed Integer MPC C++ library
Solver.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <eigen3/Eigen/Dense>
12 #include "System.hpp"
13 
14 namespace mimpc {
22  OPTIMAL = 1,
34  USER_INTERRUPT = -10
35  };
36 
40  enum COST_TYPE {
44  L1Linear = 0,
48  L2Quadratic = 1
49  };
50 
62  BACKWARD_EULER = 1
63  };
64 
81  template<class SystemType, int N, int min_steps_on, int min_steps_off, int max_steps_on, int num_steps_solver_delay, INTEGRATION_SCHEME integration_scheme> requires
82  std::derived_from<SystemType, System<SystemType::NUM_STATES, SystemType::NUM_CONT_INPUTS, SystemType::NUM_BIN_INPUTS>>
83  class Solver {
84  public:
85  using SYSTEM_TYPE = SystemType;
86  static constexpr auto PRED_STEPS = N;
87  static constexpr auto MIN_STEPS_ON = min_steps_on;
88  static constexpr auto MIN_STEPS_OFF = min_steps_off;
89  static constexpr auto MAX_STEPS_ON = max_steps_on;
90  static constexpr auto NUM_STEPS_SOLVER_DELAY = num_steps_solver_delay;
91  static constexpr auto INTEGRATION_SCHEME = integration_scheme;
92 
96  static constexpr int history_depth = std::max(
97  {0, max_steps_on - num_steps_solver_delay, min_steps_off - num_steps_solver_delay,
98  min_steps_on - num_steps_solver_delay}); //retunrs 0 or biggest of thoose
99  virtual ~Solver() = default;
100 
108  virtual void addInputConstraintOnIndex(int index, double lb, double ub) = 0;
109 
117  virtual void addStateConstraintOnIndex(int index, double lb, double ub) = 0;
118 
126  virtual void
127  addInputConstraintOnStep(int step, const SystemType::InputVec &lb, const SystemType::InputVec &ub) = 0;
128 
136  virtual void
137  addStateConstraintOnStep(int step, const SystemType::StateVec &lb, const SystemType::StateVec &ub) = 0;
138 
144  virtual void setFinalWeights(const SystemType::StateVec &final_weights) = 0;
145 
151  virtual void setStateWeights(const SystemType::StateVec &state_weights) = 0;
152 
158  virtual void setInputWeights(const SystemType::InputVec &state_weights) = 0;
159 
165  virtual void setCost(COST_TYPE cost_type) = 0;
166 
173  virtual void setSetPoint(const SystemType::StateVec &set_point) = 0;
174 
180  virtual void setState(const SystemType::StateVec &state) = 0;
181 
188  virtual void setInputHistory(
189  const Eigen::Matrix<double, SystemType::NUM_INPUTS, history_depth> &bin_input_hist) = 0;
190 
196  virtual void
197  setNextInputs(const Eigen::Matrix<double, SystemType::NUM_INPUTS, num_steps_solver_delay> &next_inputs) = 0;
198 
203  virtual void setSolverTimeLimit(double max_seconds) = 0;
204 
209  virtual unsigned int getStepsToCompensateControllerDelay() = 0;
210 
221  virtual SOLVER_RETURN
222  solve(const Eigen::Matrix<double, SystemType::NUM_INPUTS, N, Eigen::RowMajor> &last_open_loop_input,
223  const Eigen::Matrix<double, SystemType::NUM_STATES, N + 1, Eigen::RowMajor> &last_open_loop_state,
224  Eigen::Matrix<double, SystemType::NUM_INPUTS, N, Eigen::RowMajor> &open_loop_input,
225  Eigen::Matrix<double, SystemType::NUM_STATES, N + 1, Eigen::RowMajor> &open_loop_state) const = 0;
226  };
227 };
INTEGRATION_SCHEME
Definition: Solver.hpp:54
@ BACKWARD_EULER
Definition: Solver.hpp:62
@ FORWARD_EULER
Definition: Solver.hpp:58
SOLVER_RETURN
Definition: Solver.hpp:18
@ TIME_LIMIT_WITH_SOLUTION
Definition: Solver.hpp:30
@ NO_SOLUTION
Definition: Solver.hpp:26
@ USER_INTERRUPT
Definition: Solver.hpp:34
@ OPTIMAL
Definition: Solver.hpp:22
COST_TYPE
Definition: Solver.hpp:40
@ L2Quadratic
Definition: Solver.hpp:48
@ L1Linear
Definition: Solver.hpp:44
Abstract class to represent system dynamics.
Definition: Solver.hpp:83
virtual void addInputConstraintOnIndex(int index, double lb, double ub)=0
virtual void addInputConstraintOnStep(int step, const SystemType::InputVec &lb, const SystemType::InputVec &ub)=0
static constexpr int history_depth
Definition: Solver.hpp:96
virtual void addStateConstraintOnStep(int step, const SystemType::StateVec &lb, const SystemType::StateVec &ub)=0
virtual void setState(const SystemType::StateVec &state)=0
virtual unsigned int getStepsToCompensateControllerDelay()=0
virtual void setInputWeights(const SystemType::InputVec &state_weights)=0
virtual void setStateWeights(const SystemType::StateVec &state_weights)=0
virtual void setFinalWeights(const SystemType::StateVec &final_weights)=0
virtual void addStateConstraintOnIndex(int index, double lb, double ub)=0
virtual void setCost(COST_TYPE cost_type)=0
virtual SOLVER_RETURN solve(const Eigen::Matrix< double, SystemType::NUM_INPUTS, N, Eigen::RowMajor > &last_open_loop_input, const Eigen::Matrix< double, SystemType::NUM_STATES, N+1, Eigen::RowMajor > &last_open_loop_state, Eigen::Matrix< double, SystemType::NUM_INPUTS, N, Eigen::RowMajor > &open_loop_input, Eigen::Matrix< double, SystemType::NUM_STATES, N+1, Eigen::RowMajor > &open_loop_state) const =0
virtual void setSolverTimeLimit(double max_seconds)=0
virtual void setNextInputs(const Eigen::Matrix< double, SystemType::NUM_INPUTS, num_steps_solver_delay > &next_inputs)=0
virtual void setSetPoint(const SystemType::StateVec &set_point)=0
virtual void setInputHistory(const Eigen::Matrix< double, SystemType::NUM_INPUTS, history_depth > &bin_input_hist)=0