[1247] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/lp_base.h - Part of LEMON, a generic C++ optimization library |
---|
[1247] | 3 | * |
---|
| 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[1247] | 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[1246] | 17 | #ifndef LEMON_LP_BASE_H |
---|
| 18 | #define LEMON_LP_BASE_H |
---|
| 19 | |
---|
[1253] | 20 | #include<vector> |
---|
[1272] | 21 | #include<map> |
---|
[1256] | 22 | #include<limits> |
---|
[1397] | 23 | #include<cmath> |
---|
[1253] | 24 | |
---|
[1256] | 25 | #include<lemon/utility.h> |
---|
[1253] | 26 | #include<lemon/error.h> |
---|
[1256] | 27 | #include<lemon/invalid.h> |
---|
[1253] | 28 | |
---|
[1272] | 29 | //#include"lin_expr.h" |
---|
| 30 | |
---|
[1246] | 31 | ///\file |
---|
| 32 | ///\brief The interface of the LP solver interface. |
---|
[1328] | 33 | ///\ingroup gen_opt_group |
---|
[1246] | 34 | namespace lemon { |
---|
[1253] | 35 | |
---|
| 36 | ///Internal data structure to convert floating id's to fix one's |
---|
| 37 | |
---|
[1279] | 38 | ///\todo This might be implemented to be also usable in other places. |
---|
[1253] | 39 | class _FixId |
---|
| 40 | { |
---|
| 41 | std::vector<int> index; |
---|
| 42 | std::vector<int> cross; |
---|
| 43 | int first_free; |
---|
| 44 | public: |
---|
| 45 | _FixId() : first_free(-1) {}; |
---|
| 46 | ///Convert a floating id to a fix one |
---|
| 47 | |
---|
| 48 | ///\param n is a floating id |
---|
| 49 | ///\return the corresponding fix id |
---|
| 50 | int fixId(int n) {return cross[n];} |
---|
| 51 | ///Convert a fix id to a floating one |
---|
| 52 | |
---|
| 53 | ///\param n is a fix id |
---|
| 54 | ///\return the corresponding floating id |
---|
| 55 | int floatingId(int n) { return index[n];} |
---|
| 56 | ///Add a new floating id. |
---|
| 57 | |
---|
| 58 | ///\param n is a floating id |
---|
| 59 | ///\return the fix id of the new value |
---|
| 60 | ///\todo Multiple additions should also be handled. |
---|
| 61 | int insert(int n) |
---|
| 62 | { |
---|
| 63 | if(n>=int(cross.size())) { |
---|
| 64 | cross.resize(n+1); |
---|
| 65 | if(first_free==-1) { |
---|
| 66 | cross[n]=index.size(); |
---|
| 67 | index.push_back(n); |
---|
| 68 | } |
---|
| 69 | else { |
---|
| 70 | cross[n]=first_free; |
---|
| 71 | int next=index[first_free]; |
---|
| 72 | index[first_free]=n; |
---|
| 73 | first_free=next; |
---|
| 74 | } |
---|
[1256] | 75 | return cross[n]; |
---|
[1253] | 76 | } |
---|
[1273] | 77 | ///\todo Create an own exception type. |
---|
[1253] | 78 | else throw LogicError(); //floatingId-s must form a continuous range; |
---|
| 79 | } |
---|
| 80 | ///Remove a fix id. |
---|
| 81 | |
---|
| 82 | ///\param n is a fix id |
---|
| 83 | /// |
---|
| 84 | void erase(int n) |
---|
| 85 | { |
---|
| 86 | int fl=index[n]; |
---|
| 87 | index[n]=first_free; |
---|
| 88 | first_free=n; |
---|
| 89 | for(int i=fl+1;i<int(cross.size());++i) { |
---|
| 90 | cross[i-1]=cross[i]; |
---|
| 91 | index[cross[i]]--; |
---|
| 92 | } |
---|
| 93 | cross.pop_back(); |
---|
| 94 | } |
---|
| 95 | ///An upper bound on the largest fix id. |
---|
| 96 | |
---|
| 97 | ///\todo Do we need this? |
---|
| 98 | /// |
---|
| 99 | std::size_t maxFixId() { return cross.size()-1; } |
---|
| 100 | |
---|
| 101 | }; |
---|
| 102 | |
---|
| 103 | ///Common base class for LP solvers |
---|
[1328] | 104 | |
---|
| 105 | ///\todo Much more docs |
---|
| 106 | ///\ingroup gen_opt_group |
---|
[1246] | 107 | class LpSolverBase { |
---|
[1323] | 108 | |
---|
[1247] | 109 | public: |
---|
| 110 | |
---|
[1263] | 111 | ///\e |
---|
[1303] | 112 | enum SolveExitStatus { |
---|
[1263] | 113 | ///\e |
---|
[1293] | 114 | SOLVED = 0, |
---|
[1263] | 115 | ///\e |
---|
[1293] | 116 | UNSOLVED = 1 |
---|
[1291] | 117 | }; |
---|
| 118 | |
---|
| 119 | ///\e |
---|
[1303] | 120 | enum SolutionStatus { |
---|
[1295] | 121 | ///Feasible solution has'n been found (but may exist). |
---|
| 122 | |
---|
| 123 | ///\todo NOTFOUND might be a better name. |
---|
| 124 | /// |
---|
[1293] | 125 | UNDEFINED = 0, |
---|
[1295] | 126 | ///The problem has no feasible solution |
---|
[1293] | 127 | INFEASIBLE = 1, |
---|
[1295] | 128 | ///Feasible solution found |
---|
[1293] | 129 | FEASIBLE = 2, |
---|
[1295] | 130 | ///Optimal solution exists and found |
---|
| 131 | OPTIMAL = 3, |
---|
| 132 | ///The cost function is unbounded |
---|
| 133 | |
---|
| 134 | ///\todo Give a feasible solution and an infinite ray (and the |
---|
| 135 | ///corresponding bases) |
---|
| 136 | INFINITE = 4 |
---|
[1263] | 137 | }; |
---|
| 138 | |
---|
[1256] | 139 | ///The floating point type used by the solver |
---|
[1247] | 140 | typedef double Value; |
---|
[1256] | 141 | ///The infinity constant |
---|
[1247] | 142 | static const Value INF; |
---|
[1264] | 143 | ///The not a number constant |
---|
| 144 | static const Value NaN; |
---|
[1253] | 145 | |
---|
[1256] | 146 | ///Refer to a column of the LP. |
---|
| 147 | |
---|
| 148 | ///This type is used to refer to a column of the LP. |
---|
| 149 | /// |
---|
| 150 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 151 | ///other columns. |
---|
[1256] | 152 | /// |
---|
| 153 | ///\todo Document what can one do with a Col (INVALID, comparing, |
---|
| 154 | ///it is similar to Node/Edge) |
---|
| 155 | class Col { |
---|
| 156 | protected: |
---|
| 157 | int id; |
---|
| 158 | friend class LpSolverBase; |
---|
| 159 | public: |
---|
[1259] | 160 | typedef Value ExprValue; |
---|
[1256] | 161 | typedef True LpSolverCol; |
---|
| 162 | Col() {} |
---|
| 163 | Col(const Invalid&) : id(-1) {} |
---|
| 164 | bool operator<(Col c) const {return id<c.id;} |
---|
| 165 | bool operator==(Col c) const {return id==c.id;} |
---|
| 166 | bool operator!=(Col c) const {return id==c.id;} |
---|
| 167 | }; |
---|
| 168 | |
---|
| 169 | ///Refer to a row of the LP. |
---|
| 170 | |
---|
| 171 | ///This type is used to refer to a row of the LP. |
---|
| 172 | /// |
---|
| 173 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 174 | ///other rows. |
---|
[1256] | 175 | /// |
---|
| 176 | ///\todo Document what can one do with a Row (INVALID, comparing, |
---|
| 177 | ///it is similar to Node/Edge) |
---|
| 178 | class Row { |
---|
| 179 | protected: |
---|
| 180 | int id; |
---|
| 181 | friend class LpSolverBase; |
---|
| 182 | public: |
---|
[1259] | 183 | typedef Value ExprValue; |
---|
[1256] | 184 | typedef True LpSolverRow; |
---|
| 185 | Row() {} |
---|
| 186 | Row(const Invalid&) : id(-1) {} |
---|
[1439] | 187 | |
---|
[1256] | 188 | bool operator<(Row c) const {return id<c.id;} |
---|
| 189 | bool operator==(Row c) const {return id==c.id;} |
---|
| 190 | bool operator!=(Row c) const {return id==c.id;} |
---|
| 191 | }; |
---|
[1259] | 192 | |
---|
[1279] | 193 | ///Linear expression of variables and a constant component |
---|
| 194 | |
---|
| 195 | ///This data structure strores a linear expression of the variables |
---|
| 196 | ///(\ref Col "Col"s) and also has a constant component. |
---|
| 197 | /// |
---|
| 198 | ///There are several ways to access and modify the contents of this |
---|
| 199 | ///container. |
---|
| 200 | ///- Its it fully compatible with \c std::map<Col,double>, so for expamle |
---|
[1364] | 201 | ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can |
---|
[1279] | 202 | ///read and modify the coefficients like |
---|
| 203 | ///these. |
---|
| 204 | ///\code |
---|
| 205 | ///e[v]=5; |
---|
| 206 | ///e[v]+=12; |
---|
| 207 | ///e.erase(v); |
---|
| 208 | ///\endcode |
---|
| 209 | ///or you can also iterate through its elements. |
---|
| 210 | ///\code |
---|
| 211 | ///double s=0; |
---|
| 212 | ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) |
---|
| 213 | /// s+=i->second; |
---|
| 214 | ///\endcode |
---|
| 215 | ///(This code computes the sum of all coefficients). |
---|
| 216 | ///- Numbers (<tt>double</tt>'s) |
---|
| 217 | ///and variables (\ref Col "Col"s) directly convert to an |
---|
| 218 | ///\ref Expr and the usual linear operations are defined so |
---|
| 219 | ///\code |
---|
| 220 | ///v+w |
---|
| 221 | ///2*v-3.12*(v-w/2)+2 |
---|
| 222 | ///v*2.1+(3*v+(v*12+w+6)*3)/2 |
---|
| 223 | ///\endcode |
---|
[1328] | 224 | ///are valid \ref Expr "Expr"essions. |
---|
| 225 | ///The usual assignment operations are also defined. |
---|
[1279] | 226 | ///\code |
---|
| 227 | ///e=v+w; |
---|
| 228 | ///e+=2*v-3.12*(v-w/2)+2; |
---|
| 229 | ///e*=3.4; |
---|
| 230 | ///e/=5; |
---|
| 231 | ///\endcode |
---|
| 232 | ///- The constant member can be set and read by \ref constComp() |
---|
| 233 | ///\code |
---|
| 234 | ///e.constComp()=12; |
---|
| 235 | ///double c=e.constComp(); |
---|
| 236 | ///\endcode |
---|
| 237 | /// |
---|
[1328] | 238 | ///\note \ref clear() not only sets all coefficients to 0 but also |
---|
[1279] | 239 | ///clears the constant components. |
---|
[1328] | 240 | /// |
---|
| 241 | ///\sa Constr |
---|
| 242 | /// |
---|
[1273] | 243 | class Expr : public std::map<Col,Value> |
---|
[1272] | 244 | { |
---|
| 245 | public: |
---|
[1273] | 246 | typedef LpSolverBase::Col Key; |
---|
| 247 | typedef LpSolverBase::Value Value; |
---|
[1272] | 248 | |
---|
| 249 | protected: |
---|
[1273] | 250 | typedef std::map<Col,Value> Base; |
---|
[1272] | 251 | |
---|
[1273] | 252 | Value const_comp; |
---|
[1272] | 253 | public: |
---|
| 254 | typedef True IsLinExpression; |
---|
| 255 | ///\e |
---|
| 256 | Expr() : Base(), const_comp(0) { } |
---|
| 257 | ///\e |
---|
[1273] | 258 | Expr(const Key &v) : const_comp(0) { |
---|
[1272] | 259 | Base::insert(std::make_pair(v, 1)); |
---|
| 260 | } |
---|
| 261 | ///\e |
---|
[1273] | 262 | Expr(const Value &v) : const_comp(v) {} |
---|
[1272] | 263 | ///\e |
---|
[1273] | 264 | void set(const Key &v,const Value &c) { |
---|
[1272] | 265 | Base::insert(std::make_pair(v, c)); |
---|
| 266 | } |
---|
| 267 | ///\e |
---|
[1273] | 268 | Value &constComp() { return const_comp; } |
---|
[1272] | 269 | ///\e |
---|
[1273] | 270 | const Value &constComp() const { return const_comp; } |
---|
[1272] | 271 | |
---|
| 272 | ///Removes the components with zero coefficient. |
---|
| 273 | void simplify() { |
---|
| 274 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 275 | Base::iterator j=i; |
---|
| 276 | ++j; |
---|
| 277 | if ((*i).second==0) Base::erase(i); |
---|
| 278 | j=i; |
---|
| 279 | } |
---|
| 280 | } |
---|
[1273] | 281 | |
---|
| 282 | ///Sets all coefficients and the constant component to 0. |
---|
| 283 | void clear() { |
---|
| 284 | Base::clear(); |
---|
| 285 | const_comp=0; |
---|
| 286 | } |
---|
| 287 | |
---|
[1272] | 288 | ///\e |
---|
| 289 | Expr &operator+=(const Expr &e) { |
---|
| 290 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 291 | (*this)[j->first]+=j->second; |
---|
| 292 | ///\todo it might be speeded up using "hints" |
---|
| 293 | const_comp+=e.const_comp; |
---|
| 294 | return *this; |
---|
| 295 | } |
---|
| 296 | ///\e |
---|
| 297 | Expr &operator-=(const Expr &e) { |
---|
| 298 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 299 | (*this)[j->first]-=j->second; |
---|
| 300 | const_comp-=e.const_comp; |
---|
| 301 | return *this; |
---|
| 302 | } |
---|
| 303 | ///\e |
---|
[1273] | 304 | Expr &operator*=(const Value &c) { |
---|
[1272] | 305 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 306 | j->second*=c; |
---|
| 307 | const_comp*=c; |
---|
| 308 | return *this; |
---|
| 309 | } |
---|
| 310 | ///\e |
---|
[1273] | 311 | Expr &operator/=(const Value &c) { |
---|
[1272] | 312 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 313 | j->second/=c; |
---|
| 314 | const_comp/=c; |
---|
| 315 | return *this; |
---|
| 316 | } |
---|
| 317 | }; |
---|
| 318 | |
---|
[1264] | 319 | ///Linear constraint |
---|
[1328] | 320 | |
---|
[1364] | 321 | ///This data stucture represents a linear constraint in the LP. |
---|
| 322 | ///Basically it is a linear expression with a lower or an upper bound |
---|
| 323 | ///(or both). These parts of the constraint can be obtained by the member |
---|
| 324 | ///functions \ref expr(), \ref lowerBound() and \ref upperBound(), |
---|
| 325 | ///respectively. |
---|
| 326 | ///There are two ways to construct a constraint. |
---|
| 327 | ///- You can set the linear expression and the bounds directly |
---|
| 328 | /// by the functions above. |
---|
| 329 | ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt> |
---|
| 330 | /// are defined between expressions, or even between constraints whenever |
---|
| 331 | /// it makes sense. Therefore if \c e and \c f are linear expressions and |
---|
| 332 | /// \c s and \c t are numbers, then the followings are valid expressions |
---|
| 333 | /// and thus they can be used directly e.g. in \ref addRow() whenever |
---|
| 334 | /// it makes sense. |
---|
| 335 | /// \code |
---|
| 336 | /// e<=s |
---|
| 337 | /// e<=f |
---|
| 338 | /// s<=e<=t |
---|
| 339 | /// e>=t |
---|
| 340 | /// \endcode |
---|
| 341 | ///\warning The validity of a constraint is checked only at run time, so |
---|
| 342 | ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a |
---|
| 343 | ///\ref LogicError exception. |
---|
[1272] | 344 | class Constr |
---|
| 345 | { |
---|
| 346 | public: |
---|
| 347 | typedef LpSolverBase::Expr Expr; |
---|
[1273] | 348 | typedef Expr::Key Key; |
---|
| 349 | typedef Expr::Value Value; |
---|
[1272] | 350 | |
---|
[1364] | 351 | // static const Value INF; |
---|
| 352 | // static const Value NaN; |
---|
| 353 | |
---|
[1273] | 354 | protected: |
---|
| 355 | Expr _expr; |
---|
| 356 | Value _lb,_ub; |
---|
| 357 | public: |
---|
| 358 | ///\e |
---|
| 359 | Constr() : _expr(), _lb(NaN), _ub(NaN) {} |
---|
| 360 | ///\e |
---|
| 361 | Constr(Value lb,const Expr &e,Value ub) : |
---|
| 362 | _expr(e), _lb(lb), _ub(ub) {} |
---|
| 363 | ///\e |
---|
| 364 | Constr(const Expr &e,Value ub) : |
---|
| 365 | _expr(e), _lb(NaN), _ub(ub) {} |
---|
| 366 | ///\e |
---|
| 367 | Constr(Value lb,const Expr &e) : |
---|
| 368 | _expr(e), _lb(lb), _ub(NaN) {} |
---|
| 369 | ///\e |
---|
[1272] | 370 | Constr(const Expr &e) : |
---|
[1273] | 371 | _expr(e), _lb(NaN), _ub(NaN) {} |
---|
| 372 | ///\e |
---|
| 373 | void clear() |
---|
| 374 | { |
---|
| 375 | _expr.clear(); |
---|
| 376 | _lb=_ub=NaN; |
---|
| 377 | } |
---|
[1364] | 378 | |
---|
| 379 | ///Reference to the linear expression |
---|
[1273] | 380 | Expr &expr() { return _expr; } |
---|
[1364] | 381 | ///Cont reference to the linear expression |
---|
[1273] | 382 | const Expr &expr() const { return _expr; } |
---|
[1364] | 383 | ///Reference to the lower bound. |
---|
| 384 | |
---|
| 385 | ///\return |
---|
| 386 | ///- -\ref INF: the constraint is lower unbounded. |
---|
| 387 | ///- -\ref NaN: lower bound has not been set. |
---|
| 388 | ///- finite number: the lower bound |
---|
[1273] | 389 | Value &lowerBound() { return _lb; } |
---|
[1364] | 390 | ///The const version of \ref lowerBound() |
---|
[1273] | 391 | const Value &lowerBound() const { return _lb; } |
---|
[1364] | 392 | ///Reference to the upper bound. |
---|
| 393 | |
---|
| 394 | ///\return |
---|
| 395 | ///- -\ref INF: the constraint is upper unbounded. |
---|
| 396 | ///- -\ref NaN: upper bound has not been set. |
---|
| 397 | ///- finite number: the upper bound |
---|
[1273] | 398 | Value &upperBound() { return _ub; } |
---|
[1364] | 399 | ///The const version of \ref upperBound() |
---|
[1273] | 400 | const Value &upperBound() const { return _ub; } |
---|
[1364] | 401 | ///Is the constraint lower bounded? |
---|
[1295] | 402 | bool lowerBounded() const { |
---|
| 403 | using namespace std; |
---|
[1397] | 404 | return finite(_lb); |
---|
[1295] | 405 | } |
---|
[1364] | 406 | ///Is the constraint upper bounded? |
---|
[1295] | 407 | bool upperBounded() const { |
---|
| 408 | using namespace std; |
---|
[1397] | 409 | return finite(_ub); |
---|
[1295] | 410 | } |
---|
[1272] | 411 | }; |
---|
| 412 | |
---|
[1253] | 413 | |
---|
| 414 | protected: |
---|
| 415 | _FixId rows; |
---|
| 416 | _FixId cols; |
---|
[1246] | 417 | |
---|
[1323] | 418 | //Abstract virtual functions |
---|
[1364] | 419 | virtual LpSolverBase &_newLp() = 0; |
---|
[1436] | 420 | virtual LpSolverBase &_copyLp(){ |
---|
| 421 | ///\todo This should be implemented here, too, when we have problem retrieving routines. It can be overriden. |
---|
| 422 | |
---|
| 423 | //Starting: |
---|
| 424 | LpSolverBase & newlp(_newLp()); |
---|
| 425 | return newlp; |
---|
| 426 | //return *(LpSolverBase*)0; |
---|
| 427 | }; |
---|
[1364] | 428 | |
---|
[1246] | 429 | virtual int _addCol() = 0; |
---|
| 430 | virtual int _addRow() = 0; |
---|
| 431 | virtual void _setRowCoeffs(int i, |
---|
[1251] | 432 | int length, |
---|
[1247] | 433 | int const * indices, |
---|
| 434 | Value const * values ) = 0; |
---|
[1246] | 435 | virtual void _setColCoeffs(int i, |
---|
[1251] | 436 | int length, |
---|
[1247] | 437 | int const * indices, |
---|
| 438 | Value const * values ) = 0; |
---|
[1431] | 439 | virtual void _setCoeff(int row, int col, Value value) = 0; |
---|
[1294] | 440 | virtual void _setColLowerBound(int i, Value value) = 0; |
---|
| 441 | virtual void _setColUpperBound(int i, Value value) = 0; |
---|
[1405] | 442 | // virtual void _setRowLowerBound(int i, Value value) = 0; |
---|
| 443 | // virtual void _setRowUpperBound(int i, Value value) = 0; |
---|
[1379] | 444 | virtual void _setRowBounds(int i, Value lower, Value upper) = 0; |
---|
[1294] | 445 | virtual void _setObjCoeff(int i, Value obj_coef) = 0; |
---|
[1377] | 446 | virtual void _clearObj()=0; |
---|
| 447 | // virtual void _setObj(int length, |
---|
| 448 | // int const * indices, |
---|
| 449 | // Value const * values ) = 0; |
---|
[1303] | 450 | virtual SolveExitStatus _solve() = 0; |
---|
[1294] | 451 | virtual Value _getPrimal(int i) = 0; |
---|
[1312] | 452 | virtual Value _getPrimalValue() = 0; |
---|
| 453 | virtual SolutionStatus _getPrimalStatus() = 0; |
---|
| 454 | virtual void _setMax() = 0; |
---|
| 455 | virtual void _setMin() = 0; |
---|
| 456 | |
---|
[1323] | 457 | //Own protected stuff |
---|
| 458 | |
---|
| 459 | //Constant component of the objective function |
---|
| 460 | Value obj_const_comp; |
---|
| 461 | |
---|
[1377] | 462 | |
---|
| 463 | |
---|
[1323] | 464 | |
---|
[1253] | 465 | public: |
---|
| 466 | |
---|
[1323] | 467 | ///\e |
---|
| 468 | LpSolverBase() : obj_const_comp(0) {} |
---|
[1253] | 469 | |
---|
| 470 | ///\e |
---|
| 471 | virtual ~LpSolverBase() {} |
---|
| 472 | |
---|
[1364] | 473 | ///Creates a new LP problem |
---|
| 474 | LpSolverBase &newLp() {return _newLp();} |
---|
[1381] | 475 | ///Makes a copy of the LP problem |
---|
[1364] | 476 | LpSolverBase ©Lp() {return _copyLp();} |
---|
| 477 | |
---|
[1294] | 478 | ///\name Build up and modify of the LP |
---|
[1263] | 479 | |
---|
| 480 | ///@{ |
---|
| 481 | |
---|
[1253] | 482 | ///Add a new empty column (i.e a new variable) to the LP |
---|
| 483 | Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} |
---|
[1263] | 484 | |
---|
[1294] | 485 | ///\brief Adds several new columns |
---|
| 486 | ///(i.e a variables) at once |
---|
[1256] | 487 | /// |
---|
[1273] | 488 | ///This magic function takes a container as its argument |
---|
[1256] | 489 | ///and fills its elements |
---|
| 490 | ///with new columns (i.e. variables) |
---|
[1273] | 491 | ///\param t can be |
---|
| 492 | ///- a standard STL compatible iterable container with |
---|
| 493 | ///\ref Col as its \c values_type |
---|
| 494 | ///like |
---|
| 495 | ///\code |
---|
| 496 | ///std::vector<LpSolverBase::Col> |
---|
| 497 | ///std::list<LpSolverBase::Col> |
---|
| 498 | ///\endcode |
---|
| 499 | ///- a standard STL compatible iterable container with |
---|
| 500 | ///\ref Col as its \c mapped_type |
---|
| 501 | ///like |
---|
| 502 | ///\code |
---|
[1364] | 503 | ///std::map<AnyType,LpSolverBase::Col> |
---|
[1273] | 504 | ///\endcode |
---|
| 505 | ///- an iterable lemon \ref concept::WriteMap "write map" like |
---|
| 506 | ///\code |
---|
| 507 | ///ListGraph::NodeMap<LpSolverBase::Col> |
---|
| 508 | ///ListGraph::EdgeMap<LpSolverBase::Col> |
---|
| 509 | ///\endcode |
---|
[1256] | 510 | ///\return The number of the created column. |
---|
| 511 | #ifdef DOXYGEN |
---|
| 512 | template<class T> |
---|
| 513 | int addColSet(T &t) { return 0;} |
---|
| 514 | #else |
---|
| 515 | template<class T> |
---|
| 516 | typename enable_if<typename T::value_type::LpSolverCol,int>::type |
---|
| 517 | addColSet(T &t,dummy<0> = 0) { |
---|
| 518 | int s=0; |
---|
| 519 | for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} |
---|
| 520 | return s; |
---|
| 521 | } |
---|
| 522 | template<class T> |
---|
| 523 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
| 524 | int>::type |
---|
| 525 | addColSet(T &t,dummy<1> = 1) { |
---|
| 526 | int s=0; |
---|
| 527 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 528 | i->second=addCol(); |
---|
| 529 | s++; |
---|
| 530 | } |
---|
| 531 | return s; |
---|
| 532 | } |
---|
[1272] | 533 | template<class T> |
---|
| 534 | typename enable_if<typename T::ValueSet::value_type::LpSolverCol, |
---|
| 535 | int>::type |
---|
| 536 | addColSet(T &t,dummy<2> = 2) { |
---|
| 537 | ///\bug <tt>return addColSet(t.valueSet());</tt> should also work. |
---|
| 538 | int s=0; |
---|
| 539 | for(typename T::ValueSet::iterator i=t.valueSet().begin(); |
---|
| 540 | i!=t.valueSet().end(); |
---|
| 541 | ++i) |
---|
| 542 | { |
---|
| 543 | *i=addCol(); |
---|
| 544 | s++; |
---|
| 545 | } |
---|
| 546 | return s; |
---|
| 547 | } |
---|
[1256] | 548 | #endif |
---|
[1263] | 549 | |
---|
[1253] | 550 | ///Add a new empty row (i.e a new constaint) to the LP |
---|
[1258] | 551 | |
---|
| 552 | ///This function adds a new empty row (i.e a new constaint) to the LP. |
---|
| 553 | ///\return The created row |
---|
[1253] | 554 | Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} |
---|
| 555 | |
---|
[1258] | 556 | ///Set a row (i.e a constaint) of the LP |
---|
[1253] | 557 | |
---|
[1258] | 558 | ///\param r is the row to be modified |
---|
[1259] | 559 | ///\param l is lower bound (-\ref INF means no bound) |
---|
[1258] | 560 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 561 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[1253] | 562 | ///\bug This is a temportary function. The interface will change to |
---|
| 563 | ///a better one. |
---|
[1328] | 564 | ///\todo Option to control whether a constraint with a single variable is |
---|
| 565 | ///added or not. |
---|
[1258] | 566 | void setRow(Row r, Value l,const Expr &e, Value u) { |
---|
[1253] | 567 | std::vector<int> indices; |
---|
| 568 | std::vector<Value> values; |
---|
| 569 | indices.push_back(0); |
---|
| 570 | values.push_back(0); |
---|
[1258] | 571 | for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i) |
---|
[1256] | 572 | if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! |
---|
| 573 | indices.push_back(cols.floatingId((*i).first.id)); |
---|
| 574 | values.push_back((*i).second); |
---|
| 575 | } |
---|
[1253] | 576 | _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, |
---|
| 577 | &indices[0],&values[0]); |
---|
[1405] | 578 | // _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); |
---|
| 579 | // _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); |
---|
| 580 | _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp()); |
---|
[1258] | 581 | } |
---|
| 582 | |
---|
[1264] | 583 | ///Set a row (i.e a constaint) of the LP |
---|
| 584 | |
---|
| 585 | ///\param r is the row to be modified |
---|
| 586 | ///\param c is a linear expression (see \ref Constr) |
---|
| 587 | void setRow(Row r, const Constr &c) { |
---|
[1273] | 588 | setRow(r, |
---|
[1275] | 589 | c.lowerBounded()?c.lowerBound():-INF, |
---|
[1273] | 590 | c.expr(), |
---|
[1275] | 591 | c.upperBounded()?c.upperBound():INF); |
---|
[1264] | 592 | } |
---|
| 593 | |
---|
[1258] | 594 | ///Add a new row (i.e a new constaint) to the LP |
---|
| 595 | |
---|
[1259] | 596 | ///\param l is the lower bound (-\ref INF means no bound) |
---|
[1258] | 597 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 598 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[1258] | 599 | ///\return The created row. |
---|
| 600 | ///\bug This is a temportary function. The interface will change to |
---|
| 601 | ///a better one. |
---|
| 602 | Row addRow(Value l,const Expr &e, Value u) { |
---|
| 603 | Row r=addRow(); |
---|
| 604 | setRow(r,l,e,u); |
---|
[1253] | 605 | return r; |
---|
| 606 | } |
---|
| 607 | |
---|
[1264] | 608 | ///Add a new row (i.e a new constaint) to the LP |
---|
| 609 | |
---|
| 610 | ///\param c is a linear expression (see \ref Constr) |
---|
| 611 | ///\return The created row. |
---|
| 612 | Row addRow(const Constr &c) { |
---|
| 613 | Row r=addRow(); |
---|
| 614 | setRow(r,c); |
---|
| 615 | return r; |
---|
| 616 | } |
---|
| 617 | |
---|
[1436] | 618 | ///Set an element of the coefficient matrix of the LP |
---|
| 619 | |
---|
| 620 | ///\param r is the row of the element to be modified |
---|
| 621 | ///\param c is the coloumn of the element to be modified |
---|
| 622 | ///\param val is the new value of the coefficient |
---|
| 623 | void setCoeff(Row r, Col c, Value val){ |
---|
| 624 | _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val); |
---|
| 625 | } |
---|
| 626 | |
---|
[1253] | 627 | /// Set the lower bound of a column (i.e a variable) |
---|
| 628 | |
---|
[1293] | 629 | /// The upper bound of a variable (column) has to be given by an |
---|
[1253] | 630 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 631 | /// Value or -\ref INF. |
---|
[1293] | 632 | void colLowerBound(Col c, Value value) { |
---|
[1253] | 633 | _setColLowerBound(cols.floatingId(c.id),value); |
---|
| 634 | } |
---|
| 635 | /// Set the upper bound of a column (i.e a variable) |
---|
| 636 | |
---|
[1293] | 637 | /// The upper bound of a variable (column) has to be given by an |
---|
[1253] | 638 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 639 | /// Value or \ref INF. |
---|
[1293] | 640 | void colUpperBound(Col c, Value value) { |
---|
[1253] | 641 | _setColUpperBound(cols.floatingId(c.id),value); |
---|
| 642 | }; |
---|
[1293] | 643 | /// Set the lower and the upper bounds of a column (i.e a variable) |
---|
| 644 | |
---|
| 645 | /// The lower and the upper bounds of |
---|
| 646 | /// a variable (column) have to be given by an |
---|
| 647 | /// extended number of type Value, i.e. a finite number of type |
---|
| 648 | /// Value, -\ref INF or \ref INF. |
---|
| 649 | void colBounds(Col c, Value lower, Value upper) { |
---|
| 650 | _setColLowerBound(cols.floatingId(c.id),lower); |
---|
| 651 | _setColUpperBound(cols.floatingId(c.id),upper); |
---|
| 652 | } |
---|
| 653 | |
---|
[1405] | 654 | // /// Set the lower bound of a row (i.e a constraint) |
---|
[1253] | 655 | |
---|
[1405] | 656 | // /// The lower bound of a linear expression (row) has to be given by an |
---|
| 657 | // /// extended number of type Value, i.e. a finite number of type |
---|
| 658 | // /// Value or -\ref INF. |
---|
| 659 | // void rowLowerBound(Row r, Value value) { |
---|
| 660 | // _setRowLowerBound(rows.floatingId(r.id),value); |
---|
| 661 | // }; |
---|
| 662 | // /// Set the upper bound of a row (i.e a constraint) |
---|
[1253] | 663 | |
---|
[1405] | 664 | // /// The upper bound of a linear expression (row) has to be given by an |
---|
| 665 | // /// extended number of type Value, i.e. a finite number of type |
---|
| 666 | // /// Value or \ref INF. |
---|
| 667 | // void rowUpperBound(Row r, Value value) { |
---|
| 668 | // _setRowUpperBound(rows.floatingId(r.id),value); |
---|
| 669 | // }; |
---|
| 670 | |
---|
| 671 | /// Set the lower and the upper bounds of a row (i.e a constraint) |
---|
[1293] | 672 | |
---|
| 673 | /// The lower and the upper bounds of |
---|
| 674 | /// a constraint (row) have to be given by an |
---|
| 675 | /// extended number of type Value, i.e. a finite number of type |
---|
| 676 | /// Value, -\ref INF or \ref INF. |
---|
| 677 | void rowBounds(Row c, Value lower, Value upper) { |
---|
[1379] | 678 | _setRowBounds(rows.floatingId(c.id),lower, upper); |
---|
| 679 | // _setRowUpperBound(rows.floatingId(c.id),upper); |
---|
[1293] | 680 | } |
---|
| 681 | |
---|
[1253] | 682 | ///Set an element of the objective function |
---|
[1293] | 683 | void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); }; |
---|
[1253] | 684 | ///Set the objective function |
---|
| 685 | |
---|
| 686 | ///\param e is a linear expression of type \ref Expr. |
---|
[1323] | 687 | ///\bug The previous objective function is not cleared! |
---|
[1253] | 688 | void setObj(Expr e) { |
---|
[1377] | 689 | _clearObj(); |
---|
[1253] | 690 | for (Expr::iterator i=e.begin(); i!=e.end(); ++i) |
---|
[1293] | 691 | objCoeff((*i).first,(*i).second); |
---|
[1323] | 692 | obj_const_comp=e.constComp(); |
---|
[1253] | 693 | } |
---|
[1263] | 694 | |
---|
[1312] | 695 | ///Maximize |
---|
| 696 | void max() { _setMax(); } |
---|
| 697 | ///Minimize |
---|
| 698 | void min() { _setMin(); } |
---|
| 699 | |
---|
| 700 | |
---|
[1263] | 701 | ///@} |
---|
| 702 | |
---|
| 703 | |
---|
[1294] | 704 | ///\name Solve the LP |
---|
[1263] | 705 | |
---|
| 706 | ///@{ |
---|
| 707 | |
---|
| 708 | ///\e |
---|
[1303] | 709 | SolveExitStatus solve() { return _solve(); } |
---|
[1263] | 710 | |
---|
| 711 | ///@} |
---|
| 712 | |
---|
[1294] | 713 | ///\name Obtain the solution |
---|
[1263] | 714 | |
---|
| 715 | ///@{ |
---|
| 716 | |
---|
| 717 | ///\e |
---|
[1312] | 718 | SolutionStatus primalStatus() { |
---|
| 719 | return _getPrimalStatus(); |
---|
[1294] | 720 | } |
---|
| 721 | |
---|
| 722 | ///\e |
---|
[1293] | 723 | Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); } |
---|
[1263] | 724 | |
---|
[1312] | 725 | ///\e |
---|
| 726 | |
---|
| 727 | ///\return |
---|
| 728 | ///- \ref INF or -\ref INF means either infeasibility or unboundedness |
---|
| 729 | /// of the primal problem, depending on whether we minimize or maximize. |
---|
[1364] | 730 | ///- \ref NaN if no primal solution is found. |
---|
[1312] | 731 | ///- The (finite) objective value if an optimal solution is found. |
---|
[1323] | 732 | Value primalValue() { return _getPrimalValue()+obj_const_comp;} |
---|
[1263] | 733 | ///@} |
---|
[1253] | 734 | |
---|
[1248] | 735 | }; |
---|
[1246] | 736 | |
---|
[1272] | 737 | ///\e |
---|
| 738 | |
---|
| 739 | ///\relates LpSolverBase::Expr |
---|
| 740 | /// |
---|
| 741 | inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, |
---|
| 742 | const LpSolverBase::Expr &b) |
---|
| 743 | { |
---|
| 744 | LpSolverBase::Expr tmp(a); |
---|
[1364] | 745 | tmp+=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
[1272] | 746 | return tmp; |
---|
| 747 | } |
---|
| 748 | ///\e |
---|
| 749 | |
---|
| 750 | ///\relates LpSolverBase::Expr |
---|
| 751 | /// |
---|
| 752 | inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, |
---|
| 753 | const LpSolverBase::Expr &b) |
---|
| 754 | { |
---|
| 755 | LpSolverBase::Expr tmp(a); |
---|
[1364] | 756 | tmp-=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
[1272] | 757 | return tmp; |
---|
| 758 | } |
---|
| 759 | ///\e |
---|
| 760 | |
---|
| 761 | ///\relates LpSolverBase::Expr |
---|
| 762 | /// |
---|
| 763 | inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, |
---|
[1273] | 764 | const LpSolverBase::Value &b) |
---|
[1272] | 765 | { |
---|
| 766 | LpSolverBase::Expr tmp(a); |
---|
[1364] | 767 | tmp*=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
[1272] | 768 | return tmp; |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | ///\e |
---|
| 772 | |
---|
| 773 | ///\relates LpSolverBase::Expr |
---|
| 774 | /// |
---|
[1273] | 775 | inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, |
---|
[1272] | 776 | const LpSolverBase::Expr &b) |
---|
| 777 | { |
---|
| 778 | LpSolverBase::Expr tmp(b); |
---|
[1364] | 779 | tmp*=a; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
[1272] | 780 | return tmp; |
---|
| 781 | } |
---|
| 782 | ///\e |
---|
| 783 | |
---|
| 784 | ///\relates LpSolverBase::Expr |
---|
| 785 | /// |
---|
| 786 | inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, |
---|
[1273] | 787 | const LpSolverBase::Value &b) |
---|
[1272] | 788 | { |
---|
| 789 | LpSolverBase::Expr tmp(a); |
---|
[1364] | 790 | tmp/=b; ///\todo Doesn't STL have some special 'merge' algorithm? |
---|
[1272] | 791 | return tmp; |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | ///\e |
---|
| 795 | |
---|
| 796 | ///\relates LpSolverBase::Constr |
---|
| 797 | /// |
---|
| 798 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
| 799 | const LpSolverBase::Expr &f) |
---|
| 800 | { |
---|
| 801 | return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | ///\e |
---|
| 805 | |
---|
| 806 | ///\relates LpSolverBase::Constr |
---|
| 807 | /// |
---|
[1273] | 808 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, |
---|
[1272] | 809 | const LpSolverBase::Expr &f) |
---|
| 810 | { |
---|
| 811 | return LpSolverBase::Constr(e,f); |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | ///\e |
---|
| 815 | |
---|
| 816 | ///\relates LpSolverBase::Constr |
---|
| 817 | /// |
---|
| 818 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
[1273] | 819 | const LpSolverBase::Value &f) |
---|
[1272] | 820 | { |
---|
| 821 | return LpSolverBase::Constr(e,f); |
---|
| 822 | } |
---|
| 823 | |
---|
| 824 | ///\e |
---|
| 825 | |
---|
| 826 | ///\relates LpSolverBase::Constr |
---|
| 827 | /// |
---|
| 828 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
| 829 | const LpSolverBase::Expr &f) |
---|
| 830 | { |
---|
| 831 | return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | |
---|
| 835 | ///\e |
---|
| 836 | |
---|
| 837 | ///\relates LpSolverBase::Constr |
---|
| 838 | /// |
---|
[1273] | 839 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, |
---|
[1272] | 840 | const LpSolverBase::Expr &f) |
---|
| 841 | { |
---|
| 842 | return LpSolverBase::Constr(f,e); |
---|
| 843 | } |
---|
| 844 | |
---|
| 845 | |
---|
| 846 | ///\e |
---|
| 847 | |
---|
| 848 | ///\relates LpSolverBase::Constr |
---|
| 849 | /// |
---|
| 850 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
[1273] | 851 | const LpSolverBase::Value &f) |
---|
[1272] | 852 | { |
---|
| 853 | return LpSolverBase::Constr(f,e); |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | ///\e |
---|
| 857 | |
---|
| 858 | ///\relates LpSolverBase::Constr |
---|
| 859 | /// |
---|
| 860 | inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, |
---|
| 861 | const LpSolverBase::Expr &f) |
---|
| 862 | { |
---|
| 863 | return LpSolverBase::Constr(0,e-f,0); |
---|
| 864 | } |
---|
| 865 | |
---|
| 866 | ///\e |
---|
| 867 | |
---|
| 868 | ///\relates LpSolverBase::Constr |
---|
| 869 | /// |
---|
[1273] | 870 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, |
---|
[1272] | 871 | const LpSolverBase::Constr&c) |
---|
| 872 | { |
---|
| 873 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 874 | ///\todo Create an own exception type. |
---|
| 875 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
| 876 | else tmp.lowerBound()=n; |
---|
[1272] | 877 | return tmp; |
---|
| 878 | } |
---|
| 879 | ///\e |
---|
| 880 | |
---|
| 881 | ///\relates LpSolverBase::Constr |
---|
| 882 | /// |
---|
| 883 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, |
---|
[1273] | 884 | const LpSolverBase::Value &n) |
---|
[1272] | 885 | { |
---|
| 886 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 887 | ///\todo Create an own exception type. |
---|
| 888 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
| 889 | else tmp.upperBound()=n; |
---|
[1272] | 890 | return tmp; |
---|
| 891 | } |
---|
| 892 | |
---|
| 893 | ///\e |
---|
| 894 | |
---|
| 895 | ///\relates LpSolverBase::Constr |
---|
| 896 | /// |
---|
[1273] | 897 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, |
---|
[1272] | 898 | const LpSolverBase::Constr&c) |
---|
| 899 | { |
---|
| 900 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 901 | ///\todo Create an own exception type. |
---|
| 902 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
| 903 | else tmp.upperBound()=n; |
---|
[1272] | 904 | return tmp; |
---|
| 905 | } |
---|
| 906 | ///\e |
---|
| 907 | |
---|
| 908 | ///\relates LpSolverBase::Constr |
---|
| 909 | /// |
---|
| 910 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, |
---|
[1273] | 911 | const LpSolverBase::Value &n) |
---|
[1272] | 912 | { |
---|
| 913 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 914 | ///\todo Create an own exception type. |
---|
| 915 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
| 916 | else tmp.lowerBound()=n; |
---|
[1272] | 917 | return tmp; |
---|
| 918 | } |
---|
| 919 | |
---|
| 920 | |
---|
[1246] | 921 | } //namespace lemon |
---|
| 922 | |
---|
| 923 | #endif //LEMON_LP_BASE_H |
---|