worhp_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_WORHP_INTERFACE_HPP
27 #define CASADI_WORHP_INTERFACE_HPP
28 
29 #include "casadi/core/nlpsol_impl.hpp"
30 #include <casadi/interfaces/worhp/casadi_nlpsol_worhp_export.h>
31 
32 // Workaround for Clang, but should not be a problem for other compilers, #771
33 #define _Bool bool
34 
35 #include <worhp.h>
36 
37 // MACROs that pollute our code
38 #undef Q
48 namespace casadi {
49 
50  struct WorhpMemory : public NlpsolMemory {
51  OptVar worhp_o;
52  Workspace worhp_w;
53  Params worhp_p;
54  Control worhp_c;
55 
56  // Stats
57  casadi_int iter;
58  casadi_int iter_sqp;
59  double inf_pr;
60  double inf_du;
61  double alpha_pr;
62  casadi_int return_code;
63  const char* return_status;
64 
65  bool init_;
66 
68  WorhpMemory();
69 
71  ~WorhpMemory();
72  };
73 
78  class WorhpInterface : public Nlpsol {
79  public:
80  // NLP functions
81  Sparsity jacg_sp_;
82  Sparsity hesslag_sp_;
83 
84  // Constructor
85  explicit WorhpInterface(const std::string& name, const Function& nlp);
86 
87  // Destructor
88  ~WorhpInterface() override;
89 
90  // Get name of the plugin
91  const char* plugin_name() const override { return "worhp";}
92 
93  // Get name of the class
94  std::string class_name() const override { return "WorhpInterface";}
95 
97  static Nlpsol* creator(const std::string& name, const Function& nlp) {
98  return new WorhpInterface(name, nlp);
99  }
100 
101  // Reset solver
102  void reset();
103 
105 
106  static const Options options_;
107  const Options& get_options() const override { return options_;}
109 
110  // Initialize the solver
111  void init(const Dict& opts) override;
112 
114  void* alloc_mem() const override { return new WorhpMemory();}
115 
117  int init_mem(void* mem) const override;
118 
120  void free_mem(void *mem) const override { delete static_cast<WorhpMemory*>(mem);}
121 
123  Dict get_stats(void* mem) const override;
124 
126  void set_work(void* mem, const double**& arg, double**& res,
127  casadi_int*& iw, double*& w) const override;
128 
129  // Solve the NLP
130  int solve(void* mem) const override;
131 
132  // Options
133  std::map<std::string, bool> bool_opts_;
134  std::map<std::string, casadi_int> int_opts_;
135  std::map<std::string, double> double_opts_;
136  Dict qp_opts_;
137 
138  // WORHP return codes
139  static const char* return_codes(casadi_int flag);
140 
142  static const std::string meta_doc;
143 
145  void serialize_body(SerializingStream &s) const override;
146 
148  static ProtoFunction* deserialize(DeserializingStream& s) { return new WorhpInterface(s); }
149 
150  protected:
152  explicit WorhpInterface(DeserializingStream& s);
153  };
154 
155 } // namespace casadi
156 
158 #endif // CASADI_WORHP_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.