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