[1247] | 1 | /* -*- C++ -*- |
---|
[1253] | 2 | * src/lemon/lp_base.h - Part of LEMON, a generic C++ optimization library |
---|
[1247] | 3 | * |
---|
| 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[1246] | 17 | #ifndef LEMON_LP_BASE_H |
---|
| 18 | #define LEMON_LP_BASE_H |
---|
| 19 | |
---|
[1253] | 20 | #include<vector> |
---|
[1272] | 21 | #include<map> |
---|
[1256] | 22 | #include<limits> |
---|
[1273] | 23 | #include<math.h> |
---|
[1253] | 24 | |
---|
[1256] | 25 | #include<lemon/utility.h> |
---|
[1253] | 26 | #include<lemon/error.h> |
---|
[1256] | 27 | #include<lemon/invalid.h> |
---|
[1253] | 28 | |
---|
[1272] | 29 | //#include"lin_expr.h" |
---|
| 30 | |
---|
[1246] | 31 | ///\file |
---|
| 32 | ///\brief The interface of the LP solver interface. |
---|
| 33 | namespace lemon { |
---|
[1253] | 34 | |
---|
| 35 | ///Internal data structure to convert floating id's to fix one's |
---|
| 36 | |
---|
[1279] | 37 | ///\todo This might be implemented to be also usable in other places. |
---|
[1253] | 38 | class _FixId |
---|
| 39 | { |
---|
| 40 | std::vector<int> index; |
---|
| 41 | std::vector<int> cross; |
---|
| 42 | int first_free; |
---|
| 43 | public: |
---|
| 44 | _FixId() : first_free(-1) {}; |
---|
| 45 | ///Convert a floating id to a fix one |
---|
| 46 | |
---|
| 47 | ///\param n is a floating id |
---|
| 48 | ///\return the corresponding fix id |
---|
| 49 | int fixId(int n) {return cross[n];} |
---|
| 50 | ///Convert a fix id to a floating one |
---|
| 51 | |
---|
| 52 | ///\param n is a fix id |
---|
| 53 | ///\return the corresponding floating id |
---|
| 54 | int floatingId(int n) { return index[n];} |
---|
| 55 | ///Add a new floating id. |
---|
| 56 | |
---|
| 57 | ///\param n is a floating id |
---|
| 58 | ///\return the fix id of the new value |
---|
| 59 | ///\todo Multiple additions should also be handled. |
---|
| 60 | int insert(int n) |
---|
| 61 | { |
---|
| 62 | if(n>=int(cross.size())) { |
---|
| 63 | cross.resize(n+1); |
---|
| 64 | if(first_free==-1) { |
---|
| 65 | cross[n]=index.size(); |
---|
| 66 | index.push_back(n); |
---|
| 67 | } |
---|
| 68 | else { |
---|
| 69 | cross[n]=first_free; |
---|
| 70 | int next=index[first_free]; |
---|
| 71 | index[first_free]=n; |
---|
| 72 | first_free=next; |
---|
| 73 | } |
---|
[1256] | 74 | return cross[n]; |
---|
[1253] | 75 | } |
---|
[1273] | 76 | ///\todo Create an own exception type. |
---|
[1253] | 77 | else throw LogicError(); //floatingId-s must form a continuous range; |
---|
| 78 | } |
---|
| 79 | ///Remove a fix id. |
---|
| 80 | |
---|
| 81 | ///\param n is a fix id |
---|
| 82 | /// |
---|
| 83 | void erase(int n) |
---|
| 84 | { |
---|
| 85 | int fl=index[n]; |
---|
| 86 | index[n]=first_free; |
---|
| 87 | first_free=n; |
---|
| 88 | for(int i=fl+1;i<int(cross.size());++i) { |
---|
| 89 | cross[i-1]=cross[i]; |
---|
| 90 | index[cross[i]]--; |
---|
| 91 | } |
---|
| 92 | cross.pop_back(); |
---|
| 93 | } |
---|
| 94 | ///An upper bound on the largest fix id. |
---|
| 95 | |
---|
| 96 | ///\todo Do we need this? |
---|
| 97 | /// |
---|
| 98 | std::size_t maxFixId() { return cross.size()-1; } |
---|
| 99 | |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | ///Common base class for LP solvers |
---|
[1246] | 103 | class LpSolverBase { |
---|
[1253] | 104 | |
---|
[1247] | 105 | public: |
---|
| 106 | |
---|
[1263] | 107 | ///\e |
---|
[1293] | 108 | enum SolutionStatus { |
---|
[1263] | 109 | ///\e |
---|
[1293] | 110 | SOLVED = 0, |
---|
[1263] | 111 | ///\e |
---|
[1293] | 112 | UNSOLVED = 1 |
---|
[1291] | 113 | }; |
---|
| 114 | |
---|
| 115 | ///\e |
---|
| 116 | enum SolutionType { |
---|
[1295] | 117 | ///Feasible solution has'n been found (but may exist). |
---|
| 118 | |
---|
| 119 | ///\todo NOTFOUND might be a better name. |
---|
| 120 | /// |
---|
[1293] | 121 | UNDEFINED = 0, |
---|
[1295] | 122 | ///The problem has no feasible solution |
---|
[1293] | 123 | INFEASIBLE = 1, |
---|
[1295] | 124 | ///Feasible solution found |
---|
[1293] | 125 | FEASIBLE = 2, |
---|
[1295] | 126 | ///Optimal solution exists and found |
---|
| 127 | OPTIMAL = 3, |
---|
| 128 | ///The cost function is unbounded |
---|
| 129 | |
---|
| 130 | ///\todo Give a feasible solution and an infinite ray (and the |
---|
| 131 | ///corresponding bases) |
---|
| 132 | INFINITE = 4 |
---|
[1263] | 133 | }; |
---|
| 134 | |
---|
[1256] | 135 | ///The floating point type used by the solver |
---|
[1247] | 136 | typedef double Value; |
---|
[1256] | 137 | ///The infinity constant |
---|
[1247] | 138 | static const Value INF; |
---|
[1264] | 139 | ///The not a number constant |
---|
| 140 | static const Value NaN; |
---|
[1253] | 141 | |
---|
[1256] | 142 | ///Refer to a column of the LP. |
---|
| 143 | |
---|
| 144 | ///This type is used to refer to a column of the LP. |
---|
| 145 | /// |
---|
| 146 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 147 | ///other columns. |
---|
[1256] | 148 | /// |
---|
| 149 | ///\todo Document what can one do with a Col (INVALID, comparing, |
---|
| 150 | ///it is similar to Node/Edge) |
---|
| 151 | class Col { |
---|
| 152 | protected: |
---|
| 153 | int id; |
---|
| 154 | friend class LpSolverBase; |
---|
| 155 | public: |
---|
[1259] | 156 | typedef Value ExprValue; |
---|
[1256] | 157 | typedef True LpSolverCol; |
---|
| 158 | Col() {} |
---|
| 159 | Col(const Invalid&) : id(-1) {} |
---|
| 160 | bool operator<(Col c) const {return id<c.id;} |
---|
| 161 | bool operator==(Col c) const {return id==c.id;} |
---|
| 162 | bool operator!=(Col c) const {return id==c.id;} |
---|
| 163 | }; |
---|
| 164 | |
---|
| 165 | ///Refer to a row of the LP. |
---|
| 166 | |
---|
| 167 | ///This type is used to refer to a row of the LP. |
---|
| 168 | /// |
---|
| 169 | ///Its value remains valid and correct even after the addition or erase of |
---|
[1273] | 170 | ///other rows. |
---|
[1256] | 171 | /// |
---|
| 172 | ///\todo Document what can one do with a Row (INVALID, comparing, |
---|
| 173 | ///it is similar to Node/Edge) |
---|
| 174 | class Row { |
---|
| 175 | protected: |
---|
| 176 | int id; |
---|
| 177 | friend class LpSolverBase; |
---|
| 178 | public: |
---|
[1259] | 179 | typedef Value ExprValue; |
---|
[1256] | 180 | typedef True LpSolverRow; |
---|
| 181 | Row() {} |
---|
| 182 | Row(const Invalid&) : id(-1) {} |
---|
| 183 | typedef True LpSolverRow; |
---|
| 184 | bool operator<(Row c) const {return id<c.id;} |
---|
| 185 | bool operator==(Row c) const {return id==c.id;} |
---|
| 186 | bool operator!=(Row c) const {return id==c.id;} |
---|
| 187 | }; |
---|
[1259] | 188 | |
---|
[1279] | 189 | ///Linear expression of variables and a constant component |
---|
| 190 | |
---|
| 191 | ///This data structure strores a linear expression of the variables |
---|
| 192 | ///(\ref Col "Col"s) and also has a constant component. |
---|
| 193 | /// |
---|
| 194 | ///There are several ways to access and modify the contents of this |
---|
| 195 | ///container. |
---|
| 196 | ///- Its it fully compatible with \c std::map<Col,double>, so for expamle |
---|
| 197 | ///if \c e is an Expr and \c v and \c w are of type \ref Col then you can |
---|
| 198 | ///read and modify the coefficients like |
---|
| 199 | ///these. |
---|
| 200 | ///\code |
---|
| 201 | ///e[v]=5; |
---|
| 202 | ///e[v]+=12; |
---|
| 203 | ///e.erase(v); |
---|
| 204 | ///\endcode |
---|
| 205 | ///or you can also iterate through its elements. |
---|
| 206 | ///\code |
---|
| 207 | ///double s=0; |
---|
| 208 | ///for(LpSolverBase::Expr::iterator i=e.begin();i!=e.end();++i) |
---|
| 209 | /// s+=i->second; |
---|
| 210 | ///\endcode |
---|
| 211 | ///(This code computes the sum of all coefficients). |
---|
| 212 | ///- Numbers (<tt>double</tt>'s) |
---|
| 213 | ///and variables (\ref Col "Col"s) directly convert to an |
---|
| 214 | ///\ref Expr and the usual linear operations are defined so |
---|
| 215 | ///\code |
---|
| 216 | ///v+w |
---|
| 217 | ///2*v-3.12*(v-w/2)+2 |
---|
| 218 | ///v*2.1+(3*v+(v*12+w+6)*3)/2 |
---|
| 219 | ///\endcode |
---|
| 220 | ///are valid expressions. The usual assignment operations are also defined. |
---|
| 221 | ///\code |
---|
| 222 | ///e=v+w; |
---|
| 223 | ///e+=2*v-3.12*(v-w/2)+2; |
---|
| 224 | ///e*=3.4; |
---|
| 225 | ///e/=5; |
---|
| 226 | ///\endcode |
---|
| 227 | ///- The constant member can be set and read by \ref constComp() |
---|
| 228 | ///\code |
---|
| 229 | ///e.constComp()=12; |
---|
| 230 | ///double c=e.constComp(); |
---|
| 231 | ///\endcode |
---|
| 232 | /// |
---|
| 233 | ///\note that \ref clear() not only sets all coefficients to 0 but also |
---|
| 234 | ///clears the constant components. |
---|
[1273] | 235 | class Expr : public std::map<Col,Value> |
---|
[1272] | 236 | { |
---|
| 237 | public: |
---|
[1273] | 238 | typedef LpSolverBase::Col Key; |
---|
| 239 | typedef LpSolverBase::Value Value; |
---|
[1272] | 240 | |
---|
| 241 | protected: |
---|
[1273] | 242 | typedef std::map<Col,Value> Base; |
---|
[1272] | 243 | |
---|
[1273] | 244 | Value const_comp; |
---|
[1272] | 245 | public: |
---|
| 246 | typedef True IsLinExpression; |
---|
| 247 | ///\e |
---|
| 248 | Expr() : Base(), const_comp(0) { } |
---|
| 249 | ///\e |
---|
[1273] | 250 | Expr(const Key &v) : const_comp(0) { |
---|
[1272] | 251 | Base::insert(std::make_pair(v, 1)); |
---|
| 252 | } |
---|
| 253 | ///\e |
---|
[1273] | 254 | Expr(const Value &v) : const_comp(v) {} |
---|
[1272] | 255 | ///\e |
---|
[1273] | 256 | void set(const Key &v,const Value &c) { |
---|
[1272] | 257 | Base::insert(std::make_pair(v, c)); |
---|
| 258 | } |
---|
| 259 | ///\e |
---|
[1273] | 260 | Value &constComp() { return const_comp; } |
---|
[1272] | 261 | ///\e |
---|
[1273] | 262 | const Value &constComp() const { return const_comp; } |
---|
[1272] | 263 | |
---|
| 264 | ///Removes the components with zero coefficient. |
---|
| 265 | void simplify() { |
---|
| 266 | for (Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 267 | Base::iterator j=i; |
---|
| 268 | ++j; |
---|
| 269 | if ((*i).second==0) Base::erase(i); |
---|
| 270 | j=i; |
---|
| 271 | } |
---|
| 272 | } |
---|
[1273] | 273 | |
---|
| 274 | ///Sets all coefficients and the constant component to 0. |
---|
| 275 | void clear() { |
---|
| 276 | Base::clear(); |
---|
| 277 | const_comp=0; |
---|
| 278 | } |
---|
| 279 | |
---|
[1272] | 280 | ///\e |
---|
| 281 | Expr &operator+=(const Expr &e) { |
---|
| 282 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 283 | (*this)[j->first]+=j->second; |
---|
| 284 | ///\todo it might be speeded up using "hints" |
---|
| 285 | const_comp+=e.const_comp; |
---|
| 286 | return *this; |
---|
| 287 | } |
---|
| 288 | ///\e |
---|
| 289 | Expr &operator-=(const Expr &e) { |
---|
| 290 | for (Base::const_iterator j=e.begin(); j!=e.end(); ++j) |
---|
| 291 | (*this)[j->first]-=j->second; |
---|
| 292 | const_comp-=e.const_comp; |
---|
| 293 | return *this; |
---|
| 294 | } |
---|
| 295 | ///\e |
---|
[1273] | 296 | Expr &operator*=(const Value &c) { |
---|
[1272] | 297 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 298 | j->second*=c; |
---|
| 299 | const_comp*=c; |
---|
| 300 | return *this; |
---|
| 301 | } |
---|
| 302 | ///\e |
---|
[1273] | 303 | Expr &operator/=(const Value &c) { |
---|
[1272] | 304 | for (Base::iterator j=Base::begin(); j!=Base::end(); ++j) |
---|
| 305 | j->second/=c; |
---|
| 306 | const_comp/=c; |
---|
| 307 | return *this; |
---|
| 308 | } |
---|
| 309 | }; |
---|
| 310 | |
---|
[1264] | 311 | ///Linear constraint |
---|
[1272] | 312 | //typedef LinConstr<Expr> Constr; |
---|
| 313 | class Constr |
---|
| 314 | { |
---|
| 315 | public: |
---|
| 316 | typedef LpSolverBase::Expr Expr; |
---|
[1273] | 317 | typedef Expr::Key Key; |
---|
| 318 | typedef Expr::Value Value; |
---|
[1272] | 319 | |
---|
[1273] | 320 | static const Value INF; |
---|
| 321 | static const Value NaN; |
---|
| 322 | // static const Value INF=0; |
---|
| 323 | // static const Value NaN=1; |
---|
[1272] | 324 | |
---|
[1273] | 325 | protected: |
---|
| 326 | Expr _expr; |
---|
| 327 | Value _lb,_ub; |
---|
| 328 | public: |
---|
| 329 | ///\e |
---|
| 330 | Constr() : _expr(), _lb(NaN), _ub(NaN) {} |
---|
| 331 | ///\e |
---|
| 332 | Constr(Value lb,const Expr &e,Value ub) : |
---|
| 333 | _expr(e), _lb(lb), _ub(ub) {} |
---|
| 334 | ///\e |
---|
| 335 | Constr(const Expr &e,Value ub) : |
---|
| 336 | _expr(e), _lb(NaN), _ub(ub) {} |
---|
| 337 | ///\e |
---|
| 338 | Constr(Value lb,const Expr &e) : |
---|
| 339 | _expr(e), _lb(lb), _ub(NaN) {} |
---|
| 340 | ///\e |
---|
[1272] | 341 | Constr(const Expr &e) : |
---|
[1273] | 342 | _expr(e), _lb(NaN), _ub(NaN) {} |
---|
| 343 | ///\e |
---|
| 344 | void clear() |
---|
| 345 | { |
---|
| 346 | _expr.clear(); |
---|
| 347 | _lb=_ub=NaN; |
---|
| 348 | } |
---|
| 349 | ///\e |
---|
| 350 | Expr &expr() { return _expr; } |
---|
| 351 | ///\e |
---|
| 352 | const Expr &expr() const { return _expr; } |
---|
| 353 | ///\e |
---|
| 354 | Value &lowerBound() { return _lb; } |
---|
| 355 | ///\e |
---|
| 356 | const Value &lowerBound() const { return _lb; } |
---|
| 357 | ///\e |
---|
| 358 | Value &upperBound() { return _ub; } |
---|
| 359 | ///\e |
---|
| 360 | const Value &upperBound() const { return _ub; } |
---|
[1275] | 361 | ///\e |
---|
[1295] | 362 | bool lowerBounded() const { |
---|
| 363 | using namespace std; |
---|
| 364 | return isfinite(_lb); |
---|
| 365 | } |
---|
[1275] | 366 | ///\e |
---|
[1295] | 367 | bool upperBounded() const { |
---|
| 368 | using namespace std; |
---|
| 369 | return isfinite(_ub); |
---|
| 370 | } |
---|
[1272] | 371 | }; |
---|
| 372 | |
---|
[1253] | 373 | |
---|
| 374 | protected: |
---|
| 375 | _FixId rows; |
---|
| 376 | _FixId cols; |
---|
[1246] | 377 | |
---|
| 378 | virtual int _addCol() = 0; |
---|
| 379 | virtual int _addRow() = 0; |
---|
| 380 | virtual void _setRowCoeffs(int i, |
---|
[1251] | 381 | int length, |
---|
[1247] | 382 | int const * indices, |
---|
| 383 | Value const * values ) = 0; |
---|
[1246] | 384 | virtual void _setColCoeffs(int i, |
---|
[1251] | 385 | int length, |
---|
[1247] | 386 | int const * indices, |
---|
| 387 | Value const * values ) = 0; |
---|
[1294] | 388 | virtual void _setColLowerBound(int i, Value value) = 0; |
---|
| 389 | virtual void _setColUpperBound(int i, Value value) = 0; |
---|
| 390 | virtual void _setRowLowerBound(int i, Value value) = 0; |
---|
| 391 | virtual void _setRowUpperBound(int i, Value value) = 0; |
---|
| 392 | virtual void _setObjCoeff(int i, Value obj_coef) = 0; |
---|
| 393 | virtual SolutionStatus _solve() = 0; |
---|
| 394 | virtual Value _getPrimal(int i) = 0; |
---|
| 395 | virtual SolutionType _getPrimalType() = 0; |
---|
[1253] | 396 | |
---|
| 397 | |
---|
| 398 | void clearObj() {} |
---|
| 399 | public: |
---|
| 400 | |
---|
| 401 | |
---|
| 402 | ///\e |
---|
| 403 | virtual ~LpSolverBase() {} |
---|
| 404 | |
---|
[1294] | 405 | ///\name Build up and modify of the LP |
---|
[1263] | 406 | |
---|
| 407 | ///@{ |
---|
| 408 | |
---|
[1253] | 409 | ///Add a new empty column (i.e a new variable) to the LP |
---|
| 410 | Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;} |
---|
[1263] | 411 | |
---|
[1294] | 412 | ///\brief Adds several new columns |
---|
| 413 | ///(i.e a variables) at once |
---|
[1256] | 414 | /// |
---|
[1273] | 415 | ///This magic function takes a container as its argument |
---|
[1256] | 416 | ///and fills its elements |
---|
| 417 | ///with new columns (i.e. variables) |
---|
[1273] | 418 | ///\param t can be |
---|
| 419 | ///- a standard STL compatible iterable container with |
---|
| 420 | ///\ref Col as its \c values_type |
---|
| 421 | ///like |
---|
| 422 | ///\code |
---|
| 423 | ///std::vector<LpSolverBase::Col> |
---|
| 424 | ///std::list<LpSolverBase::Col> |
---|
| 425 | ///\endcode |
---|
| 426 | ///- a standard STL compatible iterable container with |
---|
| 427 | ///\ref Col as its \c mapped_type |
---|
| 428 | ///like |
---|
| 429 | ///\code |
---|
| 430 | ///std::map<AnyType,LpSolverBase::Col> |
---|
| 431 | ///\endcode |
---|
| 432 | ///- an iterable lemon \ref concept::WriteMap "write map" like |
---|
| 433 | ///\code |
---|
| 434 | ///ListGraph::NodeMap<LpSolverBase::Col> |
---|
| 435 | ///ListGraph::EdgeMap<LpSolverBase::Col> |
---|
| 436 | ///\endcode |
---|
[1256] | 437 | ///\return The number of the created column. |
---|
| 438 | ///\bug Iterable nodemap hasn't been implemented yet. |
---|
| 439 | #ifdef DOXYGEN |
---|
| 440 | template<class T> |
---|
| 441 | int addColSet(T &t) { return 0;} |
---|
| 442 | #else |
---|
| 443 | template<class T> |
---|
| 444 | typename enable_if<typename T::value_type::LpSolverCol,int>::type |
---|
| 445 | addColSet(T &t,dummy<0> = 0) { |
---|
| 446 | int s=0; |
---|
| 447 | for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;} |
---|
| 448 | return s; |
---|
| 449 | } |
---|
| 450 | template<class T> |
---|
| 451 | typename enable_if<typename T::value_type::second_type::LpSolverCol, |
---|
| 452 | int>::type |
---|
| 453 | addColSet(T &t,dummy<1> = 1) { |
---|
| 454 | int s=0; |
---|
| 455 | for(typename T::iterator i=t.begin();i!=t.end();++i) { |
---|
| 456 | i->second=addCol(); |
---|
| 457 | s++; |
---|
| 458 | } |
---|
| 459 | return s; |
---|
| 460 | } |
---|
[1272] | 461 | template<class T> |
---|
| 462 | typename enable_if<typename T::ValueSet::value_type::LpSolverCol, |
---|
| 463 | int>::type |
---|
| 464 | addColSet(T &t,dummy<2> = 2) { |
---|
| 465 | ///\bug <tt>return addColSet(t.valueSet());</tt> should also work. |
---|
| 466 | int s=0; |
---|
| 467 | for(typename T::ValueSet::iterator i=t.valueSet().begin(); |
---|
| 468 | i!=t.valueSet().end(); |
---|
| 469 | ++i) |
---|
| 470 | { |
---|
| 471 | *i=addCol(); |
---|
| 472 | s++; |
---|
| 473 | } |
---|
| 474 | return s; |
---|
| 475 | } |
---|
[1256] | 476 | #endif |
---|
[1263] | 477 | |
---|
[1253] | 478 | ///Add a new empty row (i.e a new constaint) to the LP |
---|
[1258] | 479 | |
---|
| 480 | ///This function adds a new empty row (i.e a new constaint) to the LP. |
---|
| 481 | ///\return The created row |
---|
[1253] | 482 | Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;} |
---|
| 483 | |
---|
[1258] | 484 | ///Set a row (i.e a constaint) of the LP |
---|
[1253] | 485 | |
---|
[1258] | 486 | ///\param r is the row to be modified |
---|
[1259] | 487 | ///\param l is lower bound (-\ref INF means no bound) |
---|
[1258] | 488 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 489 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[1253] | 490 | ///\bug This is a temportary function. The interface will change to |
---|
| 491 | ///a better one. |
---|
[1258] | 492 | void setRow(Row r, Value l,const Expr &e, Value u) { |
---|
[1253] | 493 | std::vector<int> indices; |
---|
| 494 | std::vector<Value> values; |
---|
| 495 | indices.push_back(0); |
---|
| 496 | values.push_back(0); |
---|
[1258] | 497 | for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i) |
---|
[1256] | 498 | if((*i).second!=0) { ///\bug EPSILON would be necessary here!!! |
---|
| 499 | indices.push_back(cols.floatingId((*i).first.id)); |
---|
| 500 | values.push_back((*i).second); |
---|
| 501 | } |
---|
[1253] | 502 | _setRowCoeffs(rows.floatingId(r.id),indices.size()-1, |
---|
| 503 | &indices[0],&values[0]); |
---|
[1256] | 504 | _setRowLowerBound(rows.floatingId(r.id),l-e.constComp()); |
---|
| 505 | _setRowUpperBound(rows.floatingId(r.id),u-e.constComp()); |
---|
[1258] | 506 | } |
---|
| 507 | |
---|
[1264] | 508 | ///Set a row (i.e a constaint) of the LP |
---|
| 509 | |
---|
| 510 | ///\param r is the row to be modified |
---|
| 511 | ///\param c is a linear expression (see \ref Constr) |
---|
| 512 | void setRow(Row r, const Constr &c) { |
---|
[1273] | 513 | setRow(r, |
---|
[1275] | 514 | c.lowerBounded()?c.lowerBound():-INF, |
---|
[1273] | 515 | c.expr(), |
---|
[1275] | 516 | c.upperBounded()?c.upperBound():INF); |
---|
[1264] | 517 | } |
---|
| 518 | |
---|
[1258] | 519 | ///Add a new row (i.e a new constaint) to the LP |
---|
| 520 | |
---|
[1259] | 521 | ///\param l is the lower bound (-\ref INF means no bound) |
---|
[1258] | 522 | ///\param e is a linear expression (see \ref Expr) |
---|
[1259] | 523 | ///\param u is the upper bound (\ref INF means no bound) |
---|
[1258] | 524 | ///\return The created row. |
---|
| 525 | ///\bug This is a temportary function. The interface will change to |
---|
| 526 | ///a better one. |
---|
| 527 | Row addRow(Value l,const Expr &e, Value u) { |
---|
| 528 | Row r=addRow(); |
---|
| 529 | setRow(r,l,e,u); |
---|
[1253] | 530 | return r; |
---|
| 531 | } |
---|
| 532 | |
---|
[1264] | 533 | ///Add a new row (i.e a new constaint) to the LP |
---|
| 534 | |
---|
| 535 | ///\param c is a linear expression (see \ref Constr) |
---|
| 536 | ///\return The created row. |
---|
| 537 | Row addRow(const Constr &c) { |
---|
| 538 | Row r=addRow(); |
---|
| 539 | setRow(r,c); |
---|
| 540 | return r; |
---|
| 541 | } |
---|
| 542 | |
---|
[1253] | 543 | /// Set the lower bound of a column (i.e a variable) |
---|
| 544 | |
---|
[1293] | 545 | /// The upper bound of a variable (column) has to be given by an |
---|
[1253] | 546 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 547 | /// Value or -\ref INF. |
---|
[1293] | 548 | void colLowerBound(Col c, Value value) { |
---|
[1253] | 549 | _setColLowerBound(cols.floatingId(c.id),value); |
---|
| 550 | } |
---|
| 551 | /// Set the upper bound of a column (i.e a variable) |
---|
| 552 | |
---|
[1293] | 553 | /// The upper bound of a variable (column) has to be given by an |
---|
[1253] | 554 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 555 | /// Value or \ref INF. |
---|
[1293] | 556 | void colUpperBound(Col c, Value value) { |
---|
[1253] | 557 | _setColUpperBound(cols.floatingId(c.id),value); |
---|
| 558 | }; |
---|
[1293] | 559 | /// Set the lower and the upper bounds of a column (i.e a variable) |
---|
| 560 | |
---|
| 561 | /// The lower and the upper bounds of |
---|
| 562 | /// a variable (column) have to be given by an |
---|
| 563 | /// extended number of type Value, i.e. a finite number of type |
---|
| 564 | /// Value, -\ref INF or \ref INF. |
---|
| 565 | void colBounds(Col c, Value lower, Value upper) { |
---|
| 566 | _setColLowerBound(cols.floatingId(c.id),lower); |
---|
| 567 | _setColUpperBound(cols.floatingId(c.id),upper); |
---|
| 568 | } |
---|
| 569 | |
---|
[1253] | 570 | /// Set the lower bound of a row (i.e a constraint) |
---|
| 571 | |
---|
[1293] | 572 | /// The lower bound of a linear expression (row) has to be given by an |
---|
[1253] | 573 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 574 | /// Value or -\ref INF. |
---|
[1293] | 575 | void rowLowerBound(Row r, Value value) { |
---|
[1253] | 576 | _setRowLowerBound(rows.floatingId(r.id),value); |
---|
| 577 | }; |
---|
| 578 | /// Set the upper bound of a row (i.e a constraint) |
---|
| 579 | |
---|
[1293] | 580 | /// The upper bound of a linear expression (row) has to be given by an |
---|
[1253] | 581 | /// extended number of type Value, i.e. a finite number of type |
---|
[1259] | 582 | /// Value or \ref INF. |
---|
[1293] | 583 | void rowUpperBound(Row r, Value value) { |
---|
[1253] | 584 | _setRowUpperBound(rows.floatingId(r.id),value); |
---|
| 585 | }; |
---|
[1293] | 586 | /// Set the lower and the upper bounds of a row (i.e a variable) |
---|
| 587 | |
---|
| 588 | /// The lower and the upper bounds of |
---|
| 589 | /// a constraint (row) have to be given by an |
---|
| 590 | /// extended number of type Value, i.e. a finite number of type |
---|
| 591 | /// Value, -\ref INF or \ref INF. |
---|
| 592 | void rowBounds(Row c, Value lower, Value upper) { |
---|
| 593 | _setRowLowerBound(rows.floatingId(c.id),lower); |
---|
| 594 | _setRowUpperBound(rows.floatingId(c.id),upper); |
---|
| 595 | } |
---|
| 596 | |
---|
[1253] | 597 | ///Set an element of the objective function |
---|
[1293] | 598 | void objCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); }; |
---|
[1253] | 599 | ///Set the objective function |
---|
| 600 | |
---|
| 601 | ///\param e is a linear expression of type \ref Expr. |
---|
| 602 | ///\todo What to do with the constant component? |
---|
| 603 | void setObj(Expr e) { |
---|
| 604 | clearObj(); |
---|
| 605 | for (Expr::iterator i=e.begin(); i!=e.end(); ++i) |
---|
[1293] | 606 | objCoeff((*i).first,(*i).second); |
---|
[1253] | 607 | } |
---|
[1263] | 608 | |
---|
| 609 | ///@} |
---|
| 610 | |
---|
| 611 | |
---|
[1294] | 612 | ///\name Solve the LP |
---|
[1263] | 613 | |
---|
| 614 | ///@{ |
---|
| 615 | |
---|
| 616 | ///\e |
---|
[1293] | 617 | SolutionStatus solve() { return _solve(); } |
---|
[1263] | 618 | |
---|
| 619 | ///@} |
---|
| 620 | |
---|
[1294] | 621 | ///\name Obtain the solution |
---|
[1263] | 622 | |
---|
| 623 | ///@{ |
---|
| 624 | |
---|
| 625 | ///\e |
---|
[1294] | 626 | SolutionType primalType() { |
---|
| 627 | return _getPrimalType(); |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | ///\e |
---|
[1293] | 631 | Value primal(Col c) { return _getPrimal(cols.floatingId(c.id)); } |
---|
[1263] | 632 | |
---|
| 633 | ///@} |
---|
[1253] | 634 | |
---|
[1248] | 635 | }; |
---|
[1246] | 636 | |
---|
[1272] | 637 | ///\e |
---|
| 638 | |
---|
| 639 | ///\relates LpSolverBase::Expr |
---|
| 640 | /// |
---|
| 641 | inline LpSolverBase::Expr operator+(const LpSolverBase::Expr &a, |
---|
| 642 | const LpSolverBase::Expr &b) |
---|
| 643 | { |
---|
| 644 | LpSolverBase::Expr tmp(a); |
---|
| 645 | tmp+=b; ///\todo Don't STL have some special 'merge' algorithm? |
---|
| 646 | return tmp; |
---|
| 647 | } |
---|
| 648 | ///\e |
---|
| 649 | |
---|
| 650 | ///\relates LpSolverBase::Expr |
---|
| 651 | /// |
---|
| 652 | inline LpSolverBase::Expr operator-(const LpSolverBase::Expr &a, |
---|
| 653 | const LpSolverBase::Expr &b) |
---|
| 654 | { |
---|
| 655 | LpSolverBase::Expr tmp(a); |
---|
| 656 | tmp-=b; ///\todo Don't STL have some special 'merge' algorithm? |
---|
| 657 | return tmp; |
---|
| 658 | } |
---|
| 659 | ///\e |
---|
| 660 | |
---|
| 661 | ///\relates LpSolverBase::Expr |
---|
| 662 | /// |
---|
| 663 | inline LpSolverBase::Expr operator*(const LpSolverBase::Expr &a, |
---|
[1273] | 664 | const LpSolverBase::Value &b) |
---|
[1272] | 665 | { |
---|
| 666 | LpSolverBase::Expr tmp(a); |
---|
| 667 | tmp*=b; ///\todo Don't STL have some special 'merge' algorithm? |
---|
| 668 | return tmp; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | ///\e |
---|
| 672 | |
---|
| 673 | ///\relates LpSolverBase::Expr |
---|
| 674 | /// |
---|
[1273] | 675 | inline LpSolverBase::Expr operator*(const LpSolverBase::Value &a, |
---|
[1272] | 676 | const LpSolverBase::Expr &b) |
---|
| 677 | { |
---|
| 678 | LpSolverBase::Expr tmp(b); |
---|
| 679 | tmp*=a; ///\todo Don't STL have some special 'merge' algorithm? |
---|
| 680 | return tmp; |
---|
| 681 | } |
---|
| 682 | ///\e |
---|
| 683 | |
---|
| 684 | ///\relates LpSolverBase::Expr |
---|
| 685 | /// |
---|
| 686 | inline LpSolverBase::Expr operator/(const LpSolverBase::Expr &a, |
---|
[1273] | 687 | const LpSolverBase::Value &b) |
---|
[1272] | 688 | { |
---|
| 689 | LpSolverBase::Expr tmp(a); |
---|
| 690 | tmp/=b; ///\todo Don't STL have some special 'merge' algorithm? |
---|
| 691 | return tmp; |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | ///\e |
---|
| 695 | |
---|
| 696 | ///\relates LpSolverBase::Constr |
---|
| 697 | /// |
---|
| 698 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
| 699 | const LpSolverBase::Expr &f) |
---|
| 700 | { |
---|
| 701 | return LpSolverBase::Constr(-LpSolverBase::INF,e-f,0); |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | ///\e |
---|
| 705 | |
---|
| 706 | ///\relates LpSolverBase::Constr |
---|
| 707 | /// |
---|
[1273] | 708 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &e, |
---|
[1272] | 709 | const LpSolverBase::Expr &f) |
---|
| 710 | { |
---|
| 711 | return LpSolverBase::Constr(e,f); |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | ///\e |
---|
| 715 | |
---|
| 716 | ///\relates LpSolverBase::Constr |
---|
| 717 | /// |
---|
| 718 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Expr &e, |
---|
[1273] | 719 | const LpSolverBase::Value &f) |
---|
[1272] | 720 | { |
---|
| 721 | return LpSolverBase::Constr(e,f); |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | ///\e |
---|
| 725 | |
---|
| 726 | ///\relates LpSolverBase::Constr |
---|
| 727 | /// |
---|
| 728 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
| 729 | const LpSolverBase::Expr &f) |
---|
| 730 | { |
---|
| 731 | return LpSolverBase::Constr(-LpSolverBase::INF,f-e,0); |
---|
| 732 | } |
---|
| 733 | |
---|
| 734 | |
---|
| 735 | ///\e |
---|
| 736 | |
---|
| 737 | ///\relates LpSolverBase::Constr |
---|
| 738 | /// |
---|
[1273] | 739 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &e, |
---|
[1272] | 740 | const LpSolverBase::Expr &f) |
---|
| 741 | { |
---|
| 742 | return LpSolverBase::Constr(f,e); |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | |
---|
| 746 | ///\e |
---|
| 747 | |
---|
| 748 | ///\relates LpSolverBase::Constr |
---|
| 749 | /// |
---|
| 750 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Expr &e, |
---|
[1273] | 751 | const LpSolverBase::Value &f) |
---|
[1272] | 752 | { |
---|
| 753 | return LpSolverBase::Constr(f,e); |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | ///\e |
---|
| 757 | |
---|
| 758 | ///\relates LpSolverBase::Constr |
---|
| 759 | /// |
---|
| 760 | inline LpSolverBase::Constr operator==(const LpSolverBase::Expr &e, |
---|
| 761 | const LpSolverBase::Expr &f) |
---|
| 762 | { |
---|
| 763 | return LpSolverBase::Constr(0,e-f,0); |
---|
| 764 | } |
---|
| 765 | |
---|
| 766 | ///\e |
---|
| 767 | |
---|
| 768 | ///\relates LpSolverBase::Constr |
---|
| 769 | /// |
---|
[1273] | 770 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Value &n, |
---|
[1272] | 771 | const LpSolverBase::Constr&c) |
---|
| 772 | { |
---|
| 773 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 774 | ///\todo Create an own exception type. |
---|
| 775 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
| 776 | else tmp.lowerBound()=n; |
---|
[1272] | 777 | return tmp; |
---|
| 778 | } |
---|
| 779 | ///\e |
---|
| 780 | |
---|
| 781 | ///\relates LpSolverBase::Constr |
---|
| 782 | /// |
---|
| 783 | inline LpSolverBase::Constr operator<=(const LpSolverBase::Constr& c, |
---|
[1273] | 784 | const LpSolverBase::Value &n) |
---|
[1272] | 785 | { |
---|
| 786 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 787 | ///\todo Create an own exception type. |
---|
| 788 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
| 789 | else tmp.upperBound()=n; |
---|
[1272] | 790 | return tmp; |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | ///\e |
---|
| 794 | |
---|
| 795 | ///\relates LpSolverBase::Constr |
---|
| 796 | /// |
---|
[1273] | 797 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Value &n, |
---|
[1272] | 798 | const LpSolverBase::Constr&c) |
---|
| 799 | { |
---|
| 800 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 801 | ///\todo Create an own exception type. |
---|
| 802 | if(!isnan(tmp.upperBound())) throw LogicError(); |
---|
| 803 | else tmp.upperBound()=n; |
---|
[1272] | 804 | return tmp; |
---|
| 805 | } |
---|
| 806 | ///\e |
---|
| 807 | |
---|
| 808 | ///\relates LpSolverBase::Constr |
---|
| 809 | /// |
---|
| 810 | inline LpSolverBase::Constr operator>=(const LpSolverBase::Constr& c, |
---|
[1273] | 811 | const LpSolverBase::Value &n) |
---|
[1272] | 812 | { |
---|
| 813 | LpSolverBase::Constr tmp(c); |
---|
[1273] | 814 | ///\todo Create an own exception type. |
---|
| 815 | if(!isnan(tmp.lowerBound())) throw LogicError(); |
---|
| 816 | else tmp.lowerBound()=n; |
---|
[1272] | 817 | return tmp; |
---|
| 818 | } |
---|
| 819 | |
---|
| 820 | |
---|
[1246] | 821 | } //namespace lemon |
---|
| 822 | |
---|
| 823 | #endif //LEMON_LP_BASE_H |
---|