11 #include <boost/shared_ptr.hpp>
12 #include "sco/sco_fwd.hpp"
27 void addAffExpr(
const AffExpr&);
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&);
37 return model_ != NULL;
39 void addConstraintsToModel();
40 void removeFromModel();
41 double value(
const vector<double>& x);
50 vector<AffExpr> ineqs_;
68 void setModel(
Model* model) {
73 return model_ != NULL;
75 void addConstraintsToModel();
76 void removeFromModel();
78 vector<double> violations(
const vector<double>& x);
79 double violation(
const vector<double>& x);
83 vector<AffExpr> ineqs_;
97 virtual double value(
const vector<double>&) = 0;
99 virtual ConvexObjectivePtr
convex(
const vector<double>& x,
Model* model) = 0;
101 string name() {
return name_;}
102 void setName(
const string& name) {name_=name;}
103 Cost() : name_(
"unnamed") {}
104 Cost(
const string& name) : name_(name) {}
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);
127 string name() {
return name_;}
128 void setName(
const string& name) {name_=name;}
130 Constraint(
const string& name) : name_(name) {}
139 ConstraintType
type() {
return EQ;}
144 ConstraintType
type() {
return INEQ;}
156 VarVector
createVariables(
const vector<string>& names,
const vector<double>& lb,
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);
172 void addEqConstraint(ConstraintPtr);
173 void addIneqConstraint(ConstraintPtr);
177 vector<double> getClosestFeasiblePoint(
const vector<double>& x);
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();}
194 vector<double> lower_bounds_;
195 vector<double> upper_bounds_;
196 vector<CostPtr> costs_;
197 vector<ConstraintPtr> eqcnts_;
198 vector<ConstraintPtr> ineqcnts_;
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];
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];