MUQ  0.4.3
Optimizer.cpp
Go to the documentation of this file.
3 
5 
6 namespace pt = boost::property_tree;
7 using namespace muq::Modeling;
8 using namespace muq::Optimization;
9 
10 
11 Optimizer::Optimizer(std::shared_ptr<ModPiece> const& cost,
12  pt::ptree const& pt) :
13  WorkPiece(cost->InputTypes(),
14  cost->numInputs,
15  std::vector<std::string>({typeid(Eigen::VectorXd).name(), typeid(double).name()})),
16  opt(ConvertModPiece(cost)),
17  ftol_rel(pt.get("Ftol.RelativeTolerance", 1.0e-8)),
18  ftol_abs(pt.get("Ftol.AbsoluteTolerance", 1.0e-8)),
19  xtol_rel(pt.get("Xtol.RelativeTolerance", 1.0e-8)),
20  xtol_abs(pt.get("Xtol.AbsoluteTolerance", 1.0e-8)),
21  constraint_tol(pt.get("ConstraintTolerance", 1.0e-8)),
22  maxEvals(pt.get("MaxEvaluations", 100)) {}
23 
24 
25 std::shared_ptr<CostFunction> Optimizer::ConvertModPiece(std::shared_ptr<muq::Modeling::ModPiece> const& modCost)
26 {
27  // Check to see if the input is actually a CostFunction
28  auto cast = std::dynamic_pointer_cast<CostFunction>(modCost);
29  if(cast){
30  return cast;
31  }else{
32  return std::make_shared<ModPieceCostFunction>(modCost);
33  }
34 }
35 
36 
38 
39  std::vector<Eigen::VectorXd> inVec(1);
40  inVec.at(0) = muq::Utilities::AnyConstCast(inputs.at(0).get());
41 
42  outputs.resize(1);
43  outputs.at(0) = Solve(inVec);
44 }
45 
46 void Optimizer::AddInequalityConstraint(std::vector<std::shared_ptr<ModPiece>> const& ineq) {
47  ineqConstraints.insert(ineqConstraints.end(), ineq.begin(), ineq.end());
48 }
49 
50 void Optimizer::AddInequalityConstraint(std::shared_ptr<ModPiece> const& ineq) {
51  ineqConstraints.push_back(ineq);
52 }
53 
55  ineqConstraints.clear();
56 }
57 
58 void Optimizer::AddEqualityConstraint(std::vector<std::shared_ptr<ModPiece>> const& eq) {
59  eqConstraints.insert(eqConstraints.end(), eq.begin(), eq.end());
60 }
61 
62 void Optimizer::AddEqualityConstraint(std::shared_ptr<ModPiece> const& eq) {
63  eqConstraints.push_back(eq);
64 }
65 
67  eqConstraints.clear();
68 }
69 
70 
71 void Optimizer::ListMethods(std::string prefix)
72 {
73  auto map = GetOptimizerMap();
74  for(auto pair : *map)
75  std::cout << prefix << pair.first << std::endl;
76 }
77 
78 std::shared_ptr<Optimizer> Optimizer::Construct(std::shared_ptr<ModPiece> const& cost,
79  boost::property_tree::ptree const& options) {
80 
81  std::string method = options.get<std::string>("Algorithm");
83  auto iter = map.find(method);
84  if(iter != map.end()) {
85  return iter->second(cost,options);
86  }else{
87  std::stringstream msg;
88  msg << "Invalid \"Algorithm\" passed to Optimizer::Construct. A value of \"" << method << "\" was used, but valid options are:\n";
89  for(auto& part : map)
90  msg << " " << part.first << "\n";
91  throw std::invalid_argument(msg.str());
92  }
93 }
94 
95 std::shared_ptr<Optimizer::OptimizerMap> Optimizer::GetOptimizerMap()
96 {
97  static std::shared_ptr<Optimizer::OptimizerMap> map;
98 
99  if( !map )
100  map = std::make_shared<Optimizer::OptimizerMap>();
101 
102  return map;
103 }
Base class for MUQ's modelling envronment.
Definition: WorkPiece.h:40
std::vector< boost::any > outputs
The outputs.
Definition: WorkPiece.h:546
std::string name
A unique name for this WorkPiece. Defaults to <ClassName>_<id>
Definition: WorkPiece.h:583
std::vector< std::shared_ptr< muq::Modeling::ModPiece > > ineqConstraints
Inequality constraints.
Definition: Optimizer.h:108
void ClearEqualityConstraint()
Clear all equality constraints.
Definition: Optimizer.cpp:66
virtual void AddInequalityConstraint(std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &ineq)
Add an inequality constraint to the optimization.
Definition: Optimizer.cpp:46
static std::shared_ptr< Optimizer > Construct(std::shared_ptr< muq::Modeling::ModPiece > const &cost, boost::property_tree::ptree const &options)
Definition: Optimizer.cpp:78
static std::shared_ptr< CostFunction > ConvertModPiece(std::shared_ptr< muq::Modeling::ModPiece > const &modCost)
Definition: Optimizer.cpp:25
virtual void AddEqualityConstraint(std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &eq)
Add an equality constraint to the optimization.
Definition: Optimizer.cpp:58
std::vector< std::shared_ptr< muq::Modeling::ModPiece > > eqConstraints
Equality constraints.
Definition: Optimizer.h:114
virtual void EvaluateImpl(muq::Modeling::ref_vector< boost::any > const &inputs) override
User-implemented function that determines the behavior of this muq::Modeling::WorkPiece.
Definition: Optimizer.cpp:37
virtual std::pair< Eigen::VectorXd, double > Solve(std::vector< Eigen::VectorXd > const &inputs)=0
Solve the optimization problem.
std::map< std::string, OptimizerConstructor > OptimizerMap
Definition: Optimizer.h:50
void ClearInequalityConstraint()
Clear all inequality constraints.
Definition: Optimizer.cpp:54
static void ListMethods(std::string prefix="")
Definition: Optimizer.cpp:71
static std::shared_ptr< OptimizerMap > GetOptimizerMap()
Definition: Optimizer.cpp:95
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37