qrsqp.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  *
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_QRSQP_HPP
27 #define CASADI_QRSQP_HPP
28 
29 #include "casadi/core/nlpsol_impl.hpp"
30 #include <casadi/solvers/casadi_nlpsol_qrsqp_export.h>
31 
42 namespace casadi {
43 
44  struct QrsqpMemory : public NlpsolMemory {
46  double *z_cand;
47 
49  double *gLag, *gLag_old;
50 
52  double *gf;
53 
54  // Bounds of the QP
55  double *lbdz, *ubdz;
56 
57  // QP solution
58  double *dz, *dlam;
59 
60  // Current Jacobian
61  double *Jk;
62 
64  double *Bk;
65 
67  double reg;
68 
70  double sigma;
71 
72  // Storage for merit function
73  double* merit_mem;
74  size_t merit_ind;
75 
77  const char* return_status;
78 
80  int iter_count;
81  };
82 
87  class Qrsqp : public Nlpsol {
88  public:
89  explicit Qrsqp(const std::string& name, const Function& nlp);
90  ~Qrsqp() override;
91 
92  // Get name of the plugin
93  const char* plugin_name() const override { return "qrsqp";}
94 
95  // Name of the class
96  std::string class_name() const override { return "Qrsqp";}
97 
99  static Nlpsol* creator(const std::string& name, const Function& nlp) {
100  return new Qrsqp(name, nlp);
101  }
102 
104 
105  static const Options options_;
106  const Options& get_options() const override { return options_;}
108 
110  Dict get_stats(void* mem) const override;
111 
112  // Initialize the solver
113  void init(const Dict& opts) override;
114 
116  void* alloc_mem() const override { return new QrsqpMemory();}
117 
119  void free_mem(void *mem) const override { delete static_cast<QrsqpMemory*>(mem);}
120 
122  void set_work(void* mem, const double**& arg, double**& res,
123  casadi_int*& iw, double*& w) const override;
124 
125  // Solve the NLP
126  int solve(void* mem) const override;
127 
129  Function qpsol_;
130 
132  bool exact_hessian_;
133 
135  casadi_int max_iter_, min_iter_;
136 
138  casadi_int lbfgs_memory_;
139 
141  double tol_pr_, tol_du_;
142 
144  double min_step_size_;
145 
148  double c1_;
149  double beta_;
150  casadi_int max_iter_ls_;
151  casadi_int merit_memsize_;
153 
154  // Print options
155  bool print_header_, print_iteration_;
156 
157  // Hessian sparsity
158  Sparsity Hsp_;
159 
160  // Jacobian sparsity
161  Sparsity Asp_;
162 
164  bool regularize_;
165 
167  const Function getConic() const { return qpsol_;}
168 
170  void print_iteration() const;
171 
173  void print_iteration(casadi_int iter, double obj, double pr_inf, double du_inf,
174  double dx_norm, double rg, casadi_int ls_trials, bool ls_success) const;
175 
176  // Solve the QP subproblem
177  virtual void solve_QP(QrsqpMemory* m, const double* H, const double* g,
178  const double* lbdz, const double* ubdz,
179  const double* A, double* x_opt, double* dlam) const;
180 
182  static const std::string meta_doc;
183 
184  };
185 
186 } // namespace casadi
188 #endif // CASADI_QRSQP_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.