dart
Function.hpp
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_OPTIMIZATION_FUNCTION_HPP_
34 #define DART_OPTIMIZATION_FUNCTION_HPP_
35 
36 #include <functional>
37 #include <memory>
38 #include <vector>
39 
40 #include <Eigen/Dense>
41 
42 namespace dart {
43 namespace optimization {
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>)>
90  GradientFunction;
91 
92 typedef std::function<void(
93  const Eigen::VectorXd&, Eigen::Map<Eigen::VectorXd, Eigen::RowMajor>)>
94  HessianFunction;
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:
148  CostFunction mCostFunction;
149 
151  GradientFunction mGradientFunction;
152 
154  HessianFunction mHessianFunction;
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 optimization
201 } // namespace dart
202 
203 #endif // DART_OPTIMIZATION_FUNCTION_HPP_
std::string mName
Name of this function.
Definition: Function.hpp:81
class MultiFunction
Definition: Function.hpp:183
GradientFunction mGradientFunction
Storage for the gradient function.
Definition: Function.hpp:151
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
NullFunction is a constant-zero Function.
Definition: Function.hpp:158
ModularFunction uses C++11 std::function to allow you to easily swap out the cost function...
Definition: Function.hpp:99
const std::string & getName() const
Returns the name of this Function.
Definition: Function.cpp:59
virtual void setName(const std::string &newName)
Sets the name of this Function.
Definition: Function.cpp:53
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
Definition: Aspect.cpp:40
CostFunction mCostFunction
Storage for the cost function.
Definition: Function.hpp:148
HessianFunction mHessianFunction
Storage for the Hessian function.
Definition: Function.hpp:154
Definition: Function.hpp:45
virtual double eval(const Eigen::VectorXd &x)=0
Evaluates and returns the objective function at the point x.
Function(const std::string &_name="function")
Constructor.
Definition: Function.cpp:41
virtual ~Function()
Destructor.
Definition: Function.cpp:47