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