MUQ  0.4.3
muq::Optimization::Optimizer Class Referenceabstract

Solve an optimization problem. More...

#include <Optimizer.h>

Inheritance diagram for muq::Optimization::Optimizer:

Detailed Description

Solve an optimization problem.

\begin{eqnarray} c &=& \min{J(x; \theta_1, ..., \theta_1)} \ \ f_i(x) &\leq& 0 \ \ g_i(x) &=& 0 \end{eqnarray}

Definition at line 21 of file Optimizer.h.

Public Types

typedef std::function< std::shared_ptr< Optimizer >std::shared_ptr< muq::Modeling::ModPiece > const &, boost::property_tree::ptree)> OptimizerConstructor
 
typedef std::map< std::string, OptimizerConstructorOptimizerMap
 

Public Member Functions

 Optimizer (std::shared_ptr< muq::Modeling::ModPiece > const &cost, boost::property_tree::ptree const &pt)
 
virtual ~Optimizer ()=default
 
virtual void AddInequalityConstraint (std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &ineq)
 Add an inequality constraint to the optimization. More...
 
virtual void AddInequalityConstraint (std::shared_ptr< muq::Modeling::ModPiece > const &ineq)
 Add an inequality constraint to the optimization. More...
 
void ClearInequalityConstraint ()
 Clear all inequality constraints. More...
 
virtual void AddEqualityConstraint (std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &eq)
 Add an equality constraint to the optimization. More...
 
virtual void AddEqualityConstraint (std::shared_ptr< muq::Modeling::ModPiece > const &eq)
 Add an equality constraint to the optimization. More...
 
void ClearEqualityConstraint ()
 Clear all equality constraints. More...
 
virtual std::pair< Eigen::VectorXd, double > Solve (std::vector< Eigen::VectorXd > const &inputs)=0
 Solve the optimization problem. More...
 
- Public Member Functions inherited from muq::Modeling::WorkPiece
 WorkPiece ()
 Create a muq::Modeling::WorkPiece with no fixed number of inputs and outputs and variable input/output types. More...
 
 WorkPiece (int const num, WorkPiece::Fix const fix=WorkPiece::Fix::Inputs)
 Create a muq::Modeling::WorkPiece with either a fixed number of inputs or outputs and variable input/output types. More...
 
 WorkPiece (int const numIns, int const numOuts)
 Create a muq::Modeling::WorkPiece with a fixed number of inputs and outputs but variable input/output types. More...
 
 WorkPiece (std::vector< std::string > const &types, WorkPiece::Fix const fix=WorkPiece::Fix::Inputs)
 Create a muq::Modeling::WorkPiece with either a fixed number of inputs with specified types or a fixed number of outputs with specified types. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &types, WorkPiece::Fix const fix=WorkPiece::Fix::Inputs)
 Create a muq::Modeling::WorkPiece where either some of the inputs have specified types or some of the outputs have specified types. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &types, int const num, WorkPiece::Fix const fixTypes=WorkPiece::Fix::Inputs, WorkPiece::Fix const fixNum=WorkPiece::Fix::Inputs)
 Create a muq::Modeling::WorkPiece where either some of the inputs have specified types or some of the outputs have specified types and either the number of inputs or the number of outputs is fixed. More...
 
 WorkPiece (std::vector< std::string > const &types, int const num)
 Create a muq::Modeling::WorkPiece with a fixed number of inputs with specified types and a fixed number of outputs (of uknown type) More...
 
 WorkPiece (int const num, std::vector< std::string > const &types)
 Create a muq::Modeling::WorkPiece with a fixed number of outputs with specified types and a fixed number of inputs (of uknown type) More...
 
 WorkPiece (std::map< unsigned int, std::string > const &inTypes, int const numIns, int const numOuts)
 Create a muq::Modeling::WorkPiece where some of the inputs are known and we know the input and output numbers. More...
 
 WorkPiece (int const numIns, std::map< unsigned int, std::string > const &outTypes, int const numOuts)
 Create a muq::Modeling::WorkPiece where some of the outputs are known and we know the input and output numbers. More...
 
 WorkPiece (std::vector< std::string > const &inTypes, std::vector< std::string > const &outTypes)
 Create a muq::Modeling::WorkPiece with a fixed number of inputs and outputs with specified types. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &inTypes, std::vector< std::string > const &outTypes)
 Create a muq::Modeling::WorkPiece where some of the inputs are known and all of the outputs have specified types. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &inTypes, int const num, std::vector< std::string > const &outTypes)
 Create a muq::Modeling::WorkPiece where some of the inputs are known with a known number of inputs and all of the outputs have specified types. More...
 
 WorkPiece (std::vector< std::string > const &inTypes, std::map< unsigned int, std::string > const &outTypes)
 Create a muq::Modeling::WorkPiece where some of the outputs and all of the inputs have specified types. More...
 
 WorkPiece (std::vector< std::string > const &inTypes, std::map< unsigned int, std::string > const &outTypes, int const num)
 Create a muq::Modeling::WorkPiece where some of the outputs with a known number of outputs and all of the inputs have specified types. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &inTypes, std::map< unsigned int, std::string > const &outTypes)
 Create a muq::Mdoeling::WorkPiece where some of the inputs and some of the outputs have specified types. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &inTypes, int const numIn, std::map< unsigned int, std::string > const &outTypes)
 Create a muq::Mdoeling::WorkPiece where some of the inputs and some of the outputs have specified types with a fixed number of inputs. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &inTypes, std::map< unsigned int, std::string > const &outTypes, int const numOut)
 Create a muq::Mdoeling::WorkPiece where some of the inputs and some of the outputs have specified types with a fixed number of outputs. More...
 
 WorkPiece (std::map< unsigned int, std::string > const &inTypes, int const numIn, std::map< unsigned int, std::string > const &outTypes, int const numOut)
 Create a muq::Mdoeling::WorkPiece where some of the inputs and some of the outputs have specified types with a fixed number of inputs and outputs. More...
 
