DART  6.10.1
Function.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_FUNCTION_HPP_
34 #define DART_OPTIMIZER_FUNCTION_HPP_
35 
36 #include <functional>
37 #include <memory>
38 #include <vector>
39 
40 #include <Eigen/Dense>
41 
42 namespace dart {
43 namespace optimizer {
44 
45 class Function
46 {
47 public:
49  explicit Function(const std::string& _name = "function");
50 
52  virtual ~Function();
53 
55  virtual void setName(const std::string& newName);
56 
58  const std::string& getName() const;
59 
61  virtual double eval(const Eigen::VectorXd& x) = 0;
62 
64  virtual void evalGradient(
65  const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad);
66 
72  void evalGradient(const Eigen::VectorXd& _x, Eigen::VectorXd& _grad);
73 
75  virtual void evalHessian(
76  const Eigen::VectorXd& _x,
77  Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess);
78 
79 protected:
81  std::string mName;
82 };
83 
84 typedef std::shared_ptr<Function> FunctionPtr;
85 typedef std::unique_ptr<Function> UniqueFunctionPtr;
86 
87 typedef std::function<double(const Eigen::VectorXd&)> CostFunction;
88 
89 typedef std::function<void(const Eigen::VectorXd&, Eigen::Map<Eigen::VectorXd>)>
91 
92 typedef std::function<void(
93  const Eigen::VectorXd&, Eigen::Map<Eigen::VectorXd, Eigen::RowMajor>)>
95 
99 class ModularFunction : public Function
100 {
101 public:
103  explicit ModularFunction(const std::string& _name = "modular_function");
104 
106  ~ModularFunction() override;
107 
110  double eval(const Eigen::VectorXd& _x) override;
111 
114  void evalGradient(
115  const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad) override;
116 
119  void evalHessian(
120  const Eigen::VectorXd& _x,
121  Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess) override;
122 
124  void setCostFunction(CostFunction _cost);
125 
128  void clearCostFunction(bool _printWarning = true);
129 
131  void setGradientFunction(GradientFunction _gradient);
132 
136  void clearGradientFunction();
137 
139  void setHessianFunction(HessianFunction _hessian);
140 
144  void clearHessianFunction();
145 
146 protected:
149 
152 
155 };
156 
158 class NullFunction : public Function
159 {
160 public:
162  explicit NullFunction(const std::string& _name = "null_function");
163 
165  ~NullFunction() override;
166 
168  double eval(const Eigen::VectorXd&) override;
169 
172  void evalGradient(
173  const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad) override;
174 
177  void evalHessian(
178  const Eigen::VectorXd& _x,
179  Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess) override;
180 };
181 
184 {
185 public:
187  MultiFunction();
188 
190  virtual ~MultiFunction();
191 
193  virtual void operator()(
194  const Eigen::VectorXd& _x,
195  Eigen::Map<Eigen::VectorXd>& _f,
196  Eigen::Map<Eigen::MatrixXd>& _grad)
197  = 0;
198 };
199 
200 } // namespace optimizer
201 } // namespace dart
202 
203 #endif // DART_OPTIMIZER_FUNCTION_HPP_
Definition: Function.hpp:46
std::string mName
Name of this function.
Definition: Function.hpp:81
Function(const std::string &_name="function")
Constructor.
Definition: Function.cpp:41
virtual ~Function()
Destructor.
Definition: Function.cpp:47
const std::string & getName() const
Returns the name of this Function.
Definition: Function.cpp:59
virtual void evalHessian(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd, Eigen::RowMajor > _Hess)
Evaluates and return the objective function at the point x.
Definition: Function.cpp:80
virtual void setName(const std::string &newName)
Sets the name of this Function.
Definition: Function.cpp:53
virtual double eval(const Eigen::VectorXd &x)=0
Evaluates and returns the objective function at the point x.
virtual void evalGradient(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd > _grad)
Evaluates and returns the objective function at the point x.
Definition: Function.cpp:65
ModularFunction uses C++11 std::function to allow you to easily swap out the cost function,...
Definition: Function.hpp:100
void clearCostFunction(bool _printWarning=true)
Replace the cost function with a constant-zero function.
Definition: Function.cpp:130
HessianFunction mHessianFunction
Storage for the Hessian function.
Definition: Function.hpp:154
~ModularFunction() override
Destructor.
Definition: Function.cpp:97
void evalHessian(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd, Eigen::RowMajor > _Hess) override
evalHessian() will now call whatever HessianFunction you set using setHessianFunction()
Definition: Function.cpp:116
CostFunction mCostFunction
Storage for the cost function.
Definition: Function.hpp:148
void clearGradientFunction()
Replace the gradient function with the default evalGradient() of the base Function class.
Definition: Function.cpp:150
void clearHessianFunction()
Replace the Hessian function with the default evalHessian() of the base Function class.
Definition: Function.cpp:165
ModularFunction(const std::string &_name="modular_function")
Constructor.
Definition: Function.cpp:89
double eval(const Eigen::VectorXd &_x) override
eval() will now call whatever CostFunction you set using setCostFunction()
Definition: Function.cpp:103
void evalGradient(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd > _grad) override
evalGradient() will now call whatever GradientFunction you set using setGradientFunction()
Definition: Function.cpp:109
GradientFunction mGradientFunction
Storage for the gradient function.
Definition: Function.hpp:151
void setGradientFunction(GradientFunction _gradient)
Set the function that gets called by evalGradient()
Definition: Function.cpp:144
void setCostFunction(CostFunction _cost)
Set the function that gets called by eval()
Definition: Function.cpp:124
void setHessianFunction(HessianFunction _hessian)
Set the function that gets called by evalHessian()
Definition: Function.cpp:159
class MultiFunction
Definition: Function.hpp:184
virtual void operator()(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd > &_f, Eigen::Map< Eigen::MatrixXd > &_grad)=0
Operator ()
virtual ~MultiFunction()
Destructor.
Definition: Function.cpp:215
MultiFunction()
Constructor.
Definition: Function.cpp:209
NullFunction is a constant-zero Function.
Definition: Function.hpp:159
void evalHessian(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd, Eigen::RowMajor > _Hess) override
evalHessian() will always set _Hess to a zero matrix that matches the dimensionality of _x
Definition: Function.cpp:200
void evalGradient(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd > _grad) override
evalGradient will always set _grad to a zero vector that matches the dimensionality of _x
Definition: Function.cpp:192
NullFunction(const std::string &_name="null_function")
Constructor.
Definition: Function.cpp:174
~NullFunction() override
Destructor.
Definition: Function.cpp:180
double eval(const Eigen::VectorXd &) override
eval() will always return exactly zero
Definition: Function.cpp:186
std::shared_ptr< Function > FunctionPtr
Definition: Function.hpp:84
std::function< void(const Eigen::VectorXd &, Eigen::Map< Eigen::VectorXd >)> GradientFunction
Definition: Function.hpp:90
std::function< void(const Eigen::VectorXd &, Eigen::Map< Eigen::VectorXd, Eigen::RowMajor >)> HessianFunction
Definition: Function.hpp:94
std::unique_ptr< Function > UniqueFunctionPtr
Definition: Function.hpp:85
std::function< double(const Eigen::VectorXd &)> CostFunction
Definition: Function.hpp:87
Definition: BulletCollisionDetector.cpp:65