sleqp_interface.hpp
1 #include <sleqp.h>
2 
3 #include <casadi/interfaces/sleqp/casadi_nlpsol_sleqp_export.h>
4 #include "casadi/core/nlpsol_impl.hpp"
5 #include "casadi/core/timing.hpp"
6 
7 #include "sleqp.h"
8 
9 // TODO: Use casadi exceptions / error reporting??
10 #define SLEQP_CALL_EXC(x) \
11  do { \
12  const SLEQP_RETCODE _status = (x); \
13  if(_status != SLEQP_OKAY) { \
14  throw std::runtime_error("SLEQP error"); \
15  } \
16  } while(false)
17 
18 namespace casadi {
19 
20  class SLEQPInterface;
21 
22  struct SLEQPMemory : public NlpsolMemory {
23 
24  struct {
25  SleqpProblem* problem;
26 
27  SleqpVec* primal;
28  SleqpSolver* solver;
29  } internal;
30 
31  // Current calculated quantities
32  double* xk;
33  double *gk, *grad_fk, *jac_gk;
34 
35  // Additional callback data
36  double *cb_xk, *cb_lam_xk, *cb_lam_gk;
37 
38  // Hessian direction / product / multipliers
39  double *h_dk, *h_pk, *h_mk;
40 
42 
44  };
45 
46  class SLEQPInterface : public Nlpsol {
47  private:
48  Sparsity jacg_sp_;
49 
51  Dict opts_;
52 
53  int max_iter_;
54  double max_wall_time_;
55  int print_level_;
56 
57  SleqpSettings* settings_;
58 
59  void clear_mem_at(SLEQPMemory* m) const;
60 
61  bool exact_hess() const;
62 
63  void update_settings(const Dict& opts);
64 
65  public:
66  explicit SLEQPInterface(const std::string& name, const Function& nlp);
67  ~SLEQPInterface() override;
68 
69  static Nlpsol* creator(const std::string& name, const Function& nlp) {
70  return new SLEQPInterface(name, nlp);
71  }
72 
73  const Sparsity& jac_sparsity() const {return jacg_sp_;}
74 
75  const char* plugin_name() const override { return "sleqp";}
76 
77  // Get name of the class
78  std::string class_name() const override { return "SLEQPInterface";}
79 
80  static const Options options_;
81  static const std::string meta_doc;
82 
83  const Options& get_options() const override { return options_;}
85 
86  // Initialize the solver
87  void init(const Dict& opts) override;
88 
90  void* alloc_mem() const override { return new SLEQPMemory();}
91 
93  int init_mem(void* mem) const override;
94 
96  void free_mem(void *mem) const override;
97 
99  Dict get_stats(void* mem) const override;
100 
102  void set_work(void* mem, const double**& arg, double**& res,
103  casadi_int*& iw, double*& w) const override;
104 
105  // Solve the NLP
106  int solve(void* mem) const override;
107 
109  void serialize_body(SerializingStream &s) const override;
110 
112  static ProtoFunction* deserialize(DeserializingStream& s) { return new SLEQPInterface(s); }
113 
114  protected:
117  };
118 };
Helper class for Serialization.
Function object.
Definition: function.hpp:60
static const Options options_
int init_mem(void *mem) const override
Initalize memory block.
const Options & get_options() const override
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into MX.
void * alloc_mem() const override
Create memory block.
const Sparsity & jac_sparsity() const
int solve(void *mem) const override
std::string class_name() const override
SLEQPInterface(const std::string &name, const Function &nlp)
static const std::string meta_doc
const char * plugin_name() const override
~SLEQPInterface() override
void init(const Dict &opts) override
SLEQPInterface(DeserializingStream &s)
Deserializing constructor.
static Nlpsol * creator(const std::string &name, const Function &nlp)
Dict get_stats(void *mem) const override
Get all statistics.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
void free_mem(void *mem) const override
Free memory block.
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:99
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
SleqpProblem * problem
const SLEQPInterface * interface