uno_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 #ifndef CASADI_UNO_INTERFACE_HPP
26 #define CASADI_UNO_INTERFACE_HPP
27 
28 #include <casadi/interfaces/uno/casadi_nlpsol_uno_export.h>
29 #include "casadi/core/nlpsol_impl.hpp"
30 #include <Uno_C_API.h>
31 
32 namespace casadi {
33  #include "uno_runtime.hpp"
34 }
35 
46 namespace casadi {
47  class UnoInterface;
48 
49  struct UnoMemory : public NlpsolMemory {
50  const UnoInterface& self;
52  const char* return_status;
53  UnoMemory(const UnoInterface& uno_interface);
54  ~UnoMemory();
55  };
56 
61  class UnoInterface : public Nlpsol {
62  public:
63  explicit UnoInterface(const std::string& name, const Function& nlp);
64  ~UnoInterface() override;
65 
66  const char* plugin_name() const override { return "uno"; }
67  std::string class_name() const override { return "UnoInterface"; }
68 
69  static Nlpsol* creator(const std::string& name, const Function& nlp) {
70  return new UnoInterface(name, nlp);
71  }
72 
73  static const Options options_;
74  const Options& get_options() const override { return options_; }
75 
76  void init(const Dict& opts) override;
77  void* alloc_mem() const override { return new UnoMemory(*this); }
78  int init_mem(void* mem) const override;
79  void free_mem(void* mem) const override;
80  void set_work(void* mem, const double**& arg, double**& res,
81  casadi_int*& iw, double*& w) const override;
82  int solve(void* mem) const override;
83  Dict get_stats(void* mem) const override;
84 
85  void serialize_body(SerializingStream& s) const override;
86  static ProtoFunction* deserialize(DeserializingStream& s) {
87  return new UnoInterface(s);
88  }
89 
90  static const std::string meta_doc;
91 
92  // Codegen
93  void codegen_body(CodeGenerator& g) const override;
94  void codegen_declarations(CodeGenerator& g) const override;
95  void codegen_init_mem(CodeGenerator& g) const override;
96  void codegen_free_mem(CodeGenerator& g) const override;
97  std::string codegen_mem_type() const override { return "struct casadi_uno_data"; }
98  bool codegen_needs_mem() const override { return true; }
99 
100  protected:
101  explicit UnoInterface(DeserializingStream& s);
102 
103  private:
104  // Build the prob struct (sparsity ptrs + callback fn ptrs) from members.
105  // Used at C++ init() time. Codegen has set_uno_prob(g) instead.
106  void set_uno_prob();
107  void set_uno_prob(CodeGenerator& g) const;
108 
109  // NLP sparsities discovered in init().
110  Sparsity jacg_sp_;
111  Sparsity hesslag_sp_;
112  // Sparsity index arrays in Uno's int width, filled once in init().
113  std::vector<uno_int> jacobian_row_indices_;
114  std::vector<uno_int> jacobian_column_indices_;
115  std::vector<uno_int> hessian_row_indices_;
116  std::vector<uno_int> hessian_column_indices_;
117  // -inf / +inf placeholder constraint bounds passed to uno_set_constraints at
118  // init_mem time -- kept as members so the pointer stays valid across the call
119  // (real bounds are applied per-solve). Variable bounds aren't part of the
120  // (unconstrained) model, so no x placeholders are needed.
121  std::vector<double> placeholder_lb_g_, placeholder_ub_g_;
122  // Solver-specific options forwarded to uno (the {"uno": {...}} dict).
123  Dict opts_;
124  // Filled in by set_uno_prob(). One member per UnoInterface; the prob
125  // pointers (sparsities, fn ptrs) outlive every solve.
127  };
128 
129 } // namespace casadi
131 #endif // CASADI_UNO_INTERFACE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.