trajopt
 All Classes Namespaces Files Functions Variables Typedefs Pages
modeling.hpp
1 #pragma once
2 
3 /*
4  * Model a non-convex optimization problem by defining Cost and Constraint objects
5  * which know how to generate a convex approximation
6  *
7  *
8  */
9 
10 #include <vector>
11 #include <boost/shared_ptr.hpp>
12 #include "sco/sco_fwd.hpp"
13 #include "sco/solver_interface.hpp"
14 
15 namespace sco {
16 
17 using std::vector;
18 
25 public:
26  ConvexObjective(Model* model) : model_(model) {}
27  void addAffExpr(const AffExpr&);
28  void addQuadExpr(const QuadExpr&);
29  void addHinge(const AffExpr&, double coeff);
30  void addAbs(const AffExpr&, double coeff);
31  void addHinges(const AffExprVector&);
32  void addL1Norm(const AffExprVector&);
33  void addL2Norm(const AffExprVector&);
34  void addMax(const AffExprVector&);
35 
36  bool inModel() {
37  return model_ != NULL;
38  }
39  void addConstraintsToModel();
40  void removeFromModel();
41  double value(const vector<double>& x);
42 
43  ~ConvexObjective();
44 
45 
46  Model* model_;
47  QuadExpr quad_;
48  vector<Var> vars_;
49  vector<AffExpr> eqs_;
50  vector<AffExpr> ineqs_;
51  vector<Cnt> cnts_;
52 private:
53  ConvexObjective() {}
55 };
56 
62 public:
63  ConvexConstraints(Model* model) : model_(model) {}
65  void addEqCnt(const AffExpr&);
67  void addIneqCnt(const AffExpr&);
68  void setModel(Model* model) {
69  assert(!inModel());
70  model_ = model;
71  }
72  bool inModel() {
73  return model_ != NULL;
74  }
75  void addConstraintsToModel();
76  void removeFromModel();
77 
78  vector<double> violations(const vector<double>& x);
79  double violation(const vector<double>& x);
80 
82  vector<AffExpr> eqs_;
83  vector<AffExpr> ineqs_;
84 private:
85  Model* model_;
86  vector<Cnt> cnts_;
87  ConvexConstraints() : model_(NULL) {}
89 };
90 
94 class Cost {
95 public:
97  virtual double value(const vector<double>&) = 0;
99  virtual ConvexObjectivePtr convex(const vector<double>& x, Model* model) = 0;
100 
101  string name() {return name_;}
102  void setName(const string& name) {name_=name;}
103  Cost() : name_("unnamed") {}
104  Cost(const string& name) : name_(name) {}
105  virtual ~Cost() {}
106 protected:
107  string name_;
108 };
109 
113 class Constraint {
114 public:
115 
117  virtual ConstraintType type() = 0;
119  virtual vector<double> value(const vector<double>& x) = 0;
121  virtual ConvexConstraintsPtr convex(const vector<double>& x, Model* model) = 0;
123  vector<double> violations(const vector<double>& x);
125  double violation(const vector<double>& x);
126 
127  string name() {return name_;}
128  void setName(const string& name) {name_=name;}
129  Constraint() : name_("unnamed") {}
130  Constraint(const string& name) : name_(name) {}
131  virtual ~Constraint() {}
132 
133 protected:
134  string name_;
135 };
136 
137 class EqConstraint : public Constraint{
138 public:
139  ConstraintType type() {return EQ;}
140 };
141 
142 class IneqConstraint : public Constraint {
143 public:
144  ConstraintType type() {return INEQ;}
145 };
146 
150 class OptProb {
151 public:
152  OptProb();
154  VarVector createVariables(const vector<string>& names);
156  VarVector createVariables(const vector<string>& names, const vector<double>& lb, const vector<double>& ub);
158  void setLowerBounds(const vector<double>& lb);
160  void setUpperBounds(const vector<double>& ub);
162  void setLowerBounds(const vector<double>& lb, const vector<Var>& vars);
164  void setUpperBounds(const vector<double>& ub, const vector<Var>& vars);
167  void addLinearConstraint(const AffExpr&, ConstraintType type);
169  void addCost(CostPtr);
171  void addConstraint(ConstraintPtr);
172  void addEqConstraint(ConstraintPtr);
173  void addIneqConstraint(ConstraintPtr);
174  virtual ~OptProb() {}
176  vector<double> getCentralFeasiblePoint(const vector<double>& x);
177  vector<double> getClosestFeasiblePoint(const vector<double>& x);
178 
179  vector<ConstraintPtr> getConstraints() const;
180  vector<CostPtr>& getCosts() {return costs_;}
181  vector<ConstraintPtr>& getIneqConstraints() {return ineqcnts_;}
182  vector<ConstraintPtr>& getEqConstraints() {return eqcnts_;}
183  DblVec& getLowerBounds() {return lower_bounds_;}
184  DblVec& getUpperBounds() {return upper_bounds_;}
185  ModelPtr getModel() {return model_;}
186  vector<Var>& getVars() {return vars_;}
187  int getNumCosts() {return costs_.size();}
188  int getNumConstraints() {return eqcnts_.size() + ineqcnts_.size();}
189  int getNumVars() {return vars_.size();}
190 
191 protected:
192  ModelPtr model_;
193  vector<Var> vars_;
194  vector<double> lower_bounds_;
195  vector<double> upper_bounds_;
196  vector<CostPtr> costs_;
197  vector<ConstraintPtr> eqcnts_;
198  vector<ConstraintPtr> ineqcnts_;
199 
200  OptProb(OptProb&);
201 };
202 
203 template <typename VecType>
204 inline void setVec(DblVec& x, const VarVector& vars, const VecType& vals) {
205  assert(vars.size() == vals.size());
206  for (int i = 0; i < vars.size(); ++i) {
207  x[vars[i].var_rep->index] = vals[i];
208  }
209 }
210 template <typename OutVecType>
211 inline OutVecType getVec1(const vector<double>& x, const VarVector& vars) {
212  OutVecType out(vars.size());
213  for (unsigned i=0; i < vars.size(); ++i) out[i] = x[vars[i].var_rep->index];
214  return out;
215 }
216 
217 
218 }