DART  6.10.1
NloptSolver.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_NLOPT_NLOPTSOLVER_HPP_
34 #define DART_OPTIMIZER_NLOPT_NLOPTSOLVER_HPP_
35 
36 #include <nlopt.hpp>
37 
40 
41 namespace dart {
42 namespace optimizer {
43 
44 class Problem;
45 
60 class NloptSolver : public Solver
61 {
62 public:
63  enum Algorithm
64  {
65  GN_DIRECT = 0,
109  };
110 
112  DART_DEPRECATED(6.9)
113  NloptSolver(const Solver::Properties& properties, nlopt::algorithm alg);
114 
116  NloptSolver(
117  const Solver::Properties& properties = Solver::Properties(),
118  Algorithm alg = LN_COBYLA);
119 
121  DART_DEPRECATED(6.9)
122  NloptSolver(std::shared_ptr<Problem> problem, nlopt::algorithm alg);
123 
125  NloptSolver(std::shared_ptr<Problem> problem, Algorithm alg = LN_COBYLA);
126 
128  ~NloptSolver() override;
129 
130  // Documentation inherited
131  bool solve() override;
132 
133  // Documentation inherited
134  Eigen::VectorXd getLastConfiguration() const;
135 
136  // Documentation inherited
137  std::string getType() const override;
138 
139  // Documentation inherited
140  std::shared_ptr<Solver> clone() const override;
141 
143  void copy(const NloptSolver& other);
144 
146  NloptSolver& operator=(const NloptSolver& other);
147 
149  DART_DEPRECATED(6.9)
150  void setAlgorithm(nlopt::algorithm alg);
151 
153  void setAlgorithm(Algorithm alg);
154 
156  DART_DEPRECATED(6.9)
157  nlopt::algorithm getAlgorithm() const;
158 
160  Algorithm getAlgorithm2() const;
161  // TODO(JS): Rename to getAlgorithm2() once getAlgorithm() is removed in
162  // DART 7
163 
164 private:
166  static nlopt::algorithm convertAlgorithm(Algorithm algorithm);
167 
169  static Algorithm convertAlgorithm(nlopt::algorithm algorithm);
170 
172  static double _nlopt_func(
173  unsigned n,
174  const double* x,
175  double* gradient, // nullptr if not needed
176  void* func_data);
177 
179  static void _nlopt_mfunc(
180  unsigned m,
181  double* result,
182  unsigned n,
183  const double* x,
184  double* gradient, // nullptr if not needed
185  void* func_data);
186 
188  std::unique_ptr<nlopt::opt> mOpt;
189 
191  nlopt::algorithm mAlg;
192 
194  std::vector<double> mX;
195 
197  double mMinF;
198 };
199 
200 } // namespace optimizer
201 } // namespace dart
202 
203 #endif // DART_OPTIMIZER_NLOPT_NLOPTSOLVER_HPP_
#define DART_DEPRECATED(version)
Definition: Deprecated.hpp:51
CollisionResult * result
Collision result of DART.
Definition: FCLCollisionDetector.cpp:160
BodyPropPtr properties
Definition: SdfParser.cpp:80
NloptSolver is a nonlinear programming solver that provides many unlerlying algorithms through nlopt ...
Definition: NloptSolver.hpp:61
std::string getType() const override
Get the type (implementation) of this Solver.
Definition: NloptSolver.cpp:202
static nlopt::algorithm convertAlgorithm(Algorithm algorithm)
Converts nlopt::algorithm to NloptSolver::Algorithm.
Definition: NloptSolver.cpp:262
nlopt::algorithm getAlgorithm() const
Get the algorithm that is to be used by the nlopt solver.
Definition: NloptSolver.cpp:240
Eigen::VectorXd getLastConfiguration() const
Definition: NloptSolver.cpp:196
Algorithm getAlgorithm2() const
Get the algorithm that is to be used by the nlopt solver.
Definition: NloptSolver.cpp:246
std::shared_ptr< Solver > clone() const override
Create an identical clone of this Solver.
Definition: NloptSolver.cpp:208
std::unique_ptr< nlopt::opt > mOpt
NLOPT data structure.
Definition: NloptSolver.hpp:188
std::vector< double > mX
Optimization parameters.
Definition: NloptSolver.hpp:194
double mMinF
Optimum value of the objective function.
Definition: NloptSolver.hpp:197
void setAlgorithm(nlopt::algorithm alg)
Set the algorithm that is to be used by the nlopt solver.
Definition: NloptSolver.cpp:228
nlopt::algorithm mAlg
Algorithm to be used by the nlopt::opt.
Definition: NloptSolver.hpp:191
static void _nlopt_mfunc(unsigned m, double *result, unsigned n, const double *x, double *gradient, void *func_data)
Wrapping function for nlopt callback function, nlopt_mfunc.
Definition: NloptSolver.cpp:390
bool solve() override
Solve optimization problem.
Definition: NloptSolver.cpp:104
static double _nlopt_func(unsigned n, const double *x, double *gradient, void *func_data)
Wrapping function for nlopt callback function, nlopt_func.
Definition: NloptSolver.cpp:373
Algorithm
Definition: NloptSolver.hpp:64
@ GN_ISRES
Definition: NloptSolver.hpp:100
@ LN_NELDERMEAD
Definition: NloptSolver.hpp:93
@ LN_PRAXIS
Definition: NloptSolver.hpp:77
@ GD_MLSL_LDS
Definition: NloptSolver.hpp:88
@ GN_ESCH
Definition: NloptSolver.hpp:107
@ LN_AUGLAG
Definition: NloptSolver.hpp:95
@ LN_NEWUOA
Definition: NloptSolver.hpp:91
@ LN_BOBYQA
Definition: NloptSolver.hpp:99
@ GN_DIRECT_NOSCAL
Definition: NloptSolver.hpp:68
@ GN_DIRECT_L_RAND_NOSCAL
Definition: NloptSolver.hpp:70
@ GN_ORIG_DIRECT_L
Definition: NloptSolver.hpp:72
@ LD_MMA
Definition: NloptSolver.hpp:89
@ NUM_ALGORITHMS
Not an algorithm, just the number of them.
Definition: NloptSolver.hpp:108
@ LN_SBPLX
Definition: NloptSolver.hpp:94
@ LD_VAR1
Definition: NloptSolver.hpp:78
@ LD_CCSAQ
Definition: NloptSolver.hpp:106
@ LD_LBFGS_NOCEDAL
Definition: NloptSolver.hpp:75
@ AUGLAG
Definition: NloptSolver.hpp:101
@ LN_NEWUOA_BOUND
Definition: NloptSolver.hpp:92
@ G_MLSL_LDS
Definition: NloptSolver.hpp:104
@ LN_AUGLAG_EQ
Definition: NloptSolver.hpp:97
@ LD_TNEWTON
Definition: NloptSolver.hpp:80
@ G_MLSL
Definition: NloptSolver.hpp:103
@ LD_AUGLAG_EQ
Definition: NloptSolver.hpp:98
@ GN_DIRECT_L_NOSCAL
Definition: NloptSolver.hpp:69
@ LD_AUGLAG
Definition: NloptSolver.hpp:96
@ GD_STOGO
Definition: NloptSolver.hpp:73
@ GN_DIRECT
Definition: NloptSolver.hpp:65
@ LD_TNEWTON_PRECOND_RESTART
Definition: NloptSolver.hpp:83
@ GD_STOGO_RAND
Definition: NloptSolver.hpp:74
@ GN_MLSL
Definition: NloptSolver.hpp:85
@ GN_ORIG_DIRECT
Definition: NloptSolver.hpp:71
@ GN_DIRECT_L
Definition: NloptSolver.hpp:66
@ LD_SLSQP
Definition: NloptSolver.hpp:105
@ GD_MLSL
Definition: NloptSolver.hpp:86
@ AUGLAG_EQ
Definition: NloptSolver.hpp:102
@ LD_TNEWTON_RESTART
Definition: NloptSolver.hpp:81
@ LD_LBFGS
Definition: NloptSolver.hpp:76
@ GN_CRS2_LM
Definition: NloptSolver.hpp:84
@ GN_MLSL_LDS
Definition: NloptSolver.hpp:87
@ LD_TNEWTON_PRECOND
Definition: NloptSolver.hpp:82
@ LN_COBYLA
Definition: NloptSolver.hpp:90
@ LD_VAR2
Definition: NloptSolver.hpp:79
@ GN_DIRECT_L_RAND
Definition: NloptSolver.hpp:67
void copy(const NloptSolver &other)
Copy the Properties of another NloptSolver.
Definition: NloptSolver.cpp:214
class Problem
Definition: Problem.hpp:48
Abstract class that provides a common interface for different Solvers.
Definition: Solver.hpp:53
Definition: Random-impl.hpp:92
Definition: BulletCollisionDetector.cpp:65
Definition: SharedLibraryManager.hpp:46