sqpmethod.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, Kobe Bergmans
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_SQPMETHOD_HPP
27 #define CASADI_SQPMETHOD_HPP
28 
29 #include "casadi/core/nlpsol_impl.hpp"
30 #include <casadi/solvers/casadi_nlpsol_sqpmethod_export.h>
31 
42 namespace casadi {
43 
44  struct SqpmethodMemory : public NlpsolMemory {
45  // Problem data structure
47 
48  // Memory object of qp solver
49  int mem_qp;
51  double reg;
52 
54  double sigma;
55 
56  casadi_int merit_ind;
58  const char* return_status;
59 
61  int iter_count;
62  };
63 
68  class Sqpmethod : public Nlpsol {
69  public:
70  explicit Sqpmethod(const std::string& name, const Function& nlp);
71  ~Sqpmethod() override;
72 
73  // Get name of the plugin
74  const char* plugin_name() const override { return "sqpmethod";}
75 
76  // Name of the class
77  std::string class_name() const override { return "Sqpmethod";}
78 
80  static Nlpsol* creator(const std::string& name, const Function& nlp) {
81  return new Sqpmethod(name, nlp);
82  }
83 
85 
86  static const Options options_;
87  const Options& get_options() const override { return options_;}
89 
91  Dict get_stats(void* mem) const override;
92 
93  // Initialize the solver
94  void init(const Dict& opts) override;
95 
97  void* alloc_mem() const override { return new SqpmethodMemory();}
98 
100  int init_mem(void* mem) const override;
101 
103  void free_mem(void* mem) const override;
104 
106  void set_work(void* mem, const double**& arg, double**& res,
107  casadi_int*& iw, double*& w) const override;
108 
109  // Solve the NLP
110  int solve(void* mem) const override;
111 
112  // Memory structure
114 
116  Function qpsol_;
117 
119  Function qpsol_ela_;
120 
122  bool exact_hessian_;
123 
125  casadi_int block_size_ = 0;
126 
128  casadi_int max_iter_, min_iter_;
129 
131  casadi_int lbfgs_memory_;
132 
134  double tol_pr_, tol_du_;
135 
137  double min_step_size_;
138 
140  bool elastic_mode_;
141 
143  double gamma_0_, gamma_max_, gamma_1_min_;
144 
146  bool init_feasible_;
147 
150  double c1_;
151  double beta_;
152  casadi_int max_iter_ls_;
153  casadi_int merit_memsize_;
155 
156  // Print options
157  bool print_header_, print_iteration_, print_status_;
158 
159  // Hessian Sparsity
160  Sparsity Hsp_;
161 
162  // Jacobian sparsity
163  Sparsity Asp_;
164 
166  ConvexifyData convexify_data_;
167 
169  bool convexify_;
170 
171  // Second order corrections
172  bool so_corr_;
173 
175  void codegen_body(CodeGenerator& g) const override;
176 
178  void codegen_declarations(CodeGenerator& g) const override;
179 
181  std::string codegen_mem_type() const override { return "struct casadi_sqpmethod_data"; }
182 
184  const Function getConic() const { return qpsol_;}
185 
187  void print_iteration() const;
188 
190  void print_iteration(casadi_int iter, double obj, double pr_inf, double du_inf,
191  double dx_norm, double rg, casadi_int ls_trials, bool ls_success,
192  bool so_succes, std::string info) const;
193 
194  // Solve the QP subproblem: mode 0 = normal, mode 1 = SOC
195  virtual int solve_QP(SqpmethodMemory* m, const double* H, const double* g,
196  const double* lbdz, const double* ubdz,
197  const double* A,
198  double* x_opt, double* dlam, int mode) const;
199 
200  // Solve the QP subproblem for elastic mode
201  virtual int solve_ela_QP(SqpmethodMemory* m, const double* H, const double* g,
202  const double* lbdz, const double* ubdz, const double* A,
203  double* x_opt, double* dlam) const;
204 
205  // Execute elastic mode: mode 0 = normal, mode 1 = SOC
206  virtual int solve_elastic_mode(SqpmethodMemory* m, casadi_int* ela_it, double gamma_1,
207  casadi_int ls_iter, bool ls_success, bool so_succes, double pr_inf,
208  double du_inf, double dx_norminf, std::string* info, int mode) const;
209 
210 
211  // Solve the QP subproblem
212  void codegen_qp_solve(CodeGenerator& cg, const std::string& H, const std::string& g,
213  const std::string& lbdz, const std::string& ubdz,
214  const std::string& A, const std::string& x_opt, const std::string& dlam, int mode) const;
215 
216  // Solve the QP subproblem
217  void codegen_qp_ela_solve(CodeGenerator& cg, const std::string& H, const std::string& g,
218  const std::string& lbdz, const std::string& ubdz,
219  const std::string& A, const std::string& x_opt, const std::string& dlam) const;
220 
221  // Execute elastic mode: mode 0 = normal, mode 1 = SOC
222  void codegen_solve_elastic_mode(CodeGenerator& cg, int mode) const;
223 
224  // Codegen to calculate gama_1
225  void codegen_calc_gamma_1(CodeGenerator& cg) const;
226 
227 
228  // Calculate gamma_1
229  double calc_gamma_1(SqpmethodMemory* m) const;
230 
232  static const std::string meta_doc;
233 
234 
236  void serialize_body(SerializingStream &s) const override;
237 
239  static ProtoFunction* deserialize(DeserializingStream& s) { return new Sqpmethod(s); }
240 
241  protected:
243  explicit Sqpmethod(DeserializingStream& s);
244 
245  private:
246  void set_sqpmethod_prob();
247  };
248 
249 } // namespace casadi
251 #endif // CASADI_SQPMETHOD_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.