snopt76_adapter.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 
28 
29 namespace ifopt {
30 
32 
33 void
35 {
36  SnoptAdapter snopt(ref);
37  snopt.Init();
38  SetOptions(snopt);
39 
40  // error codes as given in the manual.
41  int Cold = 0; // Basis = 1, Warm = 2;
42 
43  // new interface for 'Solve' using SNOPT76
44  int nS = 0; // number of super-basic variables (not relevant for cold start)
45  int nInf; // nInf : number of constraints outside of the bounds
46  double sInf;// sInf : sum of infeasibilities
47 
48  int INFO = snopt.solve(Cold, snopt.neF, snopt.n, snopt.ObjAdd,
50  snopt.iAfun, snopt.jAvar, snopt.A, snopt.neA,
51  snopt.iGfun, snopt.jGvar, snopt.neG,
52  snopt.xlow, snopt.xupp, snopt.Flow, snopt.Fupp,
53  snopt.x, snopt.xstate, snopt.xmul,
54  snopt.F, snopt.Fstate, snopt.Fmul,
55  nS, nInf, sInf);
56 
57  int EXIT = INFO - INFO%10; // change least significant digit to zero
58 
59  if (EXIT != 0) {
60  std::string msg = "Snopt failed to find a solution. EXIT:" + std::to_string(EXIT) + ", INFO:" + std::to_string(INFO);
61  throw std::runtime_error(msg);
62  }
63 
64  snopt.SetVariables();
65 }
66 
68 {
69  // A complete list of options can be found in the snopt user guide:
70  // https://web.stanford.edu/group/SOL/guides/sndoc7.pdf
71 
72  solver.setProbName( "snopt" );
73  solver.setIntParameter( "Major Print level", 1 );
74  solver.setIntParameter( "Minor Print level", 1 );
75  solver.setParameter( "Solution");
76  solver.setIntParameter( "Derivative option", 1 ); // 1 = snopt will not calculate missing derivatives
77  solver.setIntParameter( "Verify level ", 3 ); // full check on gradients, will throw error
78  solver.setIntParameter("Iterations limit", 200000);
79  solver.setRealParameter( "Major feasibility tolerance", 1.0e-3); // target nonlinear constraint violation
80  solver.setRealParameter( "Minor feasibility tolerance", 1.0e-3); // for satisfying the QP bounds
81  solver.setRealParameter( "Major optimality tolerance", 1.0e-2); // target complementarity gap
82 }
83 
85 {
86  nlp_ = &ref;
87 }
88 
89 void
91 {
92  int obj_count = nlp_->HasCostTerms()? 1 : 0;
94  neF = nlp_->GetNumberOfConstraints() + obj_count;
95 
96  x = new double[n];
97  xlow = new double[n];
98  xupp = new double[n];
99  xmul = new double[n];
100  xstate = new int[n];
101 
102  F = new double[neF];
103  Flow = new double[neF];
104  Fupp = new double[neF];
105  Fmul = new double[neF];
106  Fstate = new int[neF];
107 
108  // Set the upper and lower bounds.
109  // no bounds on the spline coefficients or footholds
110  auto bounds_x = nlp_->GetBoundsOnOptimizationVariables();
111  for (uint _n=0; _n<bounds_x.size(); ++_n) {
112  xlow[_n] = bounds_x.at(_n).lower_;
113  xupp[_n] = bounds_x.at(_n).upper_;
114  xstate[_n] = 0;
115  }
116 
117  // bounds on the cost function if it exists
118  int c = 0;
119  if (nlp_->HasCostTerms()) {
120  Flow[c] = -1e20; Fupp[c] = 1e20; Fmul[c] = 0.0; // no bounds on cost function
121  c++;
122  }
123 
124  // bounds on equality and inequality constraints
125  auto bounds_g = nlp_->GetBoundsOnConstraints();
126  for (const auto& b : bounds_g) {
127  Flow[c] = b.lower_; Fupp[c] = b.upper_; Fmul[c] = 0.0;
128  c++;
129  }
130 
131  // initial values of the optimization
132  VectorXd x_all = nlp_->GetVariableValues();
133  Eigen::Map<VectorXd>(&x[0], x_all.rows()) = x_all;
134 
135  ObjRow = nlp_->HasCostTerms()? 0 : -1; // the row in user function that corresponds to the objective function
136  ObjAdd = 0.0; // the constant to be added to the objective function
137 
138  // no linear derivatives/just assume all are nonlinear
139  lenA = 0;
140  neA = 0;
141  iAfun = nullptr;
142  jAvar = nullptr;
143  A = nullptr;
144 
145  // derivatives of nonlinear part
146  lenG = obj_count*n + nlp_->GetJacobianOfConstraints().nonZeros();
147  iGfun = new int[lenG];
148  jGvar = new int[lenG];
149 
150  // the gradient terms of the cost function
151  neG=0; // nonzero cells in jacobian of Cost Function AND Constraints
152 
153  // the nonzero elements of cost function (assume all)
154  if(nlp_->HasCostTerms()) {
155  for (int var=0; var<n; ++var) {
156  iGfun[neG] = 0;
157  jGvar[neG] = var;
158  neG++;
159  }
160  }
161 
162  // the nonzero elements of constraints (assume all)
163  auto jac = nlp_->GetJacobianOfConstraints();
164  for (int k=0; k<jac.outerSize(); ++k) {
165  for (Jacobian::InnerIterator it(jac,k); it; ++it) {
166  iGfun[neG] = it.row() + obj_count;
167  jGvar[neG] = it.col();
168  neG++;
169  }
170  }
171 
172  // Snopt76 requires initialization
173  this->initialize("", 1); // no print_file and summary ON
174 
175  // Snopt76 needs to setup the workspace
176  this->setWorkspace(neF,n,neA,neG);
177 
178 }
179 
180 void
181 SnoptAdapter::ObjectiveAndConstraintFct (int* Status, int* n, double x[],
182  int* needF, int* neF, double F[],
183  int* needG, int* neG, double G[],
184  char* cu, int* lencu, int iu[],
185  int* leniu, double ru[], int* lenru)
186 {
187  if ( *needF > 0 ) {
188  int i=0;
189 
190  // the scalar objective function value
191  if (nlp_->HasCostTerms())
192  F[i++] = nlp_->EvaluateCostFunction(x);
193 
194  // the vector of constraint values
195  VectorXd g_eig = nlp_->EvaluateConstraints(x);
196  Eigen::Map<VectorXd>(F+i, g_eig.rows()) = g_eig;
197  }
198 
199 
200  if ( *needG > 0 ) {
201  int i=0;
202 
203  // the jacobian of the first row (cost function)
204  if (nlp_->HasCostTerms()) {
205  Eigen::VectorXd grad = nlp_->EvaluateCostFunctionGradient(x);
206  i = grad.rows();
207  Eigen::Map<VectorXd>(G, i) = grad;
208  }
209 
210  // the jacobian of all the constraints
211  nlp_->EvalNonzerosOfJacobian(x, G+i);
212  nlp_->SaveCurrent();
213  }
214 }
215 
216 void
218 {
219  nlp_->SetVariables(x);
220 }
221 
222 } /* namespace opt */
Problem::VectorXd VectorXd
VecBound GetBoundsOnConstraints() const
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)
double EvaluateCostFunction(const double *x)
VectorXd GetVariableValues() const
VectorXd EvaluateCostFunctionGradient(const double *x)
void SaveCurrent()
bool HasCostTerms() const
VecBound GetBoundsOnOptimizationVariables() const
static NLPPtr nlp_
void SetVariables(const double *x)
Jacobian GetJacobianOfConstraints() const
static void Solve(Problem &nlp)
Creates a snoptProblemA from nlp and solves it.
SnoptAdapter(Problem &nlp)
Creates an Adapter Object around the problem to conform to the Snopt interface.
void EvalNonzerosOfJacobian(const double *x, double *values)
static void SetOptions(SnoptAdapter &)
Sets solver settings for Snopt.
int GetNumberOfConstraints() const
VectorXd EvaluateConstraints(const double *x)
Solves the optimization problem with the new SNOPT 7.6 version.
int GetNumberOfOptimizationVariables() const


ifopt_snopt
Author(s): Alexander W. Winkler
autogenerated on Fri Apr 20 2018 02:27:35