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 96 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 47 of file nlp_builder.cpp.

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

176  {
177  // Close the NL file
178  s_.close();
179  }

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