MIMPC++
A fast typesafe Mixed Integer MPC C++ library
SCIPSolver.hpp
1 #pragma once
2 
3 #include <scip/scip.h>
4 #include <scip/scipdefplugins.h>
5 #include <array>
6 #include <type_traits>
7 #include <concepts>
8 
9 #include "../Solver.hpp"
10 
11 namespace mimpc {
12 
16  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
17  std::derived_from<SystemType, System<SystemType::NUM_STATES, SystemType::NUM_CONT_INPUTS, SystemType::NUM_BIN_INPUTS>>
18  class SCIPSolver
19  : public Solver<SystemType, N, min_steps_on, min_steps_off, max_steps_on, num_steps_solver_delay, integration_scheme> {
20  public:
22  private:
23  SCIP *scip_problem_;
24  std::array<std::array<SCIP_VAR *, N + 1>, SystemType::NUM_STATES> scip_state_vars_;
25  std::array<std::array<SCIP_VAR *, N>, SystemType::NUM_INPUTS> scip_input_vars_;
26  std::array<std::array<SCIP_CONS *, SystemType::NUM_STATES>, N> scip_dynamic_const_;
27  std::array<std::array<SCIP_CONS *,
29  max_steps_on>, SystemType::NUM_BIN_INPUTS> scip_pattern_timing_max_const_;
30  std::array<std::array<SCIP_CONS *, 1 + N - 2>, SystemType::NUM_BIN_INPUTS> scip_pattern_timing_min_const_;
31 
32  std::array<std::array<SCIP_VAR *, N + 1>, SystemType::NUM_STATES> scip_T_state_;
33  std::array<std::array<SCIP_VAR *, N>, SystemType::NUM_INPUTS> scip_T_input_;
34  std::array<std::array<SCIP_CONS *, N + 1>, SystemType::NUM_STATES> scip_Tconsp_state_;
35  std::array<std::array<SCIP_CONS *, N + 1>, SystemType::NUM_STATES> scip_Tconsm_state_;
36  std::array<std::array<SCIP_CONS *, N>, SystemType::NUM_INPUTS> scip_Tconsp_input_;
37  std::array<std::array<SCIP_CONS *, N>, SystemType::NUM_INPUTS> scip_Tconsm_input_;
38 
39 
40  std::array<std::array<SCIP_VAR *, N + 1>, SystemType::NUM_STATES> scip_QuadCostErr_states_;
41  std::array<std::array<SCIP_CONS *, N + 1>, SystemType::NUM_STATES> scip_QuadCostErrCons_states_;
42 
43  SCIP_CONS *scip_QuadCostCons_;
44  SCIP_Var *scip_QuadCostSum_;
45 
46  COST_TYPE cost_type_;
47  bool compensate_controller_delay_;
48 
49  const SystemType &system_;
50  double system_dt_;
51  Eigen::Vector<double, SystemType::NUM_STATES> state_;
52  Eigen::Vector<double, SystemType::NUM_STATES> state_cost_weights_;
53  Eigen::Vector<double, SystemType::NUM_STATES> final_state_cost_weights_;
54  Eigen::Vector<double, SystemType::NUM_INPUTS> input_cost_weights_;
55  Eigen::Vector<double, SystemType::NUM_STATES> set_point_;
56 
57  void setCost(COST_TYPE cost_type) override;
58 
59  public:
60 
71  SCIPSolver(const typename SystemType::StateVec &state_weights,
72  const typename SystemType::StateVec &final_weights,
73  const typename SystemType::InputVec &input_weights, const typename SystemType::StateVec &set_point,
74  COST_TYPE cost_type,
75  const SystemType &system, double system_dt);
76 
77  virtual ~SCIPSolver();
78 
79  void addInputConstraintOnIndex(int index, double lb, double ub) override;
80 
81  void addStateConstraintOnIndex(int index, double lb, double ub) override;
82 
83  void addInputConstraintOnStep(int step, const typename SystemType::InputVec &lb,
84  const typename SystemType::InputVec &ub) override;
85 
86  void addStateConstraintOnStep(int step, const typename SystemType::StateVec &lb,
87  const typename SystemType::StateVec &ub) override;
88 
89  void setFinalWeights(const typename SystemType::StateVec &final_weights) override;
90 
91  void setStateWeights(const typename SystemType::StateVec &state_weights) override;
92 
93  void setInputWeights(const typename SystemType::InputVec &state_weights) override;
94 
95  void setSetPoint(const typename SystemType::StateVec &set_point) override;
96 
97  void setState(const typename SystemType::StateVec &state) override;
98 
99  void setInputHistory(
100  const typename Eigen::Matrix<double, SystemType::NUM_INPUTS, SolverBase::history_depth> &bin_input_hist) override;
101 
102  void setNextInputs(const typename Eigen::Matrix<double,
103  SystemType::NUM_INPUTS, num_steps_solver_delay> &next_inputs) override;
104 
105  void setSolverTimeLimit(double max_seconds) override;
106 
108  solve(const typename Eigen::Matrix<double, SystemType::NUM_INPUTS, N, Eigen::RowMajor> &last_open_loop_input,
109  const typename Eigen::Matrix<double, SystemType::NUM_STATES,
110  N + 1, Eigen::RowMajor> &last_open_loop_state,
111  typename Eigen::Matrix<double, SystemType::NUM_INPUTS, N, Eigen::RowMajor> &open_loop_input,
112  typename Eigen::Matrix<double, SystemType::NUM_STATES,
113  N + 1, Eigen::RowMajor> &open_loop_state) const override;
114 
115  unsigned int getStepsToCompensateControllerDelay() override;
116 
117  void writeProblemToFile(const std::string & file_path);
118  };
119 };
120 
121 #include "SCIPSolver.tpp"
SOLVER_RETURN
Definition: Solver.hpp:18
COST_TYPE
Definition: Solver.hpp:40
Definition: SCIPSolver.hpp:19
void setSolverTimeLimit(double max_seconds) override
void addStateConstraintOnIndex(int index, double lb, double ub) override
unsigned int getStepsToCompensateControllerDelay() override
void addInputConstraintOnIndex(int index, double lb, double ub) override
SCIPSolver(const typename SystemType::StateVec &state_weights, const typename SystemType::StateVec &final_weights, const typename SystemType::InputVec &input_weights, const typename SystemType::StateVec &set_point, COST_TYPE cost_type, const SystemType &system, double system_dt)
Definition: Solver.hpp:83
static constexpr int history_depth
Definition: Solver.hpp:96