[1247] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2391] | 5 | * Copyright (C) 2003-2007 |
---|
[1956] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[1247] | 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[1246] | 19 | #ifndef LEMON_LP_BASE_H |
---|
| 20 | #define LEMON_LP_BASE_H |
---|
| 21 | |
---|
[2345] | 22 | #include<iostream> |
---|
[1253] | 23 | #include<vector> |
---|
[1272] | 24 | #include<map> |
---|
[1256] | 25 | #include<limits> |
---|
[1397] | 26 | #include<cmath> |
---|
[1253] | 27 | |
---|
| 28 | #include<lemon/error.h> |
---|
[1993] | 29 | #include<lemon/bits/invalid.h> |
---|
[2363] | 30 | #include<lemon/bits/utility.h> |
---|
| 31 | #include<lemon/bits/lp_id.h> |
---|
[1253] | 32 | |
---|
[1246] | 33 | ///\file |
---|
| 34 | ///\brief The interface of the LP solver interface. |
---|
[2370] | 35 | ///\ingroup lp_group |
---|
[1246] | 36 | namespace lemon { |
---|
[2312] | 37 | |
---|
[1253] | 38 | ///Common base class for LP solvers |
---|
[1328] | 39 | |
---|
| 40 | ///\todo Much more docs |
---|
[2370] | 41 | ///\ingroup lp_group |
---|
[1246] | 42 | class LpSolverBase { |
---|
[1323] | 43 | |
---|
[2303] | 44 | protected: |
---|
| 45 | |
---|
[2363] | 46 | _lp_bits::LpId rows; |
---|
| 47 | _lp_bits::LpId cols; |
---|
| 48 | |
---|
[1247] | 49 | public: |
---|
[2364] | 50 | |
---|
[1458] | 51 | ///Possible outcomes of an LP solving procedure |
---|
[1303] | 52 | enum SolveExitStatus { |
---|
[1458] | 53 | ///This means that the problem has been successfully solved: either |
---|
| 54 | ///an optimal solution has been found or infeasibility/unboundedness |
---|
| 55 | ///has been proved. |
---|
[1293] | 56 | SOLVED = 0, |
---|
[2312] | 57 | ///Any other case (including the case when some user specified |
---|
| 58 | ///limit has been exceeded) |
---|
[1293] | 59 | UNSOLVED = 1 |
---|
[1291] | 60 | }; |
---|
| 61 | |
---|
[1460] | 62 | ///\e |
---|
[1303] | 63 | enum SolutionStatus { |
---|
[2185] | 64 | ///Feasible solution hasn't been found (but may exist). |
---|
[1295] | 65 | |
---|
| 66 | ///\todo NOTFOUND might be a better name. |
---|
| 67 | /// |
---|
[1293] | 68 | UNDEFINED = 0, |
---|
[1295] | 69 | ///The problem has no feasible solution |
---|
[1293] | 70 | INFEASIBLE = 1, |
---|
[1295] | 71 | ///Feasible solution found |
---|
[1293] | 72 | FEASIBLE = 2, |
---|
[1295] | 73 | ///Optimal solution exists and found |
---|
| 74 | OPTIMAL = 3, |
---|
| 75 | ///The cost function is unbounded |
---|
| 76 | |
---|
| 77 | ///\todo Give a feasible solution and an infinite ray (and the |
---|
| 78 | ///corresponding bases) |
---|
| 79 | INFINITE = 4 |
---|
[1263] | 80 | }; |
---|
[1460] | 81 | |
---|
[1542] | 82 | ///\e The type of the investigated LP problem |
---|
| 83 | enum ProblemTypes { |
---|
| 84 | ///Primal-dual feasible |
---|
| 85 | PRIMAL_DUAL_FEASIBLE = 0, |
---|
| 86 | ///Primal feasible dual infeasible |
---|
| 87 | PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1, |
---|
| 88 | ///Primal infeasible dual feasible |
---|
| 89 | PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2, |
---|
| 90 | ///Primal-dual infeasible |
---|
| 91 | PRIMAL_DUAL_INFEASIBLE = 3, |
---|
| 92 | ///Could not determine so far |
---|
| 93 | UNKNOWN = 4 |
---|
| 94 | }; |
---|
[1508] | 95 | |
---|
[1256] | 96 | ///The floating point type used by the solver |
---|
[1247] | 97 | typedef double Value; |
---|
[1256] | 98 | ///The infinity constant |
---|
[1247] | 99 | static const Value INF; |
---|
[1264] | 100 | ///The not a number constant |
---|
| 101 | static const Value NaN; |
---|
[2026] | 102 | |
---|
| 103 | static inline bool isNaN(const Value& v) { return v!=v; } |
---|
[1253] | 104 | |
---|
[2303] | 105 | friend class Col; |
---|
| 106 | friend class ColIt; |
---|
| 107 | friend class Row; |
---|
| 108 | |
---|
[1256] | 109 | ///Refer to a column of the LP. |
---|
| 110 | |
---|
| 111 | ///This type is used to refer to a column of the LP. |
---|
| 112 | /// |
---|
| 113 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 114 | ///other columns. |
---|
[1256] | 115 | /// |
---|
| 116 | ///\todo Document what can one do with a Col (INVALID, comparing, |
---|
| 117 | ///it is similar to Node/Edge) |
---|
| 118 | class Col { |
---|
| 119 | protected: |
---|
| 120 | int id; |
---|
| 121 | friend class LpSolverBase; |
---|
[2144] | 122 | friend class MipSolverBase; |
---|
[2364] | 123 | explicit Col(int _id) : id(_id) {} |
---|
[1256] | 124 | public: |
---|
[1259] | 125 | typedef Value ExprValue; |
---|
[1256] | 126 | typedef True LpSolverCol; |
---|
| 127 | Col() {} |
---|
| 128 | Col(const Invalid&) : id(-1) {} |
---|
[1900] | 129 | bool operator< (Col c) const {return id< c.id;} |
---|
| 130 | bool operator> (Col c) const {return id> c.id;} |
---|
[1256] | 131 | bool operator==(Col c) const {return id==c.id;} |
---|
[1900] | 132 | bool operator!=(Col c) const {return id!=c.id;} |
---|
[1256] | 133 | }; |
---|
| 134 | |
---|
[2303] | 135 | class ColIt : public Col { |
---|
[2366] | 136 | const LpSolverBase *_lp; |
---|
[2309] | 137 | public: |
---|
[2303] | 138 | ColIt() {} |
---|
[2366] | 139 | ColIt(const LpSolverBase &lp) : _lp(&lp) |
---|
[2303] | 140 | { |
---|
[2363] | 141 | _lp->cols.firstFix(id); |
---|
[2303] | 142 | } |
---|
| 143 | ColIt(const Invalid&) : Col(INVALID) {} |
---|
| 144 | ColIt &operator++() |
---|
| 145 | { |
---|
[2363] | 146 | _lp->cols.nextFix(id); |
---|
[2303] | 147 | return *this; |
---|
| 148 | } |
---|
| 149 | }; |
---|
[2312] | 150 | |
---|
| 151 | static int id(const Col& col) { return col.id; } |
---|
| 152 | |
---|
[2303] | 153 | |
---|
[1256] | 154 | ///Refer to a row of the LP. |
---|
| 155 | |
---|
| 156 | ///This type is used to refer to a row of the LP. |
---|
| 157 | /// |
---|
| 158 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 159 | ///other rows. |
---|
[1256] | 160 | /// |
---|
| 161 | ///\todo Document what can one do with a Row (INVALID, comparing, |
---|
| 162 | ///it is similar to Node/Edge) |
---|
| 163 | class Row { |
---|
| 164 | protected: |
---|
| 165 | int id; |
---|
| 166 | friend class LpSolverBase; |
---|
[2364] | 167 | explicit Row(int _id) : id(_id) {} |
---|
[1256] | 168 | public: |
---|
[1259] | 169 | typedef Value ExprValue; |
---|
[1256] | 170 | typedef True LpSolverRow; |
---|
| 171 | Row() {} |
---|
| 172 | Row(const Invalid&) : id(-1) {} |
---|
[1439] | 173 | |
---|
[1900] | 174 | bool operator< (Row c) const {return id< c.id;} |
---|
| 175 | bool operator> (Row c) const {return id> c.id;} |
---|
[1256] | 176 | bool operator==(Row c) const {return id==c.id;} |
---|
[1900] | 177 | bool operator!=(Row c) const {return id!=c.id;} |
---|
[2312] | 178 | }; |
---|
| 179 | |
---|
[2364] | 180 | class RowIt : public Row { |
---|
[2366] | 181 | const LpSolverBase *_lp; |
---|
[2364] | 182 | public: |
---|
| 183 | RowIt() {} |
---|
[2366] | 184 | RowIt(const LpSolverBase &lp) : _lp(&lp) |
---|
[2364] | 185 | { |
---|
| 186 | _lp->rows.firstFix(id); |
---|
| 187 | } |
---|
| 188 | RowIt(const Invalid&) : Row(INVALID) {} |
---|
| 189 | RowIt &operator++() |
---|
| 190 | { |
---|
| 191 | _lp->rows.nextFix(id); |
---|
| 192 | return *this; |
---|
| 193 | } |
---|
| 194 | }; |
---|
| 195 | |
---|
[2312] | 196 | static int id(const Row& row) { return row.id; } |
---|
| 197 | |
---|
| 198 | protected: |
---|
| 199 | |
---|
[2386] | 200 | int _lpId(const Col& c) const { |
---|
| 201 | return cols.floatingId(id(c)); |
---|
[2312] | 202 | } |
---|
| 203 | |
---|
[2386] | 204 | int _lpId(const Row& r) const { |
---|
| 205 | return rows.floatingId(id(r)); |
---|
[2312] | 206 | } |
---|
| 207 | |
---|
[2386] | 208 | Col _item(int i, Col) const { |
---|
| 209 | return Col(cols.fixId(i)); |
---|
[2364] | 210 | } |
---|
| 211 | |
---|
[2386] | 212 | Row _item(int i, Row) const { |
---|
| 213 | return Row(rows.fixId(i)); |
---|
[2364] | 214 | } |
---|
| 215 | |
---|
[2312] | 216 | |
---|
| 217 | public: |
---|
[1259] | 218 | |
---|
[1279] | 219 | ///Linear expression of variables and a constant component |
---|
| 220 | |
---|
[2345] | 221 | ///This data structure stores a linear expression of the variables |
---|
[1279] | 222 | ///(\ref Col "Col"s) and also has a constant component. |
---|
| 223 | /// |
---|
| 224 | ///There are several ways to access and modify the contents of this |
---|
| 225 | ///container. |
---|
| 226 | ///- Its it fully compatible with \c std::map<Col,double>, so for expamle |
---|
[1364] | 227 | ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can |
---|
[1279] | 228 | ///read and modify the coefficients like |
---|
| 229 | ///these. |
---|
| 230 | ///\code |
---|
| 231 | ///e[v]=5; |
---|
| 232 | ///e[v]+=12; |
---|
| 233 | ///e.erase(v); |
---|
| 234 | ///\endcode |
---|
| 235 | ///or you can also iterate through its elements. |
---|
| 236 | ///\code |
---|
| 237 | ///double s=0; |
---|
| 238 | ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) |
---|
| 239 | /// s+=i->second; |
---|
| 240 | ///\endcode |
---|
| 241 | ///(This code computes the sum of all coefficients). |
---|
| 242 | ///- Numbers (<tt>double</tt>'s) |
---|
| 243 | ///and variables (\ref Col "Col"s) directly convert to an |
---|
[1908] | 244 | ///\ref Expr and the usual linear operations are defined, so |
---|
[1279] | 245 | ///\code |
---|
| 246 | ///v+w |
---|
| 247 | ///2*v-3.12*(v-w/2)+2 |
---|
| 248 | ///v*2.1+(3*v+(v*12+w+6)*3)/2 |
---|
| 249 | ///\endcode |
---|
[1328] | 250 | ///are valid \ref Expr "Expr"essions. |
---|
| 251 | ///The usual assignment operations are also defined. |
---|
[1279] | 252 | ///\code |
---|
| 253 | ///e=v+w; |
---|
| 254 | ///e+=2*v-3.12*(v-w/2)+2; |
---|
| 255 | ///e*=3.4; |
---|
| 256 | ///e/=5; |
---|
| 257 | ///\endcode |
---|
| 258 | ///- The constant member can be set and read by \ref constComp() |
---|
| 259 | ///\code |
---|
| 260 | ///e.constComp()=12; |
---|
| 261 | ///double c=e.constComp(); |
---|
| 262 | ///\endcode |
---|
| 263 | /// |
---|
[1328] | 264 | ///\note \ref clear() not only sets all coefficients to 0 but also |
---|
[1279] | 265 | ///clears the constant components. |
---|
[1328] | 266 | /// |
---|
| 267 | ///\sa Constr |
---|
| 268 | /// |
---|
[1273] | 269 | class Expr : public std::map<Col,Value> |
---|
[1272] | 270 | { |
---|
| 271 | public: |
---|
[1273] | 272 | typedef LpSolverBase::Col Key; |
---|
| 273 | typedef LpSolverBase::Value Value; |
---|
[1272] | 274 | |
---|
| 275 | protected: |
---|
[1273] | 276 | typedef std::map<Col,Value> Base; |
---|
[1272] | 277 | |
---|
[1273] | 278 | Value const_comp; |
---|
[2345] | 279 | public: |
---|
[1272] | 280 | typedef True IsLinExpression; |
---|
| 281 | ///\e |
---|
| 282 | Expr() : Base(), const_comp(0) { } |
---|
| 283 | ///\e |
---|
[1273] | 284 | Expr(const Key &v) : const_comp(0) { |
---|
[1272] | 285 | Base::insert(std::make_pair(v, 1)); |
---|
| 286 | } |
---|
| 287 | ///\e |
---|
[1273] | 288 | Expr(const Value &v) : const_comp(v) {} |
---|
[1272] | 289 | ///\e |
---|
[1273] | 290 | void set(const Key &v,const Value &c) { |
---|
[1272] | 291 | Base::insert(std::make_pair(v, c)); |
---|
| 292 | } |
---|
| 293 | ///\e |
---|
[1273] | 294 | Value &constComp() { return const_comp; } |
---|
[1272] | 295 | ///\e |
---|
[1273] | 296 | const Value &constComp() const { return const_comp; } |
---|
[1272] | 297 | |
---|
| 298 | ///Removes the components with zero coefficient. |
---|
| 299 | void simplify() { |
---|
| 300 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 301 | Base::iterator j=i; |
---|
| 302 | ++j; |
---|
| 303 | if ((*i).second==0) Base::erase(i); |
---|
[2085] | 304 | i=j; |
---|
[1272] | 305 | } |
---|
| 306 | } |
---|
[1273] | 307 | |
---|
[2312] | 308 | void simplify() const { |
---|
| 309 | const_cast<Expr*>(this)->simplify(); |
---|
| 310 | } |
---|
| 311 | |
---|
[1771] | 312 | ///Removes the coefficients closer to zero than \c tolerance. |
---|
| 313 | void simplify(double &tolerance) { |
---|
| 314 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 315 | Base::iterator j=i; |
---|
| 316 | ++j; |
---|
| 317 | if (std::fabs((*i).second)<tolerance) Base::erase(i); |
---|
[2085] | 318 | i=j; |
---|
[1771] | 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[1273] | 322 | ///Sets all coefficients and the constant component to 0. |
---|
| 323 | void clear() { |
---|
| 324 | Base::clear(); |
---|
| 325 | const_comp=0; |
---|
| 326 | } |
---|
| 327 | |
---|
[1272] | 328 | ///\e |
---|
| 329 | Expr &operator+=(const Expr &e) { |
---|
| 330 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 331 | (*this)[j->first]+=j->second; |
---|
| 332 | const_comp+=e.const_comp; |
---|
| 333 | return *this; |
---|
| 334 | } |
---|
| 335 | ///\e |
---|
| 336 | Expr &operator-=(const Expr &e) { |
---|
| 337 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 338 | (*this)[j->first]-=j->second; |
---|
| 339 | const_comp-=e.const_comp; |
---|
| 340 | return *this; |
---|
| 341 | } |
---|
| 342 | ///\e |
---|
[1273] | 343 | Expr &operator*=(const Value &c) { |
---|
[1272] | 344 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 345 | j->second*=c; |
---|
| 346 | const_comp*=c; |
---|
| 347 | return *this; |
---|
| 348 | } |
---|
| 349 | ///\e |
---|
[1273] | 350 | Expr &operator/=(const Value &c) { |
---|
[1272] | 351 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 352 | j->second/=c; |
---|
| 353 | const_comp/=c; |
---|
| 354 | return *this; |
---|
| 355 | } |
---|
[2345] | 356 | |
---|
| 357 | //std::ostream & |
---|
| 358 | void prettyPrint(std::ostream &os) { |
---|
| 359 | //std::fmtflags os.flags(); |
---|
| 360 | //os.setf(std::ios::showpos); |
---|
| 361 | Base::iterator j=Base::begin(); |
---|
| 362 | if (j!=Base::end()) |
---|
| 363 | os<<j->second<<"*x["<<id(j->first)<<"]"; |
---|
| 364 | ++j; |
---|
| 365 | for (; j!=Base::end(); ++j){ |
---|
| 366 | if (j->second>=0) |
---|
| 367 | os<<"+"; |
---|
| 368 | os<<j->second<<"*x["<<id(j->first)<<"]"; |
---|
| 369 | } |
---|
| 370 | //Nem valami korrekt, de nem talaltam meg, hogy kell |
---|
| 371 | //os.unsetf(std::ios::showpos); |
---|
| 372 | |
---|
| 373 | //return os; |
---|
| 374 | } |
---|
| 375 | |
---|
[1272] | 376 | }; |
---|
| 377 | |
---|
[1264] | 378 | ///Linear constraint |
---|
[1328] | 379 | |
---|
[1364] | 380 | ///This data stucture represents a linear constraint in the LP. |
---|
| 381 | ///Basically it is a linear expression with a lower or an upper bound |
---|
| 382 | ///(or both). These parts of the constraint can be obtained by the member |
---|
| 383 | ///functions \ref expr(), \ref lowerBound() and \ref upperBound(), |
---|
| 384 | ///respectively. |
---|
| 385 | ///There are two ways to construct a constraint. |
---|
| 386 | ///- You can set the linear expression and the bounds directly |
---|
| 387 | /// by the functions above. |
---|
| 388 | ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt> |
---|
| 389 | /// are defined between expressions, or even between constraints whenever |
---|
| 390 | /// it makes sense. Therefore if \c e and \c f are linear expressions and |
---|
| 391 | /// \c s and \c t are numbers, then the followings are valid expressions |
---|
| 392 | /// and thus they can be used directly e.g. in \ref addRow() whenever |
---|
| 393 | /// it makes sense. |
---|
[1908] | 394 | ///\code |
---|
[1364] | 395 | /// e<=s |
---|
| 396 | /// e<=f |
---|
[1908] | 397 | /// e==f |
---|
[1364] | 398 | /// s<=e<=t |
---|
| 399 | /// e>=t |
---|
[1908] | 400 | ///\endcode |
---|
[1364] | 401 | ///\warning The validity of a constraint is checked only at run time, so |
---|
| 402 | ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a |
---|
| 403 | ///\ref LogicError exception. |
---|
[1272] | 404 | class Constr |
---|
| 405 | { |
---|
| 406 | public: |
---|
| 407 | typedef LpSolverBase::Expr Expr; |
---|
[1273] | 408 | typedef Expr::Key Key; |
---|
| 409 | typedef Expr::Value Value; |
---|
[1272] | 410 | |
---|
[1273] | 411 | protected: |
---|
| 412 | Expr _expr; |
---|
| 413 | Value _lb,_ub; |
---|
| 414 | public: |
---|
| 415 | ///\e |
---|
| 416 | Constr() : _expr(), _lb(NaN), _ub(NaN) {} |
---|
| 417 | ///\e |
---|
| 418 | Constr(Value lb,const Expr &e,Value ub) : |
---|
| 419 | _expr(e), _lb(lb), _ub(ub) {} |
---|
| 420 | ///\e |
---|
| 421 | Constr(const Expr &e,Value ub) : |
---|
| 422 | _expr(e), _lb(NaN), _ub(ub) {} |
---|
| 423 | ///\e |
---|
| 424 | Constr(Value lb,const Expr &e) : |
---|
| 425 | _expr(e), _lb(lb), _ub(NaN) {} |
---|
| 426 | ///\e |
---|
[1272] | 427 | Constr(const Expr &e) : |
---|
[1273] | 428 | _expr(e), _lb(NaN), _ub(NaN) {} |
---|
| 429 | ///\e |
---|
| 430 | void clear() |
---|
| 431 | { |
---|
| 432 | _expr.clear(); |
---|
| 433 | _lb=_ub=NaN; |
---|
| 434 | } |
---|
[1364] | 435 | |
---|
| 436 | ///Reference to the linear expression |
---|
[1273] | 437 | Expr &expr() { return _expr; } |
---|
[1364] | 438 | ///Cont reference to the linear expression |
---|
[1273] | 439 | const Expr &expr() const { return _expr; } |
---|
[1364] | 440 | ///Reference to the lower bound. |
---|
| 441 | |
---|
| 442 | ///\return |
---|
[1536] | 443 | ///- \ref INF "INF": the constraint is lower unbounded. |
---|
| 444 | ///- \ref NaN "NaN": lower bound has not been set. |
---|
[1364] | 445 | ///- finite number: the lower bound |
---|
[1273] | 446 | Value &lowerBound() { return _lb; } |
---|
[1364] | 447 | ///The const version of \ref lowerBound() |
---|
[1273] | 448 | const Value &lowerBound() const { return _lb; } |
---|
[1364] | 449 | ///Reference to the upper bound. |
---|
| 450 | |
---|
| 451 | ///\return |
---|
[1536] | 452 | ///- \ref INF "INF": the constraint is upper unbounded. |
---|
| 453 | ///- \ref NaN "NaN": upper bound has not been set. |
---|
[1364] | 454 | ///- finite number: the upper bound |
---|
[1273] | 455 | Value &upperBound() { return _ub; } |
---|
[1364] | 456 | ///The const version of \ref upperBound() |
---|
[1273] | 457 | const Value &upperBound() const { return _ub; } |
---|
[1364] | 458 | ///Is the constraint lower bounded? |
---|
[1295] | 459 | bool lowerBounded() const { |
---|
| 460 | using namespace std; |
---|
[1397] | 461 | return finite(_lb); |
---|
[1295] | 462 | } |
---|
[1364] | 463 | ///Is the constraint upper bounded? |
---|
[1295] | 464 | bool upperBounded() const { |
---|
| 465 | using namespace std; |
---|
[1397] | 466 | return finite(_ub); |
---|
[1295] | 467 | } |
---|
[2345] | 468 | |
---|
| 469 | void prettyPrint(std::ostream &os) { |
---|
| 470 | if (_lb==-LpSolverBase::INF||isNaN(_lb)) |
---|
| 471 | os<<"-infty<="; |
---|
| 472 | else |
---|
| 473 | os<<_lb<<"<="; |
---|
| 474 | _expr.prettyPrint(os); |
---|
| 475 | if (_ub==LpSolverBase::INF) |
---|
| 476 | os<<"<=infty"; |
---|
| 477 | else |
---|
| 478 | os<<"<="<<_ub; |
---|
| 479 | //return os; |
---|
| 480 | } |
---|
| 481 | |
---|
[1272] | 482 | }; |
---|
| 483 | |
---|
[1445] | 484 | ///Linear expression of rows |
---|
| 485 | |
---|
| 486 | ///This data structure represents a column of the matrix, |
---|
| 487 | ///thas is it strores a linear expression of the dual variables |
---|
| 488 | ///(\ref Row "Row"s). |
---|
| 489 | /// |
---|
| 490 | ///There are several ways to access and modify the contents of this |
---|
| 491 | ///container. |
---|
| 492 | ///- Its it fully compatible with \c std::map<Row,double>, so for expamle |
---|
| 493 | ///if \c e is an DualExpr and \c v |
---|
| 494 | ///and \c w are of type \ref Row, then you can |
---|
| 495 | ///read and modify the coefficients like |
---|
| 496 | ///these. |
---|
| 497 | ///\code |
---|
| 498 | ///e[v]=5; |
---|
| 499 | ///e[v]+=12; |
---|
| 500 | ///e.erase(v); |
---|
| 501 | ///\endcode |
---|
| 502 | ///or you can also iterate through its elements. |
---|
| 503 | ///\code |
---|
| 504 | ///double s=0; |
---|
| 505 | ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i) |
---|
| 506 | /// s+=i->second; |
---|
| 507 | ///\endcode |
---|
| 508 | ///(This code computes the sum of all coefficients). |
---|
| 509 | ///- Numbers (<tt>double</tt>'s) |
---|
| 510 | ///and variables (\ref Row "Row"s) directly convert to an |
---|
[1908] | 511 | ///\ref DualExpr and the usual linear operations are defined, so |
---|
[1445] | 512 | ///\code |
---|
| 513 | ///v+w |
---|
| 514 | ///2*v-3.12*(v-w/2) |
---|
| 515 | ///v*2.1+(3*v+(v*12+w)*3)/2 |
---|
| 516 | ///\endcode |
---|
| 517 | ///are valid \ref DualExpr "DualExpr"essions. |
---|
| 518 | ///The usual assignment operations are also defined. |
---|
| 519 | ///\code |
---|
| 520 | ///e=v+w; |
---|
| 521 | ///e+=2*v-3.12*(v-w/2); |
---|
| 522 | ///e*=3.4; |
---|
| 523 | ///e/=5; |
---|
| 524 | ///\endcode |
---|
| 525 | /// |
---|
| 526 | ///\sa Expr |
---|
| 527 | /// |
---|
| 528 | class DualExpr : public std::map<Row,Value> |
---|
| 529 | { |
---|
| 530 | public: |
---|
| 531 | typedef LpSolverBase::Row Key; |
---|
| 532 | typedef LpSolverBase::Value Value; |
---|
| 533 | |
---|
| 534 | protected: |
---|
| 535 | typedef std::map<Row,Value> Base; |
---|
| 536 | |
---|
| 537 | public: |
---|
| 538 | typedef True IsLinExpression; |
---|
| 539 | ///\e |
---|
| 540 | DualExpr() : Base() { } |
---|
| 541 | ///\e |
---|
| 542 | DualExpr(const Key &v) { |
---|
| 543 | Base::insert(std::make_pair(v, 1)); |
---|
| 544 | } |
---|
| 545 | ///\e |
---|
| 546 | void set(const Key &v,const Value &c) { |
---|
| 547 | Base::insert(std::make_pair(v, c)); |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | ///Removes the components with zero coefficient. |
---|
| 551 | void simplify() { |
---|
| 552 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 553 | Base::iterator j=i; |
---|
| 554 | ++j; |
---|
| 555 | if ((*i).second==0) Base::erase(i); |
---|
[2085] | 556 | i=j; |
---|
[1445] | 557 | } |
---|
| 558 | } |
---|
| 559 | |
---|
[2312] | 560 | void simplify() const { |
---|
| 561 | const_cast<DualExpr*>(this)->simplify(); |
---|
| 562 | } |
---|
| 563 | |
---|
[1771] | 564 | ///Removes the coefficients closer to zero than \c tolerance. |
---|
| 565 | void simplify(double &tolerance) { |
---|
| 566 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 567 | Base::iterator j=i; |
---|
| 568 | ++j; |
---|
| 569 | if (std::fabs((*i).second)<tolerance) Base::erase(i); |
---|
[2085] | 570 | i=j; |
---|
[1771] | 571 | } |
---|
| 572 | } |
---|
| 573 | |
---|
[1445] | 574 | ///Sets all coefficients to 0. |
---|
| 575 | void clear() { |
---|
| 576 | Base::clear(); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | ///\e |
---|
| 580 | DualExpr &operator+=(const DualExpr &e) { |
---|
| 581 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 582 | (*this)[j->first]+=j->second; |
---|
| 583 | return *this; |
---|
| 584 | } |
---|
| 585 | ///\e |
---|
| 586 | DualExpr &operator-=(const DualExpr &e) { |
---|
| 587 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 588 | (*this)[j->first]-=j->second; |
---|
| 589 | return *this; |
---|
| 590 | } |
---|
| 591 | ///\e |
---|
| 592 | DualExpr &operator*=(const Value &c) { |
---|
| 593 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 594 | j->second*=c; |
---|
| 595 | return *this; |
---|
| 596 | } |
---|
| 597 | ///\e |
---|
| 598 | DualExpr &operator/=(const Value &c) { |
---|
| 599 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 600 | j->second/=c; |
---|
| 601 | return *this; |
---|
| 602 | } |
---|
| 603 | }; |
---|
| 604 | |
---|
[1253] | 605 | |
---|
[2312] | 606 | private: |
---|
| 607 | |
---|
[2364] | 608 | template <typename _Expr> |
---|
| 609 | class MappedOutputIterator { |
---|
[2312] | 610 | public: |
---|
| 611 | |
---|
[2364] | 612 | typedef std::insert_iterator<_Expr> Base; |
---|
| 613 | |
---|
| 614 | typedef std::output_iterator_tag iterator_category; |
---|
| 615 | typedef void difference_type; |
---|
| 616 | typedef void value_type; |
---|
| 617 | typedef void reference; |
---|
| 618 | typedef void pointer; |
---|
| 619 | |
---|
| 620 | MappedOutputIterator(const Base& _base, const LpSolverBase& _lp) |
---|
| 621 | : base(_base), lp(_lp) {} |
---|
| 622 | |
---|
| 623 | MappedOutputIterator& operator*() { |
---|
| 624 | return *this; |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | MappedOutputIterator& operator=(const std::pair<int, Value>& value) { |
---|
| 628 | *base = std::make_pair(lp._item(value.first, typename _Expr::Key()), |
---|
| 629 | value.second); |
---|
| 630 | return *this; |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | MappedOutputIterator& operator++() { |
---|
| 634 | ++base; |
---|
| 635 | return *this; |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | MappedOutputIterator operator++(int) { |
---|
| 639 | MappedOutputIterator tmp(*this); |
---|
| 640 | ++base; |
---|
| 641 | return tmp; |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | bool operator==(const MappedOutputIterator& it) const { |
---|
| 645 | return base == it.base; |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | bool operator!=(const MappedOutputIterator& it) const { |
---|
| 649 | return base != it.base; |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | private: |
---|
| 653 | Base base; |
---|
| 654 | const LpSolverBase& lp; |
---|
| 655 | }; |
---|
| 656 | |
---|
| 657 | template <typename Expr> |
---|
| 658 | class MappedInputIterator { |
---|
| 659 | public: |
---|
| 660 | |
---|
| 661 | typedef typename Expr::const_iterator Base; |
---|
[2312] | 662 | |
---|
| 663 | typedef typename Base::iterator_category iterator_category; |
---|
| 664 | typedef typename Base::difference_type difference_type; |
---|
| 665 | typedef const std::pair<int, Value> value_type; |
---|
| 666 | typedef value_type reference; |
---|
| 667 | class pointer { |
---|
| 668 | public: |
---|
| 669 | pointer(value_type& _value) : value(_value) {} |
---|
| 670 | value_type* operator->() { return &value; } |
---|
| 671 | private: |
---|
| 672 | value_type value; |
---|
| 673 | }; |
---|
| 674 | |
---|
[2364] | 675 | MappedInputIterator(const Base& _base, const LpSolverBase& _lp) |
---|
[2312] | 676 | : base(_base), lp(_lp) {} |
---|
| 677 | |
---|
| 678 | reference operator*() { |
---|
| 679 | return std::make_pair(lp._lpId(base->first), base->second); |
---|
| 680 | } |
---|
| 681 | |
---|
| 682 | pointer operator->() { |
---|
| 683 | return pointer(operator*()); |
---|
| 684 | } |
---|
| 685 | |
---|
[2364] | 686 | MappedInputIterator& operator++() { |
---|
[2312] | 687 | ++base; |
---|
| 688 | return *this; |
---|
| 689 | } |
---|
| 690 | |
---|
[2364] | 691 | MappedInputIterator operator++(int) { |
---|
| 692 | MappedInputIterator tmp(*this); |
---|
[2312] | 693 | ++base; |
---|
| 694 | return tmp; |
---|
| 695 | } |
---|
| 696 | |
---|
[2364] | 697 | bool operator==(const MappedInputIterator& it) const { |
---|
[2312] | 698 | return base == it.base; |
---|
| 699 | } |
---|
| 700 | |
---|
[2364] | 701 | bool operator!=(const MappedInputIterator& it) const { |
---|
[2312] | 702 | return base != it.base; |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | private: |
---|
| 706 | Base base; |
---|
| 707 | const LpSolverBase& lp; |
---|
| 708 | }; |
---|
| 709 | |
---|
[1253] | 710 | protected: |
---|
[1246] | 711 | |
---|
[2312] | 712 | /// STL compatible iterator for lp col |
---|
[2364] | 713 | typedef MappedInputIterator<Expr> ConstRowIterator; |
---|
[2312] | 714 | /// STL compatible iterator for lp row |
---|
[2364] | 715 | typedef MappedInputIterator<DualExpr> ConstColIterator; |
---|
| 716 | |
---|
| 717 | /// STL compatible iterator for lp col |
---|
| 718 | typedef MappedOutputIterator<Expr> RowIterator; |
---|
| 719 | /// STL compatible iterator for lp row |
---|
| 720 | typedef MappedOutputIterator<DualExpr> ColIterator; |
---|
[2312] | 721 | |
---|
[1323] | 722 | //Abstract virtual functions |
---|
[1364] | 723 | virtual LpSolverBase &_newLp() = 0; |
---|
[1436] | 724 | virtual LpSolverBase &_copyLp(){ |
---|
[2312] | 725 | ///\todo This should be implemented here, too, when we have |
---|
| 726 | ///problem retrieving routines. It can be overriden. |
---|
[1436] | 727 | |
---|
| 728 | //Starting: |
---|
| 729 | LpSolverBase & newlp(_newLp()); |
---|
| 730 | return newlp; |
---|
| 731 | //return *(LpSolverBase*)0; |
---|
| 732 | }; |
---|
[1364] | 733 | |
---|
[1246] | 734 | virtual int _addCol() = 0; |
---|
[2303] | 735 | virtual int _addRow() = 0; |
---|
[2366] | 736 | |
---|
[1542] | 737 | virtual void _eraseCol(int col) = 0; |
---|
| 738 | virtual void _eraseRow(int row) = 0; |
---|
[2366] | 739 | |
---|
| 740 | virtual void _getColName(int col, std::string & name) const = 0; |
---|
[1895] | 741 | virtual void _setColName(int col, const std::string & name) = 0; |
---|
[2366] | 742 | virtual int _colByName(const std::string& name) const = 0; |
---|
| 743 | |
---|
[2364] | 744 | virtual void _setRowCoeffs(int i, ConstRowIterator b, |
---|
| 745 | ConstRowIterator e) = 0; |
---|
[2366] | 746 | virtual void _getRowCoeffs(int i, RowIterator b) const = 0; |
---|
[2364] | 747 | virtual void _setColCoeffs(int i, ConstColIterator b, |
---|
| 748 | ConstColIterator e) = 0; |
---|
[2366] | 749 | virtual void _getColCoeffs(int i, ColIterator b) const = 0; |
---|
[1431] | 750 | virtual void _setCoeff(int row, int col, Value value) = 0; |
---|
[2366] | 751 | virtual Value _getCoeff(int row, int col) const = 0; |
---|
[1294] | 752 | virtual void _setColLowerBound(int i, Value value) = 0; |
---|
[2366] | 753 | virtual Value _getColLowerBound(int i) const = 0; |
---|
[1294] | 754 | virtual void _setColUpperBound(int i, Value value) = 0; |
---|
[2366] | 755 | virtual Value _getColUpperBound(int i) const = 0; |
---|
[1379] | 756 | virtual void _setRowBounds(int i, Value lower, Value upper) = 0; |
---|
[2366] | 757 | virtual void _getRowBounds(int i, Value &lower, Value &upper) const = 0; |
---|
[2328] | 758 | |
---|
[1294] | 759 | virtual void _setObjCoeff(int i, Value obj_coef) = 0; |
---|
[2366] | 760 | virtual Value _getObjCoeff(int i) const = 0; |
---|
[1377] | 761 | virtual void _clearObj()=0; |
---|
[2312] | 762 | |
---|
[1303] | 763 | virtual SolveExitStatus _solve() = 0; |
---|
[2366] | 764 | virtual Value _getPrimal(int i) const = 0; |
---|
| 765 | virtual Value _getDual(int i) const = 0; |
---|
| 766 | virtual Value _getPrimalValue() const = 0; |
---|
| 767 | virtual bool _isBasicCol(int i) const = 0; |
---|
| 768 | virtual SolutionStatus _getPrimalStatus() const = 0; |
---|
| 769 | virtual SolutionStatus _getDualStatus() const = 0; |
---|
| 770 | virtual ProblemTypes _getProblemType() const = 0; |
---|
[1460] | 771 | |
---|
[1312] | 772 | virtual void _setMax() = 0; |
---|
| 773 | virtual void _setMin() = 0; |
---|
| 774 | |
---|
[2324] | 775 | |
---|
[2366] | 776 | virtual bool _isMax() const = 0; |
---|
[2324] | 777 | |
---|
[1323] | 778 | //Own protected stuff |
---|
| 779 | |
---|
| 780 | //Constant component of the objective function |
---|
| 781 | Value obj_const_comp; |
---|
[2312] | 782 | |
---|
[1253] | 783 | public: |
---|
| 784 | |
---|
[1323] | 785 | ///\e |
---|
| 786 | LpSolverBase() : obj_const_comp(0) {} |
---|
[1253] | 787 | |
---|
| 788 | ///\e |
---|
| 789 | virtual ~LpSolverBase() {} |
---|
| 790 | |
---|
[1364] | 791 | ///Creates a new LP problem |
---|
| 792 | LpSolverBase &newLp() {return _newLp();} |
---|
[1381] | 793 | ///Makes a copy of the LP problem |
---|
[1364] | 794 | LpSolverBase ©Lp() {return _copyLp();} |
---|
| 795 | |
---|
[1612] | 796 | ///\name Build up and modify the LP |
---|
[1263] | 797 | |
---|
| 798 | ///@{ |
---|
| 799 | |
---|
[1253] | 800 | ///Add a new empty column (i.e a new variable) to the LP |
---|
[2363] | 801 | Col addCol() { Col c; _addCol(); c.id = cols.addId(); return c;} |
---|
[1263] | 802 | |
---|
[1294] | 803 | ///\brief Adds several new columns |
---|
| 804 | ///(i.e a variables) at once |
---|
[1256] | 805 | /// |
---|
[1273] | 806 | ///This magic function takes a container as its argument |
---|
[1256] | 807 | ///and fills its elements |
---|
| 808 | ///with new columns (i.e. variables) |
---|
[1273] | 809 | ///\param t can be |
---|
| 810 | ///- a standard STL compatible iterable container with |
---|
| 811 | ///\ref Col as its \c values_type |
---|
| 812 | ///like |
---|
| 813 | ///\code |
---|
| 814 | ///std::vector<LpSolverBase::Col> |
---|
| 815 | ///std::list<LpSolverBase::Col> |
---|
| 816 | ///\endcode |
---|
| 817 | ///- a standard STL compatible iterable container with |
---|
| 818 | ///\ref Col as its \c mapped_type |
---|
| 819 | ///like |
---|
| 820 | ///\code |
---|
[1364] | 821 | ///std::map<AnyType,LpSolverBase::Col> |
---|
[1273] | 822 | ///\endcode |
---|
[2260] | 823 | ///- an iterable lemon \ref concepts::WriteMap "write map" like |
---|
[1273] | 824 | ///\code |
---|
| 825 | ///ListGraph::NodeMap<LpSolverBase::Col> |
---|
| 826 | ///ListGraph::EdgeMap<LpSolverBase::Col> |
---|
| 827 | ///\endcode |
---|
[1256] | 828 | ///\return The number of the created column. |
---|
| 829 | #ifdef DOXYGEN |
---|
| 830 | template<class T> |
---|
| 831 | int addColSet(T &t) { return 0;} |
---|
| 832 | #else |
---|
| 833 | template<class T> |
---|
| 834 | typename enable_if<typename T::value_type::LpSolverCol,int>::type |
---|
| 835 | addColSet(T &t,dummy<0> = 0) { |
---|
| 836 | int s=0; |
---|
| 837 | for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} |
---|
| 838 | return s; |
---|
| 839 | } |
---|
| 840 | template<class T> |
---|
| 841 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
| 842 | int>::type |
---|
| 843 | addColSet(T &t,dummy<1> = 1) { |
---|
| 844 | int s=0; |
---|
| 845 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 846 | i->second=addCol(); |
---|
| 847 | s++; |
---|
| 848 | } |
---|
| 849 | return s; |
---|
| 850 | } |
---|
[1272] | 851 | template<class T> |
---|
[1810] | 852 | typename enable_if<typename T::MapIt::Value::LpSolverCol, |
---|
[1272] | 853 | int>::type |
---|
| 854 | addColSet(T &t,dummy<2> = 2) { |
---|
| 855 | int s=0; |
---|
[1810] | 856 | for(typename T::MapIt i(t); i!=INVALID; ++i) |
---|
[1272] | 857 | { |
---|
[1810] | 858 | i.set(addCol()); |
---|
[1272] | 859 | s++; |
---|
| 860 | } |
---|
| 861 | return s; |
---|
| 862 | } |
---|
[1256] | 863 | #endif |
---|
[1263] | 864 | |
---|
[1445] | 865 | ///Set a column (i.e a dual constraint) of the LP |
---|
[1258] | 866 | |
---|
[1445] | 867 | ///\param c is the column to be modified |
---|
| 868 | ///\param e is a dual linear expression (see \ref DualExpr) |
---|
| 869 | ///a better one. |
---|
[1899] | 870 | void col(Col c,const DualExpr &e) { |
---|
[2312] | 871 | e.simplify(); |
---|
[2364] | 872 | _setColCoeffs(_lpId(c), ConstColIterator(e.begin(), *this), |
---|
| 873 | ConstColIterator(e.end(), *this)); |
---|
| 874 | } |
---|
| 875 | |
---|
| 876 | ///Get a column (i.e a dual constraint) of the LP |
---|
| 877 | |
---|
| 878 | ///\param r is the column to get |
---|
| 879 | ///\return the dual expression associated to the column |
---|
[2366] | 880 | DualExpr col(Col c) const { |
---|
[2364] | 881 | DualExpr e; |
---|
| 882 | _getColCoeffs(_lpId(c), ColIterator(std::inserter(e, e.end()), *this)); |
---|
| 883 | return e; |
---|
[1445] | 884 | } |
---|
| 885 | |
---|
| 886 | ///Add a new column to the LP |
---|
| 887 | |
---|
| 888 | ///\param e is a dual linear expression (see \ref DualExpr) |
---|
| 889 | ///\param obj is the corresponding component of the objective |
---|
| 890 | ///function. It is 0 by default. |
---|
| 891 | ///\return The created column. |
---|
[2386] | 892 | Col addCol(const DualExpr &e, Value o = 0) { |
---|
[1445] | 893 | Col c=addCol(); |
---|
[1899] | 894 | col(c,e); |
---|
[2386] | 895 | objCoeff(c,o); |
---|
[1445] | 896 | return c; |
---|
| 897 | } |
---|
| 898 | |
---|
| 899 | ///Add a new empty row (i.e a new constraint) to the LP |
---|
| 900 | |
---|
| 901 | ///This function adds a new empty row (i.e a new constraint) to the LP. |
---|
[1258] | 902 | ///\return The created row |
---|
[2363] | 903 | Row addRow() { Row r; _addRow(); r.id = rows.addId(); return r;} |
---|
[1253] | 904 | |
---|
[1542] | 905 | ///\brief Add several new rows |
---|
| 906 | ///(i.e a constraints) at once |
---|
[1445] | 907 | /// |
---|
| 908 | ///This magic function takes a container as its argument |
---|
| 909 | ///and fills its elements |
---|
| 910 | ///with new row (i.e. variables) |
---|
| 911 | ///\param t can be |
---|
| 912 | ///- a standard STL compatible iterable container with |
---|
| 913 | ///\ref Row as its \c values_type |
---|
| 914 | ///like |
---|
| 915 | ///\code |
---|
| 916 | ///std::vector<LpSolverBase::Row> |
---|
| 917 | ///std::list<LpSolverBase::Row> |
---|
| 918 | ///\endcode |
---|
| 919 | ///- a standard STL compatible iterable container with |
---|
| 920 | ///\ref Row as its \c mapped_type |
---|
| 921 | ///like |
---|
| 922 | ///\code |
---|
| 923 | ///std::map<AnyType,LpSolverBase::Row> |
---|
| 924 | ///\endcode |
---|
[2260] | 925 | ///- an iterable lemon \ref concepts::WriteMap "write map" like |
---|
[1445] | 926 | ///\code |
---|
| 927 | ///ListGraph::NodeMap<LpSolverBase::Row> |
---|
| 928 | ///ListGraph::EdgeMap<LpSolverBase::Row> |
---|
| 929 | ///\endcode |
---|
| 930 | ///\return The number of rows created. |
---|
| 931 | #ifdef DOXYGEN |
---|
| 932 | template<class T> |
---|
| 933 | int addRowSet(T &t) { return 0;} |
---|
| 934 | #else |
---|
| 935 | template<class T> |
---|
| 936 | typename enable_if<typename T::value_type::LpSolverRow,int>::type |
---|
| 937 | addRowSet(T &t,dummy<0> = 0) { |
---|
| 938 | int s=0; |
---|
| 939 | for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;} |
---|
| 940 | return s; |
---|
| 941 | } |
---|
| 942 | template<class T> |
---|
| 943 | typename enable_if<typename T::value_type::second_type::LpSolverRow, |
---|
| 944 | int>::type |
---|
| 945 | addRowSet(T &t,dummy<1> = 1) { |
---|
| 946 | int s=0; |
---|
| 947 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 948 | i->second=addRow(); |
---|
| 949 | s++; |
---|
| 950 | } |
---|
| 951 | return s; |
---|
| 952 | } |
---|
| 953 | template<class T> |
---|
[1810] | 954 | typename enable_if<typename T::MapIt::Value::LpSolverRow, |
---|
[1445] | 955 | int>::type |
---|
| 956 | addRowSet(T &t,dummy<2> = 2) { |
---|
| 957 | int s=0; |
---|
[1810] | 958 | for(typename T::MapIt i(t); i!=INVALID; ++i) |
---|
[1445] | 959 | { |
---|
[1810] | 960 | i.set(addRow()); |
---|
[1445] | 961 | s++; |
---|
| 962 | } |
---|
| 963 | return s; |
---|
| 964 | } |
---|
| 965 | #endif |
---|
| 966 | |
---|
| 967 | ///Set a row (i.e a constraint) of the LP |
---|
[1253] | 968 | |
---|
[1258] | 969 | ///\param r is the row to be modified |
---|
[1259] | 970 | ///\param l is lower bound (-\ref INF means no bound) |
---|
[1258] | 971 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 972 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[2369] | 973 | ///\bug This is a temporary function. The interface will change to |
---|
[1253] | 974 | ///a better one. |
---|
[1328] | 975 | ///\todo Option to control whether a constraint with a single variable is |
---|
| 976 | ///added or not. |
---|
[2366] | 977 | void row(Row r, Value l, const Expr &e, Value u) { |
---|
[2312] | 978 | e.simplify(); |
---|
[2364] | 979 | _setRowCoeffs(_lpId(r), ConstRowIterator(e.begin(), *this), |
---|
| 980 | ConstRowIterator(e.end(), *this)); |
---|
| 981 | _setRowBounds(_lpId(r),l-e.constComp(),u-e.constComp()); |
---|
[1258] | 982 | } |
---|
| 983 | |
---|
[1445] | 984 | ///Set a row (i.e a constraint) of the LP |
---|
[1264] | 985 | |
---|
| 986 | ///\param r is the row to be modified |
---|
| 987 | ///\param c is a linear expression (see \ref Constr) |
---|
[1895] | 988 | void row(Row r, const Constr &c) { |
---|
[2312] | 989 | row(r, c.lowerBounded()?c.lowerBound():-INF, |
---|
| 990 | c.expr(), c.upperBounded()?c.upperBound():INF); |
---|
[1264] | 991 | } |
---|
| 992 | |
---|
[2364] | 993 | |
---|
| 994 | ///Get a row (i.e a constraint) of the LP |
---|
| 995 | |
---|
| 996 | ///\param r is the row to get |
---|
| 997 | ///\return the expression associated to the row |
---|
[2366] | 998 | Expr row(Row r) const { |
---|
[2364] | 999 | Expr e; |
---|
| 1000 | _getRowCoeffs(_lpId(r), RowIterator(std::inserter(e, e.end()), *this)); |
---|
| 1001 | return e; |
---|
| 1002 | } |
---|
| 1003 | |
---|
[1445] | 1004 | ///Add a new row (i.e a new constraint) to the LP |
---|
[1258] | 1005 | |
---|
[1259] | 1006 | ///\param l is the lower bound (-\ref INF means no bound) |
---|
[1258] | 1007 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 1008 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[1258] | 1009 | ///\return The created row. |
---|
[2369] | 1010 | ///\bug This is a temporary function. The interface will change to |
---|
[1258] | 1011 | ///a better one. |
---|
| 1012 | Row addRow(Value l,const Expr &e, Value u) { |
---|
| 1013 | Row r=addRow(); |
---|
[1895] | 1014 | row(r,l,e,u); |
---|
[1253] | 1015 | return r; |
---|
| 1016 | } |
---|
| 1017 | |
---|
[1445] | 1018 | ///Add a new row (i.e a new constraint) to the LP |
---|
[1264] | 1019 | |
---|
| 1020 | ///\param c is a linear expression (see \ref Constr) |
---|
| 1021 | ///\return The created row. |
---|
| 1022 | Row addRow(const Constr &c) { |
---|
| 1023 | Row r=addRow(); |
---|
[1895] | 1024 | row(r,c); |
---|
[1264] | 1025 | return r; |
---|
| 1026 | } |
---|
[1542] | 1027 | ///Erase a coloumn (i.e a variable) from the LP |
---|
| 1028 | |
---|
| 1029 | ///\param c is the coloumn to be deleted |
---|
| 1030 | ///\todo Please check this |
---|
| 1031 | void eraseCol(Col c) { |
---|
[2312] | 1032 | _eraseCol(_lpId(c)); |
---|
[2363] | 1033 | cols.eraseId(c.id); |
---|
[1542] | 1034 | } |
---|
| 1035 | ///Erase a row (i.e a constraint) from the LP |
---|
| 1036 | |
---|
| 1037 | ///\param r is the row to be deleted |
---|
| 1038 | ///\todo Please check this |
---|
| 1039 | void eraseRow(Row r) { |
---|
[2312] | 1040 | _eraseRow(_lpId(r)); |
---|
[2363] | 1041 | rows.eraseId(r.id); |
---|
[1542] | 1042 | } |
---|
[1264] | 1043 | |
---|
[1895] | 1044 | /// Get the name of a column |
---|
| 1045 | |
---|
| 1046 | ///\param c is the coresponding coloumn |
---|
| 1047 | ///\return The name of the colunm |
---|
[2366] | 1048 | std::string colName(Col c) const { |
---|
[1895] | 1049 | std::string name; |
---|
[2312] | 1050 | _getColName(_lpId(c), name); |
---|
[1895] | 1051 | return name; |
---|
| 1052 | } |
---|
| 1053 | |
---|
| 1054 | /// Set the name of a column |
---|
| 1055 | |
---|
| 1056 | ///\param c is the coresponding coloumn |
---|
| 1057 | ///\param name The name to be given |
---|
[2366] | 1058 | void colName(Col c, const std::string& name) { |
---|
[2312] | 1059 | _setColName(_lpId(c), name); |
---|
[1895] | 1060 | } |
---|
[2368] | 1061 | |
---|
| 1062 | /// Get the column by its name |
---|
| 1063 | |
---|
| 1064 | ///\param name The name of the column |
---|
| 1065 | ///\return the proper column or \c INVALID |
---|
| 1066 | Col colByName(const std::string& name) const { |
---|
| 1067 | int k = _colByName(name); |
---|
| 1068 | return k != -1 ? Col(cols.fixId(k)) : Col(INVALID); |
---|
| 1069 | } |
---|
[1895] | 1070 | |
---|
| 1071 | /// Set an element of the coefficient matrix of the LP |
---|
[1436] | 1072 | |
---|
| 1073 | ///\param r is the row of the element to be modified |
---|
| 1074 | ///\param c is the coloumn of the element to be modified |
---|
| 1075 | ///\param val is the new value of the coefficient |
---|
[1895] | 1076 | |
---|
[2366] | 1077 | void coeff(Row r, Col c, Value val) { |
---|
[2312] | 1078 | _setCoeff(_lpId(r),_lpId(c), val); |
---|
[1436] | 1079 | } |
---|
| 1080 | |
---|
[2324] | 1081 | /// Get an element of the coefficient matrix of the LP |
---|
| 1082 | |
---|
| 1083 | ///\param r is the row of the element in question |
---|
| 1084 | ///\param c is the coloumn of the element in question |
---|
| 1085 | ///\return the corresponding coefficient |
---|
| 1086 | |
---|
[2366] | 1087 | Value coeff(Row r, Col c) const { |
---|
[2324] | 1088 | return _getCoeff(_lpId(r),_lpId(c)); |
---|
| 1089 | } |
---|
| 1090 | |
---|
[1253] | 1091 | /// Set the lower bound of a column (i.e a variable) |
---|
| 1092 | |
---|
[1895] | 1093 | /// The lower bound of a variable (column) has to be given by an |
---|
[1253] | 1094 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 1095 | /// Value or -\ref INF. |
---|
[1293] | 1096 | void colLowerBound(Col c, Value value) { |
---|
[2312] | 1097 | _setColLowerBound(_lpId(c),value); |
---|
[1253] | 1098 | } |
---|
[2328] | 1099 | |
---|
| 1100 | /// Get the lower bound of a column (i.e a variable) |
---|
| 1101 | |
---|
| 1102 | /// This function returns the lower bound for column (variable) \t c |
---|
| 1103 | /// (this might be -\ref INF as well). |
---|
| 1104 | ///\return The lower bound for coloumn \t c |
---|
[2366] | 1105 | Value colLowerBound(Col c) const { |
---|
[2328] | 1106 | return _getColLowerBound(_lpId(c)); |
---|
| 1107 | } |
---|
[1895] | 1108 | |
---|
| 1109 | ///\brief Set the lower bound of several columns |
---|
| 1110 | ///(i.e a variables) at once |
---|
| 1111 | /// |
---|
| 1112 | ///This magic function takes a container as its argument |
---|
| 1113 | ///and applies the function on all of its elements. |
---|
| 1114 | /// The lower bound of a variable (column) has to be given by an |
---|
| 1115 | /// extended number of type Value, i.e. a finite number of type |
---|
| 1116 | /// Value or -\ref INF. |
---|
| 1117 | #ifdef DOXYGEN |
---|
| 1118 | template<class T> |
---|
| 1119 | void colLowerBound(T &t, Value value) { return 0;} |
---|
| 1120 | #else |
---|
| 1121 | template<class T> |
---|
| 1122 | typename enable_if<typename T::value_type::LpSolverCol,void>::type |
---|
| 1123 | colLowerBound(T &t, Value value,dummy<0> = 0) { |
---|
| 1124 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 1125 | colLowerBound(*i, value); |
---|
| 1126 | } |
---|
| 1127 | } |
---|
| 1128 | template<class T> |
---|
| 1129 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
| 1130 | void>::type |
---|
| 1131 | colLowerBound(T &t, Value value,dummy<1> = 1) { |
---|
| 1132 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 1133 | colLowerBound(i->second, value); |
---|
| 1134 | } |
---|
| 1135 | } |
---|
| 1136 | template<class T> |
---|
| 1137 | typename enable_if<typename T::MapIt::Value::LpSolverCol, |
---|
| 1138 | void>::type |
---|
| 1139 | colLowerBound(T &t, Value value,dummy<2> = 2) { |
---|
| 1140 | for(typename T::MapIt i(t); i!=INVALID; ++i){ |
---|
| 1141 | colLowerBound(*i, value); |
---|
| 1142 | } |
---|
| 1143 | } |
---|
| 1144 | #endif |
---|
| 1145 | |
---|
[1253] | 1146 | /// Set the upper bound of a column (i.e a variable) |
---|
| 1147 | |
---|
[1293] | 1148 | /// The upper bound of a variable (column) has to be given by an |
---|
[1253] | 1149 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 1150 | /// Value or \ref INF. |
---|
[1293] | 1151 | void colUpperBound(Col c, Value value) { |
---|
[2312] | 1152 | _setColUpperBound(_lpId(c),value); |
---|
[1253] | 1153 | }; |
---|
[1895] | 1154 | |
---|
[2328] | 1155 | /// Get the upper bound of a column (i.e a variable) |
---|
| 1156 | |
---|
| 1157 | /// This function returns the upper bound for column (variable) \t c |
---|
| 1158 | /// (this might be \ref INF as well). |
---|
| 1159 | ///\return The upper bound for coloumn \t c |
---|
[2366] | 1160 | Value colUpperBound(Col c) const { |
---|
[2328] | 1161 | return _getColUpperBound(_lpId(c)); |
---|
| 1162 | } |
---|
| 1163 | |
---|
| 1164 | ///\brief Set the upper bound of several columns |
---|
[1895] | 1165 | ///(i.e a variables) at once |
---|
| 1166 | /// |
---|
| 1167 | ///This magic function takes a container as its argument |
---|
| 1168 | ///and applies the function on all of its elements. |
---|
| 1169 | /// The upper bound of a variable (column) has to be given by an |
---|
| 1170 | /// extended number of type Value, i.e. a finite number of type |
---|
| 1171 | /// Value or \ref INF. |
---|
| 1172 | #ifdef DOXYGEN |
---|
| 1173 | template<class T> |
---|
| 1174 | void colUpperBound(T &t, Value value) { return 0;} |
---|
| 1175 | #else |
---|
| 1176 | template<class T> |
---|
| 1177 | typename enable_if<typename T::value_type::LpSolverCol,void>::type |
---|
| 1178 | colUpperBound(T &t, Value value,dummy<0> = 0) { |
---|
| 1179 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 1180 | colUpperBound(*i, value); |
---|
| 1181 | } |
---|
| 1182 | } |
---|
| 1183 | template<class T> |
---|
| 1184 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
| 1185 | void>::type |
---|
| 1186 | colUpperBound(T &t, Value value,dummy<1> = 1) { |
---|
| 1187 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 1188 | colUpperBound(i->second, value); |
---|
| 1189 | } |
---|
| 1190 | } |
---|
| 1191 | template<class T> |
---|
| 1192 | typename enable_if<typename T::MapIt::Value::LpSolverCol, |
---|
| 1193 | void>::type |
---|
| 1194 | colUpperBound(T &t, Value value,dummy<2> = 2) { |
---|
| 1195 | for(typename T::MapIt i(t); i!=INVALID; ++i){ |
---|
| 1196 | colUpperBound(*i, value); |
---|
| 1197 | } |
---|
| 1198 | } |
---|
| 1199 | #endif |
---|
| 1200 | |
---|
[1293] | 1201 | /// Set the lower and the upper bounds of a column (i.e a variable) |
---|
| 1202 | |
---|
| 1203 | /// The lower and the upper bounds of |
---|
| 1204 | /// a variable (column) have to be given by an |
---|
| 1205 | /// extended number of type Value, i.e. a finite number of type |
---|
| 1206 | /// Value, -\ref INF or \ref INF. |
---|
| 1207 | void colBounds(Col c, Value lower, Value upper) { |
---|
[2312] | 1208 | _setColLowerBound(_lpId(c),lower); |
---|
| 1209 | _setColUpperBound(_lpId(c),upper); |
---|
[1293] | 1210 | } |
---|
| 1211 | |
---|
[1895] | 1212 | ///\brief Set the lower and the upper bound of several columns |
---|
| 1213 | ///(i.e a variables) at once |
---|
| 1214 | /// |
---|
| 1215 | ///This magic function takes a container as its argument |
---|
| 1216 | ///and applies the function on all of its elements. |
---|
| 1217 | /// The lower and the upper bounds of |
---|
| 1218 | /// a variable (column) have to be given by an |
---|
| 1219 | /// extended number of type Value, i.e. a finite number of type |
---|
| 1220 | /// Value, -\ref INF or \ref INF. |
---|
| 1221 | #ifdef DOXYGEN |
---|
| 1222 | template<class T> |
---|
| 1223 | void colBounds(T &t, Value lower, Value upper) { return 0;} |
---|
| 1224 | #else |
---|
| 1225 | template<class T> |
---|
| 1226 | typename enable_if<typename T::value_type::LpSolverCol,void>::type |
---|
| 1227 | colBounds(T &t, Value lower, Value upper,dummy<0> = 0) { |
---|
| 1228 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 1229 | colBounds(*i, lower, upper); |
---|
| 1230 | } |
---|
| 1231 | } |
---|
| 1232 | template<class T> |
---|
| 1233 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
| 1234 | void>::type |
---|
| 1235 | colBounds(T &t, Value lower, Value upper,dummy<1> = 1) { |
---|
| 1236 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 1237 | colBounds(i->second, lower, upper); |
---|
| 1238 | } |
---|
| 1239 | } |
---|
| 1240 | template<class T> |
---|
| 1241 | typename enable_if<typename T::MapIt::Value::LpSolverCol, |
---|
| 1242 | void>::type |
---|
| 1243 | colBounds(T &t, Value lower, Value upper,dummy<2> = 2) { |
---|
| 1244 | for(typename T::MapIt i(t); i!=INVALID; ++i){ |
---|
| 1245 | colBounds(*i, lower, upper); |
---|
| 1246 | } |
---|
| 1247 | } |
---|
| 1248 | #endif |
---|
| 1249 | |
---|
[1405] | 1250 | |
---|
| 1251 | /// Set the lower and the upper bounds of a row (i.e a constraint) |
---|
[1293] | 1252 | |
---|
[2363] | 1253 | /// The lower and the upper bound of a constraint (row) have to be |
---|
| 1254 | /// given by an extended number of type Value, i.e. a finite |
---|
| 1255 | /// number of type Value, -\ref INF or \ref INF. There is no |
---|
| 1256 | /// separate function for the lower and the upper bound because |
---|
| 1257 | /// that would have been hard to implement for CPLEX. |
---|
[1293] | 1258 | void rowBounds(Row c, Value lower, Value upper) { |
---|
[2312] | 1259 | _setRowBounds(_lpId(c),lower, upper); |
---|
[1293] | 1260 | } |
---|
| 1261 | |
---|
[2328] | 1262 | /// Get the lower and the upper bounds of a row (i.e a constraint) |
---|
| 1263 | |
---|
| 1264 | /// The lower and the upper bound of |
---|
| 1265 | /// a constraint (row) are |
---|
| 1266 | /// extended numbers of type Value, i.e. finite numbers of type |
---|
| 1267 | /// Value, -\ref INF or \ref INF. |
---|
| 1268 | /// \todo There is no separate function for the |
---|
| 1269 | /// lower and the upper bound because we had problems with the |
---|
| 1270 | /// implementation of the setting functions for CPLEX: |
---|
| 1271 | /// check out whether this can be done for these functions. |
---|
[2366] | 1272 | void getRowBounds(Row c, Value &lower, Value &upper) const { |
---|
[2328] | 1273 | _getRowBounds(_lpId(c),lower, upper); |
---|
| 1274 | } |
---|
| 1275 | |
---|
[1253] | 1276 | ///Set an element of the objective function |
---|
[2312] | 1277 | void objCoeff(Col c, Value v) {_setObjCoeff(_lpId(c),v); }; |
---|
[2324] | 1278 | |
---|
| 1279 | ///Get an element of the objective function |
---|
[2366] | 1280 | Value objCoeff(Col c) const { return _getObjCoeff(_lpId(c)); }; |
---|
[2324] | 1281 | |
---|
[1253] | 1282 | ///Set the objective function |
---|
[2324] | 1283 | |
---|
[1253] | 1284 | ///\param e is a linear expression of type \ref Expr. |
---|
[2369] | 1285 | void obj(Expr e) { |
---|
[1377] | 1286 | _clearObj(); |
---|
[1253] | 1287 | for (Expr::iterator i=e.begin(); i!=e.end(); ++i) |
---|
[1293] | 1288 | objCoeff((*i).first,(*i).second); |
---|
[1323] | 1289 | obj_const_comp=e.constComp(); |
---|
[1253] | 1290 | } |
---|
[1263] | 1291 | |
---|
[2364] | 1292 | ///Get the objective function |
---|
| 1293 | |
---|
| 1294 | ///\return the objective function as a linear expression of type \ref Expr. |
---|
[2366] | 1295 | Expr obj() const { |
---|
[2364] | 1296 | Expr e; |
---|
| 1297 | for (ColIt it(*this); it != INVALID; ++it) { |
---|
| 1298 | double c = objCoeff(it); |
---|
| 1299 | if (c != 0.0) { |
---|
| 1300 | e.insert(std::make_pair(it, c)); |
---|
| 1301 | } |
---|
| 1302 | } |
---|
| 1303 | return e; |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | |
---|
[1312] | 1307 | ///Maximize |
---|
| 1308 | void max() { _setMax(); } |
---|
| 1309 | ///Minimize |
---|
| 1310 | void min() { _setMin(); } |
---|
| 1311 | |
---|
[2324] | 1312 | ///Query function: is this a maximization problem? |
---|
[2369] | 1313 | bool isMax() const {return _isMax(); } |
---|
[2324] | 1314 | |
---|
| 1315 | ///Query function: is this a minimization problem? |
---|
[2369] | 1316 | bool isMin() const {return !isMax(); } |
---|
[1312] | 1317 | |
---|
[1263] | 1318 | ///@} |
---|
| 1319 | |
---|
| 1320 | |
---|
[1294] | 1321 | ///\name Solve the LP |
---|
[1263] | 1322 | |
---|
| 1323 | ///@{ |
---|
| 1324 | |
---|
[1458] | 1325 | ///\e Solve the LP problem at hand |
---|
| 1326 | /// |
---|
[2026] | 1327 | ///\return The result of the optimization procedure. Possible |
---|
| 1328 | ///values and their meanings can be found in the documentation of |
---|
| 1329 | ///\ref SolveExitStatus. |
---|
[1458] | 1330 | /// |
---|
| 1331 | ///\todo Which method is used to solve the problem |
---|
[1303] | 1332 | SolveExitStatus solve() { return _solve(); } |
---|
[1263] | 1333 | |
---|
| 1334 | ///@} |
---|
| 1335 | |
---|
[1294] | 1336 | ///\name Obtain the solution |
---|
[1263] | 1337 | |
---|
| 1338 | ///@{ |
---|
| 1339 | |
---|
[1460] | 1340 | /// The status of the primal problem (the original LP problem) |
---|
[2366] | 1341 | SolutionStatus primalStatus() const { |
---|
[1312] | 1342 | return _getPrimalStatus(); |
---|
[1294] | 1343 | } |
---|
| 1344 | |
---|
[1460] | 1345 | /// The status of the dual (of the original LP) problem |
---|
[2366] | 1346 | SolutionStatus dualStatus() const { |
---|
[1460] | 1347 | return _getDualStatus(); |
---|
| 1348 | } |
---|
| 1349 | |
---|
| 1350 | ///The type of the original LP problem |
---|
[2366] | 1351 | ProblemTypes problemType() const { |
---|
[1460] | 1352 | return _getProblemType(); |
---|
| 1353 | } |
---|
| 1354 | |
---|
[1294] | 1355 | ///\e |
---|
[2366] | 1356 | Value primal(Col c) const { return _getPrimal(_lpId(c)); } |
---|
[1263] | 1357 | |
---|
[1312] | 1358 | ///\e |
---|
[2366] | 1359 | Value dual(Row r) const { return _getDual(_lpId(r)); } |
---|
[1787] | 1360 | |
---|
| 1361 | ///\e |
---|
[2366] | 1362 | bool isBasicCol(Col c) const { return _isBasicCol(_lpId(c)); } |
---|
[1840] | 1363 | |
---|
| 1364 | ///\e |
---|
[1312] | 1365 | |
---|
| 1366 | ///\return |
---|
| 1367 | ///- \ref INF or -\ref INF means either infeasibility or unboundedness |
---|
| 1368 | /// of the primal problem, depending on whether we minimize or maximize. |
---|
[1364] | 1369 | ///- \ref NaN if no primal solution is found. |
---|
[1312] | 1370 | ///- The (finite) objective value if an optimal solution is found. |
---|
[2366] | 1371 | Value primalValue() const { return _getPrimalValue()+obj_const_comp;} |
---|
[1263] | 1372 | ///@} |
---|
[1253] | 1373 | |
---|
[1248] | 1374 | }; |
---|
[1246] | 1375 | |
---|
[2144] | 1376 | |
---|
[2370] | 1377 | /// \ingroup lp_group |
---|
| 1378 | /// |
---|
| 1379 | /// \brief Common base class for MIP solvers |
---|
| 1380 | /// \todo Much more docs |
---|
[2144] | 1381 | class MipSolverBase : virtual public LpSolverBase{ |
---|
| 1382 | public: |
---|
| 1383 | |
---|
[2148] | 1384 | ///Possible variable (coloumn) types (e.g. real, integer, binary etc.) |
---|
| 1385 | enum ColTypes { |
---|
| 1386 | ///Continuous variable |
---|
| 1387 | REAL = 0, |
---|
| 1388 | ///Integer variable |
---|
[2218] | 1389 | |
---|
| 1390 | ///Unfortunately, cplex 7.5 somewhere writes something like |
---|
| 1391 | ///#define INTEGER 'I' |
---|
[2267] | 1392 | INT = 1 |
---|
[2148] | 1393 | ///\todo No support for other types yet. |
---|
| 1394 | }; |
---|
| 1395 | |
---|
| 1396 | ///Sets the type of the given coloumn to the given type |
---|
[2144] | 1397 | /// |
---|
[2148] | 1398 | ///Sets the type of the given coloumn to the given type. |
---|
| 1399 | void colType(Col c, ColTypes col_type) { |
---|
[2312] | 1400 | _colType(_lpId(c),col_type); |
---|
[2144] | 1401 | } |
---|
| 1402 | |
---|
| 1403 | ///Gives back the type of the column. |
---|
| 1404 | /// |
---|
| 1405 | ///Gives back the type of the column. |
---|
[2366] | 1406 | ColTypes colType(Col c) const { |
---|
[2312] | 1407 | return _colType(_lpId(c)); |
---|
[2148] | 1408 | } |
---|
| 1409 | |
---|
| 1410 | ///Sets the type of the given Col to integer or remove that property. |
---|
| 1411 | /// |
---|
| 1412 | ///Sets the type of the given Col to integer or remove that property. |
---|
| 1413 | void integer(Col c, bool enable) { |
---|
| 1414 | if (enable) |
---|
[2267] | 1415 | colType(c,INT); |
---|
[2148] | 1416 | else |
---|
| 1417 | colType(c,REAL); |
---|
| 1418 | } |
---|
| 1419 | |
---|
| 1420 | ///Gives back whether the type of the column is integer or not. |
---|
| 1421 | /// |
---|
| 1422 | ///Gives back the type of the column. |
---|
[2144] | 1423 | ///\return true if the column has integer type and false if not. |
---|
[2366] | 1424 | bool integer(Col c) const { |
---|
[2267] | 1425 | return (colType(c)==INT); |
---|
[2144] | 1426 | } |
---|
| 1427 | |
---|
[2185] | 1428 | /// The status of the MIP problem |
---|
[2366] | 1429 | SolutionStatus mipStatus() const { |
---|
[2185] | 1430 | return _getMipStatus(); |
---|
| 1431 | } |
---|
| 1432 | |
---|
[2144] | 1433 | protected: |
---|
| 1434 | |
---|
[2366] | 1435 | virtual ColTypes _colType(int col) const = 0; |
---|
[2148] | 1436 | virtual void _colType(int col, ColTypes col_type) = 0; |
---|
[2366] | 1437 | virtual SolutionStatus _getMipStatus() const = 0; |
---|
[2148] | 1438 | |
---|
[2144] | 1439 | }; |
---|
[1272] | 1440 | |
---|
| 1441 | ///\relates LpSolverBase::Expr |
---|
| 1442 | /// |
---|
| 1443 | inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, |
---|
| 1444 | const LpSolverBase::Expr &b) |
---|
| 1445 | { |
---|
| 1446 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1447 | tmp+=b; |
---|
[1272] | 1448 | return tmp; |
---|
| 1449 | } |
---|
| 1450 | ///\e |
---|
| 1451 | |
---|
| 1452 | ///\relates LpSolverBase::Expr |
---|
| 1453 | /// |
---|
| 1454 | inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, |
---|
| 1455 | const LpSolverBase::Expr &b) |
---|
| 1456 | { |
---|
| 1457 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1458 | tmp-=b; |
---|
[1272] | 1459 | return tmp; |
---|
| 1460 | } |
---|
| 1461 | ///\e |
---|
| 1462 | |
---|
| 1463 | ///\relates LpSolverBase::Expr |
---|
| 1464 | /// |
---|
| 1465 | inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, |
---|
[1273] | 1466 | const LpSolverBase::Value &b) |
---|
[1272] | 1467 | { |
---|
| 1468 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1469 | tmp*=b; |
---|
[1272] | 1470 | return tmp; |
---|
| 1471 | } |
---|
| 1472 | |
---|
| 1473 | ///\e |
---|
| 1474 | |
---|
| 1475 | ///\relates LpSolverBase::Expr |
---|
| 1476 | /// |
---|
[1273] | 1477 | inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, |
---|
[1272] | 1478 | const LpSolverBase::Expr &b) |
---|
| 1479 | { |
---|
| 1480 | LpSolverBase::Expr tmp(b); |
---|
[1766] | 1481 | tmp*=a; |
---|
[1272] | 1482 | return tmp; |
---|
| 1483 | } |
---|
| 1484 | ///\e |
---|
| 1485 | |
---|
| 1486 | ///\relates LpSolverBase::Expr |
---|
| 1487 | /// |
---|
| 1488 | inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, |
---|
[1273] | 1489 | const LpSolverBase::Value &b) |
---|
[1272] | 1490 | { |
---|
| 1491 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1492 | tmp/=b; |
---|
[1272] | 1493 | return tmp; |
---|
| 1494 | } |
---|
| 1495 | |
---|
| 1496 | ///\e |
---|
| 1497 | |
---|
| 1498 | ///\relates LpSolverBase::Constr |
---|
| 1499 | /// |
---|
| 1500 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
| 1501 | const LpSolverBase::Expr &f) |
---|
| 1502 | { |
---|
| 1503 | return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); |
---|
| 1504 | } |
---|
| 1505 | |
---|
| 1506 | ///\e |
---|
| 1507 | |
---|
| 1508 | ///\relates LpSolverBase::Constr |
---|
| 1509 | /// |
---|
[1273] | 1510 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, |
---|
[1272] | 1511 | const LpSolverBase::Expr &f) |
---|
| 1512 | { |
---|
| 1513 | return LpSolverBase::Constr(e,f); |
---|
| 1514 | } |
---|
| 1515 | |
---|
| 1516 | ///\e |
---|
| 1517 | |
---|
| 1518 | ///\relates LpSolverBase::Constr |
---|
| 1519 | /// |
---|
| 1520 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
[1273] | 1521 | const LpSolverBase::Value &f) |
---|
[1272] | 1522 | { |
---|
| 1523 | return LpSolverBase::Constr(e,f); |
---|
| 1524 | } |
---|
| 1525 | |
---|
| 1526 | ///\e |
---|
| 1527 | |
---|
| 1528 | ///\relates LpSolverBase::Constr |
---|
| 1529 | /// |
---|
| 1530 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
| 1531 | const LpSolverBase::Expr &f) |
---|
| 1532 | { |
---|
| 1533 | return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); |
---|
| 1534 | } |
---|
| 1535 | |
---|
| 1536 | |
---|
| 1537 | ///\e |
---|
| 1538 | |
---|
| 1539 | ///\relates LpSolverBase::Constr |
---|
| 1540 | /// |
---|
[1273] | 1541 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, |
---|
[1272] | 1542 | const LpSolverBase::Expr &f) |
---|
| 1543 | { |
---|
| 1544 | return LpSolverBase::Constr(f,e); |
---|
| 1545 | } |
---|
| 1546 | |
---|
| 1547 | |
---|
| 1548 | ///\e |
---|
| 1549 | |
---|
| 1550 | ///\relates LpSolverBase::Constr |
---|
| 1551 | /// |
---|
| 1552 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
[1273] | 1553 | const LpSolverBase::Value &f) |
---|
[1272] | 1554 | { |
---|
| 1555 | return LpSolverBase::Constr(f,e); |
---|
| 1556 | } |
---|
| 1557 | |
---|
| 1558 | ///\e |
---|
[2345] | 1559 | |
---|
| 1560 | ///\relates LpSolverBase::Constr |
---|
| 1561 | /// |
---|
| 1562 | inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, |
---|
| 1563 | const LpSolverBase::Value &f) |
---|
| 1564 | { |
---|
| 1565 | return LpSolverBase::Constr(f,e,f); |
---|
| 1566 | } |
---|
| 1567 | |
---|
| 1568 | ///\e |
---|
[1272] | 1569 | |
---|
| 1570 | ///\relates LpSolverBase::Constr |
---|
| 1571 | /// |
---|
| 1572 | inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, |
---|
| 1573 | const LpSolverBase::Expr &f) |
---|
| 1574 | { |
---|
| 1575 | return LpSolverBase::Constr(0,e-f,0); |
---|
| 1576 | } |
---|
| 1577 | |
---|
| 1578 | ///\e |
---|
| 1579 | |
---|
| 1580 | ///\relates LpSolverBase::Constr |
---|
| 1581 | /// |
---|
[1273] | 1582 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, |
---|
[1272] | 1583 | const LpSolverBase::Constr&c) |
---|
| 1584 | { |
---|
| 1585 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1586 | ///\todo Create an own exception type. |
---|
[2026] | 1587 | if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError(); |
---|
[1273] | 1588 | else tmp.lowerBound()=n; |
---|
[1272] | 1589 | return tmp; |
---|
| 1590 | } |
---|
| 1591 | ///\e |
---|
| 1592 | |
---|
| 1593 | ///\relates LpSolverBase::Constr |
---|
| 1594 | /// |
---|
| 1595 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, |
---|
[1273] | 1596 | const LpSolverBase::Value &n) |
---|
[1272] | 1597 | { |
---|
| 1598 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1599 | ///\todo Create an own exception type. |
---|
[2026] | 1600 | if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError(); |
---|
[1273] | 1601 | else tmp.upperBound()=n; |
---|
[1272] | 1602 | return tmp; |
---|
| 1603 | } |
---|
| 1604 | |
---|
| 1605 | ///\e |
---|
| 1606 | |
---|
| 1607 | ///\relates LpSolverBase::Constr |
---|
| 1608 | /// |
---|
[1273] | 1609 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, |
---|
[1272] | 1610 | const LpSolverBase::Constr&c) |
---|
| 1611 | { |
---|
| 1612 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1613 | ///\todo Create an own exception type. |
---|
[2026] | 1614 | if(!LpSolverBase::isNaN(tmp.upperBound())) throw LogicError(); |
---|
[1273] | 1615 | else tmp.upperBound()=n; |
---|
[1272] | 1616 | return tmp; |
---|
| 1617 | } |
---|
| 1618 | ///\e |
---|
| 1619 | |
---|
| 1620 | ///\relates LpSolverBase::Constr |
---|
| 1621 | /// |
---|
| 1622 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, |
---|
[1273] | 1623 | const LpSolverBase::Value &n) |
---|
[1272] | 1624 | { |
---|
| 1625 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1626 | ///\todo Create an own exception type. |
---|
[2026] | 1627 | if(!LpSolverBase::isNaN(tmp.lowerBound())) throw LogicError(); |
---|
[1273] | 1628 | else tmp.lowerBound()=n; |
---|
[1272] | 1629 | return tmp; |
---|
| 1630 | } |
---|
| 1631 | |
---|
[1445] | 1632 | ///\e |
---|
| 1633 | |
---|
| 1634 | ///\relates LpSolverBase::DualExpr |
---|
| 1635 | /// |
---|
| 1636 | inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a, |
---|
[2312] | 1637 | const LpSolverBase::DualExpr &b) |
---|
[1445] | 1638 | { |
---|
| 1639 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1640 | tmp+=b; |
---|
[1445] | 1641 | return tmp; |
---|
| 1642 | } |
---|
| 1643 | ///\e |
---|
| 1644 | |
---|
| 1645 | ///\relates LpSolverBase::DualExpr |
---|
| 1646 | /// |
---|
| 1647 | inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a, |
---|
[2312] | 1648 | const LpSolverBase::DualExpr &b) |
---|
[1445] | 1649 | { |
---|
| 1650 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1651 | tmp-=b; |
---|
[1445] | 1652 | return tmp; |
---|
| 1653 | } |
---|
| 1654 | ///\e |
---|
| 1655 | |
---|
| 1656 | ///\relates LpSolverBase::DualExpr |
---|
| 1657 | /// |
---|
| 1658 | inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a, |
---|
[2312] | 1659 | const LpSolverBase::Value &b) |
---|
[1445] | 1660 | { |
---|
| 1661 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1662 | tmp*=b; |
---|
[1445] | 1663 | return tmp; |
---|
| 1664 | } |
---|
| 1665 | |
---|
| 1666 | ///\e |
---|
| 1667 | |
---|
| 1668 | ///\relates LpSolverBase::DualExpr |
---|
| 1669 | /// |
---|
| 1670 | inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a, |
---|
[2312] | 1671 | const LpSolverBase::DualExpr &b) |
---|
[1445] | 1672 | { |
---|
| 1673 | LpSolverBase::DualExpr tmp(b); |
---|
[1766] | 1674 | tmp*=a; |
---|
[1445] | 1675 | return tmp; |
---|
| 1676 | } |
---|
| 1677 | ///\e |
---|
| 1678 | |
---|
| 1679 | ///\relates LpSolverBase::DualExpr |
---|
| 1680 | /// |
---|
| 1681 | inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a, |
---|
[2312] | 1682 | const LpSolverBase::Value &b) |
---|
[1445] | 1683 | { |
---|
| 1684 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1685 | tmp/=b; |
---|
[1445] | 1686 | return tmp; |
---|
| 1687 | } |
---|
| 1688 | |
---|
[1272] | 1689 | |
---|
[1246] | 1690 | } //namespace lemon |
---|
| 1691 | |
---|
| 1692 | #endif //LEMON_LP_BASE_H |
---|