DART  6.7.3
NloptSolver.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_NLOPT_NLOPTSOLVER_HPP_
34 #define DART_OPTIMIZER_NLOPT_NLOPTSOLVER_HPP_
35 
36 #include <nlopt.hpp>
37 
39 
40 namespace dart {
41 namespace optimizer {
42 
43 class Problem;
44 
46 class NloptSolver : public Solver
47 {
48 public:
49 
51  NloptSolver(const Solver::Properties& _properties = Solver::Properties(),
52  nlopt::algorithm _alg = nlopt::LN_COBYLA);
53 
55  NloptSolver(std::shared_ptr<Problem> _problem,
56  nlopt::algorithm _alg = nlopt::LN_COBYLA);
57 
59  virtual ~NloptSolver();
60 
61  // Documentation inherited
62  bool solve() override;
63 
64  // Documentation inherited
65  Eigen::VectorXd getLastConfiguration() const;
66 
67  // Documentation inherited
68  std::string getType() const override;
69 
70  // Documentation inherited
71  std::shared_ptr<Solver> clone() const override;
72 
74  void copy(const NloptSolver& _other);
75 
77  NloptSolver& operator=(const NloptSolver& _other);
78 
80  void setAlgorithm(nlopt::algorithm _alg);
81 
83  nlopt::algorithm getAlgorithm() const;
84 
85 private:
87  static double _nlopt_func(unsigned _n,
88  const double* _x,
89  double* _gradient, // nullptr if not needed
90  void* _func_data);
91 
93  static void _nlopt_mfunc(unsigned _m,
94  double* _result,
95  unsigned _n,
96  const double* _x,
97  double* _gradient, // nullptr if not needed
98  void* _func_data);
99 
101  std::unique_ptr<nlopt::opt> mOpt;
102 
104  nlopt::algorithm mAlg;
105 
107  std::vector<double> mX;
108 
110  double mMinF;
111 };
112 
113 } // namespace optimizer
114 } // namespace dart
115 
116 #endif // DART_OPTIMIZER_NLOPT_NLOPTSOLVER_HPP_
117 
class NloptSolver
Definition: NloptSolver.hpp:47
std::string getType() const override
Get the type (implementation) of this Solver.
Definition: NloptSolver.cpp:191
nlopt::algorithm getAlgorithm() const
Get the algorithm that is to be used by the nlopt solver.
Definition: NloptSolver.cpp:223
Eigen::VectorXd getLastConfiguration() const
Definition: NloptSolver.cpp:185
void setAlgorithm(nlopt::algorithm _alg)
Set the algorithm that is to be used by the nlopt solver.
Definition: NloptSolver.cpp:217
std::shared_ptr< Solver > clone() const override
Create an identical clone of this Solver.
Definition: NloptSolver.cpp:197
std::unique_ptr< nlopt::opt > mOpt
NLOPT data structure.
Definition: NloptSolver.hpp:101
std::vector< double > mX
Optimization parameters.
Definition: NloptSolver.hpp:107
NloptSolver(const Solver::Properties &_properties=Solver::Properties(), nlopt::algorithm _alg=nlopt::LN_COBYLA)
Default Constructor.
Definition: NloptSolver.cpp:48
void copy(const NloptSolver &_other)
Copy the Properties of another NloptSolver.
Definition: NloptSolver.cpp:203
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:229
double mMinF
Optimum value of the objective function.
Definition: NloptSolver.hpp:110
nlopt::algorithm mAlg
Algorithm to be used by the nlopt::opt.
Definition: NloptSolver.hpp:104
bool solve() override
Solve optimization problem.
Definition: NloptSolver.cpp:92
virtual ~NloptSolver()
Destructor.
Definition: NloptSolver.cpp:70
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:248
NloptSolver & operator=(const NloptSolver &_other)
Copy the Properties of another NloptSolver.
Definition: NloptSolver.cpp:210
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