[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 | |
---|
[1246] | 29 | ///\file |
---|
| 30 | ///\brief The interface of the LP solver interface. |
---|
[1328] | 31 | ///\ingroup gen_opt_group |
---|
[1246] | 32 | namespace lemon { |
---|
[1253] | 33 | |
---|
| 34 | ///Internal data structure to convert floating id's to fix one's |
---|
| 35 | |
---|
[1279] | 36 | ///\todo This might be implemented to be also usable in other places. |
---|
[1253] | 37 | class _FixId |
---|
| 38 | { |
---|
[1787] | 39 | protected: |
---|
[1253] | 40 | std::vector<int> index; |
---|
| 41 | std::vector<int> cross; |
---|
| 42 | int first_free; |
---|
| 43 | public: |
---|
| 44 | _FixId() : first_free(-1) {}; |
---|
| 45 | ///Convert a floating id to a fix one |
---|
| 46 | |
---|
| 47 | ///\param n is a floating id |
---|
| 48 | ///\return the corresponding fix id |
---|
[1484] | 49 | int fixId(int n) const {return cross[n];} |
---|
[1253] | 50 | ///Convert a fix id to a floating one |
---|
| 51 | |
---|
| 52 | ///\param n is a fix id |
---|
| 53 | ///\return the corresponding floating id |
---|
[1484] | 54 | int floatingId(int n) const { return index[n];} |
---|
[1253] | 55 | ///Add a new floating id. |
---|
| 56 | |
---|
| 57 | ///\param n is a floating id |
---|
| 58 | ///\return the fix id of the new value |
---|
| 59 | ///\todo Multiple additions should also be handled. |
---|
| 60 | int insert(int n) |
---|
| 61 | { |
---|
| 62 | if(n>=int(cross.size())) { |
---|
| 63 | cross.resize(n+1); |
---|
| 64 | if(first_free==-1) { |
---|
| 65 | cross[n]=index.size(); |
---|
| 66 | index.push_back(n); |
---|
| 67 | } |
---|
| 68 | else { |
---|
| 69 | cross[n]=first_free; |
---|
| 70 | int next=index[first_free]; |
---|
| 71 | index[first_free]=n; |
---|
| 72 | first_free=next; |
---|
| 73 | } |
---|
[1256] | 74 | return cross[n]; |
---|
[1253] | 75 | } |
---|
[1273] | 76 | ///\todo Create an own exception type. |
---|
[1253] | 77 | else throw LogicError(); //floatingId-s must form a continuous range; |
---|
| 78 | } |
---|
| 79 | ///Remove a fix id. |
---|
| 80 | |
---|
| 81 | ///\param n is a fix id |
---|
| 82 | /// |
---|
| 83 | void erase(int n) |
---|
| 84 | { |
---|
| 85 | int fl=index[n]; |
---|
| 86 | index[n]=first_free; |
---|
| 87 | first_free=n; |
---|
| 88 | for(int i=fl+1;i<int(cross.size());++i) { |
---|
| 89 | cross[i-1]=cross[i]; |
---|
| 90 | index[cross[i]]--; |
---|
| 91 | } |
---|
| 92 | cross.pop_back(); |
---|
| 93 | } |
---|
| 94 | ///An upper bound on the largest fix id. |
---|
| 95 | |
---|
| 96 | ///\todo Do we need this? |
---|
| 97 | /// |
---|
| 98 | std::size_t maxFixId() { return cross.size()-1; } |
---|
| 99 | |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | ///Common base class for LP solvers |
---|
[1328] | 103 | |
---|
| 104 | ///\todo Much more docs |
---|
| 105 | ///\ingroup gen_opt_group |
---|
[1246] | 106 | class LpSolverBase { |
---|
[1323] | 107 | |
---|
[1247] | 108 | public: |
---|
| 109 | |
---|
[1458] | 110 | ///Possible outcomes of an LP solving procedure |
---|
[1303] | 111 | enum SolveExitStatus { |
---|
[1458] | 112 | ///This means that the problem has been successfully solved: either |
---|
| 113 | ///an optimal solution has been found or infeasibility/unboundedness |
---|
| 114 | ///has been proved. |
---|
[1293] | 115 | SOLVED = 0, |
---|
[1458] | 116 | ///Any other case (including the case when some user specified limit has been exceeded) |
---|
[1293] | 117 | UNSOLVED = 1 |
---|
[1291] | 118 | }; |
---|
| 119 | |
---|
[1460] | 120 | ///\e |
---|
[1303] | 121 | enum SolutionStatus { |
---|
[1295] | 122 | ///Feasible solution has'n been found (but may exist). |
---|
| 123 | |
---|
| 124 | ///\todo NOTFOUND might be a better name. |
---|
| 125 | /// |
---|
[1293] | 126 | UNDEFINED = 0, |
---|
[1295] | 127 | ///The problem has no feasible solution |
---|
[1293] | 128 | INFEASIBLE = 1, |
---|
[1295] | 129 | ///Feasible solution found |
---|
[1293] | 130 | FEASIBLE = 2, |
---|
[1295] | 131 | ///Optimal solution exists and found |
---|
| 132 | OPTIMAL = 3, |
---|
| 133 | ///The cost function is unbounded |
---|
| 134 | |
---|
| 135 | ///\todo Give a feasible solution and an infinite ray (and the |
---|
| 136 | ///corresponding bases) |
---|
| 137 | INFINITE = 4 |
---|
[1263] | 138 | }; |
---|
[1460] | 139 | |
---|
[1542] | 140 | ///\e The type of the investigated LP problem |
---|
| 141 | enum ProblemTypes { |
---|
| 142 | ///Primal-dual feasible |
---|
| 143 | PRIMAL_DUAL_FEASIBLE = 0, |
---|
| 144 | ///Primal feasible dual infeasible |
---|
| 145 | PRIMAL_FEASIBLE_DUAL_INFEASIBLE = 1, |
---|
| 146 | ///Primal infeasible dual feasible |
---|
| 147 | PRIMAL_INFEASIBLE_DUAL_FEASIBLE = 2, |
---|
| 148 | ///Primal-dual infeasible |
---|
| 149 | PRIMAL_DUAL_INFEASIBLE = 3, |
---|
| 150 | ///Could not determine so far |
---|
| 151 | UNKNOWN = 4 |
---|
| 152 | }; |
---|
[1508] | 153 | |
---|
[1256] | 154 | ///The floating point type used by the solver |
---|
[1247] | 155 | typedef double Value; |
---|
[1256] | 156 | ///The infinity constant |
---|
[1247] | 157 | static const Value INF; |
---|
[1264] | 158 | ///The not a number constant |
---|
| 159 | static const Value NaN; |
---|
[1253] | 160 | |
---|
[1256] | 161 | ///Refer to a column of the LP. |
---|
| 162 | |
---|
| 163 | ///This type is used to refer to a column of the LP. |
---|
| 164 | /// |
---|
| 165 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 166 | ///other columns. |
---|
[1256] | 167 | /// |
---|
| 168 | ///\todo Document what can one do with a Col (INVALID, comparing, |
---|
| 169 | ///it is similar to Node/Edge) |
---|
| 170 | class Col { |
---|
| 171 | protected: |
---|
| 172 | int id; |
---|
| 173 | friend class LpSolverBase; |
---|
| 174 | public: |
---|
[1259] | 175 | typedef Value ExprValue; |
---|
[1256] | 176 | typedef True LpSolverCol; |
---|
| 177 | Col() {} |
---|
| 178 | Col(const Invalid&) : id(-1) {} |
---|
| 179 | bool operator<(Col c) const {return id<c.id;} |
---|
| 180 | bool operator==(Col c) const {return id==c.id;} |
---|
| 181 | bool operator!=(Col c) const {return id==c.id;} |
---|
| 182 | }; |
---|
| 183 | |
---|
| 184 | ///Refer to a row of the LP. |
---|
| 185 | |
---|
| 186 | ///This type is used to refer to a row of the LP. |
---|
| 187 | /// |
---|
| 188 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 189 | ///other rows. |
---|
[1256] | 190 | /// |
---|
| 191 | ///\todo Document what can one do with a Row (INVALID, comparing, |
---|
| 192 | ///it is similar to Node/Edge) |
---|
| 193 | class Row { |
---|
| 194 | protected: |
---|
| 195 | int id; |
---|
| 196 | friend class LpSolverBase; |
---|
| 197 | public: |
---|
[1259] | 198 | typedef Value ExprValue; |
---|
[1256] | 199 | typedef True LpSolverRow; |
---|
| 200 | Row() {} |
---|
| 201 | Row(const Invalid&) : id(-1) {} |
---|
[1439] | 202 | |
---|
[1256] | 203 | bool operator<(Row c) const {return id<c.id;} |
---|
| 204 | bool operator==(Row c) const {return id==c.id;} |
---|
| 205 | bool operator!=(Row c) const {return id==c.id;} |
---|
| 206 | }; |
---|
[1259] | 207 | |
---|
[1279] | 208 | ///Linear expression of variables and a constant component |
---|
| 209 | |
---|
| 210 | ///This data structure strores a linear expression of the variables |
---|
| 211 | ///(\ref Col "Col"s) and also has a constant component. |
---|
| 212 | /// |
---|
| 213 | ///There are several ways to access and modify the contents of this |
---|
| 214 | ///container. |
---|
| 215 | ///- Its it fully compatible with \c std::map<Col,double>, so for expamle |
---|
[1364] | 216 | ///if \c e is an Expr and \c v and \c w are of type \ref Col, then you can |
---|
[1279] | 217 | ///read and modify the coefficients like |
---|
| 218 | ///these. |
---|
| 219 | ///\code |
---|
| 220 | ///e[v]=5; |
---|
| 221 | ///e[v]+=12; |
---|
| 222 | ///e.erase(v); |
---|
| 223 | ///\endcode |
---|
| 224 | ///or you can also iterate through its elements. |
---|
| 225 | ///\code |
---|
| 226 | ///double s=0; |
---|
| 227 | ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) |
---|
| 228 | /// s+=i->second; |
---|
| 229 | ///\endcode |
---|
| 230 | ///(This code computes the sum of all coefficients). |
---|
| 231 | ///- Numbers (<tt>double</tt>'s) |
---|
| 232 | ///and variables (\ref Col "Col"s) directly convert to an |
---|
| 233 | ///\ref Expr and the usual linear operations are defined so |
---|
| 234 | ///\code |
---|
| 235 | ///v+w |
---|
| 236 | ///2*v-3.12*(v-w/2)+2 |
---|
| 237 | ///v*2.1+(3*v+(v*12+w+6)*3)/2 |
---|
| 238 | ///\endcode |
---|
[1328] | 239 | ///are valid \ref Expr "Expr"essions. |
---|
| 240 | ///The usual assignment operations are also defined. |
---|
[1279] | 241 | ///\code |
---|
| 242 | ///e=v+w; |
---|
| 243 | ///e+=2*v-3.12*(v-w/2)+2; |
---|
| 244 | ///e*=3.4; |
---|
| 245 | ///e/=5; |
---|
| 246 | ///\endcode |
---|
| 247 | ///- The constant member can be set and read by \ref constComp() |
---|
| 248 | ///\code |
---|
| 249 | ///e.constComp()=12; |
---|
| 250 | ///double c=e.constComp(); |
---|
| 251 | ///\endcode |
---|
| 252 | /// |
---|
[1328] | 253 | ///\note \ref clear() not only sets all coefficients to 0 but also |
---|
[1279] | 254 | ///clears the constant components. |
---|
[1328] | 255 | /// |
---|
| 256 | ///\sa Constr |
---|
| 257 | /// |
---|
[1273] | 258 | class Expr : public std::map<Col,Value> |
---|
[1272] | 259 | { |
---|
| 260 | public: |
---|
[1273] | 261 | typedef LpSolverBase::Col Key; |
---|
| 262 | typedef LpSolverBase::Value Value; |
---|
[1272] | 263 | |
---|
| 264 | protected: |
---|
[1273] | 265 | typedef std::map<Col,Value> Base; |
---|
[1272] | 266 | |
---|
[1273] | 267 | Value const_comp; |
---|
[1272] | 268 | public: |
---|
| 269 | typedef True IsLinExpression; |
---|
| 270 | ///\e |
---|
| 271 | Expr() : Base(), const_comp(0) { } |
---|
| 272 | ///\e |
---|
[1273] | 273 | Expr(const Key &v) : const_comp(0) { |
---|
[1272] | 274 | Base::insert(std::make_pair(v, 1)); |
---|
| 275 | } |
---|
| 276 | ///\e |
---|
[1273] | 277 | Expr(const Value &v) : const_comp(v) {} |
---|
[1272] | 278 | ///\e |
---|
[1273] | 279 | void set(const Key &v,const Value &c) { |
---|
[1272] | 280 | Base::insert(std::make_pair(v, c)); |
---|
| 281 | } |
---|
| 282 | ///\e |
---|
[1273] | 283 | Value &constComp() { return const_comp; } |
---|
[1272] | 284 | ///\e |
---|
[1273] | 285 | const Value &constComp() const { return const_comp; } |
---|
[1272] | 286 | |
---|
| 287 | ///Removes the components with zero coefficient. |
---|
| 288 | void simplify() { |
---|
| 289 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 290 | Base::iterator j=i; |
---|
| 291 | ++j; |
---|
| 292 | if ((*i).second==0) Base::erase(i); |
---|
| 293 | j=i; |
---|
| 294 | } |
---|
| 295 | } |
---|
[1273] | 296 | |
---|
[1771] | 297 | ///Removes the coefficients closer to zero than \c tolerance. |
---|
| 298 | void simplify(double &tolerance) { |
---|
| 299 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 300 | Base::iterator j=i; |
---|
| 301 | ++j; |
---|
| 302 | if (std::fabs((*i).second)<tolerance) Base::erase(i); |
---|
| 303 | j=i; |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
[1273] | 307 | ///Sets all coefficients and the constant component to 0. |
---|
| 308 | void clear() { |
---|
| 309 | Base::clear(); |
---|
| 310 | const_comp=0; |
---|
| 311 | } |
---|
| 312 | |
---|
[1272] | 313 | ///\e |
---|
| 314 | Expr &operator+=(const Expr &e) { |
---|
| 315 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 316 | (*this)[j->first]+=j->second; |
---|
| 317 | const_comp+=e.const_comp; |
---|
| 318 | return *this; |
---|
| 319 | } |
---|
| 320 | ///\e |
---|
| 321 | Expr &operator-=(const Expr &e) { |
---|
| 322 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 323 | (*this)[j->first]-=j->second; |
---|
| 324 | const_comp-=e.const_comp; |
---|
| 325 | return *this; |
---|
| 326 | } |
---|
| 327 | ///\e |
---|
[1273] | 328 | Expr &operator*=(const Value &c) { |
---|
[1272] | 329 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 330 | j->second*=c; |
---|
| 331 | const_comp*=c; |
---|
| 332 | return *this; |
---|
| 333 | } |
---|
| 334 | ///\e |
---|
[1273] | 335 | Expr &operator/=(const Value &c) { |
---|
[1272] | 336 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 337 | j->second/=c; |
---|
| 338 | const_comp/=c; |
---|
| 339 | return *this; |
---|
| 340 | } |
---|
| 341 | }; |
---|
| 342 | |
---|
[1264] | 343 | ///Linear constraint |
---|
[1328] | 344 | |
---|
[1364] | 345 | ///This data stucture represents a linear constraint in the LP. |
---|
| 346 | ///Basically it is a linear expression with a lower or an upper bound |
---|
| 347 | ///(or both). These parts of the constraint can be obtained by the member |
---|
| 348 | ///functions \ref expr(), \ref lowerBound() and \ref upperBound(), |
---|
| 349 | ///respectively. |
---|
| 350 | ///There are two ways to construct a constraint. |
---|
| 351 | ///- You can set the linear expression and the bounds directly |
---|
| 352 | /// by the functions above. |
---|
| 353 | ///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt> |
---|
| 354 | /// are defined between expressions, or even between constraints whenever |
---|
| 355 | /// it makes sense. Therefore if \c e and \c f are linear expressions and |
---|
| 356 | /// \c s and \c t are numbers, then the followings are valid expressions |
---|
| 357 | /// and thus they can be used directly e.g. in \ref addRow() whenever |
---|
| 358 | /// it makes sense. |
---|
| 359 | /// \code |
---|
| 360 | /// e<=s |
---|
| 361 | /// e<=f |
---|
| 362 | /// s<=e<=t |
---|
| 363 | /// e>=t |
---|
| 364 | /// \endcode |
---|
| 365 | ///\warning The validity of a constraint is checked only at run time, so |
---|
| 366 | ///e.g. \ref addRow(<tt>x[1]\<=x[2]<=5</tt>) will compile, but will throw a |
---|
| 367 | ///\ref LogicError exception. |
---|
[1272] | 368 | class Constr |
---|
| 369 | { |
---|
| 370 | public: |
---|
| 371 | typedef LpSolverBase::Expr Expr; |
---|
[1273] | 372 | typedef Expr::Key Key; |
---|
| 373 | typedef Expr::Value Value; |
---|
[1272] | 374 | |
---|
[1364] | 375 | // static const Value INF; |
---|
| 376 | // static const Value NaN; |
---|
| 377 | |
---|
[1273] | 378 | protected: |
---|
| 379 | Expr _expr; |
---|
| 380 | Value _lb,_ub; |
---|
| 381 | public: |
---|
| 382 | ///\e |
---|
| 383 | Constr() : _expr(), _lb(NaN), _ub(NaN) {} |
---|
| 384 | ///\e |
---|
| 385 | Constr(Value lb,const Expr &e,Value ub) : |
---|
| 386 | _expr(e), _lb(lb), _ub(ub) {} |
---|
| 387 | ///\e |
---|
| 388 | Constr(const Expr &e,Value ub) : |
---|
| 389 | _expr(e), _lb(NaN), _ub(ub) {} |
---|
| 390 | ///\e |
---|
| 391 | Constr(Value lb,const Expr &e) : |
---|
| 392 | _expr(e), _lb(lb), _ub(NaN) {} |
---|
| 393 | ///\e |
---|
[1272] | 394 | Constr(const Expr &e) : |
---|
[1273] | 395 | _expr(e), _lb(NaN), _ub(NaN) {} |
---|
| 396 | ///\e |
---|
| 397 | void clear() |
---|
| 398 | { |
---|
| 399 | _expr.clear(); |
---|
| 400 | _lb=_ub=NaN; |
---|
| 401 | } |
---|
[1364] | 402 | |
---|
| 403 | ///Reference to the linear expression |
---|
[1273] | 404 | Expr &expr() { return _expr; } |
---|
[1364] | 405 | ///Cont reference to the linear expression |
---|
[1273] | 406 | const Expr &expr() const { return _expr; } |
---|
[1364] | 407 | ///Reference to the lower bound. |
---|
| 408 | |
---|
| 409 | ///\return |
---|
[1536] | 410 | ///- \ref INF "INF": the constraint is lower unbounded. |
---|
| 411 | ///- \ref NaN "NaN": lower bound has not been set. |
---|
[1364] | 412 | ///- finite number: the lower bound |
---|
[1273] | 413 | Value &lowerBound() { return _lb; } |
---|
[1364] | 414 | ///The const version of \ref lowerBound() |
---|
[1273] | 415 | const Value &lowerBound() const { return _lb; } |
---|
[1364] | 416 | ///Reference to the upper bound. |
---|
| 417 | |
---|
| 418 | ///\return |
---|
[1536] | 419 | ///- \ref INF "INF": the constraint is upper unbounded. |
---|
| 420 | ///- \ref NaN "NaN": upper bound has not been set. |
---|
[1364] | 421 | ///- finite number: the upper bound |
---|
[1273] | 422 | Value &upperBound() { return _ub; } |
---|
[1364] | 423 | ///The const version of \ref upperBound() |
---|
[1273] | 424 | const Value &upperBound() const { return _ub; } |
---|
[1364] | 425 | ///Is the constraint lower bounded? |
---|
[1295] | 426 | bool lowerBounded() const { |
---|
| 427 | using namespace std; |
---|
[1397] | 428 | return finite(_lb); |
---|
[1295] | 429 | } |
---|
[1364] | 430 | ///Is the constraint upper bounded? |
---|
[1295] | 431 | bool upperBounded() const { |
---|
| 432 | using namespace std; |
---|
[1397] | 433 | return finite(_ub); |
---|
[1295] | 434 | } |
---|
[1272] | 435 | }; |
---|
| 436 | |
---|
[1445] | 437 | ///Linear expression of rows |
---|
| 438 | |
---|
| 439 | ///This data structure represents a column of the matrix, |
---|
| 440 | ///thas is it strores a linear expression of the dual variables |
---|
| 441 | ///(\ref Row "Row"s). |
---|
| 442 | /// |
---|
| 443 | ///There are several ways to access and modify the contents of this |
---|
| 444 | ///container. |
---|
| 445 | ///- Its it fully compatible with \c std::map<Row,double>, so for expamle |
---|
| 446 | ///if \c e is an DualExpr and \c v |
---|
| 447 | ///and \c w are of type \ref Row, then you can |
---|
| 448 | ///read and modify the coefficients like |
---|
| 449 | ///these. |
---|
| 450 | ///\code |
---|
| 451 | ///e[v]=5; |
---|
| 452 | ///e[v]+=12; |
---|
| 453 | ///e.erase(v); |
---|
| 454 | ///\endcode |
---|
| 455 | ///or you can also iterate through its elements. |
---|
| 456 | ///\code |
---|
| 457 | ///double s=0; |
---|
| 458 | ///for(LpSolverBase::DualExpr::iterator i=e.begin();i!=e.end();++i) |
---|
| 459 | /// s+=i->second; |
---|
| 460 | ///\endcode |
---|
| 461 | ///(This code computes the sum of all coefficients). |
---|
| 462 | ///- Numbers (<tt>double</tt>'s) |
---|
| 463 | ///and variables (\ref Row "Row"s) directly convert to an |
---|
| 464 | ///\ref DualExpr and the usual linear operations are defined so |
---|
| 465 | ///\code |
---|
| 466 | ///v+w |
---|
| 467 | ///2*v-3.12*(v-w/2) |
---|
| 468 | ///v*2.1+(3*v+(v*12+w)*3)/2 |
---|
| 469 | ///\endcode |
---|
| 470 | ///are valid \ref DualExpr "DualExpr"essions. |
---|
| 471 | ///The usual assignment operations are also defined. |
---|
| 472 | ///\code |
---|
| 473 | ///e=v+w; |
---|
| 474 | ///e+=2*v-3.12*(v-w/2); |
---|
| 475 | ///e*=3.4; |
---|
| 476 | ///e/=5; |
---|
| 477 | ///\endcode |
---|
| 478 | /// |
---|
| 479 | ///\sa Expr |
---|
| 480 | /// |
---|
| 481 | class DualExpr : public std::map<Row,Value> |
---|
| 482 | { |
---|
| 483 | public: |
---|
| 484 | typedef LpSolverBase::Row Key; |
---|
| 485 | typedef LpSolverBase::Value Value; |
---|
| 486 | |
---|
| 487 | protected: |
---|
| 488 | typedef std::map<Row,Value> Base; |
---|
| 489 | |
---|
| 490 | public: |
---|
| 491 | typedef True IsLinExpression; |
---|
| 492 | ///\e |
---|
| 493 | DualExpr() : Base() { } |
---|
| 494 | ///\e |
---|
| 495 | DualExpr(const Key &v) { |
---|
| 496 | Base::insert(std::make_pair(v, 1)); |
---|
| 497 | } |
---|
| 498 | ///\e |
---|
| 499 | void set(const Key &v,const Value &c) { |
---|
| 500 | Base::insert(std::make_pair(v, c)); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | ///Removes the components with zero coefficient. |
---|
| 504 | void simplify() { |
---|
| 505 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 506 | Base::iterator j=i; |
---|
| 507 | ++j; |
---|
| 508 | if ((*i).second==0) Base::erase(i); |
---|
| 509 | j=i; |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | |
---|
[1771] | 513 | ///Removes the coefficients closer to zero than \c tolerance. |
---|
| 514 | void simplify(double &tolerance) { |
---|
| 515 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 516 | Base::iterator j=i; |
---|
| 517 | ++j; |
---|
| 518 | if (std::fabs((*i).second)<tolerance) Base::erase(i); |
---|
| 519 | j=i; |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | |
---|
[1445] | 524 | ///Sets all coefficients to 0. |
---|
| 525 | void clear() { |
---|
| 526 | Base::clear(); |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | ///\e |
---|
| 530 | DualExpr &operator+=(const DualExpr &e) { |
---|
| 531 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 532 | (*this)[j->first]+=j->second; |
---|
| 533 | return *this; |
---|
| 534 | } |
---|
| 535 | ///\e |
---|
| 536 | DualExpr &operator-=(const DualExpr &e) { |
---|
| 537 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 538 | (*this)[j->first]-=j->second; |
---|
| 539 | return *this; |
---|
| 540 | } |
---|
| 541 | ///\e |
---|
| 542 | DualExpr &operator*=(const Value &c) { |
---|
| 543 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 544 | j->second*=c; |
---|
| 545 | return *this; |
---|
| 546 | } |
---|
| 547 | ///\e |
---|
| 548 | DualExpr &operator/=(const Value &c) { |
---|
| 549 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 550 | j->second/=c; |
---|
| 551 | return *this; |
---|
| 552 | } |
---|
| 553 | }; |
---|
| 554 | |
---|
[1253] | 555 | |
---|
| 556 | protected: |
---|
| 557 | _FixId rows; |
---|
| 558 | _FixId cols; |
---|
[1246] | 559 | |
---|
[1323] | 560 | //Abstract virtual functions |
---|
[1364] | 561 | virtual LpSolverBase &_newLp() = 0; |
---|
[1436] | 562 | virtual LpSolverBase &_copyLp(){ |
---|
| 563 | ///\todo This should be implemented here, too, when we have problem retrieving routines. It can be overriden. |
---|
| 564 | |
---|
| 565 | //Starting: |
---|
| 566 | LpSolverBase & newlp(_newLp()); |
---|
| 567 | return newlp; |
---|
| 568 | //return *(LpSolverBase*)0; |
---|
| 569 | }; |
---|
[1364] | 570 | |
---|
[1246] | 571 | virtual int _addCol() = 0; |
---|
| 572 | virtual int _addRow() = 0; |
---|
[1542] | 573 | virtual void _eraseCol(int col) = 0; |
---|
| 574 | virtual void _eraseRow(int row) = 0; |
---|
[1246] | 575 | virtual void _setRowCoeffs(int i, |
---|
[1251] | 576 | int length, |
---|
[1247] | 577 | int const * indices, |
---|
| 578 | Value const * values ) = 0; |
---|
[1246] | 579 | virtual void _setColCoeffs(int i, |
---|
[1251] | 580 | int length, |
---|
[1247] | 581 | int const * indices, |
---|
| 582 | Value const * values ) = 0; |
---|
[1431] | 583 | virtual void _setCoeff(int row, int col, Value value) = 0; |
---|
[1294] | 584 | virtual void _setColLowerBound(int i, Value value) = 0; |
---|
| 585 | virtual void _setColUpperBound(int i, Value value) = 0; |
---|
[1405] | 586 | // virtual void _setRowLowerBound(int i, Value value) = 0; |
---|
| 587 | // virtual void _setRowUpperBound(int i, Value value) = 0; |
---|
[1379] | 588 | virtual void _setRowBounds(int i, Value lower, Value upper) = 0; |
---|
[1294] | 589 | virtual void _setObjCoeff(int i, Value obj_coef) = 0; |
---|
[1377] | 590 | virtual void _clearObj()=0; |
---|
| 591 | // virtual void _setObj(int length, |
---|
| 592 | // int const * indices, |
---|
| 593 | // Value const * values ) = 0; |
---|
[1303] | 594 | virtual SolveExitStatus _solve() = 0; |
---|
[1294] | 595 | virtual Value _getPrimal(int i) = 0; |
---|
[1787] | 596 | virtual Value _getDual(int i) = 0; |
---|
[1312] | 597 | virtual Value _getPrimalValue() = 0; |
---|
| 598 | virtual SolutionStatus _getPrimalStatus() = 0; |
---|
[1460] | 599 | virtual SolutionStatus _getDualStatus() = 0; |
---|
| 600 | ///\todo This could be implemented here, too, using _getPrimalStatus() and |
---|
| 601 | ///_getDualStatus() |
---|
| 602 | virtual ProblemTypes _getProblemType() = 0; |
---|
| 603 | |
---|
[1312] | 604 | virtual void _setMax() = 0; |
---|
| 605 | virtual void _setMin() = 0; |
---|
| 606 | |
---|
[1323] | 607 | //Own protected stuff |
---|
| 608 | |
---|
| 609 | //Constant component of the objective function |
---|
| 610 | Value obj_const_comp; |
---|
| 611 | |
---|
[1377] | 612 | |
---|
| 613 | |
---|
[1323] | 614 | |
---|
[1253] | 615 | public: |
---|
| 616 | |
---|
[1323] | 617 | ///\e |
---|
| 618 | LpSolverBase() : obj_const_comp(0) {} |
---|
[1253] | 619 | |
---|
| 620 | ///\e |
---|
| 621 | virtual ~LpSolverBase() {} |
---|
| 622 | |
---|
[1364] | 623 | ///Creates a new LP problem |
---|
| 624 | LpSolverBase &newLp() {return _newLp();} |
---|
[1381] | 625 | ///Makes a copy of the LP problem |
---|
[1364] | 626 | LpSolverBase ©Lp() {return _copyLp();} |
---|
| 627 | |
---|
[1612] | 628 | ///\name Build up and modify the LP |
---|
[1263] | 629 | |
---|
| 630 | ///@{ |
---|
| 631 | |
---|
[1253] | 632 | ///Add a new empty column (i.e a new variable) to the LP |
---|
| 633 | Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} |
---|
[1263] | 634 | |
---|
[1294] | 635 | ///\brief Adds several new columns |
---|
| 636 | ///(i.e a variables) at once |
---|
[1256] | 637 | /// |
---|
[1273] | 638 | ///This magic function takes a container as its argument |
---|
[1256] | 639 | ///and fills its elements |
---|
| 640 | ///with new columns (i.e. variables) |
---|
[1273] | 641 | ///\param t can be |
---|
| 642 | ///- a standard STL compatible iterable container with |
---|
| 643 | ///\ref Col as its \c values_type |
---|
| 644 | ///like |
---|
| 645 | ///\code |
---|
| 646 | ///std::vector<LpSolverBase::Col> |
---|
| 647 | ///std::list<LpSolverBase::Col> |
---|
| 648 | ///\endcode |
---|
| 649 | ///- a standard STL compatible iterable container with |
---|
| 650 | ///\ref Col as its \c mapped_type |
---|
| 651 | ///like |
---|
| 652 | ///\code |
---|
[1364] | 653 | ///std::map<AnyType,LpSolverBase::Col> |
---|
[1273] | 654 | ///\endcode |
---|
| 655 | ///- an iterable lemon \ref concept::WriteMap "write map" like |
---|
| 656 | ///\code |
---|
| 657 | ///ListGraph::NodeMap<LpSolverBase::Col> |
---|
| 658 | ///ListGraph::EdgeMap<LpSolverBase::Col> |
---|
| 659 | ///\endcode |
---|
[1256] | 660 | ///\return The number of the created column. |
---|
| 661 | #ifdef DOXYGEN |
---|
| 662 | template<class T> |
---|
| 663 | int addColSet(T &t) { return 0;} |
---|
| 664 | #else |
---|
| 665 | template<class T> |
---|
| 666 | typename enable_if<typename T::value_type::LpSolverCol,int>::type |
---|
| 667 | addColSet(T &t,dummy<0> = 0) { |
---|
| 668 | int s=0; |
---|
| 669 | for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} |
---|
| 670 | return s; |
---|
| 671 | } |
---|
| 672 | template<class T> |
---|
| 673 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
| 674 | int>::type |
---|
| 675 | addColSet(T &t,dummy<1> = 1) { |
---|
| 676 | int s=0; |
---|
| 677 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 678 | i->second=addCol(); |
---|
| 679 | s++; |
---|
| 680 | } |
---|
| 681 | return s; |
---|
| 682 | } |
---|
[1272] | 683 | template<class T> |
---|
| 684 | typename enable_if<typename T::ValueSet::value_type::LpSolverCol, |
---|
| 685 | int>::type |
---|
| 686 | addColSet(T &t,dummy<2> = 2) { |
---|
| 687 | ///\bug <tt>return addColSet(t.valueSet());</tt> should also work. |
---|
| 688 | int s=0; |
---|
| 689 | for(typename T::ValueSet::iterator i=t.valueSet().begin(); |
---|
| 690 | i!=t.valueSet().end(); |
---|
| 691 | ++i) |
---|
| 692 | { |
---|
| 693 | *i=addCol(); |
---|
| 694 | s++; |
---|
| 695 | } |
---|
| 696 | return s; |
---|
| 697 | } |
---|
[1256] | 698 | #endif |
---|
[1263] | 699 | |
---|
[1445] | 700 | ///Set a column (i.e a dual constraint) of the LP |
---|
[1258] | 701 | |
---|
[1445] | 702 | ///\param c is the column to be modified |
---|
| 703 | ///\param e is a dual linear expression (see \ref DualExpr) |
---|
[1542] | 704 | ///\bug This is a temporary function. The interface will change to |
---|
[1445] | 705 | ///a better one. |
---|
| 706 | void setCol(Col c,const DualExpr &e) { |
---|
| 707 | std::vector<int> indices; |
---|
| 708 | std::vector<Value> values; |
---|
| 709 | indices.push_back(0); |
---|
| 710 | values.push_back(0); |
---|
| 711 | for(DualExpr::const_iterator i=e.begin(); i!=e.end(); ++i) |
---|
| 712 | if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! |
---|
[1787] | 713 | indices.push_back(rows.floatingId((*i).first.id)); |
---|
[1445] | 714 | values.push_back((*i).second); |
---|
| 715 | } |
---|
| 716 | _setColCoeffs(cols.floatingId(c.id),indices.size()-1, |
---|
| 717 | &indices[0],&values[0]); |
---|
| 718 | } |
---|
| 719 | |
---|
| 720 | ///Add a new column to the LP |
---|
| 721 | |
---|
| 722 | ///\param e is a dual linear expression (see \ref DualExpr) |
---|
| 723 | ///\param obj is the corresponding component of the objective |
---|
| 724 | ///function. It is 0 by default. |
---|
| 725 | ///\return The created column. |
---|
| 726 | ///\bug This is a temportary function. The interface will change to |
---|
| 727 | ///a better one. |
---|
[1493] | 728 | Col addCol(const DualExpr &e, Value obj=0) { |
---|
[1445] | 729 | Col c=addCol(); |
---|
| 730 | setCol(c,e); |
---|
[1493] | 731 | objCoeff(c,obj); |
---|
[1445] | 732 | return c; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | ///Add a new empty row (i.e a new constraint) to the LP |
---|
| 736 | |
---|
| 737 | ///This function adds a new empty row (i.e a new constraint) to the LP. |
---|
[1258] | 738 | ///\return The created row |
---|
[1253] | 739 | Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} |
---|
| 740 | |
---|
[1542] | 741 | ///\brief Add several new rows |
---|
| 742 | ///(i.e a constraints) at once |
---|
[1445] | 743 | /// |
---|
| 744 | ///This magic function takes a container as its argument |
---|
| 745 | ///and fills its elements |
---|
| 746 | ///with new row (i.e. variables) |
---|
| 747 | ///\param t can be |
---|
| 748 | ///- a standard STL compatible iterable container with |
---|
| 749 | ///\ref Row as its \c values_type |
---|
| 750 | ///like |
---|
| 751 | ///\code |
---|
| 752 | ///std::vector<LpSolverBase::Row> |
---|
| 753 | ///std::list<LpSolverBase::Row> |
---|
| 754 | ///\endcode |
---|
| 755 | ///- a standard STL compatible iterable container with |
---|
| 756 | ///\ref Row as its \c mapped_type |
---|
| 757 | ///like |
---|
| 758 | ///\code |
---|
| 759 | ///std::map<AnyType,LpSolverBase::Row> |
---|
| 760 | ///\endcode |
---|
| 761 | ///- an iterable lemon \ref concept::WriteMap "write map" like |
---|
| 762 | ///\code |
---|
| 763 | ///ListGraph::NodeMap<LpSolverBase::Row> |
---|
| 764 | ///ListGraph::EdgeMap<LpSolverBase::Row> |
---|
| 765 | ///\endcode |
---|
| 766 | ///\return The number of rows created. |
---|
| 767 | #ifdef DOXYGEN |
---|
| 768 | template<class T> |
---|
| 769 | int addRowSet(T &t) { return 0;} |
---|
| 770 | #else |
---|
| 771 | template<class T> |
---|
| 772 | typename enable_if<typename T::value_type::LpSolverRow,int>::type |
---|
| 773 | addRowSet(T &t,dummy<0> = 0) { |
---|
| 774 | int s=0; |
---|
| 775 | for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addRow();s++;} |
---|
| 776 | return s; |
---|
| 777 | } |
---|
| 778 | template<class T> |
---|
| 779 | typename enable_if<typename T::value_type::second_type::LpSolverRow, |
---|
| 780 | int>::type |
---|
| 781 | addRowSet(T &t,dummy<1> = 1) { |
---|
| 782 | int s=0; |
---|
| 783 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 784 | i->second=addRow(); |
---|
| 785 | s++; |
---|
| 786 | } |
---|
| 787 | return s; |
---|
| 788 | } |
---|
| 789 | template<class T> |
---|
| 790 | typename enable_if<typename T::ValueSet::value_type::LpSolverRow, |
---|
| 791 | int>::type |
---|
| 792 | addRowSet(T &t,dummy<2> = 2) { |
---|
| 793 | ///\bug <tt>return addRowSet(t.valueSet());</tt> should also work. |
---|
| 794 | int s=0; |
---|
| 795 | for(typename T::ValueSet::iterator i=t.valueSet().begin(); |
---|
| 796 | i!=t.valueSet().end(); |
---|
| 797 | ++i) |
---|
| 798 | { |
---|
| 799 | *i=addRow(); |
---|
| 800 | s++; |
---|
| 801 | } |
---|
| 802 | return s; |
---|
| 803 | } |
---|
| 804 | #endif |
---|
| 805 | |
---|
| 806 | ///Set a row (i.e a constraint) of the LP |
---|
[1253] | 807 | |
---|
[1258] | 808 | ///\param r is the row to be modified |
---|
[1259] | 809 | ///\param l is lower bound (-\ref INF means no bound) |
---|
[1258] | 810 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 811 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[1253] | 812 | ///\bug This is a temportary function. The interface will change to |
---|
| 813 | ///a better one. |
---|
[1328] | 814 | ///\todo Option to control whether a constraint with a single variable is |
---|
| 815 | ///added or not. |
---|
[1258] | 816 | void setRow(Row r, Value l,const Expr &e, Value u) { |
---|
[1253] | 817 | std::vector<int> indices; |
---|
| 818 | std::vector<Value> values; |
---|
| 819 | indices.push_back(0); |
---|
| 820 | values.push_back(0); |
---|
[1258] | 821 | for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i) |
---|
[1256] | 822 | if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! |
---|
| 823 | indices.push_back(cols.floatingId((*i).first.id)); |
---|
| 824 | values.push_back((*i).second); |
---|
| 825 | } |
---|
[1253] | 826 | _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, |
---|
| 827 | &indices[0],&values[0]); |
---|
[1405] | 828 | // _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); |
---|
| 829 | // _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); |
---|
| 830 | _setRowBounds(rows.floatingId(r.id),l-e.constComp(),u-e.constComp()); |
---|
[1258] | 831 | } |
---|
| 832 | |
---|
[1445] | 833 | ///Set a row (i.e a constraint) of the LP |
---|
[1264] | 834 | |
---|
| 835 | ///\param r is the row to be modified |
---|
| 836 | ///\param c is a linear expression (see \ref Constr) |
---|
| 837 | void setRow(Row r, const Constr &c) { |
---|
[1273] | 838 | setRow(r, |
---|
[1275] | 839 | c.lowerBounded()?c.lowerBound():-INF, |
---|
[1273] | 840 | c.expr(), |
---|
[1275] | 841 | c.upperBounded()?c.upperBound():INF); |
---|
[1264] | 842 | } |
---|
| 843 | |
---|
[1445] | 844 | ///Add a new row (i.e a new constraint) to the LP |
---|
[1258] | 845 | |
---|
[1259] | 846 | ///\param l is the lower bound (-\ref INF means no bound) |
---|
[1258] | 847 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 848 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[1258] | 849 | ///\return The created row. |
---|
| 850 | ///\bug This is a temportary function. The interface will change to |
---|
| 851 | ///a better one. |
---|
| 852 | Row addRow(Value l,const Expr &e, Value u) { |
---|
| 853 | Row r=addRow(); |
---|
| 854 | setRow(r,l,e,u); |
---|
[1253] | 855 | return r; |
---|
| 856 | } |
---|
| 857 | |
---|
[1445] | 858 | ///Add a new row (i.e a new constraint) to the LP |
---|
[1264] | 859 | |
---|
| 860 | ///\param c is a linear expression (see \ref Constr) |
---|
| 861 | ///\return The created row. |
---|
| 862 | Row addRow(const Constr &c) { |
---|
| 863 | Row r=addRow(); |
---|
| 864 | setRow(r,c); |
---|
| 865 | return r; |
---|
| 866 | } |
---|
[1542] | 867 | ///Erase a coloumn (i.e a variable) from the LP |
---|
| 868 | |
---|
| 869 | ///\param c is the coloumn to be deleted |
---|
| 870 | ///\todo Please check this |
---|
| 871 | void eraseCol(Col c) { |
---|
| 872 | _eraseCol(cols.floatingId(c.id)); |
---|
| 873 | cols.erase(c.id); |
---|
| 874 | } |
---|
| 875 | ///Erase a row (i.e a constraint) from the LP |
---|
| 876 | |
---|
| 877 | ///\param r is the row to be deleted |
---|
| 878 | ///\todo Please check this |
---|
| 879 | void eraseRow(Row r) { |
---|
| 880 | _eraseRow(rows.floatingId(r.id)); |
---|
| 881 | rows.erase(r.id); |
---|
| 882 | } |
---|
[1264] | 883 | |
---|
[1436] | 884 | ///Set an element of the coefficient matrix of the LP |
---|
| 885 | |
---|
| 886 | ///\param r is the row of the element to be modified |
---|
| 887 | ///\param c is the coloumn of the element to be modified |
---|
| 888 | ///\param val is the new value of the coefficient |
---|
| 889 | void setCoeff(Row r, Col c, Value val){ |
---|
| 890 | _setCoeff(rows.floatingId(r.id),cols.floatingId(c.id), val); |
---|
| 891 | } |
---|
| 892 | |
---|
[1253] | 893 | /// Set the lower bound of a column (i.e a variable) |
---|
| 894 | |
---|
[1293] | 895 | /// The upper bound of a variable (column) has to be given by an |
---|
[1253] | 896 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 897 | /// Value or -\ref INF. |
---|
[1293] | 898 | void colLowerBound(Col c, Value value) { |
---|
[1253] | 899 | _setColLowerBound(cols.floatingId(c.id),value); |
---|
| 900 | } |
---|
| 901 | /// Set the upper bound of a column (i.e a variable) |
---|
| 902 | |
---|
[1293] | 903 | /// The upper bound of a variable (column) has to be given by an |
---|
[1253] | 904 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 905 | /// Value or \ref INF. |
---|
[1293] | 906 | void colUpperBound(Col c, Value value) { |
---|
[1253] | 907 | _setColUpperBound(cols.floatingId(c.id),value); |
---|
| 908 | }; |
---|
[1293] | 909 | /// Set the lower and the upper bounds of a column (i.e a variable) |
---|
| 910 | |
---|
| 911 | /// The lower and the upper bounds of |
---|
| 912 | /// a variable (column) have to be given by an |
---|
| 913 | /// extended number of type Value, i.e. a finite number of type |
---|
| 914 | /// Value, -\ref INF or \ref INF. |
---|
| 915 | void colBounds(Col c, Value lower, Value upper) { |
---|
| 916 | _setColLowerBound(cols.floatingId(c.id),lower); |
---|
| 917 | _setColUpperBound(cols.floatingId(c.id),upper); |
---|
| 918 | } |
---|
| 919 | |
---|
[1405] | 920 | // /// Set the lower bound of a row (i.e a constraint) |
---|
[1253] | 921 | |
---|
[1405] | 922 | // /// The lower bound of a linear expression (row) has to be given by an |
---|
| 923 | // /// extended number of type Value, i.e. a finite number of type |
---|
| 924 | // /// Value or -\ref INF. |
---|
| 925 | // void rowLowerBound(Row r, Value value) { |
---|
| 926 | // _setRowLowerBound(rows.floatingId(r.id),value); |
---|
| 927 | // }; |
---|
| 928 | // /// Set the upper bound of a row (i.e a constraint) |
---|
[1253] | 929 | |
---|
[1405] | 930 | // /// The upper bound of a linear expression (row) has to be given by an |
---|
| 931 | // /// extended number of type Value, i.e. a finite number of type |
---|
| 932 | // /// Value or \ref INF. |
---|
| 933 | // void rowUpperBound(Row r, Value value) { |
---|
| 934 | // _setRowUpperBound(rows.floatingId(r.id),value); |
---|
| 935 | // }; |
---|
| 936 | |
---|
| 937 | /// Set the lower and the upper bounds of a row (i.e a constraint) |
---|
[1293] | 938 | |
---|
| 939 | /// The lower and the upper bounds of |
---|
| 940 | /// a constraint (row) have to be given by an |
---|
| 941 | /// extended number of type Value, i.e. a finite number of type |
---|
| 942 | /// Value, -\ref INF or \ref INF. |
---|
| 943 | void rowBounds(Row c, Value lower, Value upper) { |
---|
[1379] | 944 | _setRowBounds(rows.floatingId(c.id),lower, upper); |
---|
| 945 | // _setRowUpperBound(rows.floatingId(c.id),upper); |
---|
[1293] | 946 | } |
---|
| 947 | |
---|
[1253] | 948 | ///Set an element of the objective function |
---|
[1293] | 949 | void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); }; |
---|
[1253] | 950 | ///Set the objective function |
---|
| 951 | |
---|
| 952 | ///\param e is a linear expression of type \ref Expr. |
---|
[1323] | 953 | ///\bug The previous objective function is not cleared! |
---|
[1253] | 954 | void setObj(Expr e) { |
---|
[1377] | 955 | _clearObj(); |
---|
[1253] | 956 | for (Expr::iterator i=e.begin(); i!=e.end(); ++i) |
---|
[1293] | 957 | objCoeff((*i).first,(*i).second); |
---|
[1323] | 958 | obj_const_comp=e.constComp(); |
---|
[1253] | 959 | } |
---|
[1263] | 960 | |
---|
[1312] | 961 | ///Maximize |
---|
| 962 | void max() { _setMax(); } |
---|
| 963 | ///Minimize |
---|
| 964 | void min() { _setMin(); } |
---|
| 965 | |
---|
| 966 | |
---|
[1263] | 967 | ///@} |
---|
| 968 | |
---|
| 969 | |
---|
[1294] | 970 | ///\name Solve the LP |
---|
[1263] | 971 | |
---|
| 972 | ///@{ |
---|
| 973 | |
---|
[1458] | 974 | ///\e Solve the LP problem at hand |
---|
| 975 | /// |
---|
| 976 | ///\return The result of the optimization procedure. Possible values and their meanings can be found in the documentation of \ref SolveExitStatus. |
---|
| 977 | /// |
---|
| 978 | ///\todo Which method is used to solve the problem |
---|
[1303] | 979 | SolveExitStatus solve() { return _solve(); } |
---|
[1263] | 980 | |
---|
| 981 | ///@} |
---|
| 982 | |
---|
[1294] | 983 | ///\name Obtain the solution |
---|
[1263] | 984 | |
---|
| 985 | ///@{ |
---|
| 986 | |
---|
[1460] | 987 | /// The status of the primal problem (the original LP problem) |
---|
[1312] | 988 | SolutionStatus primalStatus() { |
---|
| 989 | return _getPrimalStatus(); |
---|
[1294] | 990 | } |
---|
| 991 | |
---|
[1460] | 992 | /// The status of the dual (of the original LP) problem |
---|
| 993 | SolutionStatus dualStatus() { |
---|
| 994 | return _getDualStatus(); |
---|
| 995 | } |
---|
| 996 | |
---|
| 997 | ///The type of the original LP problem |
---|
[1462] | 998 | ProblemTypes problemType() { |
---|
[1460] | 999 | return _getProblemType(); |
---|
| 1000 | } |
---|
| 1001 | |
---|
[1294] | 1002 | ///\e |
---|
[1293] | 1003 | Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); } |
---|
[1263] | 1004 | |
---|
[1312] | 1005 | ///\e |
---|
[1787] | 1006 | Value dual(Row r) { return _getDual(rows.floatingId(r.id)); } |
---|
| 1007 | |
---|
| 1008 | ///\e |
---|
[1312] | 1009 | |
---|
| 1010 | ///\return |
---|
| 1011 | ///- \ref INF or -\ref INF means either infeasibility or unboundedness |
---|
| 1012 | /// of the primal problem, depending on whether we minimize or maximize. |
---|
[1364] | 1013 | ///- \ref NaN if no primal solution is found. |
---|
[1312] | 1014 | ///- The (finite) objective value if an optimal solution is found. |
---|
[1323] | 1015 | Value primalValue() { return _getPrimalValue()+obj_const_comp;} |
---|
[1263] | 1016 | ///@} |
---|
[1253] | 1017 | |
---|
[1248] | 1018 | }; |
---|
[1246] | 1019 | |
---|
[1272] | 1020 | ///\e |
---|
| 1021 | |
---|
| 1022 | ///\relates LpSolverBase::Expr |
---|
| 1023 | /// |
---|
| 1024 | inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, |
---|
| 1025 | const LpSolverBase::Expr &b) |
---|
| 1026 | { |
---|
| 1027 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1028 | tmp+=b; |
---|
[1272] | 1029 | return tmp; |
---|
| 1030 | } |
---|
| 1031 | ///\e |
---|
| 1032 | |
---|
| 1033 | ///\relates LpSolverBase::Expr |
---|
| 1034 | /// |
---|
| 1035 | inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, |
---|
| 1036 | const LpSolverBase::Expr &b) |
---|
| 1037 | { |
---|
| 1038 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1039 | tmp-=b; |
---|
[1272] | 1040 | return tmp; |
---|
| 1041 | } |
---|
| 1042 | ///\e |
---|
| 1043 | |
---|
| 1044 | ///\relates LpSolverBase::Expr |
---|
| 1045 | /// |
---|
| 1046 | inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, |
---|
[1273] | 1047 | const LpSolverBase::Value &b) |
---|
[1272] | 1048 | { |
---|
| 1049 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1050 | tmp*=b; |
---|
[1272] | 1051 | return tmp; |
---|
| 1052 | } |
---|
| 1053 | |
---|
| 1054 | ///\e |
---|
| 1055 | |
---|
| 1056 | ///\relates LpSolverBase::Expr |
---|
| 1057 | /// |
---|
[1273] | 1058 | inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, |
---|
[1272] | 1059 | const LpSolverBase::Expr &b) |
---|
| 1060 | { |
---|
| 1061 | LpSolverBase::Expr tmp(b); |
---|
[1766] | 1062 | tmp*=a; |
---|
[1272] | 1063 | return tmp; |
---|
| 1064 | } |
---|
| 1065 | ///\e |
---|
| 1066 | |
---|
| 1067 | ///\relates LpSolverBase::Expr |
---|
| 1068 | /// |
---|
| 1069 | inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, |
---|
[1273] | 1070 | const LpSolverBase::Value &b) |
---|
[1272] | 1071 | { |
---|
| 1072 | LpSolverBase::Expr tmp(a); |
---|
[1766] | 1073 | tmp/=b; |
---|
[1272] | 1074 | return tmp; |
---|
| 1075 | } |
---|
| 1076 | |
---|
| 1077 | ///\e |
---|
| 1078 | |
---|
| 1079 | ///\relates LpSolverBase::Constr |
---|
| 1080 | /// |
---|
| 1081 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
| 1082 | const LpSolverBase::Expr &f) |
---|
| 1083 | { |
---|
| 1084 | return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | ///\e |
---|
| 1088 | |
---|
| 1089 | ///\relates LpSolverBase::Constr |
---|
| 1090 | /// |
---|
[1273] | 1091 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, |
---|
[1272] | 1092 | const LpSolverBase::Expr &f) |
---|
| 1093 | { |
---|
| 1094 | return LpSolverBase::Constr(e,f); |
---|
| 1095 | } |
---|
| 1096 | |
---|
| 1097 | ///\e |
---|
| 1098 | |
---|
| 1099 | ///\relates LpSolverBase::Constr |
---|
| 1100 | /// |
---|
| 1101 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
[1273] | 1102 | const LpSolverBase::Value &f) |
---|
[1272] | 1103 | { |
---|
| 1104 | return LpSolverBase::Constr(e,f); |
---|
| 1105 | } |
---|
| 1106 | |
---|
| 1107 | ///\e |
---|
| 1108 | |
---|
| 1109 | ///\relates LpSolverBase::Constr |
---|
| 1110 | /// |
---|
| 1111 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
| 1112 | const LpSolverBase::Expr &f) |
---|
| 1113 | { |
---|
| 1114 | return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); |
---|
| 1115 | } |
---|
| 1116 | |
---|
| 1117 | |
---|
| 1118 | ///\e |
---|
| 1119 | |
---|
| 1120 | ///\relates LpSolverBase::Constr |
---|
| 1121 | /// |
---|
[1273] | 1122 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, |
---|
[1272] | 1123 | const LpSolverBase::Expr &f) |
---|
| 1124 | { |
---|
| 1125 | return LpSolverBase::Constr(f,e); |
---|
| 1126 | } |
---|
| 1127 | |
---|
| 1128 | |
---|
| 1129 | ///\e |
---|
| 1130 | |
---|
| 1131 | ///\relates LpSolverBase::Constr |
---|
| 1132 | /// |
---|
| 1133 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
[1273] | 1134 | const LpSolverBase::Value &f) |
---|
[1272] | 1135 | { |
---|
| 1136 | return LpSolverBase::Constr(f,e); |
---|
| 1137 | } |
---|
| 1138 | |
---|
| 1139 | ///\e |
---|
| 1140 | |
---|
| 1141 | ///\relates LpSolverBase::Constr |
---|
| 1142 | /// |
---|
| 1143 | inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, |
---|
| 1144 | const LpSolverBase::Expr &f) |
---|
| 1145 | { |
---|
| 1146 | return LpSolverBase::Constr(0,e-f,0); |
---|
| 1147 | } |
---|
| 1148 | |
---|
| 1149 | ///\e |
---|
| 1150 | |
---|
| 1151 | ///\relates LpSolverBase::Constr |
---|
| 1152 | /// |
---|
[1273] | 1153 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, |
---|
[1272] | 1154 | const LpSolverBase::Constr&c) |
---|
| 1155 | { |
---|
| 1156 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1157 | ///\todo Create an own exception type. |
---|
| 1158 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
| 1159 | else tmp.lowerBound()=n; |
---|
[1272] | 1160 | return tmp; |
---|
| 1161 | } |
---|
| 1162 | ///\e |
---|
| 1163 | |
---|
| 1164 | ///\relates LpSolverBase::Constr |
---|
| 1165 | /// |
---|
| 1166 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, |
---|
[1273] | 1167 | const LpSolverBase::Value &n) |
---|
[1272] | 1168 | { |
---|
| 1169 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1170 | ///\todo Create an own exception type. |
---|
| 1171 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
| 1172 | else tmp.upperBound()=n; |
---|
[1272] | 1173 | return tmp; |
---|
| 1174 | } |
---|
| 1175 | |
---|
| 1176 | ///\e |
---|
| 1177 | |
---|
| 1178 | ///\relates LpSolverBase::Constr |
---|
| 1179 | /// |
---|
[1273] | 1180 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, |
---|
[1272] | 1181 | const LpSolverBase::Constr&c) |
---|
| 1182 | { |
---|
| 1183 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1184 | ///\todo Create an own exception type. |
---|
| 1185 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
| 1186 | else tmp.upperBound()=n; |
---|
[1272] | 1187 | return tmp; |
---|
| 1188 | } |
---|
| 1189 | ///\e |
---|
| 1190 | |
---|
| 1191 | ///\relates LpSolverBase::Constr |
---|
| 1192 | /// |
---|
| 1193 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, |
---|
[1273] | 1194 | const LpSolverBase::Value &n) |
---|
[1272] | 1195 | { |
---|
| 1196 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 1197 | ///\todo Create an own exception type. |
---|
| 1198 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
| 1199 | else tmp.lowerBound()=n; |
---|
[1272] | 1200 | return tmp; |
---|
| 1201 | } |
---|
| 1202 | |
---|
[1445] | 1203 | ///\e |
---|
| 1204 | |
---|
| 1205 | ///\relates LpSolverBase::DualExpr |
---|
| 1206 | /// |
---|
| 1207 | inline LpSolverBase::DualExpr operator+(const LpSolverBase::DualExpr &a, |
---|
| 1208 | const LpSolverBase::DualExpr &b) |
---|
| 1209 | { |
---|
| 1210 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1211 | tmp+=b; |
---|
[1445] | 1212 | return tmp; |
---|
| 1213 | } |
---|
| 1214 | ///\e |
---|
| 1215 | |
---|
| 1216 | ///\relates LpSolverBase::DualExpr |
---|
| 1217 | /// |
---|
| 1218 | inline LpSolverBase::DualExpr operator-(const LpSolverBase::DualExpr &a, |
---|
| 1219 | const LpSolverBase::DualExpr &b) |
---|
| 1220 | { |
---|
| 1221 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1222 | tmp-=b; |
---|
[1445] | 1223 | return tmp; |
---|
| 1224 | } |
---|
| 1225 | ///\e |
---|
| 1226 | |
---|
| 1227 | ///\relates LpSolverBase::DualExpr |
---|
| 1228 | /// |
---|
| 1229 | inline LpSolverBase::DualExpr operator*(const LpSolverBase::DualExpr &a, |
---|
| 1230 | const LpSolverBase::Value &b) |
---|
| 1231 | { |
---|
| 1232 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1233 | tmp*=b; |
---|
[1445] | 1234 | return tmp; |
---|
| 1235 | } |
---|
| 1236 | |
---|
| 1237 | ///\e |
---|
| 1238 | |
---|
| 1239 | ///\relates LpSolverBase::DualExpr |
---|
| 1240 | /// |
---|
| 1241 | inline LpSolverBase::DualExpr operator*(const LpSolverBase::Value &a, |
---|
| 1242 | const LpSolverBase::DualExpr &b) |
---|
| 1243 | { |
---|
| 1244 | LpSolverBase::DualExpr tmp(b); |
---|
[1766] | 1245 | tmp*=a; |
---|
[1445] | 1246 | return tmp; |
---|
| 1247 | } |
---|
| 1248 | ///\e |
---|
| 1249 | |
---|
| 1250 | ///\relates LpSolverBase::DualExpr |
---|
| 1251 | /// |
---|
| 1252 | inline LpSolverBase::DualExpr operator/(const LpSolverBase::DualExpr &a, |
---|
| 1253 | const LpSolverBase::Value &b) |
---|
| 1254 | { |
---|
| 1255 | LpSolverBase::DualExpr tmp(a); |
---|
[1766] | 1256 | tmp/=b; |
---|
[1445] | 1257 | return tmp; |
---|
| 1258 | } |
---|
| 1259 | |
---|
[1272] | 1260 | |
---|
[1246] | 1261 | } //namespace lemon |
---|
| 1262 | |
---|
| 1263 | #endif //LEMON_LP_BASE_H |
---|