Limbo 3.5.4
Loading...
Searching...
No Matches
Solvers.h
Go to the documentation of this file.
1
7
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>
18#include <limbo/math/Math.h>
20
22namespace limbo
23{
25namespace solvers
26{
27
41
44inline 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
69template <typename T>
70class Variable;
71template <typename T>
72class VariableProperty;
73template <typename T>
74class LinearTerm;
75template <typename T>
76class LinearExpression;
77template <typename T>
78class LinearConstraint;
79template <typename T, typename V>
80class LinearModel;
81template <typename T, typename I, int StartingIndex>
82struct MatrixCSR;
83
86template <typename T>
88{
89 public:
92
95 explicit Variable(unsigned int id = std::numeric_limits<unsigned int>::max())
96 : m_id(id)
97 {
98 }
99
101 Variable(Variable const& rhs)
102 {
103 copy(rhs);
104 }
105
108 {
109 if (this != &rhs)
110 copy(rhs);
111 return *this;
112 }
113
115
117 unsigned int id() const {return m_id;}
120 void setId(unsigned int id) {m_id = id;}
121
123 bool valid() const {return m_id != std::numeric_limits<unsigned int>::max();}
124
128 bool operator==(Variable const& rhs) const
129 {
130 return m_id == rhs.m_id;
131 }
132
135 bool operator!=(Variable const& rhs) const
136 {
137 return !this->operator==(rhs);
138 }
139
142 {
144 return term.negate();
145 }
146
154
159 {
160 return var*coef;
161 }
162
170
178
183 {
184 return var+constant;
185 }
186
191 {
192 return LinearTerm<coefficient_value_type>(var1)+var2;
193 }
194
202
210
215 {
216 return LinearTerm<coefficient_value_type>(var1)-var2;
217 }
218
225
229 {
230 return (var < rhs);
231 }
232
239
243 {
244 return (var > rhs);
245 }
246
253 protected:
255 void copy(Variable const& rhs)
256 {
257 m_id = rhs.m_id;
258 }
259
260 unsigned int m_id;
261};
262
265template <typename V>
267{
268 public:
271
278 : m_lowerBound(lb)
279 , m_upperBound(ub)
280 , m_numericType(nt)
281 , m_name(n)
282 {
283 }
284
287 {
288 copy(rhs);
289 }
290
293 {
294 if (this != &rhs)
295 copy(rhs);
296 return *this;
297 }
298
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 }
323
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 {
336 m_name = rhs.m_name;
337 }
338
342 std::string m_name;
343};
344
347template <typename T>
349{
350 public:
355
360 : m_var(var)
361 , m_coef(coef)
362 {
363 }
364
367 {
368 copy(rhs);
369 }
370
373 {
374 if (this != &rhs)
375 copy(rhs);
376 return *this;
377 }
378
380 {
381 }
382
384 variable_type const& variable() const {return m_var;}
386 void setVariable(variable_type const& var) {m_var = var;}
391
395 {
396 LinearTerm term (*this);
397 return term.negate();
398 }
399
402 {
403 m_coef = -m_coef;
404 return *this;
405 }
406
411 {
412 LinearTerm other (term);
413 other *= coef;
414 return other;
415 }
416
421 {
422 return term*coef;
423 }
424
428 {
429 m_coef *= coef;
430 return *this;
431 }
432
437 {
438 LinearTerm other (term);
439 other /= coef;
440 return other;
441 }
442
446 {
447 m_coef /= coef;
448 return *this;
449 }
450
458
463 {
464 return term+LinearTerm(var);
465 }
466
474
479 {
480 return LinearTerm(var)+term;
481 }
482
487 {
489 expr += term2;
490 return expr;
491 }
492
500
505 {
506 return term-LinearTerm(var);
507 }
508
513 {
514 return LinearTerm(var)-term;
515 }
516
524
529 {
531 expr -= term2;
532 return expr;
533 }
534
541
545 {
546 return (term < rhs);
547 }
548
555
559 {
560 return (term > rhs);
561 }
562
569
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
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
605template <typename T>
607{
608 public:
615
618 : m_vTerm()
619 , m_constant(0)
620 {
621 }
622
624 {
625 m_vTerm.push_back(term);
627 }
628
631 {
632 copy(rhs);
633 }
634
637 {
638 if (this != &rhs)
639 copy(rhs);
640 return *this;
641 }
642
644 {
645 }
646
650 {
651 m_vTerm.swap(rhs.m_vTerm);
652 std::swap(m_constant, rhs.m_constant);
653 }
654
655 std::vector<term_type> const& terms() const {return m_vTerm;}
657 void clear()
658 {
659 m_vTerm.clear();
660 m_constant = 0;
661 }
662
664 void reserve(unsigned int n) {m_vTerm.reserve(n);}
675
683
689 {
690 LinearExpression other (expr);
691 other += constant;
692 return other;
693 }
694
698 friend LinearExpression operator+(LinearExpression const& expr, term_type const& term)
699 {
700 LinearExpression other (expr);
701 other += term;
702 return other;
703 }
704
709 {
710 return expr+constant;
711 }
712
716 friend LinearExpression operator+(term_type const& term, LinearExpression const& expr)
717 {
718 return expr+term;
719 }
720
725 {
726 LinearExpression other (expr1);
727 other += expr2;
728 return other;
729 }
730
734 {
735 this->incrementConstant(constant);
736 return *this;
737 }
738
742 {
743 m_vTerm.push_back(term);
744 return *this;
745 }
746
750 {
751 m_vTerm.insert(m_vTerm.end(), expr.terms().begin(), expr.terms().end());
752 this->incrementConstant(expr.constant());
753 return *this;
754 }
755
760 {
761 LinearExpression other (expr);
762 other -= constant;
763 return other;
764 }
765
769 friend LinearExpression operator-(LinearExpression const& expr, term_type const& term)
770 {
771 LinearExpression other (expr);
772 other -= term;
773 return other;
774 }
775
780 {
781 return (-expr)+constant;
782 }
783
787 friend LinearExpression operator-(term_type const& term, LinearExpression const& expr)
788 {
789 return (-expr)+term;
790 }
791
796 {
797 LinearExpression other (expr1);
798 other -= expr2;
799 return other;
800 }
801
805 {
806 this->incrementConstant(-constant);
807 return *this;
808 }
809
813 {
814 term.setCoefficient(-term.coefficient());
815 return this->operator+=(term);
816 }
817
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 }
827
832 {
833 LinearExpression other (expr);
834 other *= c;
835 return other;
836 }
837
842 {
843 return expr*c;
844 }
845
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 }
855
860 {
861 LinearExpression other (expr);
862 other /= c;
863 return other;
864 }
865
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 }
875
878 {
879 LinearExpression expr (*this);
880 return expr.negate();
881 }
882
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 }
891
898
902 {
903 return (expr < rhs);
904 }
905
912
916 {
917 return (expr > rhs);
918 }
919
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;
977};
978
981template <typename T>
983{
984 public:
993
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 }
1006
1008 {
1009 copy(rhs);
1010 }
1011
1013 {
1014 if (this != &rhs)
1015 copy(rhs);
1016 return *this;
1017 }
1018
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);}
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 }
1053
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 }
1061
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 }
1074
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 }
1092
1095 {
1096 // make sure the constant is zero in expression
1097 m_rhs -= m_expr.constant();
1098 m_expr.setConstant(0);
1099 }
1100
1102 void reserve(unsigned int n) {m_expr.reserve(n);}
1103
1108 {
1109 m_expr += term;
1110 return *this;
1111 }
1112
1116 {
1117 m_expr += expr;
1118 clearConstant();
1119 return *this;
1120 }
1121
1125 {
1126 m_expr -= term;
1127 return *this;
1128 }
1129
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;
1153 char m_sense;
1154};
1155
1159template <typename T, typename V>
1161{
1162 public:
1179
1182 : base_type()
1183 {
1184 m_optType = MIN;
1185 }
1186
1189 : base_type(rhs)
1190 {
1191 copy(rhs);
1192 }
1193
1196 {
1197 if (this != &rhs)
1198 {
1199 this->base_type::operator=(rhs);
1200 copy(rhs);
1201 }
1202 return *this;
1203 }
1204
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 }
1222
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 }
1284
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;}
1296 {
1297 m_objective = expr;
1298 m_objective.simplify();
1299 }
1300
1303 {
1304 m_objective.swap(expr);
1305 m_objective.simplify();
1306 }
1307
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 }
1329
1332 void setVariableName(variable_type const& var, std::string const& name) {m_vVariableProperty[var.id()].setName(name);}
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);}
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);}
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 }
1371
1372 std::vector<variable_value_type> const& variableSolutions() const {return m_vVariableSol;}
1374 std::vector<variable_value_type>& variableSolutions() {return m_vVariableSol;}
1382 void reserveVariables(unsigned int n)
1383 {
1384 m_vVariableProperty.reserve(n);
1385 m_vVariableSol.reserve(n);
1386 }
1387
1389 void reserveConstraints(unsigned int n)
1390 {
1391 m_vConstraint.reserve(n);
1392 m_vConstraintName.reserve(n);
1393 }
1394
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 }
1403
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 }
1422
1425 coefficient_value_type evaluateObjective(std::vector<variable_value_type> const& vVariableSol) const
1426 {
1427 return evaluateExpression(m_objective, vVariableSol);
1428 }
1429
1435
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 }
1447
1451 {
1452 return evaluateConstraint(constr, m_vVariableSol);
1453 }
1454
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 }
1468
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>())
1483 if (r >= (double)std::numeric_limits<variable_value_type>::max())
1484 ub = std::numeric_limits<variable_value_type>::max();
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 }
1502
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 }
1518
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 }
1534
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 }
1547
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 }
1561
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 }
1625
1629 std::ostream& print(std::ostream& os, term_type const& term) const
1630 {
1631 os << term.coefficient() << " " << variableName(term.variable());
1632 return os;
1633 }
1634
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 }
1651
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 }
1664
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 }
1674
1677 std::ostream& printSolution(std::ostream& os = std::cout) const
1678 {
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;
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
1714template <typename T, typename I, int StartingIndex = 1>
1716{
1718 typedef T value_type;
1720 typedef I index_type;
1721
1729
1732 : vElement(NULL)
1733 , vColumn(NULL)
1734 , vRowBeginIndex(NULL)
1735 , numRows(0)
1736 , numColumns(0)
1737 , numElements(0)
1738 {
1739 }
1740
1742 {
1743 copy(rhs);
1744 }
1745
1747 {
1748 if (this != &rhs)
1749 copy(rhs);
1750 return *this;
1751 }
1752
1754 {
1755 reset();
1756 }
1757
1763 {
1764 reset();
1765 numRows = nr;
1766 numColumns = nc;
1767 numElements = nv;
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;
1798 if (rhs.vElement)
1799 {
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
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;
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
1860
1861 // initialize vElement and vColumn
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
1871#endif
1872 vElement[i] = itt->coefficient();
1873 vColumn[i] = itt->variable().id()+s_startingIndex;
1874 }
1875 }
1876 }
1877};
1878
1879template <typename T, typename I, int StartingIndex>
1881
1882} // namespace solvers
1883} // namespace limbo
1884
1885#endif
#define limboAssertMsg(condition, args...)
custom assertion with message
Definition AssertMsg.h:24
#define limboAssert(condition)
custom assertion without message
Definition AssertMsg.h:36
Driver for Lp parser.
mathematical utilities such as abs
header to include PrintMsg.h and AssertMsg.h
Base class for lp database. Only pure virtual functions are defined. User needs to inheritate this ...
Definition LpDataBase.h:115
Describe linear constraint.
Definition Solvers.h:983
Variable< coefficient_value_type > variable_type
variable type
Definition Solvers.h:988
LinearConstraint & operator-=(term_type term)
Definition Solvers.h:1124
LinearConstraint & operator+=(term_type const &term)
Definition Solvers.h:1107
void simplify()
simplify expression by merge terms of the same variables
Definition Solvers.h:1054
void copy(LinearConstraint const &rhs)
copy object
Definition Solvers.h:1141
void setId(unsigned int i)
Definition Solvers.h:1026
unsigned int id() const
Definition Solvers.h:1024
void swap(LinearConstraint &rhs)
swap with a constraint
Definition Solvers.h:1043
void emplaceExpression(expression_type &expr)
Definition Solvers.h:1032
coefficient_value_type rightHandSide() const
Definition Solvers.h:1034
void scale(coefficient_value_type factor)
scale constraint by a scaling factor
Definition Solvers.h:1076
void setRightHandSide(coefficient_value_type rhs)
Definition Solvers.h:1036
LinearConstraint & operator-=(expression_type const &expr)
Definition Solvers.h:1132
LinearTerm< coefficient_value_type > term_type
term type
Definition Solvers.h:990
LinearConstraint & operator=(LinearConstraint const &rhs)
assignment
Definition Solvers.h:1012
expression_type const & expression() const
Definition Solvers.h:1028
void clearConstant()
move the constant in the expression to rhs For example, x + 5 < 10 will become x < 5
Definition Solvers.h:1094
LinearExpression< coefficient_value_type > expression_type
expression type
Definition Solvers.h:992
LinearConstraint(expression_type expr=expression_type(), coefficient_value_type rhs=0, char s='<')
constructor
Definition Solvers.h:998
void normalize(char s)
normalize sense
Definition Solvers.h:1063
T coefficient_value_type
coefficient type
Definition Solvers.h:986
void setExpression(expression_type const &expr)
Definition Solvers.h:1030
LinearConstraint(LinearConstraint const &rhs)
copy constructor
Definition Solvers.h:1007
LinearConstraint & operator+=(expression_type const &expr)
Definition Solvers.h:1115
void reserve(unsigned int n)
reserve space for expression terms
Definition Solvers.h:1102
Describe linear expressions in optimization problem.
Definition Solvers.h:607
LinearTerm< coefficient_value_type > term_type
term type
Definition Solvers.h:612
LinearExpression & setConstant(coefficient_value_type constant)
set constant term
Definition Solvers.h:670
friend LinearConstraint< coefficient_value_type > operator==(LinearExpression const &expr, coefficient_value_type rhs)
Definition Solvers.h:922
friend LinearExpression operator+(LinearExpression const &expr1, LinearExpression const &expr2)
Definition Solvers.h:724
friend LinearExpression operator+(term_type const &term, LinearExpression const &expr)
Definition Solvers.h:716
friend LinearConstraint< coefficient_value_type > operator<=(LinearExpression const &expr, coefficient_value_type rhs)
overload <=, same as <
Definition Solvers.h:901
friend LinearExpression operator-(term_type const &term, LinearExpression const &expr)
Definition Solvers.h:787
friend LinearExpression operator-(coefficient_value_type constant, LinearExpression const &expr)
Definition Solvers.h:779
friend LinearExpression operator*(LinearExpression const &expr, coefficient_value_type c)
Definition Solvers.h:831
friend LinearExpression operator+(LinearExpression const &expr, coefficient_value_type constant)
Definition Solvers.h:688
LinearExpression & operator+=(coefficient_value_type constant)
Definition Solvers.h:733
std::vector< term_type > const & terms() const
Definition Solvers.h:655
LinearExpression(term_type const &term, coefficient_value_type constant=0)
constructor
Definition Solvers.h:623
void reserve(unsigned int n)
reserve space for terms
Definition Solvers.h:664
LinearExpression & operator-=(coefficient_value_type constant)
Definition Solvers.h:804
friend LinearConstraint< coefficient_value_type > operator<(LinearExpression const &expr, coefficient_value_type rhs)
overload <
Definition Solvers.h:894
LinearExpression & operator-=(LinearExpression const &expr)
Definition Solvers.h:820
friend LinearExpression operator-(LinearExpression const &expr, coefficient_value_type constant)
Definition Solvers.h:759
LinearExpression & incrementConstant(coefficient_value_type constant)
increment constant term
Definition Solvers.h:678
friend LinearExpression operator/(LinearExpression const &expr, coefficient_value_type c)
Definition Solvers.h:859
void clear()
clear expression
Definition Solvers.h:657
LinearExpression & operator*=(coefficient_value_type c)
Definition Solvers.h:848
friend LinearConstraint< coefficient_value_type > operator>=(LinearExpression const &expr, coefficient_value_type rhs)
overload >=, same as >
Definition Solvers.h:915
Variable< coefficient_value_type > variable_type
variable type
Definition Solvers.h:614
LinearExpression & operator-=(term_type term)
Definition Solvers.h:812
void swap(LinearExpression &rhs)
swap with an expression
Definition Solvers.h:649
LinearExpression & operator+=(LinearExpression const &expr)
Definition Solvers.h:749
LinearExpression & operator/=(coefficient_value_type c)
Definition Solvers.h:868
void copy(LinearExpression const &rhs)
copy object
Definition Solvers.h:969
friend LinearExpression operator+(LinearExpression const &expr, term_type const &term)
Definition Solvers.h:698
LinearExpression & operator+=(term_type const &term)
Definition Solvers.h:741
friend LinearExpression operator-(LinearExpression const &expr1, LinearExpression const &expr2)
Definition Solvers.h:795
LinearExpression & operator=(LinearExpression const &rhs)
assignment
Definition Solvers.h:636
LinearExpression operator-() const
Definition Solvers.h:877
friend LinearExpression operator*(coefficient_value_type c, LinearExpression const &expr)
Definition Solvers.h:841
friend LinearExpression operator+(coefficient_value_type constant, LinearExpression const &expr)
Definition Solvers.h:708
LinearExpression(LinearExpression const &rhs)
copy constructor
Definition Solvers.h:630
friend LinearExpression operator-(LinearExpression const &expr, term_type const &term)
Definition Solvers.h:769
T coefficient_value_type
coefficient type
Definition Solvers.h:610
LinearExpression & negate()
Definition Solvers.h:884
friend LinearConstraint< coefficient_value_type > operator>(LinearExpression const &expr, coefficient_value_type rhs)
overload >
Definition Solvers.h:908
void simplify()
simplify expression by merge terms of the same variables and remove terms with zero coefficients
Definition Solvers.h:929
std::vector< property_type > & variableProperties()
Definition Solvers.h:1314
bool print(std::string const &filename) const
print problem in lp format to file
Definition Solvers.h:1552
std::ostream & print(std::ostream &os, term_type const &term) const
print expression
Definition Solvers.h:1629
std::string variableName(variable_type const &var) const
Definition Solvers.h:1320
void setVariableUpperBound(variable_type const &var, variable_value_type ub)
set variable upper bound
Definition Solvers.h:1348
void scaleConstraint(unsigned int id, coefficient_value_type factor)
scaling a constraint
Definition Solvers.h:1409
void add_variable(std::string const &vname, double l=limbo::lowest< double >(), double r=std::numeric_limits< double >::max())
add variable that l <= vname <= r.
Definition Solvers.h:1474
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
void read(std::string const &filename)
read lp format
Definition Solvers.h:1464
std::vector< constraint_type > & constraints()
Definition Solvers.h:1212
VariableProperty< variable_value_type > property_type
Definition Solvers.h:1178
Variable< coefficient_value_type > variable_type
Definition Solvers.h:1170
void reserveConstraints(unsigned int n)
reserve space for constraints
Definition Solvers.h:1389
std::vector< variable_value_type > const & variableSolutions() const
Definition Solvers.h:1372
std::vector< property_type > m_vVariableProperty
Definition Solvers.h:1701
bool addConstraint(constraint_type const &constr, std::string name="")
add a constraint
Definition Solvers.h:1217
void setOptimizeType(SolverProperty optType)
Definition Solvers.h:1310
std::map< std::string, variable_type > m_mName2Variable
Definition Solvers.h:1707
void evaluateConstraint() const
evaluate slackness of all constraints given solutions of variables and print to screen
Definition Solvers.h:1455
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 updateVariableLowerBound(variable_type const &var, variable_value_type lb)
update variable lower bound
Definition Solvers.h:1342
std::vector< std::string > & constraintNames()
Definition Solvers.h:1290
bool emplaceConstraint(expression_type &expr, char sense, coefficient_value_type rhs, std::string name="")
emplace a constraint
Definition Solvers.h:1228
SolverProperty optimizeType() const
Definition Solvers.h:1308
SolverProperty variableNumericType(variable_type const &var)
Definition Solvers.h:1354
variable_type addVariable(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string name="")
add one variable
Definition Solvers.h:1365
void scaleObjective(coefficient_value_type factor)
scale objective
Definition Solvers.h:1405
expression_type const & objective() const
Definition Solvers.h:1292
bool printSolution(std::string const &filename) const
print solutions to file
Definition Solvers.h:1665
LinearConstraint< coefficient_value_type > constraint_type
Definition Solvers.h:1176
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
void setVariableNumericType(variable_type const &var, SolverProperty type)
set numeric type of variable
Definition Solvers.h:1358
variable_type variable(unsigned int id) const
Definition Solvers.h:1318
LinearModel(LinearModel const &rhs)
copy constructor
Definition Solvers.h:1188
coefficient_value_type evaluateObjective() const
evaluate objective
Definition Solvers.h:1431
void add_objective(bool minimize, LpParser::TermArray const &terms)
add object terms
Definition Solvers.h:1521
std::vector< std::string > m_vConstraintName
Definition Solvers.h:1703
variable_value_type variableLowerBound(variable_type const &var)
Definition Solvers.h:1334
std::string const & constraintName(constraint_type const &constr) const
Definition Solvers.h:1286
void reserveVariables(unsigned int n)
reserve space for variables
Definition Solvers.h:1382
std::vector< variable_value_type > m_vVariableSol
Definition Solvers.h:1705
coefficient_value_type evaluateObjective(std::vector< variable_value_type > const &vVariableSol) const
evaluate objective
Definition Solvers.h:1425
variable_value_type variableUpperBound(variable_type const &var)
Definition Solvers.h:1344
std::vector< std::string > const & constraintNames() const
Definition Solvers.h:1288
std::ostream & print(std::ostream &os, expression_type const &expr) const
print expression
Definition Solvers.h:1638
LinearModel & operator=(LinearModel const &rhs)
assignment
Definition Solvers.h:1195
void setVariableLowerBound(variable_type const &var, variable_value_type lb)
set variable lower bound
Definition Solvers.h:1338
std::vector< constraint_type > const & constraints() const
Definition Solvers.h:1210
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
void copy(LinearModel const &rhs)
copy object
Definition Solvers.h:1687
std::ostream & print(std::ostream &os, constraint_type const &constr) const
print constraint
Definition Solvers.h:1655
std::vector< constraint_type > m_vConstraint
Definition Solvers.h:1699
void setVariableName(variable_type const &var, std::string const &name)
set variable name
Definition Solvers.h:1332
void emplaceObjective(expression_type &expr)
set objective by swaping with the expression
Definition Solvers.h:1302
void setObjective(expression_type const &expr)
set objective
Definition Solvers.h:1295
void resizeConstraints(unsigned int n)
resize constraints
Definition Solvers.h:1396
coefficient_value_type evaluateConstraint(constraint_type const &constr) const
evaluate slackness of a constraint given solutions of variables
Definition Solvers.h:1450
void setVariableSolution(variable_type const &var, variable_value_type v)
Definition Solvers.h:1379
void updateVariableUpperBound(variable_type const &var, variable_value_type ub)
update variable upper bound
Definition Solvers.h:1352
std::ostream & print(std::ostream &os=std::cout) const
print problem in lp format
Definition Solvers.h:1564
std::ostream & printSolution(std::ostream &os=std::cout) const
print solutions
Definition Solvers.h:1677
LinearExpression< coefficient_value_type > expression_type
Definition Solvers.h:1174
LinearTerm< coefficient_value_type > term_type
Definition Solvers.h:1172
std::vector< property_type > const & variableProperties() const
Definition Solvers.h:1312
variable_value_type variableSolution(variable_type const &var) const
Definition Solvers.h:1376
unsigned int numVariables() const
Definition Solvers.h:1316
friend LinearConstraint< coefficient_value_type > operator<(LinearTerm const &term, coefficient_value_type rhs)
overload <
Definition Solvers.h:537
void setVariable(variable_type const &var)
Definition Solvers.h:386
LinearTerm & operator*=(coefficient_value_type coef)
Definition Solvers.h:427
friend LinearConstraint< coefficient_value_type > operator==(LinearTerm const &term, coefficient_value_type rhs)
Definition Solvers.h:565
coefficient_value_type coefficient() const
Definition Solvers.h:388
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term1, LinearTerm const &term2)
Definition Solvers.h:528
LinearTerm & negate()
Definition Solvers.h:401
Variable< coefficient_value_type > variable_type
variable type
Definition Solvers.h:354
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term1, LinearTerm const &term2)
Definition Solvers.h:486
friend LinearExpression< coefficient_value_type > operator+(variable_type const &var, LinearTerm const &term)
Definition Solvers.h:478
friend LinearTerm operator*(coefficient_value_type coef, LinearTerm const &term)
Definition Solvers.h:420
LinearTerm operator-() const
Definition Solvers.h:394
LinearTerm(variable_type var=variable_type(), coefficient_value_type coef=1)
constructor
Definition Solvers.h:359
void copy(LinearTerm const &rhs)
copy object
Definition Solvers.h:579
LinearTerm(LinearTerm const &rhs)
copy constructor
Definition Solvers.h:366
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term, variable_type const &var)
Definition Solvers.h:504
friend LinearExpression< coefficient_value_type > operator-(coefficient_value_type constant, LinearTerm const &term)
Definition Solvers.h:520
friend LinearConstraint< coefficient_value_type > operator>=(LinearTerm const &term, coefficient_value_type rhs)
overload >=, same as >
Definition Solvers.h:558
friend LinearExpression< coefficient_value_type > operator-(variable_type const &var, LinearTerm const &term)
Definition Solvers.h:512
friend LinearTerm operator/(LinearTerm const &term, coefficient_value_type coef)
Definition Solvers.h:436
bool operator==(LinearTerm const &rhs) const
Definition Solvers.h:572
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term, coefficient_value_type constant)
Definition Solvers.h:454
friend LinearConstraint< coefficient_value_type > operator<=(LinearTerm const &term, coefficient_value_type rhs)
overload <=, same as <
Definition Solvers.h:544
variable_type const & variable() const
Definition Solvers.h:384
friend LinearExpression< coefficient_value_type > operator+(coefficient_value_type constant, LinearTerm const &term)
Definition Solvers.h:470
LinearTerm & operator=(LinearTerm const &rhs)
assignment
Definition Solvers.h:372
friend LinearConstraint< coefficient_value_type > operator>(LinearTerm const &term, coefficient_value_type rhs)
overload >
Definition Solvers.h:551
void setCoefficient(coefficient_value_type coef)
Definition Solvers.h:390
friend LinearTerm operator*(LinearTerm const &term, coefficient_value_type coef)
Definition Solvers.h:410
friend LinearExpression< coefficient_value_type > operator+(LinearTerm const &term, variable_type const &var)
Definition Solvers.h:462
T coefficient_value_type
coefficient type
Definition Solvers.h:352
friend LinearExpression< coefficient_value_type > operator-(LinearTerm const &term, coefficient_value_type constant)
Definition Solvers.h:496
LinearTerm & operator/=(coefficient_value_type coef)
Definition Solvers.h:445
Describe variables in optimization problem.
Definition Solvers.h:88
LinearTerm< coefficient_value_type > operator-() const
Definition Solvers.h:141
friend LinearExpression< coefficient_value_type > operator-(Variable const &var1, Variable const &var2)
Definition Solvers.h:214
friend LinearExpression< coefficient_value_type > operator+(Variable const &var1, Variable const &var2)
Definition Solvers.h:190
friend LinearExpression< coefficient_value_type > operator-(coefficient_value_type constant, Variable const &var)
Definition Solvers.h:206
friend LinearConstraint< coefficient_value_type > operator<(Variable const &var, coefficient_value_type rhs)
overload <
Definition Solvers.h:221
T coefficient_value_type
coefficient type
Definition Solvers.h:91
~Variable()
destructor
Definition Solvers.h:114
friend LinearTerm< coefficient_value_type > operator*(Variable const &var, coefficient_value_type coef)
Definition Solvers.h:150
friend LinearExpression< coefficient_value_type > operator+(coefficient_value_type constant, Variable const &var)
Definition Solvers.h:182
friend LinearConstraint< coefficient_value_type > operator==(Variable const &var, coefficient_value_type rhs)
overload ==
Definition Solvers.h:249
Variable & operator=(Variable const &rhs)
assignment
Definition Solvers.h:107
friend LinearTerm< coefficient_value_type > operator/(Variable const &var, coefficient_value_type coef)
Definition Solvers.h:166
bool operator==(Variable const &rhs) const
Definition Solvers.h:128
friend LinearConstraint< coefficient_value_type > operator<=(Variable const &var, coefficient_value_type rhs)
overload <=
Definition Solvers.h:228
friend LinearExpression< coefficient_value_type > operator-(Variable const &var, coefficient_value_type constant)
Definition Solvers.h:198
void copy(Variable const &rhs)
copy object
Definition Solvers.h:255
Variable(Variable const &rhs)
copy constructor
Definition Solvers.h:101
friend LinearTerm< coefficient_value_type > operator*(coefficient_value_type coef, Variable const &var)
Definition Solvers.h:158
Variable(unsigned int id=std::numeric_limits< unsigned int >::max())
constructor
Definition Solvers.h:95
friend LinearExpression< coefficient_value_type > operator+(Variable const &var, coefficient_value_type constant)
Definition Solvers.h:174
friend LinearConstraint< coefficient_value_type > operator>(Variable const &var, coefficient_value_type rhs)
overload >
Definition Solvers.h:235
friend LinearConstraint< coefficient_value_type > operator>=(Variable const &var, coefficient_value_type rhs)
overload >=
Definition Solvers.h:242
void setId(unsigned int id)
set variable index
Definition Solvers.h:120
bool operator!=(Variable const &rhs) const
Definition Solvers.h:135
Describe properties of a variable.
Definition Solvers.h:267
void setLowerBound(variable_value_type lb)
Definition Solvers.h:302
void updateLowerBound(variable_value_type lb)
Definition Solvers.h:304
void updateUpperBound(variable_value_type ub)
Definition Solvers.h:310
VariableProperty(variable_value_type lb, variable_value_type ub, SolverProperty nt, std::string const &n)
constructor
Definition Solvers.h:277
void setName(std::string const &n)
Definition Solvers.h:326
VariableProperty & operator=(VariableProperty const &rhs)
assignment
Definition Solvers.h:292
V variable_value_type
type of bounds
Definition Solvers.h:270
SolverProperty numericType() const
Definition Solvers.h:312
void copy(VariableProperty const &rhs)
copy object
Definition Solvers.h:331
std::string const & name() const
Definition Solvers.h:324
void setNumericType(SolverProperty nt)
Definition Solvers.h:314
void setUpperBound(variable_value_type ub)
Definition Solvers.h:308
VariableProperty(VariableProperty const &rhs)
copy constructor
Definition Solvers.h:286
variable_value_type upperBound() const
Definition Solvers.h:306
variable_value_type lowerBound() const
Definition Solvers.h:300
std::vector< Term > TermArray
array of terms
Definition LpDataBase.h:105
bool read(LpDataBase &db, const string &lpFile)
API for LpParser. Read LP file and initialize database by calling user-defined callback functions.
namespace for Limbo.Solvers
SolverProperty
Some enums used in solver.
Definition Solvers.h:30
@ OPTIMAL
optimally solved
Definition Solvers.h:36
@ CONTINUOUS
floating point number
Definition Solvers.h:35
@ BINARY
binary number
Definition Solvers.h:33
@ MIN
minimize objective
Definition Solvers.h:31
@ MAX
maximize objective
Definition Solvers.h:32
@ SUBOPTIMAL
the model is suboptimal
Definition Solvers.h:38
@ INFEASIBLE
the model is infeasible
Definition Solvers.h:37
@ UNBOUNDED
the model is unbounded
Definition Solvers.h:39
@ INTEGER
integer number
Definition Solvers.h:34
std::string toString(SolverProperty sp)
Convert limbo::solvers::SolverProperty to std::string.
Definition Solvers.h:44
namespace for Limbo
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition Math.h:61
int limboPrint(MessageType m, const char *format,...)
formatted print with prefix
Definition PrintMsg.h:49
int limboSPrint(MessageType m, char *buf, const char *format,...)
formatted print with prefix to buffer
Definition PrintMsg.h:101
T lowest()
generic function to get lowest value of numbers
Comapre term by variable.
Definition Solvers.h:591
bool operator()(TermType const &t1, TermType const &t2) const
Definition Solvers.h:597
MatrixCSR & operator=(MatrixCSR const &rhs)
assignment
Definition Solvers.h:1746
void reset()
Destroy matrix and recycle memory.
Definition Solvers.h:1774
void initialize(index_type nr, index_type nc, index_type nv)
Initialize matrix.
Definition Solvers.h:1762
MatrixCSR(MatrixCSR const &rhs)
copy constructor
Definition Solvers.h:1741
void copy(MatrixCSR const &rhs)
copy object
Definition Solvers.h:1792
value_type at(index_type i, index_type j) const
get element
Definition Solvers.h:1813
void set(index_type nr, index_type nc, LinearConstraint< value_type > const *vConstraint)
Set from array of constraints.
Definition Solvers.h:1838