1 #ifndef OPTIMIZER_H_
2 #define OPTIMIZER_H_
3 
4 #include <boost/property_tree/ptree.hpp>
5 
6 #include "MUQ/Modeling/ModPiece.h"
7 #include "MUQ/Optimization/CostFunction.h"
8 
9 namespace muq {
10 namespace Optimization {
11   /// Solve an optimization problem
12   /**
13      \f{eqnarray}{
14      c &=& \min{J(x; \theta_1, ..., \theta_1)} \        \
15      f_i(x) &\leq& 0 \                                  \
16      g_i(x) &=& 0
17      \f}
18   */
19   class Optimizer : public muq::Modeling::WorkPiece {
20   public:
21 
22     Optimizer(std::shared_ptr<CostFunction> cost,
23                  boost::property_tree::ptree const& pt);
24 
25     virtual ~Optimizer();
26 
27     /// Add an inequality constraint to the optimization
28     /**
29        @param[in] ineq The constraint
30     */
31     virtual void AddInequalityConstraint(std::vector<std::shared_ptr<muq::Modeling::ModPiece>> const& ineq);
32 
33     /// Add an inequality constraint to the optimization
34     /**
35        @param[in] ineq The constraint
36     */
37     virtual void AddInequalityConstraint(std::shared_ptr<muq::Modeling::ModPiece> const& ineq);
38 
39 
40     /// Clear all inequality constraints
41     void ClearInequalityConstraint();
42 
43     /// Add an equality constraint to the optimization
44     /**
45        NOTE: the NLOPT algorithm used must be able to handle equality constraints
46        @param[in] ineq The constraint
47     */
48     virtual void AddEqualityConstraint(std::vector<std::shared_ptr<muq::Modeling::ModPiece>> const& eq);
49 
50     /// Add an equality constraint to the optimization
51     /**
52        NOTE: the NLOPT algorithm used must be able to handle equality constraints
53        @param[in] ineq The constraint
54     */
55     virtual void AddEqualityConstraint(std::shared_ptr<muq::Modeling::ModPiece> const& eq);
56 
57     /// Clear all equality constraints
58     void ClearEqualityConstraint();
59 
60     /// Solve the optimization problem
61     /**
62        @param[in] inputs The first input is the variable we are optimizing over, second input are the
63                   cost function parameters, and the third input are the constraint parameters
64        \return First: the argmin, second: the minimum cost
65     */
66     virtual std::pair<Eigen::VectorXd, double> Solve(std::vector<Eigen::VectorXd> const& inputs)=0;
67 
68   protected:
69 
70     /// The cost function that we are trying to minimize
71     std::shared_ptr<CostFunction> opt;
72 
73     /// Inequality constraints
74     std::vector<std::shared_ptr<muq::Modeling::ModPiece>> ineqConstraints;
75 
76     /// Equality constraints
77     /**
78        NOTE: the solver muq::Optimization::Optimization::algorithm must be able to handle equality constraints
79     */
80     std::vector<std::shared_ptr<muq::Modeling::ModPiece>> eqConstraints;
81 
82     /// Relative and absolute tolerances on the cost function value and on the difference between successive values of the state
83     const double ftol_rel, ftol_abs, xtol_rel, xtol_abs;
84 
85     /// Tolerance on the constraints
86     const double constraint_tol;
87 
88     /// Maximum number of cost function evaluations
89     const unsigned int maxEvals;
90   };
91 
92 } // namespace Optimization
93 } // namespace muq
94 
95 #endif
96