solver
Exprs.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <vector>
12 #include <solver/interface/Var.hpp>
13 
14 namespace solver {
15 
19  class LinExpr
20  {
21  public:
22  LinExpr(double constant=0.0) { constant_ = constant; vars_.clear(); coeffs_.clear(); }
23  LinExpr(Var var, double coeff=1.0) { constant_ = 0.0; vars_.push_back(var); coeffs_.push_back(coeff); }
24 
25  double getValue() const;
26  static bool isClean(const LinExpr& rhs);
27  static LinExpr clean(const LinExpr& rhs);
28  size_t size() const { return vars_.size(); }
29  double& getConstant() { return constant_; }
30  const Var& getVar(int i) const { return vars_[i]; }
31  double getCoeff(int i) const { return coeffs_[i]; }
32  const double& getConstant() const { return constant_; }
33  void clear() { constant_ = 0.0; coeffs_.clear(); vars_.clear(); }
34 
35  LinExpr operator*(double factor) const;
36  LinExpr& operator+=(const LinExpr& rhs);
37  LinExpr& operator+=(double a) { *this += LinExpr(a); return *this; }
38  LinExpr& operator-=(double a) { *this += LinExpr(-a); return *this; }
39  LinExpr& operator-=(const LinExpr& rhs) { *this += rhs*(-1.0); return *this; }
40  LinExpr operator/(double a) { LinExpr result = 0.0; result = *this*(1.0/a); return result; }
41  LinExpr operator+(double a) { LinExpr result = *this; result += LinExpr(a); return result; }
42  LinExpr operator-(double a) { LinExpr result = *this; result += LinExpr(-a); return result; }
43  LinExpr operator+(Var var) { LinExpr result = *this; result += LinExpr(var, 1.0); return result; }
44  LinExpr operator-(Var var) { LinExpr result = *this; result += LinExpr(var, -1.0); return result; }
45  LinExpr operator+(const LinExpr& rhs) const { LinExpr result = *this; result += rhs; return result; }
46  LinExpr operator-(const LinExpr& rhs) const { LinExpr result = *this; result += rhs*(-1.0); return result; }
47  LinExpr operator=(const LinExpr& rhs) { constant_ = rhs.constant_; vars_ = rhs.vars_; coeffs_ = rhs.coeffs_; return *this; }
48  LinExpr& operator*=(double a) { constant_ *= a; for (int i=0; i<(int)vars_.size(); i++) { coeffs_[i] *= a; } return *this; }
49  LinExpr& operator/=(double a) { constant_ /= a; for (int i=0; i<(int)vars_.size(); i++) { coeffs_[i] /= a; } return *this; }
50 
51  friend LinExpr operator+(Var var) { return LinExpr(var, 1.0); }
52  friend LinExpr operator-(Var var) { return LinExpr(var, -1.0); }
53  friend LinExpr operator*(Var var, double a) { return LinExpr(var, a); }
54  friend LinExpr operator*(double a, Var var) { return LinExpr(var, a); }
55  friend LinExpr operator+(Var x, Var y) { return LinExpr(x)+LinExpr(y); }
56  friend LinExpr operator-(Var x, Var y) { return LinExpr(x)-LinExpr(y); }
57  friend LinExpr operator/(Var var, double a) { return LinExpr(var, 1.0/a); }
58  friend LinExpr operator+(double a, Var var) { return LinExpr(a)+LinExpr(var); }
59  friend LinExpr operator+(Var var, double a) { return LinExpr(var)+LinExpr(a); }
60  friend LinExpr operator-(double a, Var var) { return LinExpr(a)-LinExpr(var); }
61  friend LinExpr operator-(Var var, double a) { return LinExpr(var)-LinExpr(a); }
62  friend LinExpr operator+(const LinExpr& rhs) { LinExpr result = rhs; return result; }
63  friend LinExpr operator-(const LinExpr& rhs) { LinExpr result = LinExpr()-rhs; return result; }
64  friend LinExpr operator-(Var var, LinExpr rhs) { LinExpr result = LinExpr(var)-rhs; return result; }
65  friend LinExpr operator+(double a, const LinExpr& rhs) { LinExpr result = LinExpr(a)+rhs; return result; }
66  friend LinExpr operator-(double a, const LinExpr& rhs) { LinExpr result = LinExpr(a)-rhs; return result; }
67  friend LinExpr operator*(double factor, const LinExpr& rhs) { LinExpr result = rhs*factor; return result; }
68 
69  friend std::ostream& operator<<(std::ostream &stream, LinExpr expr);
70 
71  private:
72  double constant_;
73  std::vector<Var> vars_;
74  std::vector<double> coeffs_;
75  };
76 
77  LinExpr operator+(Var var);
78  LinExpr operator-(Var var);
79  LinExpr operator+(Var x, Var y);
80  LinExpr operator-(Var x, Var y);
81  LinExpr operator*(Var var, double a);
82  LinExpr operator*(double a, Var var);
83  LinExpr operator/(Var var, double a);
84  LinExpr operator+(double a, Var var);
85  LinExpr operator+(Var var, double a);
86  LinExpr operator-(double a, Var var);
87  LinExpr operator-(Var var, double a);
88  LinExpr operator+(const LinExpr& rhs);
89  LinExpr operator-(const LinExpr& rhs);
90  LinExpr operator-(Var var, LinExpr rhs);
91  LinExpr operator+(double a, const LinExpr& rhs);
92  LinExpr operator-(double a, const LinExpr& rhs);
93  LinExpr operator*(double factor, const LinExpr& rhs);
94  std::ostream& operator<<(std::ostream &stream, LinExpr expr);
95 
101  {
102  public:
103  DCPQuadExpr() : lexpr_(0.0), trust_region_(false), soft_constraint_(false) {}
104  ~DCPQuadExpr(){}
105 
106  double getValue() const;
107  void addLinTerm(const LinExpr& lexpr) { lexpr_ += lexpr; }
108  void clear() { lexpr_ = 0.0; qexpr_.clear(); coeffs_.clear(); extra_vars_.clear(); }
109  void addQuaTerm(double coeff, const LinExpr& lexpr) { if (coeff != 0.0) { qexpr_.push_back(lexpr); coeffs_.push_back(coeff); } }
110 
111  LinExpr& lexpr() { return lexpr_; }
112  std::string& sense() { return sense_; }
113  bool& trustRegion() { return trust_region_; }
114  std::vector<LinExpr>& qexpr() { return qexpr_; }
115  std::vector<double>& coeffs() { return coeffs_; }
116  bool& softConstraint() { return soft_constraint_; }
117  std::vector<Var>& extraVars() { return extra_vars_; }
118 
119  const LinExpr& lexpr() const { return lexpr_; }
120  const std::string& sense() const { return sense_; }
121  const bool& trustRegion() const { return trust_region_; }
122  const std::vector<LinExpr>& qexpr() const { return qexpr_; }
123  const std::vector<double>& coeffs() const { return coeffs_; }
124  const bool& softConstraint() const { return soft_constraint_; }
125  const std::vector<Var>& extraVars() const { return extra_vars_; }
126  const Var& getVar(int qid, int lid) const { return qexpr_[qid].getVar(lid); }
127 
128  private:
129  LinExpr lexpr_;
130  std::string sense_;
131  std::vector<double> coeffs_;
132  std::vector<LinExpr> qexpr_;
133  std::vector<Var> extra_vars_;
134  bool trust_region_, soft_constraint_;
135  };
136 }
Helper class to ease the construction of a quadratic expression (e.g.
Definition: Exprs.hpp:100
Definition: Cone.hpp:20
Helper class to define an optimization variable, used in the construction of linear and quadratic exp...
Definition: Var.hpp:36
Helper class to ease the construction of a linear expression (e.g.
Definition: Exprs.hpp:19