nlp_builder.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_NLP_BUILDER_HPP
27 #define CASADI_NLP_BUILDER_HPP
28 
29 #include "mx.hpp"
30 #include <memory>
31 
32 namespace casadi {
33 
40  class CASADI_EXPORT NlpBuilder
41  : public SWIG_IF_ELSE(PrintableCommon, Printable<NlpBuilder>) {
42  public:
43 
48 
50  std::vector<MX> x;
51 
53  MX f;
54 
56  std::vector<MX> g;
57 
59  std::vector<double> x_lb, x_ub;
60 
62  std::vector<double> g_lb, g_ub;
63 
65  std::vector<double> x_init;
66 
68  std::vector<double> lambda_init;
69 
71  std::vector<bool> discrete;
73 
75  void import_nl(const std::string& filename, const Dict& opts = Dict());
76 
78  std::string type_name() const {return "NlpBuilder";}
79 
81  void disp(std::ostream& stream, bool more=false) const;
82 
84  std::string get_str(bool more=false) const {
85  std::stringstream ss;
86  disp(ss, more);
87  return ss.str();
88  }
89  };
90 
91 #ifndef SWIG
97  class CASADI_EXPORT NlImporter {
98  public:
99  // Constructor
100  NlImporter(NlpBuilder& nlp, const std::string& filename, const Dict& opts);
101  // Destructor
102  ~NlImporter();
103  private:
104  int read_int();
105  char read_char();
106  double read_double();
107  short read_short();
108  long read_long();
109  // Reference to the class
110  NlpBuilder& nlp_;
111  // Options
112  bool verbose_;
113  // Binary mode
114  bool binary_;
115  // File stream
116  std::unique_ptr<std::istream> s_ptr_;
117  // All variables, including dependent
118  std::vector<MX> v_;
119  // Number of objectives and constraints
120  casadi_int n_var_, n_con_, n_obj_, n_eq_, n_lcon_;
121  // nonlinear vars in constraints, objectives, both
122  // see JuliaOpt/AmplNLWriter.jl/src/nl_write.jl
123  casadi_int nlvc_, nlvo_, nlvb_;
124  // Number of discrete variables // see JuliaOpt/AmplNLWriter.jl/src/nl_write.jl
125  casadi_int nbv_, niv_, nlvbi_, nlvci_, nlvoi_;
126  // objective sign
127  MX sign_;
128  // Parse the file
129  void parse();
130  // Imported function description
131  void F_segment();
132  // Suffix values
133  void S_segment();
134  // Defined variable definition
135  void V_segment();
136  // Algebraic constraint body
137  void C_segment();
138  // Logical constraint expression
139  void L_segment();
140  // Objective function
141  void O_segment();
142  // Dual initial guess
143  void d_segment();
144  // Primal initial guess
145  void x_segment();
146  // Bounds on algebraic constraint bodies ("ranges")
147  void r_segment();
148  // Bounds on variable
149  void b_segment();
150  // Jacobian row counts
151  void k_segment();
152  // Linear terms in the constraint function
153  void J_segment();
154  // Linear terms in the objective function
155  void G_segment();
157  MX expr();
158  };
159 #endif // SWIG
160 
161 } // namespace casadi
162 
163 #endif // CASADI_NLP_BUILDER_HPP
MX - Matrix expression.
Definition: mx.hpp:92
A symbolic NLP representation.
Definition: nlp_builder.hpp:41
std::vector< double > x_lb
Bounds on x.
Definition: nlp_builder.hpp:59
std::vector< bool > discrete
Discrete variables.
Definition: nlp_builder.hpp:71
std::vector< double > lambda_init
Dual initial guess.
Definition: nlp_builder.hpp:68
std::vector< MX > x
Variables.
Definition: nlp_builder.hpp:50
std::vector< double > g_lb
Bounds on g.
Definition: nlp_builder.hpp:62
std::vector< MX > g
Constraints.
Definition: nlp_builder.hpp:56
std::string get_str(bool more=false) const
Get string representation.
Definition: nlp_builder.hpp:84
std::string type_name() const
Readable name of the class.
Definition: nlp_builder.hpp:78
MX f
Objective.
Definition: nlp_builder.hpp:53
std::vector< double > x_init
Primal initial guess.
Definition: nlp_builder.hpp:65
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::string filename(const std::string &path)
Definition: ghc.cpp:55