List of all members | Classes | Public Member Functions | Static Public Member Functions | Public Attributes
casadi::Options Struct Reference

Options metadata for a class. More...

#include <options.hpp>

Detailed Description

Author
Joel Andersson, Joris Gillis
Date
2010-2016

Extra doc: https://github.com/casadi/casadi/wiki/L_x9

Definition at line 40 of file options.hpp.

Classes

struct  Entry
 

Public Member Functions

const Options::Entryfind (const std::string &name) const
 
std::vector< std::string > all () const
 
std::string type (const std::string &name) const
 
std::string info (const std::string &name) const
 
void disp (std::ostream &stream) const
 
std::vector< std::string > suggestions (const std::string &word, casadi_int amount=5) const
 Get the best suggestions for a misspelled word. More...
 
void best_matches (const std::string &word, std::vector< std::pair< double, std::string > > &best) const
 Find best matches. More...
 
void check (const Dict &opts) const
 Check if options exist. More...
 
void print_all (std::ostream &stream) const
 Print list of options. More...
 
void print_one (const std::string &name, std::ostream &stream) const
 Print all information there is to know about a certain option. More...
 

Static Public Member Functions

static double word_distance (const std::string &a, const std::string &b)
 A distance metric between two words. More...
 
static bool has_dot (const Dict &opts)
 Does the dictionary contain a dot. More...
 
static bool has_null (const Dict &opts)
 Does the dictionary has null objects. More...
 
static bool is_sane (const Dict &opts)
 Is the dictionary sane. More...
 
static Dict sanitize (const Dict &opts, bool top_level=true)
 Sanitize a options dictionary. More...
 

Public Attributes

std::vector< const Options * > bases
 
std::map< std::string, Entryentries
 

Member Function Documentation

◆ all()

std::vector< std::string > casadi::Options::all ( ) const

Definition at line 283 of file options.cpp.

283  {
284  std::vector<std::string> ret;
285  for (auto&& e : entries) ret.push_back(e.first);
286  return ret;
287  }
std::map< std::string, Entry > entries
Definition: options.hpp:55

References entries.

Referenced by casadi::conic_options(), casadi::nlpsol_options(), and casadi::rootfinder_options().

◆ best_matches()

