snopt_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
34 SnoptAdapter::Solve (Problem& ref)
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  int INFO = snopt.solve(Cold);
43  int EXIT = INFO - INFO%10; // change least significant digit to zero
44 
45  if (EXIT != 0) {
46  std::string msg = "Snopt failed to find a solution. EXIT:" + std::to_string(EXIT) + ", INFO:" + std::to_string(INFO);
47  throw std::runtime_error(msg);
48  }
49 
50  snopt.SetVariables();
51 }
52 
54 {
55  // A complete list of options can be found in the snopt user guide:
56  // https://web.stanford.edu/group/SOL/guides/sndoc7.pdf
57 
58  solver.setProbName( "snopt" );
59  solver.setIntParameter( "Major Print level", 1 );
60  solver.setIntParameter( "Minor Print level", 1 );
61  solver.setParameter( "Solution");
62  solver.setIntParameter( "Derivative option", 1 ); // 1 = snopt will not calculate missing derivatives
63  solver.setIntParameter( "Verify level ", 3 ); // full check on gradients, will throw error
64  solver.setIntParameter("Iterations limit", 200000);
65  solver.setRealParameter( "Major feasibility tolerance", 1.0e-3); // target nonlinear constraint violation
66  solver.setRealParameter( "Minor feasibility tolerance", 1.0e-3); // for satisfying the QP bounds
67  solver.setRealParameter( "Major optimality tolerance", 1.0e-2); // target complementarity gap
68 }
69 
70 SnoptAdapter::SnoptAdapter (Problem& ref)
71 {
72  nlp_ = &ref;
73 }
74 
75 void
77 {
78  int obj_count = nlp_->HasCostTerms()? 1 : 0;
80  neF = nlp_->GetNumberOfConstraints() + obj_count;
81 
82  x = new double[n];
83  xlow = new double[n];
84  xupp = new double[n];
85  xmul = new double[n];
86  xstate = new int[n];
87 
88  F = new double[neF];
89  Flow = new double[neF];
90  Fupp = new double[neF];
91  Fmul = new double[neF];
92  Fstate = new int[neF];
93 
94  // Set the upper and lower bounds.
95  // no bounds on the spline coefficients or footholds
96  auto bounds_x = nlp_->GetBoundsOnOptimizationVariables();
97  for (uint _n=0; _n<bounds_x.size(); ++_n) {
98  xlow[_n] = bounds_x.at(_n).lower_;
99  xupp[_n] = bounds_x.at(_n).upper_;
100  xstate[_n] = 0;
101  }
102 
103  // bounds on the cost function if it exists
104  int c = 0;
105  if (nlp_->HasCostTerms()) {
106  Flow[c] = -1e20; Fupp[c] = 1e20; Fmul[c] = 0.0; // no bounds on cost function
107  c++;
108  }
109 
110  // bounds on equality and inequality constraints
111  auto bounds_g = nlp_->GetBoundsOnConstraints();
112  for (const auto& b : bounds_g) {
113  Flow[c] = b.lower_; Fupp[c] = b.upper_; Fmul[c] = 0.0;
114  c++;
115  }
116 
117  // initial values of the optimization
118  VectorXd x_all = nlp_->GetVariableValues();
119  Eigen::Map<VectorXd>(&x[0], x_all.rows()) = x_all;
120 
121  ObjRow = nlp_->HasCostTerms()? 0 : -1; // the row in user function that corresponds to the objective function
122  ObjAdd = 0.0; // the constant to be added to the objective function
123 
124  // no linear derivatives/just assume all are nonlinear
125  lenA = 0;
126  neA = 0;
127  iAfun = nullptr;
128  jAvar = nullptr;
129  A = nullptr;
130 
131  // derivatives of nonlinear part
132  lenG = obj_count*n + nlp_->GetJacobianOfConstraints().nonZeros();
133  iGfun = new int[lenG];
134  jGvar = new int[lenG];
135 
136  // the gradient terms of the cost function
137  neG=0; // nonzero cells in jacobian of Cost Function AND Constraints
138 
139  // the nonzero elements of cost function (assume all)
140  if(nlp_->HasCostTerms()) {
141  for (int var=0; var<n; ++var) {
142  iGfun[neG] = 0;
143  jGvar[neG] = var;
144  neG++;
145  }
146  }
147 
148  // the nonzero elements of constraints (assume all)
149  auto jac = nlp_->GetJacobianOfConstraints();
150  for (int k=0; k<jac.outerSize(); ++k) {
151  for (Jacobian::InnerIterator it(jac,k); it; ++it) {
152  iGfun[neG] = it.row() + obj_count;
153  jGvar[neG] = it.col();
154  neG++;
155  }
156  }
157 
159 }
160 
161 void
162 SnoptAdapter::ObjectiveAndConstraintFct (int* Status, int* n, double x[],
163  int* needF, int* neF, double F[],
164  int* needG, int* neG, double G[],
165  char* cu, int* lencu, int iu[],
166  int* leniu, double ru[], int* lenru)
167 {
168  if ( *needF > 0 ) {
169  int i=0;
170 
171  // the scalar objective function value
172  if (nlp_->HasCostTerms())
173  F[i++] = nlp_->EvaluateCostFunction(x);
174 
175  // the vector of constraint values
176  VectorXd g_eig = nlp_->EvaluateConstraints(x);
177  Eigen::Map<VectorXd>(F+i, g_eig.rows()) = g_eig;
178  }
179 
180 
181  if ( *needG > 0 ) {
182  int i=0;
183 
184  // the jacobian of the first row (cost function)
185  if (nlp_->HasCostTerms()) {
186  Eigen::VectorXd grad = nlp_->EvaluateCostFunctionGradient(x);
187  i = grad.rows();
188  Eigen::Map<VectorXd>(G, i) = grad;
189  }
190 
191  // the jacobian of all the constraints
192  nlp_->EvalNonzerosOfJacobian(x, G+i);
193  nlp_->SaveCurrent();
194  }
195 }
196 
197 void
199 {
200  nlp_->SetVariables(x);
201 }
202 
203 } /* 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)
int GetNumberOfOptimizationVariables() const


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