- 3.0.2 optimal control module.
IpoptSolver.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 #pragma once
27 
28 #include <ct/optcon/nlp/Nlp.h>
29 #include "NlpSolver.h"
30 
31 #ifdef BUILD_WITH_IPOPT_SUPPORT // build IPOPT interface
32 
33 #include <cstddef>
34 #include <stddef.h>
35 #include <coin/IpTNLP.hpp>
36 #include <coin/IpIpoptApplication.hpp>
37 #include <coin/IpSolveStatistics.hpp>
38 
39 #endif // BUILD_WITH_IPOPT_SUPPORT
40 
41 // #define DEBUG_PRINT
42 
43 namespace ct {
44 namespace optcon {
45 namespace tpl {
46 
47 #ifdef BUILD_WITH_IPOPT_SUPPORT // build IPOPT interface
48 
56 template <typename SCALAR>
57 class IpoptSolver : public Ipopt::TNLP, public NlpSolver<SCALAR>
58 {
59 public:
60  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
61  using BASE = NlpSolver<SCALAR>;
62  using VectorXs = Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>;
63  using MapVecXs = Eigen::Map<VectorXs>;
64  using MapConstVecXs = Eigen::Map<const VectorXs>;
65 
72  IpoptSolver(std::shared_ptr<tpl::Nlp<SCALAR>> nlp, const NlpSolverSettings& settings);
73 
77  ~IpoptSolver() override;
78 
79  IpoptSolver(const IpoptSolver&) = delete;
80  IpoptSolver& operator=(const IpoptSolver&) = delete;
81 
82  bool solve() override;
83 
84  void prepareWarmStart(size_t maxIterations) override;
85 
86  void configureDerived(const NlpSolverSettings& settings) override;
87 
91  bool get_nlp_info(Ipopt::Index& n,
92  Ipopt::Index& m,
93  Ipopt::Index& nnz_jac_g,
94  Ipopt::Index& nnz_h_lag,
95  IndexStyleEnum& index_style) override;
96 
98  bool get_bounds_info(Ipopt::Index n, SCALAR* x_l, SCALAR* x_u, Ipopt::Index m, SCALAR* g_l, SCALAR* g_u) override;
99 
101  bool get_starting_point(Ipopt::Index n,
102  bool init_x,
103  SCALAR* x,
104  bool init_z,
105  SCALAR* z_L,
106  SCALAR* z_U,
107  Ipopt::Index m,
108  bool init_lambda,
109  SCALAR* lambda) override;
110 
112  bool eval_f(Ipopt::Index n, const SCALAR* x, bool new_x, SCALAR& obj_value) override;
113 
115  bool eval_grad_f(Ipopt::Index n, const SCALAR* x, bool new_x, SCALAR* grad_f) override;
116 
118  bool eval_g(Ipopt::Index n, const SCALAR* x, bool new_x, Ipopt::Index m, SCALAR* g) override;
119 
124  bool eval_jac_g(Ipopt::Index n,
125  const SCALAR* x,
126  bool new_x,
127  Ipopt::Index m,
128  Ipopt::Index nele_jac,
129  Ipopt::Index* iRow,
130  Ipopt::Index* jCol,
131  SCALAR* values) override;
132 
137  bool eval_h(Ipopt::Index n,
138  const SCALAR* x,
139  bool new_x,
140  SCALAR obj_factor,
141  Ipopt::Index m,
142  const SCALAR* lambda,
143  bool new_lambda,
144  Ipopt::Index nele_hess,
145  Ipopt::Index* iRow,
146  Ipopt::Index* jCol,
147  SCALAR* values) override;
148 
150 
154  void finalize_solution(Ipopt::SolverReturn status,
155  Ipopt::Index n,
156  const SCALAR* x,
157  const SCALAR* z_L,
158  const SCALAR* z_U,
159  Ipopt::Index m,
160  const SCALAR* g,
161  const SCALAR* lambda,
162  SCALAR obj_value,
163  const Ipopt::IpoptData* ip_data,
164  Ipopt::IpoptCalculatedQuantities* ip_cq) override;
166 
167 private:
171  void setSolverOptions();
172  std::shared_ptr<Ipopt::IpoptApplication> ipoptApp_;
173  Ipopt::ApplicationReturnStatus status_;
174  IpoptSettings settings_;
175 };
176 
178 
179 #else // BUILD_WITH_IPOPT_SUPPORT -- not building with IPOPT support, create dummy class
180 
181 template <typename SCALAR>
182 class IpoptSolver : public NlpSolver<SCALAR>
183 {
184 public:
185  IpoptSolver() { throw(std::runtime_error("Error - IPOPT interface not compiled.")); }
186  IpoptSolver(std::shared_ptr<tpl::Nlp<SCALAR>> nlp, NlpSolverSettings settings)
187  {
188  throw(std::runtime_error("Error - IPOPT interface not compiled."));
189  }
190 
191  bool solve() override { return false; }
192  void prepareWarmStart(size_t maxIterations) override {}
193  void configureDerived(const NlpSolverSettings& settings) override {}
194 };
195 
196 #endif // BUILD_WITH_IPOPT_SUPPORT
197  //
198 } // namespace tpl
199 
201 
202 } // namespace optcon
203 } // namespace ct
Abstract base class for the NLP solvers.
Definition: NlpSolver.h:21
Definition: IpoptSolver.h:182
Contains the NLP solver settings.
Definition: NlpSolverSettings.h:261
void prepareWarmStart(size_t maxIterations) override
Prepares the solver for a warmstarting scenario with available (good) initial guess.
Definition: IpoptSolver.h:192
The NLP base class. This class serves as abstract base class to use as an interface to the NLP solver...
Definition: Nlp.h:32
tpl::NlpSolver< double > NlpSolver
Definition: NlpSolver.h:93
CppAD::AD< CppAD::cg::CG< double > > SCALAR
IpoptSolver(std::shared_ptr< tpl::Nlp< SCALAR >> nlp, NlpSolverSettings settings)
Definition: IpoptSolver.h:186
IpoptSolver()
Definition: IpoptSolver.h:185
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
NlpSolverSettings settings_
Definition: NlpSolver.h:88
tpl::IpoptSolver< double > IpoptSolver
Definition: IpoptSolver.h:200
bool solve() override
Solves the nlp.
Definition: IpoptSolver.h:191
void configureDerived(const NlpSolverSettings &settings) override
Forwards the settings to the corresponding nlp solver.
Definition: IpoptSolver.h:193