snopt_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_SNOPT_INTERFACE_HPP
27 #define CASADI_SNOPT_INTERFACE_HPP
28 
29 #include "casadi/core/nlpsol_impl.hpp"
30 #include "casadi/core/im.hpp"
31 
32 #include "casadi/interfaces/snopt/casadi_nlpsol_snopt_export.h"
33 extern "C" {
34 #include "snopt_cwrap.h" // NOLINT(build/include)
35 }
36 
47 namespace casadi {
48 
49  // Forward declaration
50  class SnoptInterface;
51 
52  struct SnoptMemory : public NlpsolMemory {
54  const SnoptInterface& self;
55 
56  // Current solution
57  double *xk2, *lam_gk, *lam_xk;
58 
59  // Current calculated quantities
60  double *gk, *jac_fk, *jac_gk;
61 
62  std::vector<double> bl, bu, xx;
63 
64  std::vector<int> hs, locJ, indJ;
65 
66  casadi_int n_iter; // number of major iterations
67 
68  std::vector<double> A_data, valJ, rc, pi;
69 
70  // Memory pool
71  static std::vector<SnoptMemory*> mempool;
72  int memind;
73 
74  int return_status;
75 
77  SnoptMemory(const SnoptInterface& self);
78 
80  ~SnoptMemory();
81  };
82 
87  class SnoptInterface : public Nlpsol {
88  public:
89  // NLP functions
90  Sparsity jacf_sp_;
91  Sparsity jacg_sp_;
92 
93  // Constructor
94  explicit SnoptInterface(const std::string& name, const Function& nlp);
95 
96  // Destructor
97  ~SnoptInterface() override;
98 
99  // Get name of the plugin
100  const char* plugin_name() const override { return "snopt";}
101 
102  // Get name of the class
103  std::string class_name() const override { return "SnoptInterface";}
104 
106  static Nlpsol* creator(const std::string& name, const Function& nlp) {
107  return new SnoptInterface(name, nlp);
108  }
109 
111 
112  static const Options options_;
113  const Options& get_options() const override { return options_;}
115 
116  // Initialize the solver
117  void init(const Dict& opts) override;
118 
120  void* alloc_mem() const override { return new SnoptMemory(*this);}
121 
123  int init_mem(void* mem) const override;
124 
126  void free_mem(void *mem) const override { delete static_cast<SnoptMemory*>(mem);}
127 
129  void set_work(void* mem, const double**& arg, double**& res,
130  casadi_int*& iw, double*& w) const override;
131 
132  // Solve the NLP
133  int solve(void* mem) const override;
134 
136  Dict get_stats(void* mem) const override;
137 
138  static std::map<int, std::string> status_;
139  static std::map<int, std::string> secondary_status_;
140 
141  std::string formatStatus(int status) const;
142  std::string formatSecondaryStatus(int status) const;
143 
144  void userfun(SnoptMemory* m, int* mode, int nnObj, int nnCon, int nnJac, int nnL, int neJac,
145  const double* x, double* fObj, double*gObj, double* fCon, double* gCon,
146  int nState, char* cu, int lencu, int* iu, int leniu, double* ru, int lenru) const;
147 
148  casadi_int nnJac_;
149  casadi_int nnObj_;
150  casadi_int nnCon_;
151 
152  IM A_structure_;
153 
154  casadi_int m_;
155  casadi_int iObj_;
156 
157  static void userfunPtr(int * mode, int* nnObj, int * nnCon, int *nJac, int *nnL, int * neJac, // NOLINT
158  double *x, double *fObj, double *gObj, double * fCon, double* gCon, // NOLINT
159  int* nState, char* cu, int* lencu, int* iu, int* leniu, // NOLINT
160  double* ru, int *lenru); // NOLINT
161 
162  // Matrix A has a linear objective row
163  bool jacF_row_;
164  // Matrix A has a dummy row
165  bool dummyrow_;
166 
168  static const std::string meta_doc;
169 
171  casadi_int Cold_;
172 
173  double inf_;
174 
175 
177  void serialize_body(SerializingStream &s) const override;
178 
180  static ProtoFunction* deserialize(DeserializingStream& s) { return new SnoptInterface(s); }
181 
182  protected:
184  explicit SnoptInterface(DeserializingStream& s);
185 
186  private:
187  // options
188  Dict opts_;
189  };
190 
191 } // namespace casadi
192 
194 #endif // CASADI_SNOPT_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Matrix< casadi_int > IM
Definition: im_fwd.hpp:31