snopt_solver.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2017, Alexander W. Winkler, ETH Zurich. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright notice,
7  this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice,
9  this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of ETH ZURICH nor the names of its contributors may be
12  used to endorse or promote products derived from this software without
13  specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL ETH ZURICH BE LIABLE FOR ANY DIRECT, INDIRECT,
19 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ******************************************************************************/
26 
27 #include <ifopt/snopt_adapter.h>
28 #include <ifopt/snopt_solver.h>
29 
30 namespace ifopt {
31 
32 void SnoptSolver::Solve(Problem& ref)
33 {
34  SnoptAdapter snopt(ref);
35  snopt.Init();
36 
37  // A complete list of options can be found in the snopt user guide:
38  // https://web.stanford.edu/group/SOL/guides/sndoc7.pdf
39  snopt.setProbName("snopt");
40  snopt.setIntParameter("Major Print level", 1);
41  snopt.setIntParameter("Minor Print level", 1);
42  snopt.setIntParameter("Derivative option",
43  1); // 1 = snopt will not calculate missing derivatives
44  snopt.setIntParameter("Verify level ",
45  3); // full check on gradients, will throw error
46  snopt.setIntParameter("Iterations limit", 200000);
47  snopt.setRealParameter("Major feasibility tolerance",
48  1.0e-4); // target nonlinear constraint violation
49  snopt.setRealParameter("Minor feasibility tolerance",
50  1.0e-4); // for satisfying the QP bounds
51  snopt.setRealParameter("Major optimality tolerance",
52  1.0e-2); // target complementarity gap
53 
54  // error codes as given in the manual.
55  int Cold = 0; // Basis = 1, Warm = 2;
56 
57  // interface changed with snopt version 7.6
58 #ifdef SNOPT76
59  int nS = 0; // number of super-basic variables (not relevant for cold start)
60  int nInf; // nInf : number of constraints outside of the bounds
61  double sInf; // sInf : sum of infeasibilities
62 
63  status_ = snopt.solve(
64  Cold, snopt.neF, snopt.n, snopt.ObjAdd, snopt.ObjRow,
65  &SnoptAdapter::ObjectiveAndConstraintFct, snopt.iAfun, snopt.jAvar,
66  snopt.A, snopt.neA, snopt.iGfun, snopt.jGvar, snopt.neG, snopt.xlow,
67  snopt.xupp, snopt.Flow, snopt.Fupp, snopt.x, snopt.xstate, snopt.xmul,
68  snopt.F, snopt.Fstate, snopt.Fmul, nS, nInf, sInf);
69 #else
70  status_ = snopt.solve(Cold);
71 #endif
72 
73  int EXIT = status_ - status_ % 10; // change least significant digit to zero
74 
75  if (EXIT != 0) {
76  std::string msg =
77  "ERROR: Snopt failed to find a solution. EXIT:" + std::to_string(EXIT) +
78  ", INFO:" + std::to_string(status_) + "\n";
79  throw std::runtime_error(msg);
80  }
81 
82  snopt.SetVariables();
83 }
84 
85 } /* namespace ifopt */
ifopt::SnoptAdapter::ObjectiveAndConstraintFct
static void ObjectiveAndConstraintFct(int *Status, int *n, double x[], int *needF, int *neF, double F[], int *needG, int *neG, double G[], char *cu, int *lencu, int iu[], int *leniu, double ru[], int *lenru)
Definition: snopt_adapter.cc:154
ifopt::Solver::status_
int status_
Definition: solver.h:87
ifopt
common namespace for all elements in this library.
Definition: bounds.h:33
snopt_adapter.h
snopt_solver.h
ifopt::SnoptSolver::Solve
void Solve(Problem &nlp) override
Creates a snoptProblemA from nlp and solves it.
Definition: snopt_solver.cc:56


ifopt
Author(s): Alexander W. Winkler
autogenerated on Mon Sep 18 2023 02:14:38