trajopt
 All Classes Namespaces Files Functions Variables Typedefs Pages
solver_interface.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <boost/shared_ptr.hpp>
3 #include <vector>
4 #include <string>
5 #include "sco_fwd.hpp"
6 #include <iosfwd>
7 #include <limits>
16 namespace sco{
17 
18 using std::string;
19 using std::vector;
20 using std::ostream;
21 
22 typedef vector<double> DblVec;
23 typedef vector<int> IntVec;
24 
25 enum ConstraintType {
26  EQ,
27  INEQ
28 };
29 
30 enum CvxOptStatus {
31  CVX_SOLVED,
32  CVX_INFEASIBLE,
33  CVX_FAILED
34 };
35 
36 typedef vector<Var> VarVector;
37 typedef vector<AffExpr> AffExprVector;
38 typedef vector<QuadExpr> QuadExprVector;
39 
46 class Model {
47 public:
48  virtual Var addVar(const string& name)=0;
49  virtual Var addVar(const string& name, double lb, double ub);
50 
51  virtual Cnt addEqCnt(const AffExpr&, const string& name)=0; // expr == 0
52  virtual Cnt addIneqCnt(const AffExpr&, const string& name)=0; // expr <= 0
53  virtual Cnt addIneqCnt(const QuadExpr&, const string& name)=0; // expr <= 0
54 
55  virtual void removeVar(const Var& var);
56  virtual void removeCnt(const Cnt& cnt);
57  virtual void removeVars(const VarVector& vars)=0;
58  virtual void removeCnts(const vector<Cnt>& cnts)=0;
59 
60  virtual void update() = 0; // call after adding/deleting stuff
61  virtual void setVarBounds(const Var& var, double lower, double upper);
62  virtual void setVarBounds(const VarVector& vars, const vector<double>& lower, const vector<double>& upper)=0;
63  virtual double getVarValue(const Var& var) const;
64  virtual vector<double> getVarValues(const VarVector& vars) const=0;
65  virtual CvxOptStatus optimize()=0;
66 
67  virtual void setObjective(const AffExpr&)=0;
68  virtual void setObjective(const QuadExpr&)=0;
69  virtual void writeToFile(const string& fname)=0;
70 
71  virtual VarVector getVars() const=0;
72 
73  virtual ~Model() {}
74 
75 };
76 
77 struct VarRep {
78  VarRep(int _index, const string& _name, void* _creator) : index(_index), name(_name), removed(false), creator(_creator) {}
79  int index;
80  string name;
81  bool removed;
82  void* creator;
83 };
84 
85 struct Var {
86  VarRep* var_rep;
87  Var() : var_rep(NULL) {}
88  Var(VarRep* var_rep) : var_rep(var_rep) {}
89  Var(const Var& other) : var_rep(other.var_rep) {}
90  double value(const double* x) const {return x[var_rep->index];}
91  double value(const vector<double>& x) const {assert(var_rep->index < (int)x.size()); return x[var_rep->index];}
92 };
93 
94 struct CntRep {
95  CntRep(int _index, void* _creator) : index(_index), removed(false), creator(_creator){}
96  int index;
97  bool removed;
98  void* creator;
99  ConstraintType type;
100  string expr; // todo placeholder
101 };
102 
103 struct Cnt {
104  CntRep* cnt_rep;
105  Cnt(): cnt_rep(NULL) {}
106  Cnt(CntRep* cnt_rep) : cnt_rep(cnt_rep) {}
107  Cnt(const Cnt& other) : cnt_rep(other.cnt_rep) {}
108 };
109 
110 struct AffExpr { // affine expression
111  double constant;
112  vector<double> coeffs;
113  vector<Var> vars;
114  AffExpr() : constant(0) {}
115  explicit AffExpr(double a) : constant(a) {}
116  explicit AffExpr(const Var& v) : constant(0), coeffs(1, 1), vars(1, v) {}
117  AffExpr(const AffExpr& other) :
118  constant(other.constant), coeffs(other.coeffs), vars(other.vars) {}
119  size_t size() const {return coeffs.size();}
120  double value(const double* x) const;
121  double value(const vector<double>& x) const;
122 };
123 
124 struct QuadExpr {
125  AffExpr affexpr;
126  vector<double> coeffs;
127  vector<Var> vars1;
128  vector<Var> vars2;
129  QuadExpr() {}
130  explicit QuadExpr(double a) : affexpr(a) {}
131  explicit QuadExpr(const Var& v) : affexpr(v) {}
132  explicit QuadExpr(const AffExpr& aff) : affexpr(aff) {}
133  size_t size() const {return coeffs.size();}
134  double value(const double* x) const;
135  double value(const vector<double>& x) const;
136 };
137 
138 
139 ostream& operator<<(ostream&, const Var&);
140 ostream& operator<<(ostream&, const Cnt&);
141 ostream& operator<<(ostream&, const AffExpr&);
142 ostream& operator<<(ostream&, const QuadExpr&);
143 
144 
145 ModelPtr createModel();
146 
147 }