33 #ifndef DART_OPTIMIZER_FUNCTION_HPP_
34 #define DART_OPTIMIZER_FUNCTION_HPP_
40 #include <Eigen/Dense>
49 explicit Function(
const std::string& _name =
"function");
55 virtual void setName(
const std::string& newName);
58 const std::string&
getName()
const;
61 virtual double eval(
const Eigen::VectorXd& x) = 0;
65 const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad);
72 void evalGradient(
const Eigen::VectorXd& _x, Eigen::VectorXd& _grad);
76 const Eigen::VectorXd& _x,
77 Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess);
87 typedef std::function<double(
const Eigen::VectorXd&)>
CostFunction;
89 typedef std::function<void(
const Eigen::VectorXd&, Eigen::Map<Eigen::VectorXd>)>
92 typedef std::function<void(
93 const Eigen::VectorXd&, Eigen::Map<Eigen::VectorXd, Eigen::RowMajor>)>
103 explicit ModularFunction(
const std::string& _name =
"modular_function");
110 double eval(
const Eigen::VectorXd& _x)
override;
115 const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad)
override;
120 const Eigen::VectorXd& _x,
121 Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess)
override;
162 explicit NullFunction(
const std::string& _name =
"null_function");
168 double eval(
const Eigen::VectorXd&)
override;
173 const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad)
override;
178 const Eigen::VectorXd& _x,
179 Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess)
override;
194 const Eigen::VectorXd& _x,
195 Eigen::Map<Eigen::VectorXd>& _f,
196 Eigen::Map<Eigen::MatrixXd>& _grad)
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