snopt_interface.cpp
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 #include "snopt_interface.hpp"
26 #include "casadi/core/casadi_misc.hpp"
27 
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctime>
32 #include <utility>
33 #include <algorithm>
34 #include <iomanip>
35 
36 namespace casadi {
37 
38  extern "C"
39  int CASADI_NLPSOL_SNOPT_EXPORT
40  casadi_register_nlpsol_snopt(Nlpsol::Plugin* plugin) {
41  plugin->creator = SnoptInterface::creator;
42  plugin->name = "snopt";
43  plugin->doc = SnoptInterface::meta_doc.c_str();
44  plugin->version = CASADI_VERSION;
45  plugin->options = &SnoptInterface::options_;
46  plugin->deserialize = &SnoptInterface::deserialize;
47  return 0;
48  }
49 
50  extern "C"
51  void CASADI_NLPSOL_SNOPT_EXPORT casadi_load_nlpsol_snopt() {
53  }
54 
55  SnoptInterface::SnoptInterface(const std::string& name, const Function& nlp)
56  : Nlpsol(name, nlp) {
57  }
58 
60  clear_mem();
61  }
62 
64  = {{&Nlpsol::options_},
65  {{"snopt",
66  {OT_DICT,
67  "Options to be passed to SNOPT"}},
68  {"start",
69  {OT_STRING,
70  "Warm-start options for Worhp: cold|warm|hot"}}
71  }
72  };
73 
74  void SnoptInterface::init(const Dict& opts) {
75  // Call the init method of the base class
76  Nlpsol::init(opts);
77 
78  // Default: cold start
79  Cold_ = 0;
80 
81  // Read user options
82  for (auto&& op : opts) {
83  if (op.first=="snopt") {
84  opts_ = op.second;
85  } else if (op.first=="start") {
86  std::string start = op.second.to_string();
87  if (start=="cold") {
88  Cold_ = 0;
89  } else if (start=="warm") {
90  Cold_ = 1;
91  } else if (start=="hot") {
92  Cold_ = 2;
93  } else {
94  casadi_error("Unknown start option: " + start);
95  }
96  }
97  }
98 
99  inf_ = 1e20;
100 
101  for (auto&& op : opts_) {
102  if (op.first=="Infinite_bound") {
103  inf_ = op.second;
104  }
105  }
106 
107  // Get/generate required functions
108  Function jac_f_fcn = create_function("nlp_jac_f", {"x", "p"}, {"f", "jac:f:x"});
109  casadi_assert_dev(!jac_f_fcn.is_null());
110  Function jac_g_fcn = create_function("nlp_jac_g", {"x", "p"}, {"g", "jac:g:x"});
111  casadi_assert_dev(!jac_g_fcn.is_null());
112  jacg_sp_ = jac_g_fcn.sparsity_out(1);
113  jacf_sp_ = jac_f_fcn.sparsity_out(1);
114 
115  // prepare the mapping for constraints
116  nnJac_ = nx_;
117  nnObj_ = nx_;
118  nnCon_ = ng_;
119 
120  casadi_assert(ng_>0, "SNOPT requires at least one constraint");
121 
122  // Here follows the core of the mapping
123  // Two integer matrices are constructed:
124  // one with gradF sparsity, and one with jacG sparsity
125  // the integer values denote the nonzero locations into the original gradF/jacG
126  // but with a special encoding: entries of gradF are encoded "-1-i" and
127  // entries of jacG are encoded "1+i"
128  // "0" is to be interpreted not as an index but as a literal zero
129 
130  IM mapping_jacG = IM(0, nx_);
131  IM mapping_gradF = IM(jacf_sp_,
132  range(-1, -1-jacf_sp_.nnz(), -1));
133 
134  mapping_jacG = IM(jacg_sp_, range(1, jacg_sp_.nnz()+1));
135 
136  // First, remap jacG
137  A_structure_ = mapping_jacG;
138 
139  m_ = ng_;
140 
141  // Construct the linear objective row
142  IM d = mapping_gradF(Slice(0), Slice()); // NOLINT(cppcoreguidelines-slicing)
143 
144  std::vector<casadi_int> ii = mapping_gradF.sparsity().get_col();
145  for (casadi_int j = 0; j < nnObj_; ++j) {
146  if (d.colind(j) != d.colind(j+1)) {
147  casadi_int k = d.colind(j);
148  d.nz(k) = 0;
149  }
150  }
151 
152  // Make it as sparse as you can
153  d = sparsify(d);
154 
155  jacF_row_ = d.nnz() != 0;
156  if (jacF_row_) { // We need an objective gradient row
157  A_structure_ = vertcat(A_structure_, d);
158  m_ +=1;
159  }
160  iObj_ = jacF_row_ ? (m_ - 1) : -1;
161 
162  // Is the A matrix completely empty?
163  dummyrow_ = A_structure_.nnz() == 0; // Then we need a dummy row
164  if (dummyrow_) {
165  IM dummyrow = IM(1, nx_);
166  dummyrow(0, 0) = 0;
167  A_structure_ = vertcat(A_structure_, dummyrow);
168  m_+=1;
169  }
170 
171  // We don't need a dummy row if a linear objective row is present
172  casadi_assert_dev(!(dummyrow_ && jacF_row_));
173 
174  // Allocate temporary memory
175  alloc_w(nx_, true); // xk2_
176  alloc_w(ng_, true); // lam_gk_
177  alloc_w(nx_, true); // lam_xk_
178  alloc_w(ng_, true); // gk_
179  alloc_w(jacf_sp_.nnz(), true); // jac_fk_
180  if (!jacg_sp_.is_null()) {
181  alloc_w(jacg_sp_.nnz(), true); // jac_gk_
182  }
183  }
184 
185  int SnoptInterface::init_mem(void* mem) const {
186  if (Nlpsol::init_mem(mem)) return 1;
187  auto m = static_cast<SnoptMemory*>(mem);
188 
189  // Allocate data structures needed in evaluate
190  m->A_data.resize(A_structure_.nnz());
191  m->bl.resize(nx_+ng_);
192  m->bu.resize(nx_+ng_);
193  m->hs.resize(nx_+ng_);
194  m->xx.resize(nx_+ng_);
195  m->rc.resize(nx_+ng_);
196  m->pi.resize(ng_);
197  m->locJ.resize(A_structure_.size2()+1);
198  m->indJ.resize(A_structure_.nnz());
199  m->valJ.resize(A_structure_.nnz());
200  return 0;
201  }
202 
203 std::map<int, std::string> SnoptInterface::status_ =
204  {{0, "Finished successfully"},
205  {1, "The problem appears to be infeasible"},
206  {2, "The problem appears to be unbounded"},
207  {3, "Resource limit error"},
208  {4, "Terminated after numerical difficulties"},
209  {5, "Error in the user-supplied functions"},
210  {6, "Undefined user-supplied functions"},
211  {7, "User requested termination"},
212  {8, "Insufficient storage allocated"},
213  {9, "Input arguments out of range"},
214  {14, "System error"}};
215 
216 std::map<int, std::string> SnoptInterface::secondary_status_ =
217  {{1, "optimality conditions satisfied"},
218  {2, "feasible point found"},
219  {3, "requested accuracy could not be achieve"},
220  {5, "elastic objective minimized"},
221  {6, "elastic infeasibilities minimized"},
222  {11, "infeasible linear constraints"},
223  {12, "infeasible linear equality constraints"},
224  {13, "nonlinear infeasibilities minimized"},
225  {14, "linear infeasibilities minimized"},
226  {15, "infeasible linear constraints in QP subproblem"},
227  {16, "infeasible nonelastic constraints"},
228  {21, "unbounded objective"},
229  {22, "constraint violation limit reached"},
230  {31, "iteration limit reached"},
231  {32, "major iteration limit reached"},
232  {33, "the superbasics limit is too small"},
233  {34, "time limit reached"},
234  {41, "current point cannot be improved"},
235  {42, "singular basis"},
236  {43, "cannot satisfy the general constraints"},
237  {44, "ill-conditioned null-space basis"},
238  {45, "unable to compute acceptable LU factors"},
239  {51, "incorrect objective derivatives"},
240  {52, "incorrect constraint derivatives"},
241  {56, "irregular or badly scaled problem functions"},
242  {61, "undefined function at the first feasible point"},
243  {62, "undefined function at the initial point"},
244  {63, "unable to proceed into undefined region"},
245  {71, "terminated during function evaluation"},
246  {74, "terminated from monitor routine"},
247  {81, "work arrays must have at least 500 elements"},
248  {82, "not enough character storage"},
249  {83, "not enough integer storage"},
250  {84, "not enough real storage"},
251  {91, "invalid input argument"},
252  {92, "basis file dimensions do not match this problem"},
253  {141, "wrong number of basic variables"},
254  {142, "error in basis package"}};
255 
256  std::string SnoptInterface::formatStatus(int status) const {
257  status = status/10;
258  if (status_.find(status) == status_.end()) {
259  return "Unknown status: " + str(status);
260  } else {
261  return (*status_.find(status)).second;
262  }
263  }
264 
265  std::string SnoptInterface::formatSecondaryStatus(int status) const {
266  if (secondary_status_.find(status) == secondary_status_.end()) {
267  return "Unknown status: " + str(status);
268  } else {
269  return (*secondary_status_.find(status)).second;
270  }
271  }
272 
273  void SnoptInterface::set_work(void* mem, const double**& arg, double**& res,
274  casadi_int*& iw, double*& w) const {
275  auto m = static_cast<SnoptMemory*>(mem);
276 
277  // Set work in base classes
278  Nlpsol::set_work(mem, arg, res, iw, w);
279 
280  // Work vectors
281  m->xk2 = w; w += nx_;
282  m->lam_gk = w; w += ng_;
283  m->lam_xk = w; w += nx_;
284  m->gk = w; w += ng_;
285  m->jac_fk = w; w += jacf_sp_.nnz();
286  if (!jacg_sp_.is_null()) {
287  m->jac_gk = w; w += jacg_sp_.nnz();
288  }
289  }
290 
291  int SnoptInterface::solve(void* mem) const {
292  auto m = static_cast<SnoptMemory*>(mem);
293  auto d_nlp = &m->d_nlp;
294 
295  // Memory object
296  snProblem prob;
297 
298  // Problem has not been solved at this point
299  m->return_status = -1;
300 
301  // Evaluate gradF and jacG at initial value
302  m->arg[0] = d_nlp->z;
303  m->arg[1] = d_nlp->p;
304  m->res[0] = nullptr;
305  m->res[1] = m->jac_gk;
306  calc_function(m, "nlp_jac_g");
307  m->res[0] = nullptr;
308  m->res[1] = m->jac_fk;
309  calc_function(m, "nlp_jac_f");
310 
311  // perform the mapping:
312  // populate A_data_ (the nonzeros of A)
313  // with numbers pulled from jacG and gradF
314  for (casadi_int k = 0; k < A_structure_.nnz(); ++k) {
315  casadi_int i = A_structure_.nonzeros()[k];
316  if (i == 0) {
317  m->A_data[k] = 0;
318  } else if (i > 0) {
319  m->A_data[k] = m->jac_gk[i-1];
320  } else {
321  m->A_data[k] = m->jac_fk[-i-1];
322  }
323  }
324 
325  casadi_int n = nx_;
326  casadi_int nea = A_structure_.nnz();
327  double ObjAdd = 0;
328 
329  casadi_assert_dev(m_ > 0);
330  casadi_assert_dev(n > 0);
331  casadi_assert_dev(nea > 0);
332  casadi_assert_dev(A_structure_.nnz() == nea);
333 
334  // snInit must be called first.
335  // 9, 6 are print and summary unit numbers (for Fortran).
336  // 6 == standard out
337  casadi_int isumm = 6;
338  std::string outname = name_ + ".out";
339  snInit(&prob, const_cast<char*>(name_.c_str()),
340  const_cast<char*>(outname.c_str()), isumm);
341 
342  // user data
343  prob.leniu = 1;
344  prob.iu = &m->memind;
345 
346  // Pass bounds
347  casadi_copy(d_nlp->lbz, nx_+ng_, get_ptr(m->bl));
348  casadi_copy(d_nlp->ubz, nx_+ng_, get_ptr(m->bu));
349 
350  for (casadi_int i=0; i<nx_+ng_; ++i) if (isinf(m->bl[i])) m->bl[i] = -inf_;
351  for (casadi_int i=0; i<nx_+ng_; ++i) if (isinf(m->bu[i])) m->bu[i] = inf_;
352  // Initialize states and slack
353  casadi_clear(get_ptr(m->hs), ng_ + nx_);
354  casadi_copy(d_nlp->z, nx_, get_ptr(m->xx));
355  casadi_clear(get_ptr(m->xx) + nx_, ng_);
356 
357  // Initialize multipliers
358  casadi_copy(d_nlp->lam + nx_, ng_, get_ptr(m->pi));
359 
360  // Set up Jacobian matrix
361  copy_vector(A_structure_.colind(), m->locJ);
362  copy_vector(A_structure_.row(), m->indJ);
363  casadi_copy(get_ptr(m->A_data), A_structure_.nnz(), get_ptr(m->valJ));
364 
365  for (auto&& op : opts_) {
366  // Replace underscores with spaces
367  std::string opname = op.first;
368  std::replace(opname.begin(), opname.end(), '_', ' ');
369 
370  // Try integer
371  if (op.second.can_cast_to(OT_INT)) {
372  casadi_assert_dev(opname.size() <= 55);
373  casadi_int flag = setIntParameter(&prob, const_cast<char*>(opname.c_str()),
374  op.second.to_int());
375  if (flag==0) continue;
376  }
377 
378  // Try double
379  if (op.second.can_cast_to(OT_DOUBLE)) {
380  casadi_assert_dev(opname.size() <= 55);
381  casadi_int flag = setRealParameter(&prob, const_cast<char*>(opname.c_str()),
382  op.second.to_double());
383  if (flag==0) continue;
384  }
385 
386  // try string
387  if (op.second.can_cast_to(OT_STRING)) {
388  std::string buffer = opname + " " + op.second.to_string();
389  casadi_assert_dev(buffer.size() <= 72);
390  casadi_int flag = setParameter(&prob, const_cast<char*>(buffer.c_str()));
391  if (flag==0) continue;
392  }
393 
394  // Error if reached this point
395  casadi_error("SNOPT error setting option \"" + opname + "\"");
396  }
397 
398  int nS = 0, nInf = 0;
399  double sInf;
400 
401  // Run SNOPT
402  int info = solveC(&prob, Cold_, m_, nx_, nea, nnCon_, nnObj_, nnJac_,
403  iObj_, ObjAdd,
404  userfunPtr,
405  get_ptr(m->valJ), get_ptr(m->indJ), get_ptr(m->locJ),
406  get_ptr(m->bl), get_ptr(m->bu), get_ptr(m->hs),
407  get_ptr(m->xx), get_ptr(m->pi), get_ptr(m->rc),
408  &d_nlp->objective, &nS, &nInf, &sInf);
409  m->success = info<10;
410  m->return_status = info;
411  casadi_assert(99 != info, "snopt problem set up improperly");
412  if (info/10==3) m->unified_return_status = SOLVER_RET_LIMITED;
413 
414  if (verbose_) casadi_message("SNOPT return status: " + formatStatus(m->return_status) +
415  ":" + formatSecondaryStatus(m->return_status));
416 
417  // Negate rc to match CasADi's definition
418  casadi_scal(nx_ + ng_, -1., get_ptr(m->rc));
419 
420  // Get primal solution
421  casadi_copy(get_ptr(m->xx), nx_, d_nlp->z);
422 
423  // Get dual solution
424  casadi_copy(get_ptr(m->rc), nx_, d_nlp->lam);
425  casadi_copy(get_ptr(m->rc)+nx_, ng_, d_nlp->lam + nx_);
426 
427  // Copy optimal constraint values to output
428  casadi_copy(m->gk, ng_, d_nlp->z + nx_);
429 
430  // Free memory
431  deleteSNOPT(&prob);
432  return 0;
433  }
434 
436  userfun(SnoptMemory* m, int* mode, int nnObj, int nnCon, int nnJac, int nnL, int neJac,
437  const double* x, double* fObj, double*gObj, double* fCon, double* gCon,
438  int nState, char* cu, int lencu, int* iu, int leniu, double* ru,
439  int lenru) const {
440  try {
441  auto d_nlp = &m->d_nlp;
442  casadi_assert(nnCon_ == nnCon, "Con " + str(nnCon_) + " <-> " + str(nnCon));
443  casadi_assert(nnObj_ == nnObj, "Obj " + str(nnObj_) + " <-> " + str(nnObj));
444  casadi_assert(nnJac_ == nnJac, "Jac " + str(nnJac_) + " <-> " + str(nnJac));
445 
446  // Get reduced decision variables
447  casadi_clear(m->xk2, nx_);
448  for (casadi_int k = 0; k < nnObj; ++k) m->xk2[k] = x[k];
449 
450  // Evaluate gradF with the linear variables put to zero
451  const double** arg = m->arg;
452  *arg++ = m->xk2;
453  *arg++ = d_nlp->p;
454  double** res = m->res;
455  *res++ = fObj;
456  *res++ = m->jac_fk;
457  calc_function(m, "nlp_jac_f");
458 
459  // provide nonlinear part of objective gradient to SNOPT
460  for (casadi_int k = 0; k < nnObj; ++k) {
461  casadi_int el = jacf_sp_.colind(k);
462  if (jacf_sp_.colind(k+1) > el) {
463  gObj[k] = m->jac_fk[el];
464  } else {
465  gObj[k] = 0;
466  }
467  }
468 
469  {
470  // Get reduced decision variables
471  casadi_clear(m->xk2, nx_);
472  for (casadi_int k = 0; k < nnJac; ++k) {
473  m->xk2[k] = x[k];
474  }
475 
476  // Evaluate jacG with the linear variabes put to zero
477  const double** arg = m->arg;
478  *arg++ = m->xk2;
479  *arg++ = d_nlp->p;
480  double** res = m->res;
481  *res++ = m->gk;
482  *res++ = m->jac_gk;
483  calc_function(m, "nlp_jac_g");
484 
485  // provide nonlinear part of constraint jacobian to SNOPT
486  casadi_int kk = 0;
487  for (casadi_int j = 0; j < nnJac; ++j) {
488  for (casadi_int k = A_structure_.colind(j);
489  k < A_structure_.sparsity().colind(j+1); ++k) {
490  if (A_structure_.row(k) >= nnCon) break;
491  casadi_int i = A_structure_.nonzeros()[k];
492  if (i > 0) {
493  gCon[kk++] = m->jac_gk[i-1];
494  }
495  }
496  }
497 
498  casadi_assert_dev(kk == 0 || kk == neJac);
499 
500  // provide nonlinear part of objective to SNOPT
501  for (casadi_int k = 0; k < nnCon; ++k) {
502  fCon[k] = m->gk[k];
503  }
504  }
505  } catch(KeyboardInterruptException& ex) {
506  casadi_warning("KeyboardInterruptException");
507  *mode = -2;
508  return;
509  } catch(std::exception& ex) {
510  uerr() << "eval_nlp failed: " << ex.what() << std::endl;
511  *mode = -1; // Reduce step size - we've got problems
512  return;
513  }
514  }
515 
517  userfunPtr(int * mode, int* nnObj, int * nnCon, int *nJac, // NOLINT
518  int *nnL, int * neJac, double *x, double *fObj, // NOLINT
519  double *gObj, double * fCon, double* gCon, // NOLINT
520  int* nState, char* cu, int* lencu, int* iu, // NOLINT
521  int* leniu, double* ru, int *lenru) { // NOLINT
522  auto m = SnoptMemory::mempool.at(iu[0]);
523  m->self.userfun(m, mode, *nnObj, *nnCon, *nJac, *nnL, *neJac,
524  x, fObj, gObj, fCon, gCon, *nState,
525  cu, *lencu, iu, *leniu, ru, *lenru);
526  }
527 
528  SnoptMemory::SnoptMemory(const SnoptInterface& self) : self(self) {
529  // Put in memory pool
530  auto mem_it = std::find(mempool.begin(), mempool.end(), nullptr);
531  if (mem_it==mempool.end()) {
532  // Append to end
533  memind = mempool.size();
534  mempool.push_back(this);
535  } else {
536  // Reuse freed element
537  memind = mem_it - mempool.begin();
538  *mem_it = this;
539  }
540  }
541 
543  // Remove from memory pool
544  auto mem_it = std::find(mempool.begin(), mempool.end(), this);
545  if (mem_it==mempool.end()) {
546  casadi_warning("SNOPT memory pool failure");
547  } else {
548  *mem_it = nullptr;
549  }
550  }
551 
552  Dict SnoptInterface::get_stats(void* mem) const {
553  Dict stats = Nlpsol::get_stats(mem);
554  auto m = static_cast<SnoptMemory*>(mem);
555  stats["return_status"] = formatStatus(m->return_status);
556  stats["secondary_return_status"] = formatSecondaryStatus(m->return_status);
557 
558  return stats;
559  }
560 
561  std::vector<SnoptMemory*> SnoptMemory::mempool;
562 
564  s.version("SnoptInterface", 1);
565  s.unpack("SnoptInterface::jacf_sp", jacf_sp_);
566  s.unpack("SnoptInterface::jacg_sp", jacg_sp_);
567  s.unpack("SnoptInterface::nnJac", nnJac_);
568  s.unpack("SnoptInterface::nnObj", nnObj_);
569  s.unpack("SnoptInterface::nnCon", nnCon_);
570  s.unpack("SnoptInterface::A_structure", A_structure_);
571  s.unpack("SnoptInterface::m", m_);
572  s.unpack("SnoptInterface::iObj", iObj_);
573 
574  s.unpack("SnoptInterface::jacF_row", jacF_row_);
575  s.unpack("SnoptInterface::dummyrow", dummyrow_);
576  s.unpack("SnoptInterface::Cold_", Cold_);
577  s.unpack("SnoptInterface::inf", inf_);
578 
579  s.unpack("SnoptInterface::opts", opts_);
580  }
581 
584  s.version("SnoptInterface", 1);
585  s.pack("SnoptInterface::jacf_sp", jacf_sp_);
586  s.pack("SnoptInterface::jacg_sp", jacg_sp_);
587  s.pack("SnoptInterface::nnJac", nnJac_);
588  s.pack("SnoptInterface::nnObj", nnObj_);
589  s.pack("SnoptInterface::nnCon", nnCon_);
590  s.pack("SnoptInterface::A_structure", A_structure_);
591  s.pack("SnoptInterface::m", m_);
592  s.pack("SnoptInterface::iObj", iObj_);
593 
594  s.pack("SnoptInterface::jacF_row", jacF_row_);
595  s.pack("SnoptInterface::dummyrow", dummyrow_);
596  s.pack("SnoptInterface::Cold_", Cold_);
597  s.pack("SnoptInterface::inf", inf_);
598 
599  s.pack("SnoptInterface::opts", opts_);
600  }
601 } // namespace casadi
const char * what() const override
Display error.
Definition: exception.hpp:90
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
Function object.
Definition: function.hpp:60
const Sparsity & sparsity_out(casadi_int ind) const
Get sparsity of a given output.
Definition: function.cpp:1031
casadi_int nnz() const
Get the number of (structural) non-zero elements.
casadi_int size2() const
Get the second dimension (i.e. number of columns)
const MatType nz(const K &k) const
Get vector nonzero or slice of nonzeros.
const casadi_int * colind() const
Get the sparsity pattern. See the Sparsity class for details.
const casadi_int * row() const
Get the sparsity pattern. See the Sparsity class for details.
bool is_null() const
Is a null pointer?
std::vector< Scalar > & nonzeros()
const Sparsity & sparsity() const
Const access the sparsity - reference to data member.
NLP solver storage class.
Definition: nlpsol_impl.hpp:59
Dict get_stats(void *mem) const override
Get all statistics.
Definition: nlpsol.cpp:1162
static const Options options_
Options.
void init(const Dict &opts) override
Initialize.
Definition: nlpsol.cpp:420
casadi_int ng_
Number of constraints.
Definition: nlpsol_impl.hpp:69
int init_mem(void *mem) const override
Initalize memory block.
Definition: nlpsol.cpp:603
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: nlpsol.cpp:1306
casadi_int nx_
Number of variables.
Definition: nlpsol_impl.hpp:66
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
Definition: nlpsol.cpp:795
Function create_function(const Function &oracle, const std::string &fname, const std::vector< std::string > &s_in, const std::vector< std::string > &s_out, const Function::AuxOut &aux=Function::AuxOut(), const Dict &opts=Dict())
int calc_function(OracleMemory *m, const std::string &fcn, const double *const *arg=nullptr, int thread_id=0) const
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
bool verbose_
Verbose printout.
void clear_mem()
Clear all memory (called from destructor)
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
Class representing a Slice.
Definition: slice.hpp:48
'snopt' plugin for Nlpsol
static const std::string meta_doc
A documentation string.
Dict get_stats(void *mem) const override
Get all statistics.
std::string formatSecondaryStatus(int status) const
void init(const Dict &opts) override
Initialize.
static const Options options_
Options.
static std::map< int, std::string > secondary_status_
static void userfunPtr(int *mode, int *nnObj, int *nnCon, int *nJac, int *nnL, int *neJac, double *x, double *fObj, double *gObj, double *fCon, double *gCon, int *nState, char *cu, int *lencu, int *iu, int *leniu, double *ru, int *lenru)
casadi_int Cold_
Warm-start settings.
static std::map< int, std::string > status_
static Nlpsol * creator(const std::string &name, const Function &nlp)
Create a new NLP Solver.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into MX.
std::string formatStatus(int status) const
int solve(void *mem) const override
void userfun(SnoptMemory *m, int *mode, int nnObj, int nnCon, int nnJac, int nnL, int neJac, const double *x, double *fObj, double *gObj, double *fCon, double *gCon, int nState, char *cu, int lencu, int *iu, int leniu, double *ru, int lenru) const
int init_mem(void *mem) const override
Initalize memory block.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
SnoptInterface(const std::string &name, const Function &nlp)
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
std::vector< casadi_int > get_col() const
Get the column for each non-zero entry.
Definition: sparsity.cpp:368
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
Definition: sparsity.cpp:168
The casadi namespace.
Definition: archiver.cpp:28
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.
std::ostream & uerr()
void copy_vector(const std::vector< S > &s, std::vector< D > &d)
void CASADI_NLPSOL_SNOPT_EXPORT casadi_load_nlpsol_snopt()
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
int CASADI_NLPSOL_SNOPT_EXPORT casadi_register_nlpsol_snopt(Nlpsol::Plugin *plugin)
Matrix< casadi_int > IM
Definition: im_fwd.hpp:31
void casadi_scal(casadi_int n, T1 alpha, T1 *x)
SCAL: x <- alpha*x.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
void casadi_clear(T1 *x, casadi_int n)
CLEAR: x <- 0.
@ SOLVER_RET_LIMITED
casadi_nlpsol_data< double > d_nlp
Definition: nlpsol_impl.hpp:42
Options metadata for a class.
Definition: options.hpp:40
SnoptMemory(const SnoptInterface &self)
Constructor.
std::vector< double > A_data
static std::vector< SnoptMemory * > mempool