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