25 #include "snopt_interface.hpp"
26 #include "casadi/core/casadi_misc.hpp"
39 int CASADI_NLPSOL_SNOPT_EXPORT
42 plugin->name =
"snopt";
44 plugin->version = CASADI_VERSION;
67 "Options to be passed to SNOPT"}},
70 "Warm-start options for Worhp: cold|warm|hot"}}
82 for (
auto&& op : opts) {
83 if (op.first==
"snopt") {
85 }
else if (op.first==
"start") {
86 std::string start = op.second.to_string();
89 }
else if (start==
"warm") {
91 }
else if (start==
"hot") {
94 casadi_error(
"Unknown start option: " + start);
101 for (
auto&& op : opts_) {
102 if (op.first==
"Infinite_bound") {
109 casadi_assert_dev(!jac_f_fcn.
is_null());
111 casadi_assert_dev(!jac_g_fcn.
is_null());
120 casadi_assert(
ng_>0,
"SNOPT requires at least one constraint");
145 for (casadi_int j = 0; j <
nnObj_; ++j) {
147 casadi_int k = d.
colind(j);
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"}};
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"}};
259 return "Unknown status: " +
str(status);
261 return (*
status_.find(status)).second;
267 return "Unknown status: " +
str(status);
274 casadi_int*& iw,
double*& w)
const {
281 m->xk2 = w; w +=
nx_;
282 m->lam_gk = w; w +=
ng_;
283 m->lam_xk = w; w +=
nx_;
293 auto d_nlp = &m->
d_nlp;
299 m->return_status = -1;
302 m->arg[0] = d_nlp->
z;
303 m->arg[1] = d_nlp->p;
305 m->res[1] = m->jac_gk;
308 m->res[1] = m->jac_fk;
319 m->A_data[k] = m->jac_gk[i-1];
321 m->A_data[k] = m->jac_fk[-i-1];
329 casadi_assert_dev(
m_ > 0);
330 casadi_assert_dev(n > 0);
331 casadi_assert_dev(nea > 0);
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);
344 prob.iu = &m->memind;
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_;
365 for (
auto&& op : opts_) {
367 std::string opname = op.first;
368 std::replace(opname.begin(), opname.end(),
'_',
' ');
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()),
375 if (flag==0)
continue;
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;
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;
395 casadi_error(
"SNOPT error setting option \"" + opname +
"\"");
398 int nS = 0, nInf = 0;
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");
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,
441 auto d_nlp = &m->
d_nlp;
448 for (casadi_int k = 0; k < nnObj; ++k) m->
xk2[k] = x[k];
451 const double** arg = m->
arg;
454 double** res = m->
res;
460 for (casadi_int k = 0; k < nnObj; ++k) {
472 for (casadi_int k = 0; k < nnJac; ++k) {
477 const double** arg = m->
arg;
480 double** res = m->
res;
487 for (casadi_int j = 0; j < nnJac; ++j) {
493 gCon[kk++] = m->
jac_gk[i-1];
498 casadi_assert_dev(kk == 0 || kk == neJac);
501 for (casadi_int k = 0; k < nnCon; ++k) {
506 casadi_warning(
"KeyboardInterruptException");
509 }
catch(std::exception& ex) {
510 uerr() <<
"eval_nlp failed: " << ex.
what() << std::endl;
517 userfunPtr(
int * mode,
int* nnObj,
int * nnCon,
int *nJac,
518 int *nnL,
int * neJac,
double *x,
double *fObj,
519 double *gObj,
double * fCon,
double* gCon,
520 int* nState,
char* cu,
int* lencu,
int* iu,
521 int* leniu,
double* ru,
int *lenru) {
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);
546 casadi_warning(
"SNOPT memory pool failure");
555 stats[
"return_status"] =
formatStatus(m->return_status);
564 s.
version(
"SnoptInterface", 1);
579 s.
unpack(
"SnoptInterface::opts", opts_);
584 s.
version(
"SnoptInterface", 1);
591 s.
pack(
"SnoptInterface::m",
m_);
597 s.
pack(
"SnoptInterface::inf",
inf_);
599 s.
pack(
"SnoptInterface::opts", opts_);
const char * what() const override
Display error.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
virtual Dict info() const
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
const Sparsity & sparsity_out(casadi_int ind) const
Get sparsity of a given output.
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.
Dict get_stats(void *mem) const override
Get all statistics.
static const Options options_
Options.
void init(const Dict &opts) override
Initialize.
casadi_int ng_
Number of constraints.
int init_mem(void *mem) const override
Initalize memory block.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
casadi_int nx_
Number of variables.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
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.
'snopt' plugin for Nlpsol
~SnoptInterface() override
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.
casadi_int nnz() const
Get the number of (structural) non-zeros.
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.
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)
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.
casadi_nlpsol_data< double > d_nlp
Options metadata for a class.
SnoptMemory(const SnoptInterface &self)
Constructor.
std::vector< double > A_data
static std::vector< SnoptMemory * > mempool
~SnoptMemory()
Destructor.