feasiblesqpmethod.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  * Copyright (C) 2022-2023 David Kiessling
9  *
10  * CasADi is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * CasADi is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with CasADi; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 #ifndef CASADI_FEASIBLESQPMETHOD_HPP
28 #define CASADI_FEASIBLESQPMETHOD_HPP
29 
30 #include "casadi/core/nlpsol_impl.hpp"
31 #include <casadi/solvers/casadi_nlpsol_feasiblesqpmethod_export.h>
32 
42 namespace casadi {
43 
44  struct FeasiblesqpmethodMemory : public NlpsolMemory {
45  // Problem data structure
48  double reg;
49 
51  double sigma;
52 
53  casadi_int merit_ind;
55  const char* return_status;
56 
58  int iter_count;
59  };
60 
65  class Feasiblesqpmethod : public Nlpsol {
66  public:
67  explicit Feasiblesqpmethod(const std::string& name, const Function& nlp);
68  ~Feasiblesqpmethod() override;
69 
70  // Get name of the plugin
71  const char* plugin_name() const override { return "feasiblesqpmethod";}
72 
73  // Name of the class
74  std::string class_name() const override { return "Feasiblesqpmethod";}
75 
77  static Nlpsol* creator(const std::string& name, const Function& nlp) {
78  return new Feasiblesqpmethod(name, nlp);
79  }
80 
82 
83  static const Options options_;
84  const Options& get_options() const override { return options_;}
86 
88  Dict get_stats(void* mem) const override;
89 
90  // Initialize the solver
91  void init(const Dict& opts) override;
92 
94  void* alloc_mem() const override { return new FeasiblesqpmethodMemory();}
95 
97  int init_mem(void* mem) const override;
98 
100  void free_mem(void *mem) const override { delete static_cast<FeasiblesqpmethodMemory*>(mem);}
101 
103  void set_work(void* mem, const double**& arg, double**& res,
104  casadi_int*& iw, double*& w) const override;
105 
106  double eval_m_k(void* mem) const;
107 
108  double eval_tr_ratio(double val_f, double val_f_corr, double val_m_k) const;
109 
110  void tr_update(void* mem, double& tr_rad, double tr_ratio) const;
111 
112  int step_update(void* mem, double tr_ratio) const;
113 
114  // function to get feasible iterate
115  int feasibility_iterations(void* mem, double tr_rad) const;
116 
117  void anderson_acc_step_update(void* mem, casadi_int iter_index) const;
118 
119  void anderson_acc_init_memory(void* mem, double* step, double* iterate) const;
120 
121  void anderson_acc_update_memory(void* mem, double* step, double* iterate) const;
122 
123  // Solve the NLP
124  int solve(void* mem) const override;
125 
126  // Memory structure
128 
130  Function qpsol_;
131 
133  Function qpsol_ela_;
134 
136  bool exact_hessian_;
137 
139  bool use_sqp_;
140 
142  bool use_anderson_;
143 
145  casadi_int block_size_ = 0;
146 
148  casadi_int max_iter_, min_iter_;
149 
151  casadi_int lbfgs_memory_;
152 
153  // Memory size of Anderson acceleration
154  casadi_int sz_anderson_memory_;
155 
157  double tol_pr_, tol_du_;
158 
160  bool init_feasible_;
161 
164  // double c1_;
165  // double beta_;
166  // casadi_int max_iter_ls_;
167  // casadi_int merit_memsize_;
169 
170  // Print options
171  bool print_header_, print_iteration_, print_status_;
172 
173  // Hessian Sparsity
174  Sparsity Hsp_;
175 
176  // Jacobian sparsity
177  Sparsity Asp_;
178 
180  ConvexifyData convexify_data_;
181 
183  bool convexify_;
184 
185  // -------- FROM HERE OPTIONS FOR FP-SQP ------------------
186 
187  // tolerances
188  double optim_tol_, feas_tol_;
189 
190  // trust-region parameters
191  double tr_eta1_, tr_eta2_;
192  double tr_alpha1_, tr_alpha2_;
193  double tr_tol_;
194  double tr_rad0_;
195  double tr_acceptance_;
196  double tr_rad_min_, tr_rad_max_;
197 
198  std::vector<double> tr_scale_vector_;
199 
200  // inner iterations
201  double contraction_acceptance_value_;
202  casadi_int watchdog_;
203  casadi_int max_inner_iter_;
204 
205 
207  void codegen_body(CodeGenerator& g) const override;
208 
210  void codegen_declarations(CodeGenerator& g) const override;
211 
213  const Function getConic() const { return qpsol_;}
214 
216  void print_iteration() const;
217 
219  void print_iteration(casadi_int iter, double obj, double m_k,
220  double tr_ratio, double pr_inf, double du_inf,
221  double dx_norm, double rg, double tr_rad,
222  std::string info) const;
223 
224  // Solve the QP subproblem: mode 0 = normal, mode 1 = SOC
225  virtual int solve_QP(FeasiblesqpmethodMemory* m, const double* H, const double* g,
226  const double* lbdz, const double* ubdz,
227  const double* A,
228  double* x_opt, double* dlam, int mode) const;
229 
230  // Solve the LP
231  virtual int solve_LP(FeasiblesqpmethodMemory* m, const double* g,
232  const double* lbdz, const double* ubdz,
233  const double* A,
234  double* x_opt, double* dlam, int mode) const;
235 
236  // Solve the QP subproblem
237  void codegen_qp_solve(CodeGenerator& cg, const std::string& H, const std::string& g,
238  const std::string& lbdz, const std::string& ubdz,
239  const std::string& A, const std::string& x_opt,
240  const std::string& dlam, int mode) const;
241 
242  void codegen_tr_update(CodeGenerator& cg,
243  const std::string& tr_rad, const std::string& tr_ratio) const;
244 
245  void codegen_eval_m_k(CodeGenerator& cg) const;
246 
247  void codegen_eval_tr_ratio(CodeGenerator& cg,
248  const std::string& val_f, const std::string& val_f_corr, const std::string& val_m_k) const;
249 
250  void codegen_step_update(CodeGenerator& cg, const std::string& tr_ratio) const;
251 
252  void codegen_feasibility_iterations(CodeGenerator& cg, const std::string& tr_rad) const;
253 
254  // Solve the QP subproblem
255  // void codegen_qp_ela_solve(CodeGenerator& cg, const std::string& H, const std::string& g,
256  // const std::string& lbdz, const std::string& ubdz,
257  // const std::string& A, const std::string& x_opt, const std::string& dlam) const;
258 
259  // Execute elastic mode: mode 0 = normal, mode 1 = SOC
260  // void codegen_solve_elastic_mode(CodeGenerator& cg, int mode) const;
261 
262  // Codegen to calculate gama_1
263  // void codegen_calc_gamma_1(CodeGenerator& cg) const;
264 
265 
266  // Calculate gamma_1
267  // double calc_gamma_1(FeasiblesqpmethodMemory* m) const;
268 
270  static const std::string meta_doc;
271 
272 
274  void serialize_body(SerializingStream &s) const override;
275 
277  static ProtoFunction* deserialize(DeserializingStream& s) { return new Feasiblesqpmethod(s); }
278 
279  protected:
281  explicit Feasiblesqpmethod(DeserializingStream& s);
282 
283  private:
284  void set_feasiblesqpmethod_prob();
285  };
286 
287 } // namespace casadi
289 #endif // CASADI_FEASIBLESQPMETHOD_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.