DART  6.10.1
IpoptSolver.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, 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 #include <memory>
37 
39 #if IPOPT_VERSION_GE(3, 13, 0)
40 # include <IpIpoptApplication.hpp>
41 # include <IpTNLP.hpp>
42 #else
43 # include <coin/IpIpoptApplication.hpp>
44 # include <coin/IpTNLP.hpp>
45 #endif
46 
48 
49 namespace dart {
50 namespace optimizer {
51 
52 class Problem;
53 class DartTNLP;
54 
56 class IpoptSolver : public Solver
57 {
58 public:
60  IpoptSolver(const Solver::Properties& _properties = Solver::Properties());
61 
63  explicit IpoptSolver(std::shared_ptr<Problem> _problem);
64 
66  virtual ~IpoptSolver();
67 
68  // Documentation inherited
69  bool solve() override;
70 
71  // Documentation inherited
72  std::string getType() const override;
73 
74  // Documentation inherited
75  std::shared_ptr<Solver> clone() const override;
76 
78  const Ipopt::SmartPtr<Ipopt::IpoptApplication>& getApplication();
79 
81  Ipopt::SmartPtr<const Ipopt::IpoptApplication> getApplication() const;
82 
83 private:
86  const Properties& _properties,
87  const Ipopt::SmartPtr<Ipopt::IpoptApplication>& _app);
88 
90  Ipopt::SmartPtr<Ipopt::TNLP> mNlp;
91 
93  Ipopt::SmartPtr<Ipopt::IpoptApplication> mIpoptApp;
94 };
95 
97 class DartTNLP : public Ipopt::TNLP
98 {
99 public:
100  friend class IpoptSolver;
101 
103  virtual ~DartTNLP();
104 
105  //------------------------- Ipopt::TNLP --------------------------------------
107  bool get_nlp_info(
108  Ipopt::Index& n,
109  Ipopt::Index& m,
110  Ipopt::Index& nnz_jac_g,
111  Ipopt::Index& nnz_h_lag,
112  Ipopt::TNLP::IndexStyleEnum& index_style) override;
113 
115  bool get_bounds_info(
116  Ipopt::Index n,
117  Ipopt::Number* x_l,
118  Ipopt::Number* x_u,
119  Ipopt::Index m,
120  Ipopt::Number* g_l,
121  Ipopt::Number* g_u) override;
122 
124  bool get_starting_point(
125  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(
137  Ipopt::Index _n,
138  const Ipopt::Number* _x,
139  bool _new_x,
140  Ipopt::Number& _obj_value) override;
141 
143  bool eval_grad_f(
144  Ipopt::Index _n,
145  const Ipopt::Number* _x,
146  bool _new_x,
147  Ipopt::Number* _grad_f) override;
148 
150  bool eval_g(
151  Ipopt::Index _n,
152  const Ipopt::Number* _x,
153  bool _new_x,
154  Ipopt::Index _m,
155  Ipopt::Number* _g) override;
156 
160  bool eval_jac_g(
161  Ipopt::Index _n,
162  const Ipopt::Number* _x,
163  bool _new_x,
164  Ipopt::Index _m,
165  Ipopt::Index _nele_jac,
166  Ipopt::Index* _iRow,
167  Ipopt::Index* _jCol,
168  Ipopt::Number* _values) override;
169 
175  bool eval_h(
176  Ipopt::Index _n,
177  const Ipopt::Number* _x,
178  bool _new_x,
179  Ipopt::Number _obj_factor,
180  Ipopt::Index _m,
181  const Ipopt::Number* _lambda,
182  bool _new_lambda,
183  Ipopt::Index _nele_hess,
184  Ipopt::Index* _iRow,
185  Ipopt::Index* _jCol,
186  Ipopt::Number* _values) override;
187 
190  void finalize_solution(
191  Ipopt::SolverReturn _status,
192  Ipopt::Index _n,
193  const Ipopt::Number* _x,
194  const Ipopt::Number* _z_L,
195  const Ipopt::Number* _z_U,
196  Ipopt::Index _m,
197  const Ipopt::Number* _g,
198  const Ipopt::Number* _lambda,
199  Ipopt::Number _obj_value,
200  const Ipopt::IpoptData* _ip_data,
201  Ipopt::IpoptCalculatedQuantities* _ip_cq) override;
202 
203 private:
205  explicit DartTNLP(IpoptSolver* _solver);
206 
209 
211  Ipopt::Number mObjValue;
212 
214  Eigen::VectorXd mObjGradient;
215 
217  Eigen::MatrixXd mObjHessian;
218 };
219 
220 } // namespace optimizer
221 } // namespace dart
222 
223 #endif // DART_OPTIMIZER_IPOPT_IPOPTSOLVER_HPP_
class DartTNLP
Definition: IpoptSolver.hpp:98
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:238
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:447
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:297
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:163
virtual ~DartTNLP()
Definition: IpoptSolver.cpp:157
IpoptSolver * mSolver
DART optimization problem.
Definition: IpoptSolver.hpp:208
Eigen::VectorXd mObjGradient
Objective gradient.
Definition: IpoptSolver.hpp:214
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:416
Eigen::MatrixXd mObjHessian
Objective Hessian.
Definition: IpoptSolver.hpp:217
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:280
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:355
Ipopt::Number mObjValue
Objective value.
Definition: IpoptSolver.hpp:211
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:313
DartTNLP(IpoptSolver *_solver)
Definition: IpoptSolver.cpp:151
class IpoptSolver
Definition: IpoptSolver.hpp:57
std::shared_ptr< Solver > clone() const override
Create an identical clone of this Solver.
Definition: IpoptSolver.cpp:120
Ipopt::SmartPtr< Ipopt::IpoptApplication > mIpoptApp
Main application class for making calls to Ipopt.
Definition: IpoptSolver.hpp:93
const Ipopt::SmartPtr< Ipopt::IpoptApplication > & getApplication()
Get the application interface for this IpoptSolver.
Definition: IpoptSolver.cpp:129
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:114
bool solve() override
Solve optimization problem.
Definition: IpoptSolver.cpp:76
virtual ~IpoptSolver()
Destructor.
Definition: IpoptSolver.cpp:70
Ipopt::SmartPtr< Ipopt::TNLP > mNlp
IPOPT nonlinear programming problem.
Definition: IpoptSolver.hpp:90
Abstract class that provides a common interface for different Solvers.
Definition: Solver.hpp:53
Definition: BulletCollisionDetector.cpp:65
The Solver::Properties class contains Solver parameters that are common to all Solver types.
Definition: Solver.hpp:60