bonmin_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_BONMIN_INTERFACE_HPP
27 #define CASADI_BONMIN_INTERFACE_HPP
28 
29 #include <IpIpoptApplication.hpp>
30 #include "BonOsiTMINLPInterface.hpp"
31 #include "BonIpoptSolver.hpp"
32 #include "BonCbc.hpp"
33 #include "BonBonminSetup.hpp"
34 #include "BonOACutGenerator2.hpp"
35 #include "BonEcpCuts.hpp"
36 #include "BonOaNlpOptim.hpp"
37 
38 #include <casadi/interfaces/bonmin/casadi_nlpsol_bonmin_export.h>
39 #include "casadi/core/nlpsol_impl.hpp"
40 #include "casadi/core/timing.hpp"
41 
42 
72 namespace casadi {
73 
74  struct BonminMemory : public NlpsolMemory {
75  // Current calculated quantities
76  double *gk, *grad_fk, *jac_gk, *hess_lk, *grad_lk;
77 
78  // Stats
79  std::vector<double> inf_pr, inf_du, mu, d_norm, regularization_size,
80  obj, alpha_pr, alpha_du;
81  std::vector<casadi_int> ls_trials;
82  const char* return_status;
83  casadi_int iter_count;
84 
85  Bonmin::TMINLP::SosInfo sos_info;
86 
88  BonminMemory();
89 
91  ~BonminMemory();
92  };
93 
99  class BonminInterface : public Nlpsol {
100  friend class BonminUserClass;
101  public:
102  Sparsity jacg_sp_;
103  Sparsity hesslag_sp_;
104 
105  explicit BonminInterface(const std::string& name, const Function& nlp);
106  ~BonminInterface() override;
107 
108  // Get name of the plugin
109  const char* plugin_name() const override { return "bonmin";}
110 
111  // Get name of the class
112  std::string class_name() const override { return "BonminInterface";}
113 
115  static Nlpsol* creator(const std::string& name, const Function& nlp) {
116  return new BonminInterface(name, nlp);
117  }
118 
120 
121  static const Options options_;
122  const Options& get_options() const override { return options_;}
124 
125  // Initialize the solver
126  void init(const Dict& opts) override;
127 
129  void* alloc_mem() const override { return new BonminMemory();}
130 
132  int init_mem(void* mem) const override;
133 
135  void free_mem(void *mem) const override { delete static_cast<BonminMemory*>(mem);}
136 
138  Dict get_stats(void* mem) const override;
139 
141  void set_work(void* mem, const double**& arg, double**& res,
142  casadi_int*& iw, double*& w) const override;
143 
144  // Solve the NLP
145  int solve(void* mem) const override;
146 
148  bool exact_hessian_;
149 
151  Dict opts_;
152 
153  // Bonmin callback functions
154  void finalize_solution(BonminMemory* m, Bonmin::TMINLP::SolverReturn status,
155  const double* x, double obj_value) const;
156  bool get_bounds_info(BonminMemory* m, double* x_l, double* x_u,
157  double* g_l, double* g_u) const;
158  bool get_starting_point(BonminMemory* m, bool init_x, double* x,
159  bool init_z, double* z_L, double* z_U,
160  bool init_lambda, double* lambda) const;
161  void get_nlp_info(BonminMemory* m, int& nx, int& ng,
162  int& nnz_jac_g, int& nnz_h_lag) const;
163  int get_number_of_nonlinear_variables() const;
164  bool get_list_of_nonlinear_variables(int num_nonlin_vars, int* pos_nonlin_vars) const;
165  bool intermediate_callback(BonminMemory* m, const double* x, const double* z_L,
166  const double* z_U, const double* g,
167  const double* lambda, double obj_value, int iter,
168  double inf_pr, double inf_du, double mu, double d_norm,
169  double regularization_size, double alpha_du, double alpha_pr,
170  int ls_trials, bool full_callback) const;
171  const Bonmin::TMINLP::SosInfo& sosConstraints(BonminMemory* m) const;
172 
174  bool integer_support() const override { return true;}
175 
177  static const std::string meta_doc;
178 
180  std::vector<double> sos1_weights_;
181  std::vector<int> sos1_indices_;
182  std::vector<int> sos1_priorities_;
183  std::vector<int> sos1_starts_;
184  std::vector<char> sos1_types_;
185  casadi_int sos_num_;
186  casadi_int sos_num_nz_;
187 
188  // Options
189  bool pass_nonlinear_variables_;
190  bool pass_nonlinear_constraints_;
191  std::vector<bool> nl_ex_;
192  std::vector<bool> nl_g_;
193  Dict var_string_md_, var_integer_md_, var_numeric_md_,
194  con_string_md_, con_integer_md_, con_numeric_md_;
195 
197  void serialize_body(SerializingStream &s) const override;
198 
200  static ProtoFunction* deserialize(DeserializingStream& s) { return new BonminInterface(s); }
201 
202  protected:
204  explicit BonminInterface(DeserializingStream& s);
205  };
206 
207 } // namespace casadi
209 
210 #endif // CASADI_BONMIN_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.