bug fix in SubBidirGraphWrapper::firstIn(Edge&,const Node&), due to Gabor Retvari
2 * src/lemon/lp_base.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
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.
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
17 #ifndef LEMON_LP_BASE_H
18 #define LEMON_LP_BASE_H
23 #include<lemon/utility.h>
24 #include<lemon/error.h>
25 #include<lemon/invalid.h>
29 ///\brief The interface of the LP solver interface.
32 ///Internal data structure to convert floating id's to fix one's
34 ///\todo This might by implemented to be usable in other places.
37 std::vector<int> index;
38 std::vector<int> cross;
41 _FixId() : first_free(-1) {};
42 ///Convert a floating id to a fix one
44 ///\param n is a floating id
45 ///\return the corresponding fix id
46 int fixId(int n) {return cross[n];}
47 ///Convert a fix id to a floating one
49 ///\param n is a fix id
50 ///\return the corresponding floating id
51 int floatingId(int n) { return index[n];}
52 ///Add a new floating id.
54 ///\param n is a floating id
55 ///\return the fix id of the new value
56 ///\todo Multiple additions should also be handled.
59 if(n>=int(cross.size())) {
62 cross[n]=index.size();
67 int next=index[first_free];
73 else throw LogicError(); //floatingId-s must form a continuous range;
77 ///\param n is a fix id
84 for(int i=fl+1;i<int(cross.size());++i) {
90 ///An upper bound on the largest fix id.
92 ///\todo Do we need this?
94 std::size_t maxFixId() { return cross.size()-1; }
98 ///Common base class for LP solvers
115 ///The floating point type used by the solver
116 typedef double Value;
117 ///The infinity constant
118 static const Value INF;
119 ///The not a number constant
120 static const Value NaN;
122 ///Refer to a column of the LP.
124 ///This type is used to refer to a column of the LP.
126 ///Its value remains valid and correct even after the addition or erase of
127 ///new column (unless the referred column itself was also deleted,
130 ///\todo Document what can one do with a Col (INVALID, comparing,
131 ///it is similar to Node/Edge)
135 friend class LpSolverBase;
137 typedef Value ExprValue;
138 typedef True LpSolverCol;
140 Col(const Invalid&) : id(-1) {}
141 bool operator<(Col c) const {return id<c.id;}
142 bool operator==(Col c) const {return id==c.id;}
143 bool operator!=(Col c) const {return id==c.id;}
146 ///Refer to a row of the LP.
148 ///This type is used to refer to a row of the LP.
150 ///Its value remains valid and correct even after the addition or erase of
151 ///new rows (unless the referred row itself was also deleted, of course).
153 ///\todo Document what can one do with a Row (INVALID, comparing,
154 ///it is similar to Node/Edge)
158 friend class LpSolverBase;
160 typedef Value ExprValue;
161 typedef True LpSolverRow;
163 Row(const Invalid&) : id(-1) {}
164 typedef True LpSolverRow;
165 bool operator<(Row c) const {return id<c.id;}
166 bool operator==(Row c) const {return id==c.id;}
167 bool operator!=(Row c) const {return id==c.id;}
171 typedef SparseLinExpr<Col> Expr;
173 typedef LinConstr<Expr> Constr;
180 virtual int _addCol() = 0;
182 virtual int _addRow() = 0;
185 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
187 virtual void _setRowCoeffs(int i,
190 Value const * values ) = 0;
193 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
195 virtual void _setColCoeffs(int i,
198 Value const * values ) = 0;
202 /// The lower bound of a variable (column) have to be given by an
203 /// extended number of type Value, i.e. a finite number of type
204 /// Value or -\ref INF.
205 virtual void _setColLowerBound(int i, Value value) = 0;
208 /// The upper bound of a variable (column) have to be given by an
209 /// extended number of type Value, i.e. a finite number of type
210 /// Value or \ref INF.
211 virtual void _setColUpperBound(int i, Value value) = 0;
214 /// The lower bound of a linear expression (row) have to be given by an
215 /// extended number of type Value, i.e. a finite number of type
216 /// Value or -\ref INF.
217 virtual void _setRowLowerBound(int i, Value value) = 0;
220 /// The upper bound of a linear expression (row) have to be given by an
221 /// extended number of type Value, i.e. a finite number of type
222 /// Value or \ref INF.
223 virtual void _setRowUpperBound(int i, Value value) = 0;
226 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
230 ///\bug Wrong interface
232 virtual SolutionType _solve() = 0;
236 ///\bug Wrong interface
238 virtual Value _getSolution(int i) = 0;
241 ///\bug unimplemented!!!!
247 virtual ~LpSolverBase() {}
249 ///\name Building up and modification of the LP
253 ///Add a new empty column (i.e a new variable) to the LP
254 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
256 ///\brief Fill the elements of a container with newly created columns
257 ///(i.e a new variables)
259 ///This magic function takes container as its argument
260 ///and fills its elements
261 ///with new columns (i.e. variables)
262 ///\param t can be either any standard STL iterable container with
263 ///\ref Col \c values_type or \c mapped_type
264 ///like <tt>std::vector<LpSolverBase::Col></tt>,
265 /// <tt>std::list<LpSolverBase::Col></tt> or
266 /// <tt>std::map<AnyType,LpSolverBase::Col></tt> or
267 ///it can be an iterable lemon map like
268 /// <tt>ListGraph::NodeMap<LpSolverBase::Col></tt>.
269 ///\return The number of the created column.
270 ///\bug Iterable nodemap hasn't been implemented yet.
273 int addColSet(T &t) { return 0;}
276 typename enable_if<typename T::value_type::LpSolverCol,int>::type
277 addColSet(T &t,dummy<0> = 0) {
279 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
283 typename enable_if<typename T::value_type::second_type::LpSolverCol,
285 addColSet(T &t,dummy<1> = 1) {
287 for(typename T::iterator i=t.begin();i!=t.end();++i) {
295 ///Add a new empty row (i.e a new constaint) to the LP
297 ///This function adds a new empty row (i.e a new constaint) to the LP.
298 ///\return The created row
299 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
301 ///Set a row (i.e a constaint) of the LP
303 ///\param r is the row to be modified
304 ///\param l is lower bound (-\ref INF means no bound)
305 ///\param e is a linear expression (see \ref Expr)
306 ///\param u is the upper bound (\ref INF means no bound)
307 ///\bug This is a temportary function. The interface will change to
309 void setRow(Row r, Value l,const Expr &e, Value u) {
310 std::vector<int> indices;
311 std::vector<Value> values;
312 indices.push_back(0);
314 for(Expr::const_iterator i=e.begin(); i!=e.end(); ++i)
315 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
316 indices.push_back(cols.floatingId((*i).first.id));
317 values.push_back((*i).second);
319 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
320 &indices[0],&values[0]);
321 _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
322 _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
325 ///Set a row (i.e a constaint) of the LP
327 ///\param r is the row to be modified
328 ///\param c is a linear expression (see \ref Constr)
329 ///\bug This is a temportary function. The interface will change to
331 void setRow(Row r, const Constr &c) {
332 Value lb= c.lb==NaN?-INF:lb;
333 Value ub= c.ub==NaN?INF:lb;
334 setRow(r,lb,c.expr,ub);
337 ///Add a new row (i.e a new constaint) to the LP
339 ///\param l is the lower bound (-\ref INF means no bound)
340 ///\param e is a linear expression (see \ref Expr)
341 ///\param u is the upper bound (\ref INF means no bound)
342 ///\return The created row.
343 ///\bug This is a temportary function. The interface will change to
345 Row addRow(Value l,const Expr &e, Value u) {
351 ///Add a new row (i.e a new constaint) to the LP
353 ///\param c is a linear expression (see \ref Constr)
354 ///\return The created row.
355 ///\bug This is a temportary function. The interface will change to
357 Row addRow(const Constr &c) {
363 /// Set the lower bound of a column (i.e a variable)
365 /// The upper bound of a variable (column) have to be given by an
366 /// extended number of type Value, i.e. a finite number of type
367 /// Value or -\ref INF.
368 virtual void setColLowerBound(Col c, Value value) {
369 _setColLowerBound(cols.floatingId(c.id),value);
371 /// Set the upper bound of a column (i.e a variable)
373 /// The upper bound of a variable (column) have to be given by an
374 /// extended number of type Value, i.e. a finite number of type
375 /// Value or \ref INF.
376 virtual void setColUpperBound(Col c, Value value) {
377 _setColUpperBound(cols.floatingId(c.id),value);
379 /// Set the lower bound of a row (i.e a constraint)
381 /// The lower bound of a linear expression (row) have to be given by an
382 /// extended number of type Value, i.e. a finite number of type
383 /// Value or -\ref INF.
384 virtual void setRowLowerBound(Row r, Value value) {
385 _setRowLowerBound(rows.floatingId(r.id),value);
387 /// Set the upper bound of a row (i.e a constraint)
389 /// The upper bound of a linear expression (row) have to be given by an
390 /// extended number of type Value, i.e. a finite number of type
391 /// Value or \ref INF.
392 virtual void setRowUpperBound(Row r, Value value) {
393 _setRowUpperBound(rows.floatingId(r.id),value);
395 ///Set an element of the objective function
396 void setObjCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
397 ///Set the objective function
399 ///\param e is a linear expression of type \ref Expr.
400 ///\todo What to do with the constant component?
401 void setObj(Expr e) {
403 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
404 setObjCoeff((*i).first,(*i).second);
410 ///\name Solving the LP
415 SolutionType solve() { return _solve(); }
419 ///\name Obtaining the solution LP
424 Value solution(Col c) { return _getSolution(cols.floatingId(c.id)); }
432 #endif //LEMON_LP_BASE_H