fatrop_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_FATROP_INTERFACE_HPP
27 #define CASADI_FATROP_INTERFACE_HPP
28 
29 #include <casadi/interfaces/fatrop/casadi_nlpsol_fatrop_export.h>
30 #include "casadi/core/nlpsol_impl.hpp"
31 #include "casadi/core/timing.hpp"
32 #include <fatrop/ocp/OCPAbstract.hpp>
33 #include <fatrop/ocp/StageOCPApplication.hpp>
34 #include <fatrop/ocp/OCPCInterface.h>
35 
36 namespace casadi {
37  #include "fatrop_runtime.hpp"
38 }
39 
84 namespace casadi {
85 
86  struct FatropMemory : public NlpsolMemory {
87  // Problem data structure
89 
90 
91  };
92 
98  class FatropInterface : public Nlpsol {
99  public:
100  Sparsity jacg_sp_;
101  Sparsity hesslag_sp_;
102 
103  explicit FatropInterface(const std::string& name, const Function& nlp);
104  ~FatropInterface() override;
105 
106  // Get name of the plugin
107  const char* plugin_name() const override { return "fatrop";}
108 
109  // Get name of the class
110  std::string class_name() const override { return "FatropInterface";}
111 
113  static Nlpsol* creator(const std::string& name, const Function& nlp) {
114  return new FatropInterface(name, nlp);
115  }
116 
118 
119  static const Options options_;
120  const Options& get_options() const override { return options_;}
122 
123  // Initialize the solver
124  void init(const Dict& opts) override;
125 
127  void* alloc_mem() const override { return new FatropMemory();}
128 
130  int init_mem(void* mem) const override;
131 
133  void free_mem(void *mem) const override;
134 
136  Dict get_stats(void* mem) const override;
137 
139  void set_work(void* mem, const double**& arg, double**& res,
140  casadi_int*& iw, double*& w) const override;
141 
142  // Solve the NLP
143  int solve(void* mem) const override;
144 
146  bool exact_hessian_;
147 
149  Dict opts_;
150 
152  static const std::string meta_doc;
153 
154  // Options
155 
157  ConvexifyData convexify_data_;
158 
160  bool convexify_;
161 
162  void set_fatrop_prob();
163  void set_fatrop_prob(CodeGenerator& g) const;
164 
166  void codegen_body(CodeGenerator& g) const override;
167 
169  void codegen_declarations(CodeGenerator& g) const override;
170 
172  void codegen_init_mem(CodeGenerator& g) const override;
173 
175  void codegen_free_mem(CodeGenerator& g) const override;
176 
178  std::string codegen_mem_type() const override { return "struct casadi_fatrop_data"; }
179 
181  void serialize_body(SerializingStream &s) const override;
182 
184  static ProtoFunction* deserialize(DeserializingStream& s) { return new FatropInterface(s); }
185 
186  protected:
188  explicit FatropInterface(DeserializingStream& s);
189 
190  private:
191  // Memory structure
193 
194  static Sparsity blocksparsity(casadi_int rows, casadi_int cols,
195  const std::vector<casadi_ocp_block>& blocks, bool eye=false);
196  static void blockptr(std::vector<double *>& vs, std::vector<double>& v,
197  const std::vector<casadi_ocp_block>& blocks, bool eye=false);
198  Sparsity Isp_, ABsp_, CDsp_, RSQsp_;
199 
200  std::vector< casadi_ocp_block > AB_blocks_, CD_blocks_, RSQ_blocks_, I_blocks_;
201 
202  std::vector<casadi_int> nxs_;
203  std::vector<casadi_int> nus_;
204  std::vector<casadi_int> ngs_;
205  casadi_int N_;
206 
207  // An enum field for the structure detection
208  enum StructureDetection {
209  STRUCTURE_NONE,
210  STRUCTURE_AUTO,
211  STRUCTURE_MANUAL
212  };
213  StructureDetection structure_detection_;
214 
215  std::vector<casadi_int> AB_offsets_, CD_offsets_, RSQ_offsets_, I_offsets_;
216  bool debug_;
217  };
218 
219 } // namespace casadi
221 
222 #endif // CASADI_FATROP_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.