List of all members | Public Member Functions
casadi::NlImporter Class Reference

#include <nlp_builder.hpp>

Detailed Description

\Helper class for .nl import The .nl format is described in "Writing .nl Files" paper by David M. Gay (2005)

Date
2016
Author
Joel Andersson

Definition at line 97 of file nlp_builder.hpp.

Public Member Functions

 NlImporter (NlpBuilder &nlp, const std::string &filename, const Dict &opts)
 
 ~NlImporter ()
 

Constructor & Destructor Documentation

◆ NlImporter()

casadi::NlImporter::NlImporter ( NlpBuilder nlp,
const std::string &  filename,
const Dict opts 
)

Definition at line 48 of file nlp_builder.cpp.

49  : nlp_(nlp), s_ptr_(Filesystem::ifstream_ptr(filename)) {
50  // Set default options
51  verbose_=false;
52  std::istream& s = *s_ptr_;
53 
54  // Read user options
55  for (auto&& op : opts) {
56  if (op.first == "verbose") {
57  verbose_ = op.second;
58  } else {
59  std::stringstream ss;
60  ss << "Unknown option \"" << op.first << "\"" << std::endl;
61  throw CasadiException(ss.str());
62  }
63  }
64  // Open file for reading
65  if (verbose_) casadi_message("Reading file \"" + filename + "\"");
66 
67  // Read the header of the NL-file (first 10 lines)
68  const casadi_int header_sz = 10;
69  std::vector<std::string> header(header_sz);
70  for (casadi_int k=0; k<header_sz; ++k) {
71  getline(s, header[k]);
72  }
73 
74  // Assert that the file is not in binary form
75  if (header.at(0).at(0)=='g') {
76  binary_ = false;
77  } else if (header.at(0).at(0)=='b') {
78  binary_ = true;
79  } else {
80  casadi_error("File could not be read");
81  }
82 
83  // Get the number of objectives and constraints
84  std::stringstream ss(header[1]);
85  ss >> n_var_ >> n_con_ >> n_obj_ >> n_eq_ >> n_lcon_;
86  if (verbose_) {
87  casadi_message("n_var=" + str(n_var_) + ", n_con =" + str(n_con_) + ", "
88  "n_obj=" + str(n_obj_) + ", n_eq=" + str(n_eq_) + ", "
89  "n_lcon=" + str(n_lcon_));
90  }
91 
92  // Get the number of nonlinear vars in constraints, objectives, both
93  std::stringstream ss4(header[4]);
94  ss4 >> nlvc_ >> nlvo_ >> nlvb_;
95  if (verbose_) {
96  casadi_message("nlvc=" + str(nlvc_) + ", nlvo=" + str(nlvo_) + ", nlvb=" + str(nlvb_));
97  }
98 
99  // Get the number of discrete variables
100  std::stringstream ss6(header[6]);
101  ss6 >> nbv_ >> niv_ >> nlvbi_ >> nlvci_ >> nlvoi_;
102  if (verbose_) {
103  casadi_message("nbv=" + str(nbv_) + ", niv =" + str(niv_) + ", "
104  "nlvbi=" + str(nlvbi_) + ", nlvci=" + str(nlvci_) + ", "
105  "nlvoi=" + str(nlvoi_));
106  }
107 
108  // Allocate variables
109  nlp_.x = MX::sym("x", 1, 1, n_var_);
110 
111  // Allocate f and c
112  nlp_.f = 0;
113  nlp_.g.resize(n_con_, 0);
114 
115  // Allocate bounds for x and primal initial guess
116  nlp_.x_lb.resize(n_var_, -inf);
117  nlp_.x_ub.resize(n_var_, inf);
118  nlp_.x_init.resize(n_var_, 0);
119 
120  // Allocate bounds for g and dual initial guess
121  nlp_.g_lb.resize(n_con_, -inf);
122  nlp_.g_ub.resize(n_con_, inf);
123  nlp_.lambda_init.resize(n_con_, 0);
124 
125  // Allocate binary variables vector
126  nlp_.discrete.clear();
127 
128  //D. M. Gay and M. Hill, 'Hooking Your Solver to AMPL' October, 1997.
129  // continuous in an objective and in a constraint
130  for (casadi_int j=0; j<nlvb_-nlvbi_; ++j) nlp_.discrete.push_back(false);
131 
132  // integer in an objective and in a constraint
133  for (casadi_int j=0; j<nlvbi_; ++j) nlp_.discrete.push_back(true);
134 
135  // continuous just in constraints
136  for (casadi_int j=0; j<nlvc_ - (nlvb_ + nlvci_); ++j) nlp_.discrete.push_back(false);
137 
138  // integer just in constraints
139  for (casadi_int j=0; j<nlvci_; ++j) nlp_.discrete.push_back(true);
140 
141  // continuous just in objectives
142  for (casadi_int j=0; j<nlvo_ - (nlvc_ + nlvoi_); ++j) nlp_.discrete.push_back(false);
143 
144  // integer just in objectives
145  for (casadi_int j=0; j < nlvoi_; ++j) nlp_.discrete.push_back(true);
146 
147  // linear
148  casadi_int max_nlvc_nlvo = (nlvc_ < nlvo_) ? nlvo_ : nlvc_;
149  for (casadi_int j=0; j<n_var_-(max_nlvc_nlvo+niv_+nbv_); ++j) nlp_.discrete.push_back(false);
150 
151  // binary
152  for (casadi_int j = 0; j<nbv_; ++j) nlp_.discrete.push_back(true);
153 
154  // other integer
155  for (casadi_int j = 0; j<niv_; ++j) nlp_.discrete.push_back(true);
156 
157  casadi_assert(nlp_.discrete.size()==n_var_,
158  "Number of variables in the header don't match");
159 
160  // All variables, including dependent
161  v_ = nlp_.x;
162 
163  if (binary_) {
164  std::streampos offset = s.tellg();
165  s_ptr_.reset();
166  s_ptr_ = Filesystem::ifstream_ptr(filename, std::ifstream::binary);
167  s_ptr_->seekg(offset);
168  }
169 
170  // Read segments
171  parse();
172 
173  // multiple the objective sign
174  nlp_.f = sign_*nlp_.f;
175  }
static std::unique_ptr< std::istream > ifstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::in, bool fail=true)
Definition: filesystem.cpp:135
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
std::vector< double > x_lb
Bounds on x.
Definition: nlp_builder.hpp:59
std::vector< double > g_ub
Variables.
Definition: nlp_builder.hpp:62
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< double > x_ub
Variables.
Definition: nlp_builder.hpp:59
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
MX f
Objective.
Definition: nlp_builder.hpp:53
std::vector< double > x_init
Primal initial guess.
Definition: nlp_builder.hpp:65
std::string str(const T &v)
String representation, any type.
const double inf
infinity
Definition: calculus.hpp:50
std::string filename(const std::string &path)
Definition: ghc.cpp:55

References casadi::NlpBuilder::discrete, casadi::NlpBuilder::f, casadi::filename(), casadi::NlpBuilder::g, casadi::NlpBuilder::g_lb, casadi::NlpBuilder::g_ub, casadi::Filesystem::ifstream_ptr(), casadi::inf, casadi::NlpBuilder::lambda_init, casadi::str(), casadi::GenericMatrix< MX >::sym(), casadi::NlpBuilder::x, casadi::NlpBuilder::x_init, casadi::NlpBuilder::x_lb, and casadi::NlpBuilder::x_ub.

◆ ~NlImporter()

casadi::NlImporter::~NlImporter ( )

Definition at line 177 of file nlp_builder.cpp.

177  {
178  // Close the NL file
179  s_ptr_.reset();
180  }

The documentation for this class was generated from the following files: