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