DART  6.7.3
IpoptSolver.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_OPTIMIZER_IPOPT_IPOPTSOLVER_HPP_
34 #define DART_OPTIMIZER_IPOPT_IPOPTSOLVER_HPP_
35 
36 //------------------------------------------------------------------------------
37 // Workaround for bug:
38 // (see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=684062)
39 // This is fixed at IpOpt 3.11.4-1
40 #define HAVE_CSTDDEF
41 #include <coin/IpTNLP.hpp>
42 #include <coin/IpIpoptApplication.hpp>
43 #undef HAVE_CSTDDEF
44 //------------------------------------------------------------------------------
45 
46 #include <memory>
47 
49 
50 namespace dart {
51 namespace optimizer {
52 
53 class Problem;
54 class DartTNLP;
55 
57 class IpoptSolver : public Solver
58 {
59 public:
60 
62  IpoptSolver(const Solver::Properties& _properties = Solver::Properties());
63 
65  explicit IpoptSolver(std::shared_ptr<Problem> _problem);
66 
68  virtual ~IpoptSolver();
69 
70  // Documentation inherited
71  bool solve() override;
72 
73  // Documentation inherited
74  std::string getType() const override;
75 
76  // Documentation inherited
77  std::shared_ptr<Solver> clone() const override;
78 
80  const Ipopt::SmartPtr<Ipopt::IpoptApplication>& getApplication();
81 
83  Ipopt::SmartPtr<const Ipopt::IpoptApplication> getApplication() const;
84 
85 private:
86 
88  IpoptSolver(const Properties& _properties,
89  const Ipopt::SmartPtr<Ipopt::IpoptApplication>& _app);
90 
92  Ipopt::SmartPtr<Ipopt::TNLP> mNlp;
93 
95  Ipopt::SmartPtr<Ipopt::IpoptApplication> mIpoptApp;
96 };
97 
99 class DartTNLP : public Ipopt::TNLP
100 {
101 public:
102 
103  friend class IpoptSolver;
104 
106  virtual ~DartTNLP();
107 
108  //------------------------- Ipopt::TNLP --------------------------------------
110  bool get_nlp_info(Ipopt::Index& n,
111  Ipopt::Index& m,
112  Ipopt::Index& nnz_jac_g,
113  Ipopt::Index& nnz_h_lag,
114  Ipopt::TNLP::IndexStyleEnum& index_style) override;
115 
117  bool get_bounds_info(Ipopt::Index n,
118  Ipopt::Number* x_l,
119  Ipopt::Number* x_u,
120  Ipopt::Index m,
121  Ipopt::Number* g_l,
122  Ipopt::Number* g_u) override;
123 
125  bool get_starting_point(Ipopt::Index n,
126  bool init_x,
127  Ipopt::Number* x,
128  bool init_z,
129  Ipopt::Number* z_L,
130  Ipopt::Number* z_U,
131  Ipopt::Index m,
132  bool init_lambda,
133  Ipopt::Number* lambda) override;
134 
136  bool eval_f(Ipopt::Index _n,
137  const Ipopt::Number* _x,
138  bool _new_x,
139  Ipopt::Number&
140  _obj_value) override;
141 
143  bool eval_grad_f(Ipopt::Index _n,
144  const Ipopt::Number* _x,
145  bool _new_x,
146  Ipopt::Number* _grad_f) override;
147 
149  bool eval_g(Ipopt::Index _n,
150  const Ipopt::Number* _x,
151  bool _new_x,
152  Ipopt::Index _m,
153  Ipopt::Number* _g) override;
154 
158  bool eval_jac_g(Ipopt::Index _n,
159  const Ipopt::Number* _x,
160  bool _new_x,
161  Ipopt::Index _m,
162  Ipopt::Index _nele_jac,
163  Ipopt::Index* _iRow,
164  Ipopt::Index* _jCol,
165  Ipopt::Number* _values) override;
166 
172  bool eval_h(Ipopt::Index _n,
173  const Ipopt::Number* _x,
174  bool _new_x,
175  Ipopt::Number _obj_factor,
176  Ipopt::Index _m,
177  const Ipopt::Number* _lambda,
178  bool _new_lambda,
179  Ipopt::Index _nele_hess,
180  Ipopt::Index* _iRow,
181  Ipopt::Index* _jCol,
182  Ipopt::Number* _values) override;
183 
186  void finalize_solution(Ipopt::SolverReturn _status,
187  Ipopt::Index _n,
188  const Ipopt::Number* _x,
189  const Ipopt::Number* _z_L,
190  const Ipopt::Number* _z_U,
191  Ipopt::Index _m,
192  const Ipopt::Number* _g,
193  const Ipopt::Number* _lambda,
194  Ipopt::Number _obj_value,
195  const Ipopt::IpoptData* _ip_data,
196  Ipopt::IpoptCalculatedQuantities* _ip_cq) override;
197 
198 private:
199 
201  explicit DartTNLP(IpoptSolver* _solver);
202 
205 
207  Ipopt::Number mObjValue;
208 
210  Eigen::VectorXd mObjGradient;
211 
213  Eigen::MatrixXd mObjHessian;
214 };
215 
216 } // namespace optimizer
217 } // namespace dart
218 
219 #endif // DART_OPTIMIZER_IPOPT_IPOPTSOLVER_HPP_
class DartTNLP
Definition: IpoptSolver.hpp:100
bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number *x, bool init_z, Ipopt::Number *z_L, Ipopt::Number *z_U, Ipopt::Index m, bool init_lambda, Ipopt::Number *lambda) override
Method to return the starting point for the algorithm.
Definition: IpoptSolver.cpp:236
void finalize_solution(Ipopt::SolverReturn _status, Ipopt::Index _n, const Ipopt::Number *_x, const Ipopt::Number *_z_L, const Ipopt::Number *_z_U, Ipopt::Index _m, const Ipopt::Number *_g, const Ipopt::Number *_lambda, Ipopt::Number _obj_value, const Ipopt::IpoptData *_ip_data, Ipopt::IpoptCalculatedQuantities *_ip_cq) override
This method is called when the algorithm is complete so the TNLP can store/write the solution.
Definition: IpoptSolver.cpp:427
bool eval_grad_f(Ipopt::Index _n, const Ipopt::Number *_x, bool _new_x, Ipopt::Number *_grad_f) override
Method to return the gradient of the objective.
Definition: IpoptSolver.cpp:293
bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m, Ipopt::Index &nnz_jac_g, Ipopt::Index &nnz_h_lag, Ipopt::TNLP::IndexStyleEnum &index_style) override
Method to return some info about the nlp.
Definition: IpoptSolver.cpp:164
virtual ~DartTNLP()
Definition: IpoptSolver.cpp:158
IpoptSolver * mSolver
DART optimization problem.
Definition: IpoptSolver.hpp:204
Eigen::VectorXd mObjGradient
Objective gradient.
Definition: IpoptSolver.hpp:210
bool get_bounds_info(Ipopt::Index n, Ipopt::Number *x_l, Ipopt::Number *x_u, Ipopt::Index m, Ipopt::Number *g_l, Ipopt::Number *g_u) override
Method to return the bounds for my problem.
Definition: IpoptSolver.cpp:191
bool eval_h(Ipopt::Index _n, const Ipopt::Number *_x, bool _new_x, Ipopt::Number _obj_factor, Ipopt::Index _m, const Ipopt::Number *_lambda, bool _new_lambda, Ipopt::Index _nele_hess, Ipopt::Index *_iRow, Ipopt::Index *_jCol, Ipopt::Number *_values) override
Method to return: 1) The structure of the hessian of the lagrangian (if "values" is nullptr) 2) The v...
Definition: IpoptSolver.cpp:407
Eigen::MatrixXd mObjHessian
Objective Hessian.
Definition: IpoptSolver.hpp:213
bool eval_f(Ipopt::Index _n, const Ipopt::Number *_x, bool _new_x, Ipopt::Number &_obj_value) override
Method to return the objective value.
Definition: IpoptSolver.cpp:277
bool eval_jac_g(Ipopt::Index _n, const Ipopt::Number *_x, bool _new_x, Ipopt::Index _m, Ipopt::Index _nele_jac, Ipopt::Index *_iRow, Ipopt::Index *_jCol, Ipopt::Number *_values) override
Method to return: 1) The structure of the jacobian (if "values" is nullptr) 2) The values of the jaco...
Definition: IpoptSolver.cpp:348
Ipopt::Number mObjValue
Objective value.
Definition: IpoptSolver.hpp:207
bool eval_g(Ipopt::Index _n, const Ipopt::Number *_x, bool _new_x, Ipopt::Index _m, Ipopt::Number *_g) override
Method to return the constraint residuals.
Definition: IpoptSolver.cpp:308
DartTNLP(IpoptSolver *_solver)
Definition: IpoptSolver.cpp:150
class IpoptSolver
Definition: IpoptSolver.hpp:58
std::shared_ptr< Solver > clone() const override
Create an identical clone of this Solver.
Definition: IpoptSolver.cpp:121
Ipopt::SmartPtr< Ipopt::IpoptApplication > mIpoptApp
Main application class for making calls to Ipopt.
Definition: IpoptSolver.hpp:95
const Ipopt::SmartPtr< Ipopt::IpoptApplication > & getApplication()
Get the application interface for this IpoptSolver.
Definition: IpoptSolver.cpp:130
IpoptSolver(const Solver::Properties &_properties=Solver::Properties())
Default constructor.
Definition: IpoptSolver.cpp:45
std::string getType() const override
Get the type (implementation) of this Solver.
Definition: IpoptSolver.cpp:115
bool solve() override
Solve optimization problem.
Definition: IpoptSolver.cpp:77
virtual ~IpoptSolver()
Destructor.
Definition: IpoptSolver.cpp:71
Ipopt::SmartPtr< Ipopt::TNLP > mNlp
IPOPT nonlinear programming problem.
Definition: IpoptSolver.hpp:92
Abstract class that provides a common interface for different Solvers.
Definition: Solver.hpp:53
Definition: BulletCollisionDetector.cpp:63
The Solver::Properties class contains Solver parameters that are common to all Solver types.
Definition: Solver.hpp:61