knitro_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_KNITRO_INTERFACE_HPP
27 #define CASADI_KNITRO_INTERFACE_HPP
28 
29 #include <casadi/interfaces/knitro/casadi_nlpsol_knitro_export.h>
30 #include <knitro.h>
31 #include "casadi/core/nlpsol_impl.hpp"
32 
43 namespace casadi {
44  // Forward declaration
45  class KnitroInterface;
46 
47  struct KnitroMemory : public NlpsolMemory {
49  const KnitroInterface& self;
50 
51  // KNITRO context pointer
52  KN_context *kc;
53 
54  // KNITRO callback pointer
55  CB_context *cb;
56 
57  // Inputs
58  double *wlbx, *wubx, *wlbg, *wubg;
59 
60  // Stats
61  const char* return_status;
62 
64  KnitroMemory(const KnitroInterface& self);
65 
67  ~KnitroMemory();
68  };
69 
74  class KnitroInterface : public Nlpsol {
75  public:
76  Sparsity jacg_sp_;
77  Sparsity hesslag_sp_;
78 
79  explicit KnitroInterface(const std::string& name, const Function& nlp);
80  ~KnitroInterface() override;
81 
82  // Get name of the plugin
83  const char* plugin_name() const override { return "knitro";}
84 
85  // Get name of the class
86  std::string class_name() const override { return "KnitroInterface";}
87 
89  static Nlpsol* creator(const std::string& name, const Function& nlp) {
90  return new KnitroInterface(name, nlp);
91  }
92 
94 
95  static const Options options_;
96  const Options& get_options() const override { return options_;}
98 
99  // Initialize the solver
100  void init(const Dict& opts) override;
101 
103  void* alloc_mem() const override { return new KnitroMemory(*this);}
104 
106  int init_mem(void* mem) const override;
107 
109  void free_mem(void *mem) const override { delete static_cast<KnitroMemory*>(mem);}
110 
112  void set_work(void* mem, const double**& arg, double**& res,
113  casadi_int*& iw, double*& w) const override;
114 
115  // Solve the NLP
116  int solve(void* mem) const override;
117 
119  bool integer_support() const override { return true;}
120 
121  // KNITRO callback wrapper
122  static int callback(KN_context_ptr kc,
123  CB_context_ptr cb,
124  KN_eval_request_ptr const evalRequest,
125  KN_eval_result_ptr const evalResult,
126  void * const userParams);
127 
128  // KNITRO return codes
129  static const char* return_codes(int flag);
130 
132  Dict get_stats(void* mem) const override;
133 
134  // KNITRO options
135  Dict opts_;
136 
137  // Type of constraints
138  std::vector<int> contype_;
139 
140  // Type of complementarity constraints
141  std::vector<int> comp_type_;
142 
143  // Index pair for complementarity constraints
144  std::vector<int> comp_i1_;
145  std::vector<int> comp_i2_;
146 
147  // KNITRO options file
148  std::string options_file_;
149 
151  static const std::string meta_doc;
152 
154  void serialize_body(SerializingStream &s) const override;
155 
157  static ProtoFunction* deserialize(DeserializingStream& s) { return new KnitroInterface(s); }
158 
159  protected:
161  explicit KnitroInterface(DeserializingStream& s);
162 
163  };
164 
165 } // namespace casadi
166 
168 #endif // CASADI_KNITRO_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.