virtual ~WorkPiece ()
 Default destructor. More...
 
std::vector< boost::any > const & Evaluate (std::vector< boost::any > const &ins)
 Evaluate this muq::Modeling::WorkPiece. More...
 
std::vector< boost::any > const & Evaluate (ref_vector< boost::any > const &ins)
 Evaluate this muq::Modeling::WorkPiece using references to the inputs. More...
 
std::vector< boost::any > const & Evaluate ()
 Evaluate this muq::Modeling::WorkPiece in the case that there are no inputs. More...
 
template<typename... Args>
std::vector< boost::any > const & Evaluate (Args... args)
 Evalaute this muq::Modeling::WorkPiece using multiple arguments. More...
 
std::string const & Name ()
 Get the (unique) name of this work piece. More...
 
void SetName (std::string const &newName)
 Set the name of this work piece. More...
 
std::string InputType (unsigned int inputNum, bool const demangle=true) const
 Get the input type (if we know it) for a specific input. More...
 
int InputSize (unsigned int inputNum) const
 Get the length of a vector valued input with fixed size. More...
 
void SetInputSize (unsigned int inputNum, int newSize)
 
std::string OutputType (unsigned int outputNum, bool const demangle=true) const
 Get the output type (if we know it) for a specific output. More...
 
std::map< unsigned int, std::string > OutputTypes () const
 Get the output types. More...
 
std::map< unsigned int, std::string > InputTypes () const
 Get the input types. More...
 
unsigned int ID () const
 Get the unique ID number. More...
 
virtual double GetRunTime (const std::string &method="Evaluate") const
 Get the average run time for one of the implemented methods. More...
 
virtual unsigned long int GetNumCalls (const std::string &method="Evaluate") const
 get the number of times one of the implemented methods has been called. More...
 
virtual void ResetCallTime ()
 Resets the number of call and times. More...
 

Static Public Member Functions

static std::shared_ptr< OptimizerConstruct (std::shared_ptr< muq::Modeling::ModPiece > const &cost, boost::property_tree::ptree const &options)
 
static std::shared_ptr< OptimizerMapGetOptimizerMap ()
 
static void ListMethods (std::string prefix="")
 
- Static Public Member Functions inherited from muq::Modeling::WorkPiece
static ref_vector< const boost::any > ToRefVector (std::vector< boost::any > const &anyVec)
 Create vector of references from a vector of boost::any's. More...
 
static ref_vector< const Eigen::VectorXd > ToRefVector (std::vector< Eigen::VectorXd > const &anyVec)
 

Additional Inherited Members

- Public Attributes inherited from muq::Modeling::WorkPiece
int numInputs
 The number of inputs. More...
 
