DART  6.6.2
Function.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2018, 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 <vector>
37 #include <memory>
38 #include <functional>
39 
40 #include <Eigen/Dense>
41 
42 namespace dart {
43 namespace optimizer {
44 
46 class Function
47 {
48 public:
50  explicit Function(const std::string& _name = "function");
51 
53  virtual ~Function();
54 
56  virtual void setName(const std::string& _newName);
57 
59  const std::string& getName() const;
60 
62  virtual double eval(const Eigen::VectorXd& _x) = 0;
63 
65  virtual void evalGradient(const Eigen::VectorXd& _x,
66  Eigen::Map<Eigen::VectorXd> _grad);
67 
73  void evalGradient(const Eigen::VectorXd& _x, Eigen::VectorXd& _grad);
74 
76  virtual void evalHessian(
77  const Eigen::VectorXd& _x,
78  Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess);
79 
80 protected:
82  std::string mName;
83 
84 };
85 
86 typedef std::shared_ptr<Function> FunctionPtr;
87 
88 typedef std::function<double(const Eigen::VectorXd&)> CostFunction;
89 
90 typedef std::function<void(const Eigen::VectorXd&,
91  Eigen::Map<Eigen::VectorXd>)> GradientFunction;
92 
93 typedef std::function<void(
94  const Eigen::VectorXd&,
95  Eigen::Map<Eigen::VectorXd, Eigen::RowMajor>)> HessianFunction;
96 
100 class ModularFunction : public Function
101 {
102 public:
104  explicit ModularFunction(const std::string& _name = "modular_function");
105 
107  virtual ~ModularFunction();
108 
111  double eval(const Eigen::VectorXd& _x) override;
112 
115  void evalGradient(const Eigen::VectorXd& _x,
116  Eigen::Map<Eigen::VectorXd> _grad) override;
117 
120  void evalHessian(
121  const Eigen::VectorXd& _x,
122  Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess) override;
123 
125  void setCostFunction(CostFunction _cost);
126 
129  void clearCostFunction(bool _printWarning = true);
130 
132  void setGradientFunction(GradientFunction _gradient);
133 
137  void clearGradientFunction();
138 
140  void setHessianFunction(HessianFunction _hessian);
141 
145  void clearHessianFunction();
146 
147 protected:
150 
153 
156 };
157 
159 class NullFunction : public Function
160 {
161 public:
163  explicit NullFunction(const std::string& _name = "null_function");
164 
166  virtual ~NullFunction();
167 
169  double eval(const Eigen::VectorXd&) override;
170 
173  void evalGradient(const Eigen::VectorXd& _x,
174  Eigen::Map<Eigen::VectorXd> _grad) override;
175 
178  void evalHessian(
179  const Eigen::VectorXd& _x,
180  Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess) override;
181 };
182 
185 {
186 public:
188  MultiFunction();
189 
191  virtual ~MultiFunction();
192 
194  virtual void operator()(const Eigen::VectorXd& _x,
195  Eigen::Map<Eigen::VectorXd>& _f,
196  Eigen::Map<Eigen::MatrixXd>& _grad) = 0;
197 };
198 
199 } // namespace optimizer
200 } // namespace dart
201 
202 #endif // DART_OPTIMIZER_FUNCTION_HPP_
203 
class Function
Definition: Function.hpp:47
virtual void setName(const std::string &_newName)
Set the name of this Function.
Definition: Function.cpp:54
std::string mName
Name of this function.
Definition: Function.hpp:82
Function(const std::string &_name="function")
Constructor.
Definition: Function.cpp:41
virtual double eval(const Eigen::VectorXd &_x)=0
Evaluate and return the objective function at the point x.
virtual ~Function()
Destructor.
Definition: Function.cpp:48
const std::string & getName() const
Get the name of this Function.
Definition: Function.cpp:60
virtual void evalHessian(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd, Eigen::RowMajor > _Hess)
Evaluate and return the objective function at the point x.
Definition: Function.cpp:81
virtual void evalGradient(const Eigen::VectorXd &_x, Eigen::Map< Eigen::VectorXd > _grad)
Evaluate and return the objective function at the point x.
Definition: Function.cpp:66
ModularFunction uses C++11 std::function to allow you to easily swap out the cost function,...
Definition: Function.hpp:101
void clearCostFunction(bool _printWarning=true)
Replace the cost function with a constant-zero function.
Definition: Function.cpp:131
HessianFunction mHessianFunction
Storage for the Hessian function.
Definition: Function.hpp:155
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:118
CostFunction mCostFunction
Storage for the cost function.
Definition: Function.hpp:149
void clearGradientFunction()
Replace the gradient function with the default evalGradient() of the base Function class.
Definition: Function.cpp:151
void clearHessianFunction()
Replace the Hessian function with the default evalHessian() of the base Function class.
Definition: Function.cpp:167
ModularFunction(const std::string &_name="modular_function")
Constructor.
Definition: Function.cpp:90
virtual ~ModularFunction()
Destructor.
Definition: Function.cpp:99
double eval(const Eigen::VectorXd &_x) override
eval() will now call whatever CostFunction you set using setCostFunction()
Definition: Function.cpp:105
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:111
GradientFunction mGradientFunction
Storage for the gradient function.
Definition: Function.hpp:152
void setGradientFunction(GradientFunction _gradient)
Set the function that gets called by evalGradient()
Definition: Function.cpp:145
void setCostFunction(CostFunction _cost)
Set the function that gets called by eval()
Definition: Function.cpp:125
void setHessianFunction(HessianFunction _hessian)
Set the function that gets called by evalHessian()
Definition: Function.cpp:161
class MultiFunction
Definition: Function.hpp:185
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:219
MultiFunction()
Constructor.
Definition: Function.cpp:213
NullFunction is a constant-zero Function.
Definition: Function.hpp:160
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:204
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:196
NullFunction(const std::string &_name="null_function")
Constructor.
Definition: Function.cpp:177
virtual ~NullFunction()
Destructor.
Definition: Function.cpp:184
double eval(const Eigen::VectorXd &) override
eval() will always return exactly zero
Definition: Function.cpp:190
std::shared_ptr< Function > FunctionPtr
Definition: Function.hpp:86
std::function< void(const Eigen::VectorXd &, Eigen::Map< Eigen::VectorXd >)> GradientFunction
Definition: Function.hpp:91
std::function< void(const Eigen::VectorXd &, Eigen::Map< Eigen::VectorXd, Eigen::RowMajor >)> HessianFunction
Definition: Function.hpp:95
std::function< double(const Eigen::VectorXd &)> CostFunction
Definition: Function.hpp:88
Definition: BulletCollisionDetector.cpp:63