void casadi::Options::best_matches ( const std::string &  word,
std::vector< std::pair< double, std::string > > &  best 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_xc

Definition at line 127 of file options.cpp.

128  {
129  // Iterate over bases
130  for (auto&& b : bases) {
131  b->best_matches(word, best);
132  }
133 
134  // Worst match so far
135  auto worst = max_element(best.begin(), best.end());
136 
137  // Loop over entries
138  for (auto&& e : entries) {
139  // Get word distance
140  double d = word_distance(e.first, word);
141 
142  // Keep if better than the worst amongst the suggestions
143  if (d < worst->first) {
144  worst->first = d;
145  worst->second = e.first;
146  worst = max_element(best.begin(), best.end());
147  }
148  }
149  }
std::vector< const Options * > bases
Definition: options.hpp:43
static double word_distance(const std::string &a, const std::string &b)
A distance metric between two words.
Definition: options.cpp:69

References bases, entries, and word_distance().

Referenced by suggestions().

◆ check()

void casadi::Options::check ( const Dict opts) const

Definition at line 240 of file options.cpp.

240  {
241  // Make sure all options exist and have the correct type
242  for (auto&& op : opts) {
243  const Options::Entry* entry = find(op.first);
244 
245  // Informative error message if option does not exist
246  if (entry==nullptr) {
247  std::stringstream ss;
248  ss << "Unknown option: " << op.first << std::endl;
249  ss << std::endl;
250  ss << "Did you mean one of the following?" << std::endl;
251  for (auto&& s : suggestions(op.first)) {
252  print_one(s, ss);
253  }
254  ss << "Use print_options() to get a full list of options." << std::endl;
255  casadi_error(ss.str());
256  }
257 
258  // Check type
259  casadi_assert(op.second.can_cast_to(entry->type),
260  "Illegal type for " + op.first + ": " +
261  op.second.get_description() +
262  " cannot be cast to " +
263  GenericType::get_type_description(entry->type) + ".");
264 
265  }
266  }
static std::string get_type_description(TypeID type)
Get a description of a type.
const Options::Entry * find(const std::string &name) const
Definition: options.cpp:32
std::vector< std::string > suggestions(const std::string &word, casadi_int amount=5) const
Get the best suggestions for a misspelled word.
Definition: options.cpp:105
void print_one(const std::string &name, std::ostream &stream) const
Print all information there is to know about a certain option.
Definition: options.cpp:274

References find(), casadi::GenericType::get_type_description(), print_one(), suggestions(), and casadi::Options::Entry::type.

Referenced by casadi::ProtoFunction::construct(), and casadi::ImporterInternal::construct().

◆ disp()

void casadi::Options::disp ( std::ostream &  stream) const

Definition at line 57 of file options.cpp.

57  {
58  // Print bases
59  for (auto&& b : bases) {
60  b->disp(stream);
61  }
62 
63  // Print all entries
64  for (auto&& e : entries) {
65  e.second.disp(e.first, stream);
66  }
67  }

References bases, and entries.

Referenced by print_all().

◆ find()

const Options::Entry * casadi::Options::find ( const std::string &  name) const

Definition at line 32 of file options.cpp.

32  {
33  // Check if in one of the bases
34  for (auto&& b : bases) {
35  const Options::Entry* entry = b->find(name);
36  if (entry) return entry;
37  }
38 
39  // Lookup in this class
40  auto it = entries.find(name);
41  if (it!=entries.end()) {
42  return &it->second;
43  } else {
44  return nullptr;
45  }
46  }

References bases, and entries.

Referenced by check(), casadi::ProtoFunction::has_option(), info(), print_one(), and type().

◆ has_dot()

bool casadi::Options::has_dot ( const Dict opts)
static

Definition at line 151 of file options.cpp.

151  {
152  for (auto&& op : opts) {
153  if (op.first.find('.') != std::string::npos || op.first.find("__") != std::string::npos) {
154  return true;
155  }
156  // Call recursively
157  if (op.second.is_dict() && has_dot(op.second)) return true;
158  }
159  return false;
160  }
static bool has_dot(const Dict &opts)
Does the dictionary contain a dot.
Definition: options.cpp:151

Referenced by is_sane(), and sanitize().

◆ has_null()

bool casadi::Options::has_null ( const Dict opts)
static

Definition at line 162 of file options.cpp.

162  {
163  for (auto&& op : opts) {
164  if (op.second.is_null()) return true;
165  }
166  return false;
167  }

Referenced by is_sane(), and sanitize().

◆ info()

std::string casadi::Options::info ( const std::string &  name) const

Definition at line 295 of file options.cpp.

295  {
296  const Options::Entry* entry = find(name);
297  casadi_assert(entry!=nullptr, "Option \"" + name + "\" does not exist");
298  return entry->description;
299  }

References casadi::Options::Entry::description, and find().

Referenced by casadi::conic_option_info(), casadi::nlpsol_option_info(), and casadi::rootfinder_option_info().

◆ is_sane()

bool casadi::Options::is_sane ( const Dict opts)
static

Definition at line 169 of file options.cpp.

169  {
170  return !has_dot(opts) && !has_null(opts);
171  }
static bool has_null(const Dict &opts)
Does the dictionary has null objects.
Definition: options.cpp:162

References has_dot(), and has_null().

Referenced by casadi::ProtoFunction::construct(), and casadi::ImporterInternal::construct().

◆ print_all()

void casadi::Options::print_all ( std::ostream &  stream) const

Extra doc: https://github.com/casadi/casadi/wiki/L_xd

Definition at line 268 of file options.cpp.

268  {
269  stream << "\"Option name\" [type] = value" << std::endl;
270  disp(stream);
271  stream << std::endl;
272  }
void disp(std::ostream &stream) const
Definition: options.cpp:57

References disp().

Referenced by casadi::ProtoFunction::print_options().

◆ print_one()

void casadi::Options::print_one ( const std::string &  name,
std::ostream &  stream 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_xe

Definition at line 274 of file options.cpp.

274  {
275  const Options::Entry* entry = find(name);
276  if (entry!=nullptr) {
277  entry->disp(name, stream);
278  } else {
279  stream << " \"" << name << "\" does not exist.";
280  }
281  }

References casadi::Options::Entry::disp(), and find().

Referenced by check(), and casadi::ProtoFunction::print_option().

◆ sanitize()

Dict casadi::Options::sanitize ( const Dict opts,
bool  top_level = true 
)
static

Definition at line 173 of file options.cpp.

173  {
174  // Drop nulls
175  if (top_level && has_null(opts)) {
176  // Create a new dictionary without the null entries
177  Dict ret;
178  for (auto&& op : opts) {
179  if (!op.second.is_null()) ret[op.first] = op.second;
180  }
181  return sanitize(ret, false);
182  }
183 
184  // Treat the case where any of the options have a dot (dictionary shorthand)
185  if (has_dot(opts)) {
186  // New options dictionary being constructed
187  Dict ret;
188 
189  // Sub-dictionary and corresponding name being constructed
190  Dict sopts;
191  std::string sname;
192 
193  // Process options
194  for (auto&& op : opts) {
195  // Find the dot if any
196  std::string::size_type dotpos = op.first.find('.'), dotpos_end;
197  if (dotpos==std::string::npos) {
198  dotpos = op.first.find("__");
199  if (dotpos!=std::string::npos) dotpos_end = dotpos+2;
200  } else {
201  dotpos_end = dotpos+1;
202  }
203 
204  // Flush last sub-dictionary
205  if (!sname.empty() && (dotpos==std::string::npos
206  || op.first.compare(0, dotpos, sname)!=0)) {
207  update_dict(ret, sname, sanitize(sopts, false), true);
208 
209  sname.clear();
210  sopts.clear();
211  }
212 
213  GenericType value = op.second;
214  if (value.is_dict()) {
215  value = sanitize(value, false);
216  }
217 
218  // Add to dictionary
219  if (dotpos != std::string::npos) {
220  sname = op.first.substr(0, dotpos);
221  std::string target_name = op.first.substr(dotpos_end);
222  sopts[target_name] = value;
223  } else {
224  update_dict(ret, op.first, value, true);
225  }
226  }
227 
228  // Flush trailing sub-dictionary
229  if (!sname.empty()) {
230  update_dict(ret, sname, sanitize(sopts, false), true);
231  }
232 
233  return ret;
234  }
235 
236  // Nothing to do
237  return opts;
238  }
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void update_dict(Dict &target, const Dict &source, bool recurse)
Update the target dictionary in place with source elements.
static Dict sanitize(const Dict &opts, bool top_level=true)
Sanitize a options dictionary.
Definition: options.cpp:173

References has_dot(), has_null(), casadi::GenericType::is_dict(), and casadi::update_dict().

Referenced by casadi::IpoptInterface::codegen_body(), casadi::ProtoFunction::construct(), casadi::ImporterInternal::construct(), casadi::AlpaqaInterface::init(), and casadi::IpoptInterface::init_mem().

◆ suggestions()

std::vector< std::string > casadi::Options::suggestions ( const std::string &  word,
casadi_int  amount = 5 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_xb

Definition at line 105 of file options.cpp.

105  {
106  // Best distances so far
107  const double inf = std::numeric_limits<double>::infinity();
108  std::vector<std::pair<double, std::string>> best(amount, {inf, ""});
109 
110  // Iterate over elements
111  best_matches(word, best);
112 
113  // Sort the elements in ascending order
114  stable_sort(best.begin(), best.end());
115 
116  // Collect the values that are non-infinite
117  std::vector<std::string> ret;
118  ret.reserve(amount);
119  for (auto&& e : best) {
120  if (e.first!=inf) {
121  ret.push_back(e.second);
122  }
123  }
124  return ret;
125  }
const double inf
infinity
Definition: calculus.hpp:50
void best_matches(const std::string &word, std::vector< std::pair< double, std::string > > &best) const
Find best matches.
Definition: options.cpp:127

References best_matches(), and casadi::inf.

Referenced by check().

◆ type()

std::string casadi::Options::type ( const std::string &  name) const

Definition at line 289 of file options.cpp.

289  {
290  const Options::Entry* entry = find(name);
291  casadi_assert(entry!=nullptr, "Option \"" + name + "\" does not exist");
292  return GenericType::get_type_description(entry->type);
293  }

References find(), casadi::GenericType::get_type_description(), and casadi::Options::Entry::type.

Referenced by casadi::conic_option_type(), casadi::nlpsol_option_type(), and casadi::rootfinder_option_type().

◆ word_distance()

double casadi::Options::word_distance ( const std::string &  a,
const std::string &  b 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_xa

Levenshtein edit distance

Definition at line 69 of file options.cpp.

69  {
71  if (a == b) return 0;
72  casadi_int na = a.size();
73  casadi_int nb = b.size();
74  if (na == 0) return static_cast<double>(nb);
75  if (nb == 0) return static_cast<double>(na);
76 
77  std::vector<casadi_int> v0(nb+1, 0);
78  std::vector<casadi_int> v1(nb+1, 0);
79 
80  for (casadi_int i=0;i<nb+1;++i)
81  v0[i] = i;
82 
83  char s;
84  char t;
85  std::locale loc;
86  for (casadi_int i=0;i<na;i++) {
87  v1[0] = i + 1;
88  for (casadi_int j=0; j<nb; j++) {
89  s = std::tolower(a[i], loc);
90  t = std::tolower(b[j], loc);
91  casadi_int cost = 0;
92  if (s != t)
93  cost = 1;
94 
95  v1[j+1] = std::min(std::min(v1[j] + 1, v0[j+1] + 1), v0[j] + cost);
96  }
97 
98  for (casadi_int j=0; j<nb+1; j++)
99  v0[j] = v1[j];
100  }
101 
102  return static_cast<double>(v1[nb]);
103  }

Referenced by best_matches().

Member Data Documentation

◆ bases

std::vector<const Options*> casadi::Options::bases

Definition at line 43 of file options.hpp.

Referenced by best_matches(), disp(), and find().

◆ entries

std::map<std::string, Entry> casadi::Options::entries

Definition at line 55 of file options.hpp.

Referenced by all(), best_matches(), disp(), and find().


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