- 3.0.2 optimal control module.
SnoptSolver.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 Copyright (c) 2016, Agile & Dexterous Robotics Lab, 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 used
12  to endorse or promote products derived from this software without specific
13  prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
18 SHALL ETH ZURICH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 **********************************************************************************************************************/
25 
26 
27 #pragma once
28 
29 
30 #include <ct/optcon/nlp/Nlp.h>
31 #include "NlpSolver.h"
32 
33 #ifdef BUILD_WITH_SNOPT_SUPPORT // build SNOPT interface
34 #include <snoptProblem.hpp>
35 #endif //BUILD_WITH_SNOPT_SUPPORT
36 
37 namespace ct {
38 namespace optcon {
39 
40 #ifdef BUILD_WITH_SNOPT_SUPPORT // build SNOPT interface
41 
47 class SnoptSolver;
48 
49 
58 struct SnoptMemory
59 {
60  typedef double Number;
61  const SnoptSolver& self;
64  Number* x_ = nullptr;
65  Number* xlow_ = nullptr;
66  Number* xupp_ = nullptr;
67  Number* xmul_ = nullptr;
68  int* xstate_ = nullptr; //*!<The state of the optimization variables*/
69 
70  Number* F_ = nullptr;
71  Number* Flow_ = nullptr;
72  Number* Fupp_ = nullptr;
73  Number* Fmul_ = nullptr;
74  int* Fstate_ = nullptr;
76  Number* A_ = nullptr;
77  int* iAfun_ = nullptr;
78  int* jAvar_ = nullptr;
81  int* iGfun_ = nullptr;
82  int* jGvar_ = nullptr;
84  static std::vector<SnoptMemory*> mempool;
85  int memind;
93  SnoptMemory(const SnoptSolver& self);
94 
96  ~SnoptMemory();
97 };
98 
99 
107 class SnoptSolver : public NlpSolver
108 {
109 public:
110  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
111  typedef double Number;
112  typedef NlpSolver BASE;
113  typedef Eigen::Matrix<Number, Eigen::Dynamic, 1> VectorXd;
114  typedef Eigen::Map<VectorXd> MapVecXd;
115  typedef Eigen::Map<const VectorXd> MapConstVecXd;
116  typedef Eigen::Map<Eigen::VectorXi> MapVecXi;
117 
121  SnoptSolver() {}
125  virtual ~SnoptSolver();
126  // {
127  // free_memory(memoryPtr_);
128  // }
129 
130 
137  SnoptSolver(std::shared_ptr<Nlp> nlp, const NlpSolverSettings& settings);
138 
139 
163  void NLP_Function(SnoptMemory* m,
164  int* Status,
165  int* n,
166  double x[],
167  int* needF,
168  int* neF,
169  double F[],
170  int* needG,
171  int* neG,
172  double G[],
173  char* cu,
174  int* lencu,
175  int iu[],
176  int* leniu,
177  double ru[],
178  int* lenru) const;
179 
201  static void NLP_Function(int* Status,
202  int* n,
203  double x[],
204  int* needF,
205  int* neF,
206  double F[],
207  int* needG,
208  int* neG,
209  double G[],
210  char* cu,
211  int* lencu,
212  int iu[],
213  int* leniu,
214  double ru[],
215  int* lenru);
216 
217  virtual void configureDerived(const NlpSolverSettings& settings) override;
218 
219  virtual bool solve() override;
220 
221  virtual void prepareWarmStart(size_t maxIterations) override;
222 
223 
224 private:
230  SnoptMemory* alloc_memory() const { return new SnoptMemory(*this); }
236  inline void free_memory(SnoptMemory* mem) const { delete mem; }
242  void init_memory(SnoptMemory* mem) const;
243 
250  void fill_memory(SnoptMemory* mem) const;
251 
256  void setupSnoptObjects();
257 
263  void validateSNOPTStatus(const int& status) const;
264 
268  void setSolverOptions();
269 
270  SnoptSettings settings_;
271  SnoptMemory* memoryPtr_;
272 
273  snoptProblemA snoptApp_;
274 
275  int n_ = 0;
276  int neF_ = 0;
277  int ObjRow_ = 0;
278  Number ObjAdd_ = 0.0;
280  const int Cold_ = 0, Basis_ = 1, Warm_ = 2;
281  int currStartOption_ = Cold_;
284  int lenA_ =
285  0;
286  int neA_ = 0;
288  int lenG_ = 0;
289  int neG_ = 0;
291  int status_ = 0;
292 };
293 
294 
295 #else // BUILD_WITH_SNOPT_SUPPORT -- not building with SNOPT support, create dummy class
296 
297 class SnoptSolver : public NlpSolver // <STATE_DIM, CONTROL_DIM>
298 {
299 public:
300  SnoptSolver() { throw(std::runtime_error("Error - SNOPT interface not compiled.")); }
301  SnoptSolver(std::shared_ptr<Nlp> nlp, NlpSolverSettings settings)
302  {
303  throw(std::runtime_error("Error - SNOPT interface not compiled."));
304  }
305 
306  virtual void configureDerived(const NlpSolverSettings& settings) override {}
307  virtual bool solve() override { return false; }
308  virtual void prepareWarmStart(size_t maxIterations) override {}
309 };
310 
311 #endif // BUILD_WITH_SNOPT_SUPPORT
312 
313 } // namespace optcon
314 } // namespace ct
Abstract base class for the NLP solvers.
Definition: NlpSolver.h:21
virtual void prepareWarmStart(size_t maxIterations) override
Prepares the solver for a warmstarting scenario with available (good) initial guess.
Definition: SnoptSolver.h:308
Contains the NLP solver settings.
Definition: NlpSolverSettings.h:261
SnoptSolver(std::shared_ptr< Nlp > nlp, NlpSolverSettings settings)
Definition: SnoptSolver.h:301
tpl::NlpSolver< double > NlpSolver
Definition: NlpSolver.h:93
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
virtual void configureDerived(const NlpSolverSettings &settings) override
Forwards the settings to the corresponding nlp solver.
Definition: SnoptSolver.h:306
SnoptSolver()
Definition: SnoptSolver.h:300
Definition: SnoptSolver.h:297
virtual bool solve() override
Solves the nlp.
Definition: SnoptSolver.h:307