int numOutputs
 The number of outputs. More...
 

Member Typedef Documentation

◆ OptimizerConstructor

typedef std::function<std::shared_ptr<Optimizer>std::shared_ptr<muq::Modeling::ModPiece> const&, boost::property_tree::ptree)> muq::Optimization::Optimizer::OptimizerConstructor

Definition at line 49 of file Optimizer.h.

◆ OptimizerMap

Definition at line 50 of file Optimizer.h.

Constructor & Destructor Documentation

◆ Optimizer()

Optimizer::Optimizer ( std::shared_ptr< muq::Modeling::ModPiece > const &  cost,
boost::property_tree::ptree const &  pt 
)

Definition at line 11 of file Optimizer.cpp.

References muq::Modeling::WorkPiece::name.

◆ ~Optimizer()

virtual muq::Optimization::Optimizer::~Optimizer ( )
virtualdefault

Member Function Documentation

◆ AddEqualityConstraint() [1/2]

void Optimizer::AddEqualityConstraint ( std::shared_ptr< muq::Modeling::ModPiece > const &  eq)
virtual

Add an equality constraint to the optimization.

NOTE: the NLOPT algorithm used must be able to handle equality constraints

Parameters
[in]ineqThe constraint

Definition at line 62 of file Optimizer.cpp.

References eqConstraints.

◆ AddEqualityConstraint() [2/2]

void Optimizer::AddEqualityConstraint ( std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &  eq)
virtual

Add an equality constraint to the optimization.

NOTE: the NLOPT algorithm used must be able to handle equality constraints

Parameters
[in]ineqThe constraint

Definition at line 58 of file Optimizer.cpp.

References eqConstraints.

◆ AddInequalityConstraint() [1/2]

void Optimizer::AddInequalityConstraint ( std::shared_ptr< muq::Modeling::ModPiece > const &  ineq)
virtual

Add an inequality constraint to the optimization.

Parameters
[in]ineqThe constraint

Definition at line 50 of file Optimizer.cpp.

References ineqConstraints.

◆ AddInequalityConstraint() [2/2]

void Optimizer::AddInequalityConstraint ( std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &  ineq)
virtual

Add an inequality constraint to the optimization.

Parameters
[in]ineqThe constraint

Definition at line 46 of file Optimizer.cpp.

References ineqConstraints.

◆ ClearEqualityConstraint()

void Optimizer::ClearEqualityConstraint ( )

Clear all equality constraints.

Definition at line 66 of file Optimizer.cpp.

References eqConstraints.

◆ ClearInequalityConstraint()

void Optimizer::ClearInequalityConstraint ( )

Clear all inequality constraints.

Definition at line 54 of file Optimizer.cpp.

References ineqConstraints.

◆ Construct()

std::shared_ptr< Optimizer > Optimizer::Construct ( std::shared_ptr< muq::Modeling::ModPiece > const &  cost,
boost::property_tree::ptree const &  options 
)
static

Constructs an optimizer given a cost function and ptree of options.

Options:

Option Key Optional/Required Type Possible Values Default Description
Method Required string Many. Typically the name of the Optimizer child or the name of an NLOPT algorithm, but run Optimizer::ListMethod() for a complete list. Specifies the optimization method to use.
Ftol.AbsoluteTolerance Optional double Any nonnegative real number. 1e-8 Termination criterion based on value of function value. Note: this may not be used by all algorithms.
Ftol.RelativeTolerance Optional double Any nonnegative real number. 1e-8 Termination criterion based on the relative change in the function value. Note: this may not be used by all algorithms.
Xtol.AbsoluteTolerance Optional double Any nonnegative real number. 1e-8 Termination criterion based on the change of optimization variables or gradient. Note: this may not be used by all algorithms.
Xtol.RelativeTolerance Optional double Any nonnegative real number. 1e-8 Termination criterion based on the relative change of optimization variables or gradient. Note: this may not be used by all algorithms.
ConstraintTolerance Optional double Any nonnegative real number. 1e-8 Tolerance on equality constraints.
MaxEvaluations Optional unsigned int Any natural number. 100 Depending on the algorithm, either the number of cost function evaluations allowed or the number of optimization iterations allowed.

Definition at line 78 of file Optimizer.cpp.

References GetOptimizerMap().

◆ ConvertModPiece()

