ipopt_interface.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_IPOPT_INTERFACE_HPP
27 #define CASADI_IPOPT_INTERFACE_HPP
28 
29 #include <casadi/interfaces/ipopt/casadi_nlpsol_ipopt_export.h>
30 #include "casadi/core/nlpsol_impl.hpp"
31 #include "casadi/core/timing.hpp"
32 
62 namespace casadi {
63 
64  struct IpoptMemory : public NlpsolMemory {
74  void* userclass;
75  void* app;
76 
77  // Current calculated quantities
78  double *gk, *grad_fk, *jac_gk, *hess_lk, *grad_lk;
79 
80  // Stats
81  std::vector<double> inf_pr, inf_du, mu, d_norm, regularization_size,
82  obj, alpha_pr, alpha_du;
83  std::vector<int> ls_trials;
84  const char* return_status;
85  int iter_count;
86 
87  // Meta-data
88  std::map<std::string, std::vector<std::string> > var_string_md;
89  std::map<std::string, std::vector<int> > var_integer_md;
90  std::map<std::string, std::vector<double> > var_numeric_md;
91  std::map<std::string, std::vector<std::string> > con_string_md;
92  std::map<std::string, std::vector<int> > con_integer_md;
93  std::map<std::string, std::vector<double> > con_numeric_md;
94 
96  IpoptMemory();
97 
99  ~IpoptMemory();
100  };
101 
107  class IpoptInterface : public Nlpsol {
108  friend class IpoptUserClass;
109  public:
110  Sparsity jacg_sp_;
111  Sparsity hesslag_sp_;
112 
113  explicit IpoptInterface(const std::string& name, const Function& nlp);
114  ~IpoptInterface() override;
115 
116  // Get name of the plugin
117  const char* plugin_name() const override { return "ipopt";}
118 
119  // Get name of the class
120  std::string class_name() const override { return "IpoptInterface";}
121 
123  static Nlpsol* creator(const std::string& name, const Function& nlp) {
124  return new IpoptInterface(name, nlp);
125  }
126 
128 
129  static const Options options_;
130  const Options& get_options() const override { return options_;}
132 
133  // Initialize the solver
134  void init(const Dict& opts) override;
135 
137  void* alloc_mem() const override { return new IpoptMemory();}
138 
140  int init_mem(void* mem) const override;
141 
143  void free_mem(void *mem) const override { delete static_cast<IpoptMemory*>(mem);}
144 
146  Dict get_stats(void* mem) const override;
147 
149  void set_work(void* mem, const double**& arg, double**& res,
150  casadi_int*& iw, double*& w) const override;
151 
152  // Solve the NLP
153  int solve(void* mem) const override;
154 
156  bool exact_hessian_;
157 
159  Dict opts_;
160 
161  // Ipopt callback functions
162  void finalize_solution(IpoptMemory* m, const double* x, const double* z_L, const double* z_U,
163  const double* g, const double* lambda, double obj_value,
164  int iter_count) const;
165  bool get_bounds_info(IpoptMemory* m, double* x_l, double* x_u,
166  double* g_l, double* g_u) const;
167  bool get_starting_point(IpoptMemory* m, bool init_x, double* x,
168  bool init_z, double* z_L, double* z_U,
169  bool init_lambda, double* lambda) const;
170  void get_nlp_info(IpoptMemory* m, int& nx, int& ng,
171  int& nnz_jac_g, int& nnz_h_lag) const;
172  int get_number_of_nonlinear_variables() const;
173  bool get_list_of_nonlinear_variables(int num_nonlin_vars, int* pos_nonlin_vars) const;
174  bool intermediate_callback(IpoptMemory* m, const double* x, const double* z_L,
175  const double* z_U, const double* g,
176  const double* lambda, double obj_value, int iter,
177  double inf_pr, double inf_du, double mu, double d_norm,
178  double regularization_size, double alpha_du, double alpha_pr,
179  int ls_trials, bool full_callback) const;
180  bool get_var_con_metadata(std::map<std::string, std::vector<std::string> >& var_string_md,
181  std::map<std::string, std::vector<int> >& var_integer_md,
182  std::map<std::string, std::vector<double> >& var_numeric_md,
183  std::map<std::string, std::vector<std::string> >& con_string_md,
184  std::map<std::string, std::vector<int> >& con_integer_md,
185  std::map<std::string, std::vector<double> >& con_numeric_md) const;
186 
188  static const std::string meta_doc;
189 
190  // Options
191  bool pass_nonlinear_variables_;
192  std::vector<bool> nl_ex_;
193  Dict var_string_md_, var_integer_md_, var_numeric_md_,
194  con_string_md_, con_integer_md_, con_numeric_md_;
195 
196  bool clip_inactive_lam_;
197  std::string inactive_lam_strategy_;
198  double inactive_lam_value_;
199 
201  ConvexifyData convexify_data_;
202 
204  bool convexify_;
205 
206  void set_ipopt_prob(CodeGenerator& g) const;
207 
209  void codegen_body(CodeGenerator& g) const override;
210 
212  void codegen_declarations(CodeGenerator& g) const override;
213 
215  void codegen_init_mem(CodeGenerator& g) const override;
216 
218  void codegen_free_mem(CodeGenerator& g) const override;
219 
221  std::string codegen_mem_type() const override { return "struct casadi_ipopt_data"; }
222 
224  void serialize_body(SerializingStream &s) const override;
225 
227  static ProtoFunction* deserialize(DeserializingStream& s) { return new IpoptInterface(s); }
228 
229  protected:
231  explicit IpoptInterface(DeserializingStream& s);
232  };
233 
234 } // namespace casadi
236 
237 #endif // CASADI_IPOPT_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.