Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Solvers.h
Go to the documentation of this file.
1 
8 #ifndef LIMBO_SOLVERS_SOLVERS_H
9 #define LIMBO_SOLVERS_SOLVERS_H
10 
11 #include <iostream>
12 #include <limits>
13 #include <string>
14 #include <vector>
15 #include <map>
16 #include <algorithm>
17 #include <limbo/preprocessor/Msg.h>
18 #include <limbo/math/Math.h>
20 
22 namespace limbo
23 {
25 namespace solvers
26 {
27 
30 {
31  MIN,
32  MAX,
40 };
41 
44 inline std::string toString(SolverProperty sp)
45 {
46  switch (sp)
47  {
48  case MIN:
49  return "MIN";
50  case MAX:
51  return "MAX";
52  case BINARY:
53  return "BINARY";
54  case INTEGER:
55  return "INTEGER";
56  case CONTINUOUS:
57  return "CONTINUOUS";
58  case OPTIMAL:
59  return "OPTIMAL";
60  case INFEASIBLE:
61  return "INFEASIBLE";
62  case SUBOPTIMAL:
63  default:
64  return "SUBOPTIMAL";
65  }
66 }
67 
68 // forward declaration
69 template <typename T>
70 class Variable;
71 template <typename T>
73 template <typename T>
74 class LinearTerm;
75 template <typename T>
77 template <typename T>
79 template <typename T, typename V>
80 class LinearModel;
81 template <typename T, typename I, int StartingIndex>
82 struct MatrixCSR;
83 
86 template <typename T>
87 class Variable
88 {
89  public:
92 
95  explicit Variable(unsigned int id = std::numeric_limits<unsigned int>::max())
96  : m_id(id)
97  {
98  }
101  Variable(Variable const& rhs)
102  {
103  copy(rhs);
104  }
108  {
109  if (this != &rhs)
110  copy(rhs);
111  return *this;
112  }
115 
117  unsigned int id() const {return m_id;}
120  void setId(unsigned int id) {m_id = id;}
121 
124 
128  bool operator==(Variable const& rhs) const
129  {
130  return m_id == rhs.m_id;
131  }
135  bool operator!=(Variable const& rhs) const
136  {
137  return !this->operator==(rhs);
138  }
142  {
144  return term.negate();
145  }
150  friend LinearTerm<coefficient_value_type> operator*(Variable const& var, coefficient_value_type coef)
151  {
152  return LinearTerm<coefficient_value_type>(var, coef);
153  }
158  friend LinearTerm<coefficient_value_type> operator*(coefficient_value_type coef, Variable const& var)
159  {
160  return var*coef;
161  }
166  friend LinearTerm<coefficient_value_type> operator/(Variable const& var, coefficient_value_type coef)
167  {
168  return LinearTerm<coefficient_value_type>(var)/coef;
169  }
174  friend LinearExpression<coefficient_value_type> operator+(Variable const& var, coefficient_value_type constant)
175  {
176  return LinearTerm<coefficient_value_type>(var)+constant;
177  }
182  friend LinearExpression<coefficient_value_type> operator+(coefficient_value_type constant, Variable const& var)
183  {
184  return var+constant;
185  }
191  {
192  return LinearTerm<coefficient_value_type>(var1)+var2;
193  }
198  friend LinearExpression<coefficient_value_type> operator-(Variable const& var, coefficient_value_type constant)
199  {
200  return LinearTerm<coefficient_value_type>(var)-constant;
201  }
206  friend LinearExpression<coefficient_value_type> operator-(coefficient_value_type constant, Variable const& var)
207  {
208  return LinearTerm<coefficient_value_type>(var, -1)+constant;
209  }
215  {
216  return LinearTerm<coefficient_value_type>(var1)-var2;
217  }
221  friend LinearConstraint<coefficient_value_type> operator<(Variable const& var, coefficient_value_type rhs)
222  {
223  return (LinearTerm<coefficient_value_type>(var) < rhs);
224  }
228  friend LinearConstraint<coefficient_value_type> operator<=(Variable const& var, coefficient_value_type rhs)
229  {
230  return (var < rhs);
231  }
235  friend LinearConstraint<coefficient_value_type> operator>(Variable const& var, coefficient_value_type rhs)
236  {
237  return (LinearTerm<coefficient_value_type>(var) > rhs);
238  }
242  friend LinearConstraint<coefficient_value_type> operator>=(Variable const& var, coefficient_value_type rhs)
243  {
244  return (var > rhs);
245  }
249  friend LinearConstraint<coefficient_value_type> operator==(Variable const& var, coefficient_value_type rhs)
250  {
251  return (LinearTerm<coefficient_value_type>(var) == rhs);
252  }
253  protected:
255  void copy(Variable const& rhs)
256  {
257  m_id = rhs.m_id;
258  }
259 
260  unsigned int m_id;
261 };
262 
265 template <typename V>
266 class VariableProperty
267 {
268  public:
271 
277  VariableProperty(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string const& n)
278  : m_lowerBound(lb)
279  , m_upperBound(ub)
280  , m_numericType(nt)
281  , m_name(n)
282  {
283  }
287  {
288  copy(rhs);
289  }
293  {
294  if (this != &rhs)
295  copy(rhs);
296  return *this;
297  }
298 
300  variable_value_type lowerBound() const {return m_lowerBound;}
302  void setLowerBound(variable_value_type lb) {m_lowerBound = lb;}
304  void updateLowerBound(variable_value_type lb) {m_lowerBound = std::max(m_lowerBound, lb);}
306  variable_value_type upperBound() const {return m_upperBound;}
308  void setUpperBound(variable_value_type ub) {m_upperBound = ub;}
310  void updateUpperBound(variable_value_type ub) {m_upperBound = std::min(m_upperBound, ub);}
315  {
316  m_numericType = nt;
317  if (m_numericType == BINARY) // update bounds for binary
318  {
319  m_lowerBound = 0;
320  m_upperBound = 1;
321  }
322  }
324  std::string const& name() const {return m_name;}
326  void setName(std::string const& n) {m_name = n;}
327 
328  protected:
331  void copy(VariableProperty const& rhs)
332  {
334  m_upperBound = rhs.m_upperBound;
336  m_name = rhs.m_name;
337  }
338 
339  variable_value_type m_lowerBound;
340  variable_value_type m_upperBound;
342  std::string m_name;
343 };
344 
347 template <typename T>
348 class LinearTerm
349 {
350  public:
355 
359  LinearTerm(variable_type var = variable_type(), coefficient_value_type coef = 1)
360  : m_var(var)
361  , m_coef(coef)
362  {
363  }
366  LinearTerm(LinearTerm const& rhs)
367  {
368  copy(rhs);
369  }
373  {
374  if (this != &rhs)
375  copy(rhs);
376  return *this;
377  }
380  {
381  }
382 
384  variable_type const& variable() const {return m_var;}
386  void setVariable(variable_type const& var) {m_var = var;}
388  coefficient_value_type coefficient() const {return m_coef;}
390  void setCoefficient(coefficient_value_type coef) {m_coef = coef;}
391 
395  {
396  LinearTerm term (*this);
397  return term.negate();
398  }
402  {
403  m_coef = -m_coef;
404  return *this;
405  }
410  friend LinearTerm operator*(LinearTerm const& term, coefficient_value_type coef)
411  {
412  LinearTerm other (term);
413  other *= coef;
414  return other;
415  }
420  friend LinearTerm operator*(coefficient_value_type coef, LinearTerm const& term)
421  {
422  return term*coef;
423  }
427  LinearTerm& operator*=(coefficient_value_type coef)
428  {
429  m_coef *= coef;
430  return *this;
431  }
436  friend LinearTerm operator/(LinearTerm const& term, coefficient_value_type coef)
437  {
438  LinearTerm other (term);
439  other /= coef;
440  return other;
441  }
445  LinearTerm& operator/=(coefficient_value_type coef)
446  {
447  m_coef /= coef;
448  return *this;
449  }
454  friend LinearExpression<coefficient_value_type> operator+(LinearTerm const& term, coefficient_value_type constant)
455  {
456  return LinearExpression<coefficient_value_type>(term, constant);
457  }
462  friend LinearExpression<coefficient_value_type> operator+(LinearTerm const& term, variable_type const& var)
463  {
464  return term+LinearTerm(var);
465  }
470  friend LinearExpression<coefficient_value_type> operator+(coefficient_value_type constant, LinearTerm const& term)
471  {
472  return LinearExpression<coefficient_value_type>(term, constant);
473  }
478  friend LinearExpression<coefficient_value_type> operator+(variable_type const& var, LinearTerm const& term)
479  {
480  return LinearTerm(var)+term;
481  }
487  {
489  expr += term2;
490  return expr;
491  }
496  friend LinearExpression<coefficient_value_type> operator-(LinearTerm const& term, coefficient_value_type constant)
497  {
498  return LinearExpression<coefficient_value_type>(term, -constant);
499  }
504  friend LinearExpression<coefficient_value_type> operator-(LinearTerm const& term, variable_type const& var)
505  {
506  return term-LinearTerm(var);
507  }
512  friend LinearExpression<coefficient_value_type> operator-(variable_type const& var, LinearTerm const& term)
513  {
514  return LinearTerm(var)-term;
515  }
520  friend LinearExpression<coefficient_value_type> operator-(coefficient_value_type constant, LinearTerm const& term)
521  {
522  return LinearExpression<coefficient_value_type>(-term, constant);
523  }
529  {
531  expr -= term2;
532  return expr;
533  }
537  friend LinearConstraint<coefficient_value_type> operator<(LinearTerm const& term, coefficient_value_type rhs)
538  {
539  return LinearConstraint<coefficient_value_type>(term, rhs, '<');
540  }
544  friend LinearConstraint<coefficient_value_type> operator<=(LinearTerm const& term, coefficient_value_type rhs)
545  {
546  return (term < rhs);
547  }
551  friend LinearConstraint<coefficient_value_type> operator>(LinearTerm const& term, coefficient_value_type rhs)
552  {
553  return LinearConstraint<coefficient_value_type>(term, rhs, '>');
554  }
558  friend LinearConstraint<coefficient_value_type> operator>=(LinearTerm const& term, coefficient_value_type rhs)
559  {
560  return (term > rhs);
561  }
565  friend LinearConstraint<coefficient_value_type> operator==(LinearTerm const& term, coefficient_value_type rhs)
566  {
567  return LinearConstraint<coefficient_value_type>(term, rhs, '=');
568  }
572  bool operator==(LinearTerm const& rhs) const
573  {
574  return m_var == rhs.m_var && m_coef == rhs.m_coef;
575  }
576  protected:
579  void copy(LinearTerm const& rhs)
580  {
581  m_var = rhs.m_var;
582  m_coef = rhs.m_coef;
583  }
584 
585  variable_type m_var;
586  coefficient_value_type m_coef;
587 };
588 
591 {
596  template <typename TermType>
597  bool operator()(TermType const& t1, TermType const& t2) const
598  {
599  return t1.variable().id() < t2.variable().id();
600  }
601 };
602 
605 template <typename T>
606 class LinearExpression
607 {
608  public:
615 
618  : m_vTerm()
619  , m_constant(0)
620  {
621  }
623  LinearExpression(term_type const& term, coefficient_value_type constant = 0)
624  {
625  m_vTerm.push_back(term);
626  m_constant = constant;
627  }
631  {
632  copy(rhs);
633  }
637  {
638  if (this != &rhs)
639  copy(rhs);
640  return *this;
641  }
644  {
645  }
646 
650  {
651  m_vTerm.swap(rhs.m_vTerm);
652  std::swap(m_constant, rhs.m_constant);
653  }
655  std::vector<term_type> const& terms() const {return m_vTerm;}
657  void clear()
658  {
659  m_vTerm.clear();
660  m_constant = 0;
661  }
664  void reserve(unsigned int n) {m_vTerm.reserve(n);}
666  coefficient_value_type constant() const {return m_constant;}
670  LinearExpression& setConstant(coefficient_value_type constant)
671  {
672  m_constant = constant;
673  return *this;
674  }
678  LinearExpression& incrementConstant(coefficient_value_type constant)
679  {
680  m_constant += constant;
681  return *this;
682  }
683 
688  friend LinearExpression operator+(LinearExpression const& expr, coefficient_value_type constant)
689  {
690  LinearExpression other (expr);
691  other += constant;
692  return other;
693  }
698  friend LinearExpression operator+(LinearExpression const& expr, term_type const& term)
699  {
700  LinearExpression other (expr);
701  other += term;
702  return other;
703  }
708  friend LinearExpression operator+(coefficient_value_type constant, LinearExpression const& expr)
709  {
710  return expr+constant;
711  }
716  friend LinearExpression operator+(term_type const& term, LinearExpression const& expr)
717  {
718  return expr+term;
719  }
724  friend LinearExpression operator+(LinearExpression const& expr1, LinearExpression const& expr2)
725  {
726  LinearExpression other (expr1);
727  other += expr2;
728  return other;
729  }
733  LinearExpression& operator+=(coefficient_value_type constant)
734  {
735  this->incrementConstant(constant);
736  return *this;
737  }
741  LinearExpression& operator+=(term_type const& term)
742  {
743  m_vTerm.push_back(term);
744  return *this;
745  }
750  {
751  m_vTerm.insert(m_vTerm.end(), expr.terms().begin(), expr.terms().end());
752  this->incrementConstant(expr.constant());
753  return *this;
754  }
759  friend LinearExpression operator-(LinearExpression const& expr, coefficient_value_type constant)
760  {
761  LinearExpression other (expr);
762  other -= constant;
763  return other;
764  }
769  friend LinearExpression operator-(LinearExpression const& expr, term_type const& term)
770  {
771  LinearExpression other (expr);
772  other -= term;
773  return other;
774  }
779  friend LinearExpression operator-(coefficient_value_type constant, LinearExpression const& expr)
780  {
781  return (-expr)+constant;
782  }
787  friend LinearExpression operator-(term_type const& term, LinearExpression const& expr)
788  {
789  return (-expr)+term;
790  }
795  friend LinearExpression operator-(LinearExpression const& expr1, LinearExpression const& expr2)
796  {
797  LinearExpression other (expr1);
798  other -= expr2;
799  return other;
800  }
804  LinearExpression& operator-=(coefficient_value_type constant)
805  {
806  this->incrementConstant(-constant);
807  return *this;
808  }
812  LinearExpression& operator-=(term_type term)
813  {
814  term.setCoefficient(-term.coefficient());
815  return this->operator+=(term);
816  }
821  {
822  for (typename std::vector<term_type>::const_iterator it = expr.terms().begin(), ite = expr.terms().end(); it != ite; ++it)
823  this->operator-=(*it);
824  this->incrementConstant(-expr.constant());
825  return *this;
826  }
831  friend LinearExpression operator*(LinearExpression const& expr, coefficient_value_type c)
832  {
833  LinearExpression other (expr);
834  other *= c;
835  return other;
836  }
841  friend LinearExpression operator*(coefficient_value_type c, LinearExpression const& expr)
842  {
843  return expr*c;
844  }
848  LinearExpression& operator*=(coefficient_value_type c)
849  {
850  for (typename std::vector<term_type>::iterator it = m_vTerm.begin(); it != m_vTerm.end(); ++it)
851  it->setCoefficient(c*it->coefficient());
852  m_constant *= c;
853  return *this;
854  }
859  friend LinearExpression operator/(LinearExpression const& expr, coefficient_value_type c)
860  {
861  LinearExpression other (expr);
862  other /= c;
863  return other;
864  }
868  LinearExpression& operator/=(coefficient_value_type c)
869  {
870  for (typename std::vector<term_type>::iterator it = m_vTerm.begin(); it != m_vTerm.end(); ++it)
871  it->setCoefficient(it->coefficient()/c);
872  m_constant /= c;
873  return *this;
874  }
878  {
879  LinearExpression expr (*this);
880  return expr.negate();
881  }
885  {
886  for (typename std::vector<term_type>::iterator it = m_vTerm.begin(); it != m_vTerm.end(); ++it)
887  it->setCoefficient(-it->coefficient());
889  return *this;
890  }
894  friend LinearConstraint<coefficient_value_type> operator<(LinearExpression const& expr, coefficient_value_type rhs)
895  {
896  return LinearConstraint<coefficient_value_type>(expr, rhs, '<');
897  }
901  friend LinearConstraint<coefficient_value_type> operator<=(LinearExpression const& expr, coefficient_value_type rhs)
902  {
903  return (expr < rhs);
904  }
908  friend LinearConstraint<coefficient_value_type> operator>(LinearExpression const& expr, coefficient_value_type rhs)
909  {
910  return LinearConstraint<coefficient_value_type>(expr, rhs, '>');
911  }
915  friend LinearConstraint<coefficient_value_type> operator>=(LinearExpression const& expr, coefficient_value_type rhs)
916  {
917  return (expr > rhs);
918  }
922  friend LinearConstraint<coefficient_value_type> operator==(LinearExpression const& expr, coefficient_value_type rhs)
923  {
924  return LinearConstraint<coefficient_value_type>(expr, rhs, '=');
925  }
926 
929  void simplify()
930  {
931  if (m_vTerm.empty())
932  return;
933  std::sort(m_vTerm.begin(), m_vTerm.end(), CompareTermByVariable());
934  // merge terms
935  typename std::vector<term_type>::iterator itw = m_vTerm.begin(); // write iterator
936  typename std::vector<term_type>::iterator itr = m_vTerm.begin(); // read iterator
937  typename std::vector<term_type>::iterator ite = m_vTerm.end();
938  for (; itr != ite; ++itr)
939  {
940  if (itr != itw)
941  {
942  if (itr->variable() == itw->variable())
943  itw->setCoefficient(itw->coefficient()+itr->coefficient());
944  else
945  {
946  ++itw;
947  *itw = *itr;
948  }
949  }
950  }
951  m_vTerm.resize(std::distance(m_vTerm.begin(), itw+1));
952  // remove terms with zero coefficients
953  // the order is not maintained
954  for (itr = m_vTerm.begin(); itr != m_vTerm.end(); )
955  {
956  if (itr->coefficient() == 0)
957  {
958  *itr = m_vTerm.back();
959  m_vTerm.pop_back();
960  }
961  else
962  ++itr;
963  }
964  }
965 
966  protected:
969  void copy(LinearExpression const& rhs)
970  {
971  m_vTerm = rhs.m_vTerm;
972  m_constant = rhs.m_constant;
973  }
974 
975  std::vector<term_type> m_vTerm;
976  coefficient_value_type m_constant;
977 };
978 
981 template <typename T>
982 class LinearConstraint
983 {
984  public:
993 
998  LinearConstraint(expression_type expr = expression_type(), coefficient_value_type rhs = 0, char s = '<')
999  : m_id (std::numeric_limits<unsigned int>::max())
1000  , m_expr(expr)
1001  , m_rhs(rhs)
1002  , m_sense(s)
1003  {
1004  clearConstant();
1005  }
1008  {
1009  copy(rhs);
1010  }
1013  {
1014  if (this != &rhs)
1015  copy(rhs);
1016  return *this;
1017  }
1020  {
1021  }
1022 
1024  unsigned int id() const {return m_id;}
1026  void setId(unsigned int i) {m_id = i;}
1028  expression_type const& expression() const {return m_expr;}
1030  void setExpression(expression_type const& expr) {m_expr = expr;}
1032  void emplaceExpression(expression_type& expr) {m_expr.swap(expr);}
1034  coefficient_value_type rightHandSide() const {return m_rhs;}
1036  void setRightHandSide(coefficient_value_type rhs) {m_rhs = rhs;}
1038  char sense() const {return m_sense;}
1040  void setSense(char s) {m_sense = s;}
1044  {
1045  std::swap(m_id, rhs.m_id);
1046  m_expr.swap(rhs.m_expr);
1047  std::swap(m_rhs, rhs.m_rhs);
1048  std::swap(m_sense, rhs.m_sense);
1049 #ifdef DEBUG_SOLVERS
1050  limboAssertMsg(m_expr.constant() == 0, "expression constant should always be zero in constraint");
1051 #endif
1052  }
1054  void simplify()
1055  {
1056 #ifdef DEBUG_SOLVERS
1057  limboAssertMsg(m_expr.constant() == 0, "expression constant should always be zero in constraint");
1058 #endif
1059  m_expr.simplify();
1060  }
1063  void normalize(char s)
1064  {
1065  simplify();
1066  if ((m_sense == '<' && s == '>')
1067  || (m_sense == '>' && s == '<'))
1068  {
1069  m_expr.negate();
1070  m_rhs = -m_rhs;
1071  m_sense = s;
1072  }
1073  }
1076  void scale(coefficient_value_type factor)
1077  {
1078 #ifdef DEBUG_SOLVERS
1079  limboAssertMsg(m_expr.constant() == 0, "expression constant should always be zero in constraint");
1080 #endif
1081  // in case factor is negative
1082  if (factor < 0)
1083  {
1084  if (m_sense == '<')
1085  m_sense = '>';
1086  else if (m_sense == '>')
1087  m_sense = '<';
1088  }
1089  m_expr *= factor;
1090  m_rhs *= factor;
1091  }
1095  {
1096  // make sure the constant is zero in expression
1097  m_rhs -= m_expr.constant();
1098  m_expr.setConstant(0);
1099  }
1102  void reserve(unsigned int n) {m_expr.reserve(n);}
1103 
1107  LinearConstraint& operator+=(term_type const& term)
1108  {
1109  m_expr += term;
1110  return *this;
1111  }
1115  LinearConstraint& operator+=(expression_type const& expr)
1116  {
1117  m_expr += expr;
1118  clearConstant();
1119  return *this;
1120  }
1124  LinearConstraint& operator-=(term_type term)
1125  {
1126  m_expr -= term;
1127  return *this;
1128  }
1132  LinearConstraint& operator-=(expression_type const& expr)
1133  {
1134  m_expr -= expr;
1135  clearConstant();
1136  return *this;
1137  }
1138  protected:
1141  void copy(LinearConstraint const& rhs)
1142  {
1143  m_id = rhs.m_id;
1144  m_expr = rhs.m_expr;
1145  m_rhs = rhs.m_rhs;
1146  m_sense = rhs.m_sense;
1147  clearConstant();
1148  }
1149 
1150  unsigned int m_id;
1151  expression_type m_expr;
1152  coefficient_value_type m_rhs;
1153  char m_sense;
1154 };
1155 
1159 template <typename T, typename V>
1160 class LinearModel : public LpParser::LpDataBase
1161 {
1162  public:
1179 
1182  : base_type()
1183  {
1184  m_optType = MIN;
1185  }
1189  : base_type(rhs)
1190  {
1191  copy(rhs);
1192  }
1196  {
1197  if (this != &rhs)
1198  {
1199  this->base_type::operator=(rhs);
1200  copy(rhs);
1201  }
1202  return *this;
1203  }
1206  {
1207  }
1208 
1210  std::vector<constraint_type> const& constraints() const {return m_vConstraint;}
1212  std::vector<constraint_type>& constraints() {return m_vConstraint;}
1217  bool addConstraint(constraint_type const& constr, std::string name = "")
1218  {
1219  expression_type expr = constr.expression();
1220  return emplaceConstraint(expr, constr.sense(), constr.rightHandSide(), name);
1221  }
1228  bool emplaceConstraint(expression_type& expr, char sense, coefficient_value_type rhs, std::string name = "")
1229  {
1230  // simplify expression
1231  expr.simplify();
1232  // verify whether the constraint is actually a bound constraint
1233  std::vector<term_type> const& vTerm = expr.terms();
1234  if (vTerm.empty())
1235  return false;
1236  if (vTerm.size() == 1) // one term, bound constraint
1237  {
1238  term_type const& term = vTerm.front();
1239  switch (sense)
1240  {
1241  case '>':
1242  if (term.coefficient() > 0) // lower bound
1243  m_vVariableProperty.at(term.variable().id()).updateLowerBound(rhs/term.coefficient());
1244  else if (term.coefficient() < 0) // upper bound
1245  m_vVariableProperty.at(term.variable().id()).updateUpperBound(rhs/term.coefficient());
1246  else
1247  return false;
1248  break;
1249  case '<':
1250  if (term.coefficient() > 0) // upper bound
1251  m_vVariableProperty.at(term.variable().id()).updateUpperBound(rhs/term.coefficient());
1252  else if (term.coefficient() < 0) // lower bound
1253  m_vVariableProperty.at(term.variable().id()).updateLowerBound(rhs/term.coefficient());
1254  else
1255  return false;
1256  break;
1257  case '=':
1258  if (term.coefficient() != 0)
1259  {
1260  // lower bound
1261  m_vVariableProperty.at(term.variable().id()).updateLowerBound(rhs/term.coefficient());
1262  // upper bound
1263  m_vVariableProperty.at(term.variable().id()).updateUpperBound(rhs/term.coefficient());
1264  }
1265  else
1266  return false;
1267  break;
1268  default:
1269  limboAssertMsg(0, "Unknown sense %c", sense);
1270  }
1271  }
1272  else // actual constraint
1273  {
1274  m_vConstraint.push_back(constraint_type());
1275  m_vConstraintName.push_back(name);
1276  constraint_type& constr = m_vConstraint.back();
1277  constr.setId(m_vConstraint.size()-1);
1278  constr.emplaceExpression(expr);
1279  constr.setSense(sense);
1280  constr.setRightHandSide(rhs);
1281  }
1282  return true;
1283  }
1286  std::string const& constraintName(constraint_type const& constr) const {return m_vConstraintName[constr.id()];}
1288  std::vector<std::string> const& constraintNames() const {return m_vConstraintName;}
1290  std::vector<std::string>& constraintNames() {return m_vConstraintName;}
1292  expression_type const& objective() const {return m_objective;}
1295  void setObjective(expression_type const& expr)
1296  {
1297  m_objective = expr;
1299  }
1302  void emplaceObjective(expression_type& expr)
1303  {
1304  m_objective.swap(expr);
1306  }
1310  void setOptimizeType(SolverProperty optType) {m_optType = optType;}
1312  std::vector<property_type> const& variableProperties() const {return m_vVariableProperty;}
1314  std::vector<property_type>& variableProperties() {return m_vVariableProperty;}
1316  unsigned int numVariables() const {return m_vVariableProperty.size();}
1318  variable_type variable(unsigned int id) const {return variable_type(id);}
1320  std::string variableName(variable_type const& var) const
1321  {
1322  char buf[256];
1323  if (m_vVariableProperty.at(var.id()).name().empty())
1324  limboSPrint(kNONE, buf, "x%u", var.id());
1325  else
1326  limboSPrint(kNONE, buf, "%s", m_vVariableProperty.at(var.id()).name().c_str());
1327  return buf;
1328  }
1332  void setVariableName(variable_type const& var, std::string const& name) {m_vVariableProperty[var.id()].setName(name);}
1334  variable_value_type variableLowerBound(variable_type const& var) {return m_vVariableProperty.at(var.id()).lowerBound();}
1338  void setVariableLowerBound(variable_type const& var, variable_value_type lb) {m_vVariableProperty[var.id()].setLowerBound(lb);}
1342  void updateVariableLowerBound(variable_type const& var, variable_value_type lb) {m_vVariableProperty[var.id()].updateLowerBound(lb);}
1344  variable_value_type variableUpperBound(variable_type const& var) {return m_vVariableProperty.at(var.id()).upperBound();}
1348  void setVariableUpperBound(variable_type const& var, variable_value_type ub) {m_vVariableProperty[var.id()].setUpperBound(ub);}
1352  void updateVariableUpperBound(variable_type const& var, variable_value_type ub) {m_vVariableProperty[var.id()].updateUpperBound(ub);}
1354  SolverProperty variableNumericType(variable_type const& var) {return m_vVariableProperty.at(var.id()).numericType();}
1358  void setVariableNumericType(variable_type const& var, SolverProperty type) {m_vVariableProperty[var.id()].setNumericType(type);}
1365  variable_type addVariable(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string name = "")
1366  {
1367  m_vVariableProperty.push_back(property_type(lb, ub, nt, name));
1368  m_vVariableSol.push_back(lb);
1369  return variable_type(m_vVariableProperty.size()-1);
1370  }
1372  std::vector<variable_value_type> const& variableSolutions() const {return m_vVariableSol;}
1374  std::vector<variable_value_type>& variableSolutions() {return m_vVariableSol;}
1376  variable_value_type variableSolution(variable_type const& var) const {return m_vVariableSol.at(var.id());}
1379  void setVariableSolution(variable_type const& var, variable_value_type v) {m_vVariableSol.at(var.id()) = v;}
1382  void reserveVariables(unsigned int n)
1383  {
1384  m_vVariableProperty.reserve(n);
1385  m_vVariableSol.reserve(n);
1386  }
1389  void reserveConstraints(unsigned int n)
1390  {
1391  m_vConstraint.reserve(n);
1392  m_vConstraintName.reserve(n);
1393  }
1396  void resizeConstraints(unsigned int n)
1397  {
1398  m_vConstraint.resize(n);
1399  m_vConstraintName.resize(n);
1400  for (unsigned int i = 0; i < n; ++i)
1401  m_vConstraint[i].setId(i);
1402  }
1405  void scaleObjective(coefficient_value_type factor) {m_objective *= factor;}
1409  void scaleConstraint(unsigned int id, coefficient_value_type factor) {m_vConstraint.at(id).scale(factor);}
1414  coefficient_value_type evaluateExpression(expression_type const& expr, std::vector<variable_value_type> const& vVariableSol) const
1415  {
1416  coefficient_value_type result = 0;
1417  for (typename std::vector<term_type>::const_iterator it = expr.terms().begin(), ite = expr.terms().end(); it != ite; ++it)
1418  result += it->coefficient()*vVariableSol.at(it->variable().id());
1419  result += expr.constant();
1420  return result;
1421  }
1425  coefficient_value_type evaluateObjective(std::vector<variable_value_type> const& vVariableSol) const
1426  {
1427  return evaluateExpression(m_objective, vVariableSol);
1428  }
1431  coefficient_value_type evaluateObjective() const
1432  {
1434  }
1439  coefficient_value_type evaluateConstraint(constraint_type const& constr, std::vector<variable_value_type> const& vVariableSol) const
1440  {
1441  coefficient_value_type result = constr.rightHandSide()-evaluateExpression(constr.expression(), vVariableSol);
1442  if (constr.sense() == '>')
1443  return -result;
1444  else
1445  return result;
1446  }
1450  coefficient_value_type evaluateConstraint(constraint_type const& constr) const
1451  {
1452  return evaluateConstraint(constr, m_vVariableSol);
1453  }
1455  void evaluateConstraint() const
1456  {
1457  for (unsigned int i = 0, ie = m_vConstraint.size(); i < ie; ++i)
1458  limboPrint(kNONE, "C[%u] slack = %g\n", i, (double)evaluateConstraint(m_vConstraint.at(i)));
1459  }
1460 
1464  void read(std::string const& filename)
1465  {
1466  LpParser::read(*this, filename);
1467  }
1474  void add_variable(std::string const& vname, double l = limbo::lowest<double>(), double r = std::numeric_limits<double>::max())
1475  {
1476  // in case of overflow
1477  variable_value_type lb = l;
1478  variable_value_type ub = r;
1479  if (l == -10)
1480  limboAssert(lb == -10);
1481  if (l <= (double)limbo::lowest<variable_value_type>())
1482  lb = limbo::lowest<variable_value_type>();
1485  limboAssertMsg(lb <= ub, "failed to add bound %g <= %s <= %g", l, vname.c_str(), r);
1486  if (l == -10)
1487  limboAssert(lb == -10);
1488 
1489  // no variables with the same name is allowed
1490  typename std::map<std::string, variable_type>::const_iterator found = m_mName2Variable.find(vname);
1491  if (found == m_mName2Variable.end()) // new variable
1492  limboAssertMsg(m_mName2Variable.insert(std::make_pair(vname, addVariable(lb, ub, CONTINUOUS, vname))).second,
1493  "failed to insert variable %s to hash table", vname.c_str());
1494  else // update variable
1495  {
1496  m_vVariableProperty.at(found->second.id()).updateLowerBound(lb);
1497  m_vVariableProperty.at(found->second.id()).updateUpperBound(ub);
1498  limboAssertMsg(m_vVariableProperty.at(found->second.id()).lowerBound() <= m_vVariableProperty.at(found->second.id()).upperBound(),
1499  "failed to set bound %g <= %s <= %g", m_vVariableProperty.at(found->second.id()).lowerBound(), vname.c_str(), m_vVariableProperty.at(found->second.id()).upperBound());
1500  }
1501  }
1507  void add_constraint(std::string const& cname, LpParser::TermArray const& terms, char compare, double constant)
1508  {
1509  expression_type expr;
1510  for (LpParser::TermArray::const_iterator it = terms.begin(); it != terms.end(); ++it)
1511  {
1512  // in case variable is not added yet
1513  add_variable(it->var);
1514  expr += m_mName2Variable.at(it->var)*it->coef;
1515  }
1516  addConstraint(constraint_type(expr, constant, compare), cname);
1517  }
1521  void add_objective(bool minimize, LpParser::TermArray const& terms)
1522  {
1523  setOptimizeType((minimize)? MIN : MAX);
1524 
1525  expression_type expr;
1526  for (LpParser::TermArray::const_iterator it = terms.begin(); it != terms.end(); ++it)
1527  {
1528  // in case variable is not added yet
1529  add_variable(it->var);
1530  expr += m_mName2Variable.at(it->var)*it->coef;
1531  }
1532  setObjective(expr);
1533  }
1537  void set_integer(std::string const& vname, bool binary)
1538  {
1539  // in case variable is not added yet
1540  add_variable(vname);
1541  variable_type var = m_mName2Variable.at(vname);
1542  if (binary)
1543  m_vVariableProperty.at(var.id()).setNumericType(BINARY);
1544  else
1545  m_vVariableProperty.at(var.id()).setNumericType(INTEGER);
1546  }
1548 
1552  bool print(std::string const& filename) const
1553  {
1554  std::ofstream out (filename.c_str());
1555  if (!out.good())
1556  return false;
1557  print(out);
1558  out.close();
1559  return true;
1560  }
1564  std::ostream& print(std::ostream& os = std::cout) const
1565  {
1566  switch (optimizeType())
1567  {
1568  case MIN:
1569  os << "Minimize\n";
1570  break;
1571  case MAX:
1572  os << "Maximize\n";
1573  break;
1574  default:
1575  os << "Unknown\n";
1576  break;
1577  }
1578 
1579  // print objective
1580  print(os, objective());
1581 
1582  os << "\n\nSubject To\n";
1583 
1584  // print constraints
1585  unsigned int i = 0;
1586  for (typename std::vector<constraint_type>::const_iterator it = constraints().begin(), ite = constraints().end(); it != ite; ++it, ++i)
1587  {
1588  if (constraintName(*it).empty())
1589  os << "C" << i << ": ";
1590  else
1591  os << constraintName(*it) << ": ";
1592  print(os, *it);
1593  os << "\n";
1594  }
1595 
1596  // print bounds
1597  os << "Bounds\n";
1598  i = 0;
1599  for (typename std::vector<property_type>::const_iterator it = m_vVariableProperty.begin(), ite = m_vVariableProperty.end(); it != ite; ++it, ++i)
1600  {
1601  if (it->lowerBound() <= limbo::lowest<variable_value_type>() && it->upperBound() >= std::numeric_limits<variable_value_type>::max())
1602  os << variableName(variable_type(i)) << " free";
1603  else if (it->lowerBound() <= limbo::lowest<variable_value_type>())
1604  os << variableName(variable_type(i)) << " <= " << it->upperBound();
1605  else if (it->upperBound() >= std::numeric_limits<variable_value_type>::max())
1606  os << variableName(variable_type(i)) << " >= " << it->lowerBound();
1607  else
1608  os << it->lowerBound() << " <= " << variableName(variable_type(i)) << " <= " << it->upperBound();
1609  os << "\n";
1610  }
1611 
1612  // print numeric type
1613  os << "Generals\n";
1614  i = 0;
1615  for (typename std::vector<property_type>::const_iterator it = m_vVariableProperty.begin(), ite = m_vVariableProperty.end(); it != ite; ++it, ++i)
1616  {
1617  if (it->numericType() == BINARY || it->numericType() == INTEGER)
1618  os << variableName(variable_type(i)) << "\n";
1619  }
1620 
1621  os << "End\n";
1622 
1623  return os;
1624  }
1629  std::ostream& print(std::ostream& os, term_type const& term) const
1630  {
1631  os << term.coefficient() << " " << variableName(term.variable());
1632  return os;
1633  }
1638  std::ostream& print(std::ostream& os, expression_type const& expr) const
1639  {
1640  int i = 0;
1641  for (typename std::vector<term_type>::const_iterator it = expr.terms().begin(), ite = expr.terms().end(); it != ite; ++it, ++i)
1642  {
1643  if (i)
1644  os << " + ";
1645  print(os, *it);
1646  if (i%4 == 3)
1647  os << "\n";
1648  }
1649  return os;
1650  }
1655  std::ostream& print(std::ostream& os, constraint_type const& constr) const
1656  {
1657  print(os, constr.expression());
1658  if (constr.sense() == '=')
1659  os << " " << constr.sense() << " " << constr.rightHandSide();
1660  else
1661  os << " " << constr.sense() << "= " << constr.rightHandSide();
1662  return os;
1663  }
1665  bool printSolution(std::string const& filename) const
1666  {
1667  std::ofstream out (filename.c_str());
1668  if (!out.good())
1669  return false;
1670  printSolution(out);
1671  out.close();
1672  return true;
1673  }
1677  std::ostream& printSolution(std::ostream& os = std::cout) const
1678  {
1679  coefficient_value_type obj = evaluateObjective();
1680  os << "# Objective " << obj << "\n";
1681  for (unsigned int i = 0, ie = variableSolutions().size(); i < ie; ++i)
1682  os << variableName(variable_type(i)) << " " << variableSolution(variable_type(i)) << "\n";
1683  return os;
1684  }
1685  protected:
1687  void copy(LinearModel const& rhs)
1688  {
1690  m_objective = rhs.m_objective;
1692  m_optType = rhs.m_optType;
1693 
1695 
1697  }
1698 
1699  std::vector<constraint_type> m_vConstraint;
1700  expression_type m_objective;
1701  std::vector<property_type> m_vVariableProperty;
1703  std::vector<std::string> m_vConstraintName;
1704 
1705  std::vector<variable_value_type> m_vVariableSol;
1706 
1707  std::map<std::string, variable_type> m_mName2Variable;
1708 };
1709 
1714 template <typename T, typename I, int StartingIndex = 1>
1715 struct MatrixCSR
1716 {
1718  typedef T value_type;
1720  typedef I index_type;
1721 
1722  value_type* vElement;
1723  index_type* vColumn;
1724  index_type* vRowBeginIndex;
1725  index_type numRows;
1726  index_type numColumns;
1727  index_type numElements;
1728  static index_type s_startingIndex;
1729 
1732  : vElement(NULL)
1733  , vColumn(NULL)
1734  , vRowBeginIndex(NULL)
1735  , numRows(0)
1736  , numColumns(0)
1737  , numElements(0)
1738  {
1739  }
1741  MatrixCSR(MatrixCSR const& rhs)
1742  {
1743  copy(rhs);
1744  }
1747  {
1748  if (this != &rhs)
1749  copy(rhs);
1750  return *this;
1751  }
1754  {
1755  reset();
1756  }
1757 
1762  void initialize(index_type nr, index_type nc, index_type nv)
1763  {
1764  reset();
1765  numRows = nr;
1766  numColumns = nc;
1767  numElements = nv;
1768  vElement = new value_type [numElements];
1769  vColumn = new index_type [numElements];
1770  vRowBeginIndex = new index_type [numRows+1];
1771  }
1772 
1774  void reset()
1775  {
1776  if (vElement)
1777  delete [] vElement;
1778  if (vColumn)
1779  delete [] vColumn;
1780  if (vRowBeginIndex)
1781  delete [] vRowBeginIndex;
1782  vElement = NULL;
1783  vColumn = NULL;
1784  vRowBeginIndex = NULL;
1785  numRows = 0;
1786  numColumns = 0;
1787  numElements = 0;
1788  }
1789 
1792  void copy(MatrixCSR const& rhs)
1793  {
1794  reset();
1795  numRows = rhs.numRows;
1796  numColumns = rhs.numColumns;
1797  numElements = rhs.numElements;
1798  if (rhs.vElement)
1799  {
1800  vElement = new value_type [numElements];
1801  vColumn = new index_type [numElements];
1802  vRowBeginIndex = new index_type [numRows+1];
1803  std::copy(rhs.vElement, rhs.vElement+numElements, vElement);
1804  std::copy(rhs.vColumn, rhs.vColumn+numElements, vColumn);
1805  std::copy(rhs.vRowBeginIndex, rhs.vRowBeginIndex+numRows+1, vRowBeginIndex);
1806  }
1807  }
1808 
1813  value_type at(index_type i, index_type j) const
1814  {
1815 #ifdef DEBUG_SOLVERS
1816  limboAssert(i < numRows);
1817  limboAssert(i+1 <= numRows);
1818 #endif
1819  index_type* elementColumnBegin = vColumn+vRowBeginIndex[i]-vRowBeginIndex[0];
1820  index_type* elementColumnEnd = vColumn+vRowBeginIndex[i+1]-vRowBeginIndex[0];
1821  // binary search to see if any element match column j
1822  index_type* found = std::lower_bound(elementColumnBegin, elementColumnEnd, j+s_startingIndex);
1823  if (found != elementColumnEnd && *found == j+s_startingIndex)
1824  {
1825 #ifdef DEBUG_SOLVERS
1826  limboAssert(std::distance(vColumn, found)-s_startingIndex < numElements);
1827 #endif
1828  return vElement[std::distance(vColumn, found)];
1829  }
1830  else
1831  return 0;
1832  }
1833 
1838  void set(index_type nr, index_type nc, LinearConstraint<value_type> const* vConstraint)
1839  {
1840  numRows = nr;
1841  numColumns = nc;
1842  numElements = 0;
1843  vRowBeginIndex = new index_type [numRows+1];
1844  vRowBeginIndex[0] = s_startingIndex;
1845 
1846  typedef LinearConstraint<value_type> constraint_type;
1847  constraint_type const* it = vConstraint;
1848  constraint_type const* ite = vConstraint+nr;
1849  // initialize vRowBeginIndex
1850  index_type i = 1;
1851  for (; it != ite; ++it, ++i)
1852  {
1853 #ifdef DEBUG_SOLVERS
1854  limboAssert(i <= numRows);
1855 #endif
1856  vRowBeginIndex[i] = vRowBeginIndex[i-1]+it->expression().terms().size();
1857  }
1858  // last element of vRowBeginIndex denotes the total number of elements
1859  numElements = vRowBeginIndex[numRows];
1860 
1861  // initialize vElement and vColumn
1862  vElement = new value_type [numElements];
1863  vColumn = new index_type [numElements];
1864  i = 0;
1865  for (it = vConstraint; it != ite; ++it)
1866  {
1867  for (typename std::vector<typename constraint_type::term_type>::const_iterator itt = it->expression().terms().begin(), itte = it->expression().terms().end(); itt != itte; ++itt, ++i)
1868  {
1869 #ifdef DEBUG_SOLVERS
1870  limboAssert(i < numElements);
1871 #endif
1872  vElement[i] = itt->coefficient();
1873  vColumn[i] = itt->variable().id()+s_startingIndex;
1874  }
1875  }
1876  }
1877 };
1878 
1879 template <typename T, typename I, int StartingIndex>
1880 typename MatrixCSR<T, I, StartingIndex>::index_type MatrixCSR<T, I, StartingIndex>::s_startingIndex = StartingIndex;
1881 
1882 } // namespace solvers
1883 } // namespace limbo
1884 
1885 #endif
friend LinearConstraint< coefficient_value_type > operator<=(LinearExpression const &expr, coefficient_value_type rhs)
overload <=, same as <
Definition: Solvers.h:901
bool operator==(Variable const &rhs) const
Definition: Solvers.h:128
header to include PrintMsg.h and AssertMsg.h
index_type numColumns
number of columns, not in the CSR format
Definition: Solvers.h:1726
friend LinearConstraint< coefficient_value_type > operator<(Variable const &var, coefficient_value_type rhs)
overload <
Definition: Solvers.h:221
bool operator()(TermType const &t1, TermType const &t2) const
Definition: Solvers.h:597
coefficient_value_type evaluateConstraint(constraint_type const &constr, std::vector< variable_value_type > const &vVariableSol) const
evaluate slackness of a constraint given solutions of variables
Definition: Solvers.h:1439
unsigned int id() const
Definition: Solvers.h:117
friend LinearConstraint< coefficient_value_type > operator==(Variable const &var, coefficient_value_type rhs)
overload ==
Definition: Solvers.h:249
LinearExpression & operator/=(coefficient_value_type c)
Definition: Solvers.h:868
variable_value_type variableSolution(variable_type const &var) const
Definition: Solvers.h:1376
void swap(LinearConstraint &rhs)
swap with a constraint
Definition: Solvers.h:1043
LinearTerm & operator/=(coefficient_value_type coef)
Definition: Solvers.h:445
Base class for lp database. Only pure virtual functions are defined. User needs to inheritate this cl...
Definition: LpDataBase.h:114
LinearConstraint(LinearConstraint const &rhs)
copy constructor
Definition: Solvers.h:1007
Variable & operator=(Variable const &rhs)
assignment
Definition: Solvers.h:107
LinearTerm< coefficient_value_type > term_type
term type
Definition: Solvers.h:1172
index_type * vRowBeginIndex
Element j of this integer array gives the index of the element in the values array that is first non-...
Definition: Solvers.h:1724
maximize objective
Definition: Solvers.h:32
LinearTerm(LinearTerm const &rhs)
copy constructor
Definition: Solvers.h:366
LinearExpression(LinearExpression const &rhs)
copy constructor
Definition: Solvers.h:630
friend LinearExpression< coefficient_value_type > operator-(coefficient_value_type constant, Variable const &var)
Definition: Solvers.h:206
Describe properties of a variable.
Definition: Solvers.h:72
void copy(LinearExpression const &rhs)
copy object
Definition: Solvers.h:969
void setVariable(variable_type const &var)
Definition: Solvers.h:386
friend LinearConstraint< coefficient_value_type > operator==(LinearTerm const &term, coefficient_value_type rhs)
Definition: Solvers.h:565
std::vector< property_type > m_vVariableProperty
variable properties
Definition: Solvers.h:1701
LinearTerm< coefficient_value_type > term_type
term type
Definition: Solvers.h:990
coefficient_value_type evaluateObjective() const
evaluate objective
Definition: Solvers.h:1431
LinearTerm & operator*=(coefficient_value_type coef)
Definition: Solvers.h:427
friend LinearExpression< coefficient_value_type > operator-(Variable const &var, coefficient_value_type constant)
Definition: Solvers.h:198
friend LinearTerm< coefficient_value_type > operator*(coefficient_value_type coef, Variable const &var)
Definition: Solvers.h:158
SolverProperty numericType() const
Definition: Solvers.h:312
SolverProperty
Some enums used in solver.
Definition: Solvers.h:29
LinearTerm< coefficient_value_type > term_type
term type
Definition: Solvers.h:612
variable_value_type lowerBound() const
Definition: Solvers.h:300
void initialize(index_type nr, index_type nc, index_type nv)
Initialize matrix.
Definition: Solvers.h:1762
void setExpression(expression_type const &expr)
Definition: Solvers.h:1030
std::string m_name
name of variable
Definition: Solvers.h:342
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term, coefficient_value_type constant)
Definition: Solvers.h:496
int limboSPrint(MessageType m, char *buf, const char *format,...)
formatted print with prefix to buffer
Definition: PrintMsg.h:101
LinearModel(LinearModel const &rhs)
copy constructor
Definition: Solvers.h:1188
std::ostream & print(std::ostream &os=std::cout) const
print problem in lp format
Definition: Solvers.h:1564
coefficient_value_type coefficient() const
Definition: Solvers.h:388
LinearExpression & setConstant(coefficient_value_type constant)
set constant term
Definition: Solvers.h:670
std::map< std::string, variable_type > m_mName2Variable
mapping from variable name to variable, only used when reading from files
Definition: Solvers.h:1707
void resizeConstraints(unsigned int n)
resize constraints
Definition: Solvers.h:1396
SolverProperty optimizeType() const
Definition: Solvers.h:1308
bool printSolution(std::string const &filename) const
print solutions to file
Definition: Solvers.h:1665
friend LinearTerm operator/(LinearTerm const &term, coefficient_value_type coef)
Definition: Solvers.h:436
friend LinearConstraint< coefficient_value_type > operator<(LinearExpression const &expr, coefficient_value_type rhs)
overload <
Definition: Solvers.h:894
VariableProperty(VariableProperty const &rhs)
copy constructor
Definition: Solvers.h:286
Variable< coefficient_value_type > variable_type
variable type
Definition: Solvers.h:1170
void add_objective(bool minimize, LpParser::TermArray const &terms)
add object terms
Definition: Solvers.h:1521
friend LinearExpression< coefficient_value_type > operator-(coefficient_value_type constant, LinearTerm const &term)
Definition: Solvers.h:520
~LinearTerm()
destructor
Definition: Solvers.h:379
void reset()
Destroy matrix and recycle memory.
Definition: Solvers.h:1774
void setNumericType(SolverProperty nt)
Definition: Solvers.h:314
T value_type
value type
Definition: Solvers.h:1718
std::vector< Term > TermArray
array of terms
Definition: LpDataBase.h:105
std::vector< constraint_type > & constraints()
Definition: Solvers.h:1212
bool addConstraint(constraint_type const &constr, std::string name="")
add a constraint
Definition: Solvers.h:1217
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term, variable_type const &var)
Definition: Solvers.h:504
void reserveVariables(unsigned int n)
reserve space for variables
Definition: Solvers.h:1382
std::string const & constraintName(constraint_type const &constr) const
Definition: Solvers.h:1286
bool operator!=(Variable const &rhs) const
Definition: Solvers.h:135
Variable< coefficient_value_type > variable_type
variable type
Definition: Solvers.h:988
SolverProperty variableNumericType(variable_type const &var)
Definition: Solvers.h:1354
void copy(LinearTerm const &rhs)
copy object
Definition: Solvers.h:579
T coefficient_value_type
coefficient type
Definition: Solvers.h:610
LinearTerm operator-() const
Definition: Solvers.h:394
void reserveConstraints(unsigned int n)
reserve space for constraints
Definition: Solvers.h:1389
V variable_value_type
V variable.
Definition: Solvers.h:1166
LinearTerm & operator=(LinearTerm const &rhs)
assignment
Definition: Solvers.h:372
std::vector< std::string > const & constraintNames() const
Definition: Solvers.h:1288
coefficient_value_type m_coef
coefficient
Definition: Solvers.h:586
LinearTerm & negate()
Definition: Solvers.h:401
LinearModel & operator=(LinearModel const &rhs)
assignment
Definition: Solvers.h:1195
void updateUpperBound(variable_value_type ub)
Definition: Solvers.h:310
LinearTerm< coefficient_value_type > operator-() const
Definition: Solvers.h:141
variable_type m_var
variable
Definition: Solvers.h:585
friend LinearExpression< coefficient_value_type > operator-(Variable const &var1, Variable const &var2)
Definition: Solvers.h:214
T coefficient_value_type
T coefficient.
Definition: Solvers.h:1164
void setOptimizeType(SolverProperty optType)
Definition: Solvers.h:1310
void simplify()
simplify expression by merge terms of the same variables
Definition: Solvers.h:1054
LinearExpression & operator*=(coefficient_value_type c)
Definition: Solvers.h:848
I index_type
index type
Definition: Solvers.h:1720
VariableProperty & operator=(VariableProperty const &rhs)
assignment
Definition: Solvers.h:292
friend LinearTerm< coefficient_value_type > operator/(Variable const &var, coefficient_value_type coef)
Definition: Solvers.h:166
void simplify()
simplify expression by merge terms of the same variables and remove terms with zero coefficients ...
Definition: Solvers.h:929
expression_type const & objective() const
Definition: Solvers.h:1292
void swap(LinearExpression &rhs)
swap with an expression
Definition: Solvers.h:649
LinearExpression & operator+=(LinearExpression const &expr)
Definition: Solvers.h:749
variable_value_type upperBound() const
Definition: Solvers.h:306
void copy(LinearModel const &rhs)
copy object
Definition: Solvers.h:1687
friend LinearConstraint< coefficient_value_type > operator<(LinearTerm const &term, coefficient_value_type rhs)
overload <
Definition: Solvers.h:537
Describe linear constraint.
Definition: Solvers.h:78
index_type * vColumn
Element i of the integer array columns is the number of the column in A that contains the i-th value ...
Definition: Solvers.h:1723
coefficient_value_type m_rhs
constant at the right hand side
Definition: Solvers.h:1152
std::vector< constraint_type > m_vConstraint
constraints
Definition: Solvers.h:1699
LinearExpression< coefficient_value_type > expression_type
expression type
Definition: Solvers.h:992
friend LinearConstraint< coefficient_value_type > operator>=(LinearTerm const &term, coefficient_value_type rhs)
overload >=, same as >
Definition: Solvers.h:558
friend LinearConstraint< coefficient_value_type > operator>(LinearExpression const &expr, coefficient_value_type rhs)
overload >
Definition: Solvers.h:908
Variable(Variable const &rhs)
copy constructor
Definition: Solvers.h:101
~MatrixCSR()
destructor
Definition: Solvers.h:1753
void setObjective(expression_type const &expr)
set objective
Definition: Solvers.h:1295
void reserve(unsigned int n)
reserve space for expression terms
Definition: Solvers.h:1102
SolverProperty m_numericType
numeric type, BINARY, INTEGER, CONTINUOUS
Definition: Solvers.h:341
std::vector< constraint_type > const & constraints() const
Definition: Solvers.h:1210
bool operator==(LinearTerm const &rhs) const
Definition: Solvers.h:572
unsigned int numVariables() const
Definition: Solvers.h:1316
void clear()
clear expression
Definition: Solvers.h:657
friend LinearExpression operator-(term_type const &term, LinearExpression const &expr)
Definition: Solvers.h:787
SolverProperty m_optType
optimization objective
Definition: Solvers.h:1702
T coefficient_value_type
coefficient type
Definition: Solvers.h:986
~LinearModel()
destructor
Definition: Solvers.h:1205
friend LinearConstraint< coefficient_value_type > operator==(LinearExpression const &expr, coefficient_value_type rhs)
Definition: Solvers.h:922
friend LinearExpression operator+(coefficient_value_type constant, LinearExpression const &expr)
Definition: Solvers.h:708
LinearConstraint(expression_type expr=expression_type(), coefficient_value_type rhs=0, char s= '<')
constructor
Definition: Solvers.h:998
void updateLowerBound(variable_value_type lb)
Definition: Solvers.h:304
friend LinearExpression operator*(coefficient_value_type c, LinearExpression const &expr)
Definition: Solvers.h:841
void setId(unsigned int id)
set variable index
Definition: Solvers.h:120
LinearTerm(variable_type var=variable_type(), coefficient_value_type coef=1)
constructor
Definition: Solvers.h:359
Describe linear expressions in optimization problem.
Definition: Solvers.h:76
LinearConstraint & operator+=(term_type const &term)
Definition: Solvers.h:1107
LinearConstraint & operator-=(term_type term)
Definition: Solvers.h:1124
std::string const & name() const
Definition: Solvers.h:324
LinearExpression & operator-=(LinearExpression const &expr)
Definition: Solvers.h:820
VariableProperty(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string const &n)
constructor
Definition: Solvers.h:277
bool emplaceConstraint(expression_type &expr, char sense, coefficient_value_type rhs, std::string name="")
emplace a constraint
Definition: Solvers.h:1228
the model is infeasible
Definition: Solvers.h:37
coefficient_value_type evaluateConstraint(constraint_type const &constr) const
evaluate slackness of a constraint given solutions of variables
Definition: Solvers.h:1450
mathematical utilities such as abs
void setId(unsigned int i)
Definition: Solvers.h:1026
value_type at(index_type i, index_type j) const
get element
Definition: Solvers.h:1813
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term1, LinearTerm const &term2)
Definition: Solvers.h:486
friend LinearConstraint< coefficient_value_type > operator>=(Variable const &var, coefficient_value_type rhs)
overload >=
Definition: Solvers.h:242
MatrixCSR & operator=(MatrixCSR const &rhs)
assignment
Definition: Solvers.h:1746
friend LinearTerm operator*(coefficient_value_type coef, LinearTerm const &term)
Definition: Solvers.h:420
LinearConstraint< coefficient_value_type > constraint_type
constraint type
Definition: Solvers.h:1176
expression_type const & expression() const
Definition: Solvers.h:1028
variable_type variable(unsigned int id) const
Definition: Solvers.h:1318
Comapre term by variable.
Definition: Solvers.h:590
friend LinearExpression operator*(LinearExpression const &expr, coefficient_value_type c)
Definition: Solvers.h:831
void add_constraint(std::string const &cname, LpParser::TermArray const &terms, char compare, double constant)
add constraint that terms compare constant.
Definition: Solvers.h:1507
LinearExpression & incrementConstant(coefficient_value_type constant)
increment constant term
Definition: Solvers.h:678
void copy(LinearConstraint const &rhs)
copy object
Definition: Solvers.h:1141
friend LinearConstraint< coefficient_value_type > operator<=(Variable const &var, coefficient_value_type rhs)
overload <=
Definition: Solvers.h:228
bool valid() const
Definition: Solvers.h:123
value_type * vElement
Flatten data values. A real or complex array that contains the non-zero elements of A...
Definition: Solvers.h:1722
the model is suboptimal
Definition: Solvers.h:38
void setCoefficient(coefficient_value_type coef)
Definition: Solvers.h:390
V variable_value_type
type of bounds
Definition: Solvers.h:270
void setVariableLowerBound(variable_type const &var, variable_value_type lb)
set variable lower bound
Definition: Solvers.h:1338
std::string toString(SolverProperty sp)
Convert limbo::solvers::SolverProperty to std::string.
Definition: Solvers.h:44
binary number
Definition: Solvers.h:33
MatrixCSR(MatrixCSR const &rhs)
copy constructor
Definition: Solvers.h:1741
friend LinearTerm operator*(LinearTerm const &term, coefficient_value_type coef)
Definition: Solvers.h:410
char m_sense
sign of operator, < (<=), > (>=), = (==)
Definition: Solvers.h:1153
coefficient_value_type m_constant
constant term for the linear expression
Definition: Solvers.h:976
unsigned int m_id
variable index
Definition: Solvers.h:260
std::ostream & print(std::ostream &os, expression_type const &expr) const
print expression
Definition: Solvers.h:1638
T coefficient_value_type
coefficient type
Definition: Solvers.h:91
Variable(unsigned int id=std::numeric_limits< unsigned int >::max())
constructor
Definition: Solvers.h:95
void copy(VariableProperty const &rhs)
copy object
Definition: Solvers.h:331
friend LinearExpression< coefficient_value_type > operator+(variable_type const &var, LinearTerm const &term)
Definition: Solvers.h:478
void setLowerBound(variable_value_type lb)
Definition: Solvers.h:302
void normalize(char s)
normalize sense
Definition: Solvers.h:1063
void updateVariableLowerBound(variable_type const &var, variable_value_type lb)
update variable lower bound
Definition: Solvers.h:1342
LinearConstraint & operator-=(expression_type const &expr)
Definition: Solvers.h:1132
std::ostream & printSolution(std::ostream &os=std::cout) const
print solutions
Definition: Solvers.h:1677
namespace for Limbo
Definition: GraphUtility.h:22
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term1, LinearTerm const &term2)
Definition: Solvers.h:528
floating point number
Definition: Solvers.h:35
void emplaceObjective(expression_type &expr)
set objective by swaping with the expression
Definition: Solvers.h:1302
friend LinearExpression operator+(LinearExpression const &expr1, LinearExpression const &expr2)
Definition: Solvers.h:724
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition: Math.h:61
void set(index_type nr, index_type nc, LinearConstraint< value_type > const *vConstraint)
Set from array of constraints.
Definition: Solvers.h:1838
LinearExpression< coefficient_value_type > expression_type
expression type
Definition: Solvers.h:1174
Variable< coefficient_value_type > variable_type
variable type
Definition: Solvers.h:614
std::string variableName(variable_type const &var) const
Definition: Solvers.h:1320
LinearExpression(term_type const &term, coefficient_value_type constant=0)
constructor
Definition: Solvers.h:623
void copy(MatrixCSR const &rhs)
copy object
Definition: Solvers.h:1792
friend LinearExpression< coefficient_value_type > operator+(Variable const &var1, Variable const &var2)
Definition: Solvers.h:190
coefficient_value_type rightHandSide() const
Definition: Solvers.h:1034
LinearExpression & negate()
Definition: Solvers.h:884
MatrixCSR()
constructor
Definition: Solvers.h:1731
void setVariableNumericType(variable_type const &var, SolverProperty type)
set numeric type of variable
Definition: Solvers.h:1358
void evaluateConstraint() const
evaluate slackness of all constraints given solutions of variables and print to screen ...
Definition: Solvers.h:1455
the model is unbounded
Definition: Solvers.h:39
LinearConstraint & operator=(LinearConstraint const &rhs)
assignment
Definition: Solvers.h:1012
VariableProperty< variable_value_type > property_type
variable property type
Definition: Solvers.h:1178
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term, coefficient_value_type constant)
Definition: Solvers.h:454
void scaleObjective(coefficient_value_type factor)
scale objective
Definition: Solvers.h:1405
std::vector< property_type > const & variableProperties() const
Definition: Solvers.h:1312
friend LinearExpression operator-(LinearExpression const &expr, term_type const &term)
Definition: Solvers.h:769
void setName(std::string const &n)
Definition: Solvers.h:326
friend LinearExpression< coefficient_value_type > operator+(Variable const &var, coefficient_value_type constant)
Definition: Solvers.h:174
#define limboAssert(condition)
custom assertion without message
Definition: AssertMsg.h:64
int limboPrint(MessageType m, const char *format,...)
formatted print with prefix
Definition: PrintMsg.h:49
void setVariableUpperBound(variable_type const &var, variable_value_type ub)
set variable upper bound
Definition: Solvers.h:1348
std::vector< term_type > const & terms() const
Definition: Solvers.h:655
void copy(Variable const &rhs)
copy object
Definition: Solvers.h:255
optimally solved
Definition: Solvers.h:36
LinearModel()
constructor
Definition: Solvers.h:1181
Describe variables in optimization problem.
Definition: Solvers.h:70
index_type numElements
number of non-zero elements
Definition: Solvers.h:1727
expression_type m_expr
linear expression
Definition: Solvers.h:1151
void setVariableName(variable_type const &var, std::string const &name)
set variable name
Definition: Solvers.h:1332
Driver for Lp parser.
LinearExpression & operator-=(coefficient_value_type constant)
Definition: Solvers.h:804
LinearConstraint & operator+=(expression_type const &expr)
Definition: Solvers.h:1115
double lowest< double >()
specialization for floating point types
Definition: Math.h:163
coefficient_value_type evaluateExpression(expression_type const &expr, std::vector< variable_value_type > const &vVariableSol) const
evaluate expression given solutions of variables
Definition: Solvers.h:1414
LpParser::LpDataBase base_type
base class
Definition: Solvers.h:1168
friend LinearExpression operator/(LinearExpression const &expr, coefficient_value_type c)
Definition: Solvers.h:859
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term, variable_type const &var)
Definition: Solvers.h:462
friend LinearExpression operator+(term_type const &term, LinearExpression const &expr)
Definition: Solvers.h:716
expression_type m_objective
objective
Definition: Solvers.h:1700
LinearExpression & operator=(LinearExpression const &rhs)
assignment
Definition: Solvers.h:636
unsigned int m_id
constraint index
Definition: Solvers.h:1150
std::vector< term_type > m_vTerm
linear expression with terms
Definition: Solvers.h:975
std::vector< property_type > & variableProperties()
Definition: Solvers.h:1314
void setUpperBound(variable_value_type ub)
Definition: Solvers.h:308
void read(std::string const &filename)
read lp format
Definition: Solvers.h:1464
LinearExpression & operator+=(term_type const &term)
Definition: Solvers.h:741
coefficient_value_type evaluateObjective(std::vector< variable_value_type > const &vVariableSol) const
evaluate objective
Definition: Solvers.h:1425
std::vector< variable_value_type > & variableSolutions()
Definition: Solvers.h:1374
void set_integer(std::string const &vname, bool binary)
set integer variables
Definition: Solvers.h:1537
void setVariableSolution(variable_type const &var, variable_value_type v)
Definition: Solvers.h:1379
variable_value_type variableLowerBound(variable_type const &var)
Definition: Solvers.h:1334
friend LinearExpression operator-(coefficient_value_type constant, LinearExpression const &expr)
Definition: Solvers.h:779
variable_type const & variable() const
Definition: Solvers.h:384
variable_value_type m_lowerBound
lower bound
Definition: Solvers.h:339
integer number
Definition: Solvers.h:34
void emplaceExpression(expression_type &expr)
Definition: Solvers.h:1032
model to describe an optimization problem
Definition: Solvers.h:80
LinearExpression & operator-=(term_type term)
Definition: Solvers.h:812
std::vector< std::string > m_vConstraintName
constraint names
Definition: Solvers.h:1703
variable_value_type variableUpperBound(variable_type const &var)
Definition: Solvers.h:1344
T coefficient_value_type
coefficient type
Definition: Solvers.h:352
~Variable()
destructor
Definition: Solvers.h:114
friend LinearExpression operator-(LinearExpression const &expr1, LinearExpression const &expr2)
Definition: Solvers.h:795
minimize objective
Definition: Solvers.h:31
LinearExpression operator-() const
Definition: Solvers.h:877
index_type numRows
number of rows, not in the CSR format
Definition: Solvers.h:1725
variable_type addVariable(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string name="")
add one variable
Definition: Solvers.h:1365
friend LinearExpression< coefficient_value_type > operator+(coefficient_value_type constant, Variable const &var)
Definition: Solvers.h:182
std::vector< variable_value_type > m_vVariableSol
variable solutions, it can be either initial solution or final solution
Definition: Solvers.h:1705
friend LinearExpression operator-(LinearExpression const &expr, coefficient_value_type constant)
Definition: Solvers.h:759
#define limboAssertMsg(condition, args...)
custom assertion with message
Definition: AssertMsg.h:52
std::iterator_traits< Iterator >::value_type min(Iterator first, Iterator last)
get min of an array
Definition: Math.h:77
friend LinearExpression< coefficient_value_type > operator-(variable_type const &var, LinearTerm const &term)
Definition: Solvers.h:512
friend LinearExpression< coefficient_value_type > operator+(coefficient_value_type constant, LinearTerm const &term)
Definition: Solvers.h:470
friend LinearConstraint< coefficient_value_type > operator>(LinearTerm const &term, coefficient_value_type rhs)
overload >
Definition: Solvers.h:551
Compressed sparse row (CSR) matrix.
Definition: Solvers.h:82
bool read(LpDataBase &db, const string &lpFile)
API for LpParser. Read LP file and initialize database by calling user-defined callback functions...
LinearExpression & operator+=(coefficient_value_type constant)
Definition: Solvers.h:733
void scale(coefficient_value_type factor)
scale constraint by a scaling factor
Definition: Solvers.h:1076
friend LinearConstraint< coefficient_value_type > operator>(Variable const &var, coefficient_value_type rhs)
overload >
Definition: Solvers.h:235
friend LinearConstraint< coefficient_value_type > operator<=(LinearTerm const &term, coefficient_value_type rhs)
overload <=, same as <
Definition: Solvers.h:544
void updateVariableUpperBound(variable_type const &var, variable_value_type ub)
update variable upper bound
Definition: Solvers.h:1352
void scaleConstraint(unsigned int id, coefficient_value_type factor)
scaling a constraint
Definition: Solvers.h:1409
void clearConstant()
move the constant in the expression to rhs For example, x + 5 < 10 will become x < 5 ...
Definition: Solvers.h:1094
bool print(std::string const &filename) const
print problem in lp format to file
Definition: Solvers.h:1552
static index_type s_startingIndex
starting index, like zero-based indexing or one-based indexing
Definition: Solvers.h:1728
variable_value_type m_upperBound
upper bound
Definition: Solvers.h:340
friend LinearTerm< coefficient_value_type > operator*(Variable const &var, coefficient_value_type coef)
Definition: Solvers.h:150
coefficient_value_type constant() const
Definition: Solvers.h:666
LinearExpression()
constructor
Definition: Solvers.h:617
std::ostream & print(std::ostream &os, constraint_type const &constr) const
print constraint
Definition: Solvers.h:1655
Variable< coefficient_value_type > variable_type
variable type
Definition: Solvers.h:354
friend LinearConstraint< coefficient_value_type > operator>=(LinearExpression const &expr, coefficient_value_type rhs)
overload >=, same as >
Definition: Solvers.h:915
unsigned int id() const
Definition: Solvers.h:1024
std::ostream & print(std::ostream &os, term_type const &term) const
print expression
Definition: Solvers.h:1629
void setRightHandSide(coefficient_value_type rhs)
Definition: Solvers.h:1036
friend LinearExpression operator+(LinearExpression const &expr, term_type const &term)
Definition: Solvers.h:698
friend LinearExpression operator+(LinearExpression const &expr, coefficient_value_type constant)
Definition: Solvers.h:688
void reserve(unsigned int n)
reserve space for terms
Definition: Solvers.h:664
std::vector< variable_value_type > const & variableSolutions() const
Definition: Solvers.h:1372
std::vector< std::string > & constraintNames()
Definition: Solvers.h:1290