std::shared_ptr< CostFunction > Optimizer::ConvertModPiece ( std::shared_ptr< muq::Modeling::ModPiece > const &  modCost)
staticprotected

Definition at line 25 of file Optimizer.cpp.

◆ EvaluateImpl()

void Optimizer::EvaluateImpl ( muq::Modeling::ref_vector< boost::any > const &  inputs)
overrideprotectedvirtual

User-implemented function that determines the behavior of this muq::Modeling::WorkPiece.

This function determines how the WorkPiece::inputs determine WorkPiece::outputs. Must be implemented by a child.

WorkPiece::Evaluate() calls this function after checking the inputs and storing them in WorkPiece::inputs. This function populates WorkPiece::outputs, the outputs of this muq::Modeling::WorkPiece. WorkPiece::Evaluate() checks the outputs after calling this function.

Implements muq::Modeling::WorkPiece.

Reimplemented in muq::Optimization::NLoptOptimizer.

Definition at line 37 of file Optimizer.cpp.

References muq::Modeling::WorkPiece::outputs, and Solve().

◆ GetOptimizerMap()

std::shared_ptr< Optimizer::OptimizerMap > Optimizer::GetOptimizerMap ( )
static

Definition at line 95 of file Optimizer.cpp.

Referenced by Construct(), and ListMethods().

◆ ListMethods()

void Optimizer::ListMethods ( std::string  prefix = "")
static

Prints the list of registered optimization method to std::cout

Definition at line 71 of file Optimizer.cpp.

References GetOptimizerMap().

◆ Solve()

virtual std::pair<Eigen::VectorXd, double> muq::Optimization::Optimizer::Solve ( std::vector< Eigen::VectorXd > const &  inputs)
pure virtual

Solve the optimization problem.

Parameters
[in]inputsThe first input is the variable we are optimizing over, second input are the cost function parameters, and the third input are the constraint parameters
Returns
First: the argmin, second: the minimum cost

Implemented in muq::Optimization::NLoptOptimizer, and muq::Optimization::NewtonTrust.

Referenced by EvaluateImpl().

Member Data Documentation

◆ constraint_tol

const double muq::Optimization::Optimizer::constraint_tol
protected

Tolerance on the constraints.

Definition at line 120 of file Optimizer.h.

◆ eqConstraints

std::vector<std::shared_ptr<muq::Modeling::ModPiece> > muq::Optimization::Optimizer::eqConstraints
protected

Equality constraints.

NOTE: the solver muq::Optimization::Optimization::algorithm must be able to handle equality constraints

Definition at line 114 of file Optimizer.h.

Referenced by AddEqualityConstraint(), and ClearEqualityConstraint().

◆ ftol_abs

const double muq::Optimization::Optimizer::ftol_abs
protected

Definition at line 117 of file Optimizer.h.

Referenced by muq::Optimization::NewtonTrust::Solve().

◆ ftol_rel

const double muq::Optimization::Optimizer::ftol_rel
protected

Relative and absolute tolerances on the cost function value and on the difference between successive values of the state.

Definition at line 117 of file Optimizer.h.

◆ ineqConstraints

std::vector<std::shared_ptr<muq::Modeling::ModPiece> > muq::Optimization::Optimizer::ineqConstraints
protected

Inequality constraints.

Definition at line 108 of file Optimizer.h.

Referenced by AddInequalityConstraint(), and ClearInequalityConstraint().

◆ maxEvals

const unsigned int muq::Optimization::Optimizer::maxEvals
protected

Maximum number of cost function evaluations.

Definition at line 123 of file Optimizer.h.

Referenced by muq::Optimization::NewtonTrust::Solve().

◆ opt

std::shared_ptr<CostFunction> muq::Optimization::Optimizer::opt
protected

The cost function that we are trying to minimize.

Definition at line 105 of file Optimizer.h.

Referenced by muq::Optimization::NewtonTrust::Solve(), and muq::Optimization::NewtonTrust::SolveSub().

◆ xtol_abs

const double muq::Optimization::Optimizer::xtol_abs
protected

Definition at line 117 of file Optimizer.h.

Referenced by muq::Optimization::NewtonTrust::Solve().

◆ xtol_rel

const double muq::Optimization::Optimizer::xtol_rel
protected

Definition at line 117 of file Optimizer.h.


The documentation for this class was generated from the following files: