nlpsol_impl.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_NLPSOL_IMPL_HPP
27 #define CASADI_NLPSOL_IMPL_HPP
28 
29 #include "nlpsol.hpp"
30 #include "oracle_function.hpp"
31 #include "plugin_interface.hpp"
32 
33 
35 namespace casadi {
36 
40  struct CASADI_EXPORT NlpsolMemory : public OracleMemory {
41  // Problem data structure
43  // number of iterations
44  casadi_int n_iter;
45  // Success?
46  bool success;
47  // Return status
48  UnifiedReturnStatus unified_return_status;
49  };
50 
58  class CASADI_EXPORT
59  Nlpsol : public OracleFunction, public PluginInterface<Nlpsol> {
60  public:
61 
62  // Memory structure
64 
66  casadi_int nx_;
67 
69  casadi_int ng_;
70 
72  casadi_int np_;
73 
75  Function fcallback_;
76 
78  casadi_int callback_step_;
79 
81  std::string sens_linsol_;
82  Dict sens_linsol_options_;
83 
84  std::vector<char> detect_simple_bounds_is_simple_;
85  Function detect_simple_bounds_parts_;
86  std::vector<casadi_int> detect_simple_bounds_target_x_;
87  std::vector<casadi_int> detect_simple_bounds_target_g_;
88 
90 
93  bool eval_errors_fatal_;
94  bool warn_initial_bounds_;
95  bool iteration_callback_ignore_errors_;
96  bool calc_multipliers_;
97  bool calc_lam_x_, calc_lam_p_, calc_f_, calc_g_;
98  bool bound_consistency_;
99  double min_lam_;
100  bool no_nlp_grad_;
101  std::vector<bool> discrete_;
102  std::vector<bool> equality_;
104 
105  // Mixed integer problem?
106  bool mi_;
107 
109  mutable WeakRef kkt_;
110 
114  void serialize_body(SerializingStream &s) const override;
118  void serialize_type(SerializingStream &s) const override;
119 
123  static ProtoFunction* deserialize(DeserializingStream& s);
124 
128  std::string serialize_base_function() const override { return "Nlpsol"; }
129 
131  Nlpsol(const std::string& name, const Function& oracle);
132 
134  ~Nlpsol() override = 0;
135 
137 
140  size_t get_n_in() override { return NLPSOL_NUM_IN;}
141  size_t get_n_out() override { return NLPSOL_NUM_OUT;}
143 
145 
148  Sparsity get_sparsity_in(casadi_int i) override;
149  Sparsity get_sparsity_out(casadi_int i) override;
151 
153 
156  std::string get_name_in(casadi_int i) override { return nlpsol_in(i);}
157  std::string get_name_out(casadi_int i) override { return nlpsol_out(i);}
159 
161 
164  static const Options options_;
165  const Options& get_options() const override { return options_;}
167 
171  void disp_more(std::ostream& stream) const override;
172 
174  void init(const Dict& opts) override;
175 
179  void* alloc_mem() const override { return new NlpsolMemory();}
180 
184  int init_mem(void* mem) const override;
185 
189  void free_mem(void *mem) const override { delete static_cast<NlpsolMemory*>(mem);}
190 
194  virtual void check_inputs(void* mem) const;
195 
199  double get_default_in(casadi_int ind) const override { return nlpsol_default_in(ind);}
200 
202  virtual bool integer_support() const { return false;}
203 
207  void set_work(void* mem, const double**& arg, double**& res,
208  casadi_int*& iw, double*& w) const override;
209 
210  // Evaluate numerically
211  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const final;
212 
213  // Solve the NLP
214  virtual int solve(void* mem) const = 0;
215 
219  void codegen_declarations(CodeGenerator& g) const override;
220 
224  void codegen_body_enter(CodeGenerator& g) const override;
225 
229  void codegen_body_exit(CodeGenerator& g) const override;
230 
234  bool uses_output() const override {return true;}
235 
237  Dict get_stats(void* mem) const override;
238 
240 
243  bool has_forward(casadi_int nfwd) const override { return true;}
244  Function get_forward(casadi_int nfwd, const std::string& name,
245  const std::vector<std::string>& inames,
246  const std::vector<std::string>& onames,
247  const Dict& opts) const override;
249 
251 
254  bool has_reverse(casadi_int nadj) const override { return true;}
255  Function get_reverse(casadi_int nadj, const std::string& name,
256  const std::vector<std::string>& inames,
257  const std::vector<std::string>& onames,
258  const Dict& opts) const override;
260 
261  // Call the callback function
262  int callback(NlpsolMemory* m) const;
263 
264  // Get KKT function
265  Function kkt() const;
266 
267  // Make sure primal-dual solution is consistent with bounds
268  static void bound_consistency(casadi_int n, double* z, double* lam,
269  const double* lbz, const double* ubz);
270 
271  // Creator function for internal class
272  typedef Nlpsol* (*Creator)(const std::string& name, const Function& oracle);
273 
274  // No static functions exposed
275  struct Exposed{ };
276 
278  static std::map<std::string, Plugin> solvers_;
279 
281  static const std::string infix_;
282 
284  static std::string shortname() { return "nlpsol";}
285 
289  std::string class_name() const override {return "Nlpsol";}
290 
294  bool is_a(const std::string& type, bool recursive) const override;
295 
296  // Get reduced Hessian
297  virtual DM getReducedHessian();
298 
300  virtual void setOptionsFromFile(const std::string & file);
301 
303  template<typename Type> static void append_to_vec(GenericType& t, Type el) {
304  std::vector<Type> v = t;
305  v.push_back(el);
306  t = v;
307  }
308 
310  template<typename XType>
311  static Function create_oracle(const std::map<std::string, XType>& d,
312  const Dict& opts);
313 
314  protected:
318  explicit Nlpsol(DeserializingStream& s);
319  private:
320  void set_nlpsol_prob();
321  };
322 
323 } // namespace casadi
325 #endif // CASADI_NLPSOL_IMPL_HPP
CASADI_EXPORT std::vector< std::string > nlpsol_in()
Get input scheme of NLP solvers.
CASADI_EXPORT std::vector< std::string > nlpsol_out()
Get NLP solver output scheme of NLP solvers.
CASADI_EXPORT double nlpsol_default_in(casadi_int ind)
Default input for an NLP solver.
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Matrix< double > DM
Definition: dm_fwd.hpp:33
Type
Variable type (FMI 3)
UnifiedReturnStatus