ipopt_nlp.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_NLP_HPP
27 #define CASADI_IPOPT_NLP_HPP
28 
29 #include <IpTNLP.hpp>
30 #include <IpIpoptCalculatedQuantities.hpp>
31 #include <IpIpoptData.hpp>
32 
33 #ifdef WITH_IPOPT_CALLBACK
34 #define private public
35 #include <IpIpoptData.hpp> // NOLINT(build/include)
36 #include <IpOrigIpoptNLP.hpp>
37 #include <IpTNLPAdapter.hpp>
38 #include <IpDenseVector.hpp>
39 #include <IpExpansionMatrix.hpp>
40 #undef private
41 #define private private
42 #endif // WITH_IPOPT_CALLBACK
43 
44 #include <iostream>
45 
46 #include <casadi/interfaces/ipopt/casadi_nlpsol_ipopt_export.h>
47 
49 using namespace Ipopt;
50 namespace casadi {
51  // Forward declarations
52  class IpoptInterface;
53  struct IpoptMemory;
54 
55  class IpoptUserClass : public TNLP {
56 #ifdef WITH_IPOPT_CALLBACK
57  friend class TNLPAdapter;
58 #endif // WITH_IPOPT_CALLBACK
59 
60  public:
61  IpoptUserClass(const IpoptInterface& solver, IpoptMemory* mem);
62  ~IpoptUserClass() override;
63 
65  bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
66  Index& nnz_h_lag, IndexStyleEnum& index_style) override;
67 
69  bool get_bounds_info(Index n, Number* x_l, Number* x_u,
70  Index m, Number* g_l, Number* g_u) override;
71 
73  bool get_starting_point(Index n, bool init_x, Number* x,
74  bool init_z, Number* z_L, Number* z_U,
75  Index m, bool init_lambda,
76  Number* lambda) override;
77 
79  bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value) override;
80 
82  bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f) override;
83 
85  bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g) override;
86 
91  bool eval_jac_g(Index n, const Number* x, bool new_x,
92  Index m, Index nele_jac, Index* iRow, Index *jCol,
93  Number* values) override;
94 
99  bool eval_h(Index n, const Number* x, bool new_x,
100  Number obj_factor, Index m, const Number* lambda,
101  bool new_lambda, Index nele_hess, Index* iRow,
102  Index* jCol, Number* values) override;
103 
106  void finalize_solution(SolverReturn status,
107  Index n, const Number* x, const Number* z_L, const Number* z_U,
108  Index m, const Number* g, const Number* lambda,
109  Number obj_value,
110  const IpoptData* ip_data,
111  IpoptCalculatedQuantities* ip_cq) override;
112 
114  Index get_number_of_nonlinear_variables() override;
115 
117  bool get_list_of_nonlinear_variables(Index num_nonlin_vars, Index* pos_nonlin_vars) override;
118 
119 
121  bool intermediate_callback(AlgorithmMode mode, Index iter, Number obj_value,
122  Number inf_pr, Number inf_du,
123  Number mu, Number d_norm,
124  Number regularization_size,
125  Number alpha_du, Number alpha_pr,
126  Index ls_trials,
127  const IpoptData* ip_data,
128  IpoptCalculatedQuantities* ip_cq) override;
129 
131  bool get_var_con_metadata(Index n, StringMetaDataMapType& var_string_md,
132  IntegerMetaDataMapType& var_integer_md,
133  NumericMetaDataMapType& var_numeric_md,
134  Index m, StringMetaDataMapType& con_string_md,
135  IntegerMetaDataMapType& con_integer_md,
136  NumericMetaDataMapType& con_numeric_md) override;
137 
139  void finalize_metadata(Index n, const StringMetaDataMapType& var_string_md,
140  const IntegerMetaDataMapType& var_integer_md,
141  const NumericMetaDataMapType& var_numeric_md,
142  Index m, const StringMetaDataMapType& con_string_md,
143  const IntegerMetaDataMapType& con_integer_md,
144  const NumericMetaDataMapType& con_numeric_md) override;
145 
146  private:
147  IpoptUserClass(const IpoptUserClass&);
148  IpoptUserClass& operator=(const IpoptUserClass&);
149  const IpoptInterface& solver_;
150  IpoptMemory* mem_;
151 
152  double * x_;
153  double * z_L_;
154  double * z_U_;
155  double * g_;
156  double * lambda_;
157  int n_;
158  int m_;
159  double obj_value_;
160  };
161 
162 } // namespace casadi
164 
165 #endif //IPOPT_NLP
The casadi namespace.