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 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&,
92 typedef std::function<void(
93 const Eigen::VectorXd&,
103 explicit ModularFunction(
const std::string& _name =
"modular_function");
110 double eval(
const Eigen::VectorXd& _x)
override;
115 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 Eigen::Map<Eigen::VectorXd> _grad)
override;
178 const Eigen::VectorXd& _x,
179 Eigen::Map<Eigen::VectorXd, Eigen::RowMajor> _Hess)
override;
194 Eigen::Map<Eigen::VectorXd>& _f,
195 Eigen::Map<Eigen::MatrixXd>& _grad) = 0;
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:48
const std::string & getName() const
Returns the name of this Function.
Definition: Function.cpp:60
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:81
virtual void setName(const std::string &newName)
Sets the name of this Function.
Definition: Function.cpp:54
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:66
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:131
HessianFunction mHessianFunction
Storage for the Hessian function.
Definition: Function.hpp:154
~ModularFunction() override
Destructor.
Definition: Function.cpp:99
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:148
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
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:151
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: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:219
MultiFunction()
Constructor.
Definition: Function.cpp:213
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: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
~NullFunction() override
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: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:63