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