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
103 ///The floating point type used by the solver
104 typedef double Value;
105 ///The infinity constant
106 static const Value INF;
108 ///Refer to a column of the LP.
110 ///This type is used to refer to a column of the LP.
112 ///Its value remains valid and correct even after the addition or erase of
113 ///new column (unless the referred column itself was also deleted,
116 ///\todo Document what can one do with a Col (INVALID, comparing,
117 ///it is similar to Node/Edge)
121 friend class LpSolverBase;
123 typedef True LpSolverCol;
125 Col(const Invalid&) : id(-1) {}
126 bool operator<(Col c) const {return id<c.id;}
127 bool operator==(Col c) const {return id==c.id;}
128 bool operator!=(Col c) const {return id==c.id;}
131 ///Refer to a row of the LP.
133 ///This type is used to refer to a row of the LP.
135 ///Its value remains valid and correct even after the addition or erase of
136 ///new rows (unless the referred row itself was also deleted, of course).
138 ///\todo Document what can one do with a Row (INVALID, comparing,
139 ///it is similar to Node/Edge)
143 friend class LpSolverBase;
145 typedef True LpSolverRow;
147 Row(const Invalid&) : id(-1) {}
148 typedef True LpSolverRow;
149 bool operator<(Row c) const {return id<c.id;}
150 bool operator==(Row c) const {return id==c.id;}
151 bool operator!=(Row c) const {return id==c.id;}
154 typedef SparseLinExpr<Col, Value> Expr;
160 //MATRIX MANIPULATING FUNCTIONS
163 virtual int _addCol() = 0;
165 virtual int _addRow() = 0;
168 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
170 virtual void _setRowCoeffs(int i,
173 Value const * values ) = 0;
176 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
178 virtual void _setColCoeffs(int i,
181 Value const * values ) = 0;
185 /// The lower bound of a variable (column) have to be given by an
186 /// extended number of type Value, i.e. a finite number of type
188 virtual void _setColLowerBound(int i, Value value) = 0;
191 /// The upper bound of a variable (column) have to be given by an
192 /// extended number of type Value, i.e. a finite number of type
194 virtual void _setColUpperBound(int i, Value value) = 0;
197 /// The lower bound of a linear expression (row) have to be given by an
198 /// extended number of type Value, i.e. a finite number of type
200 virtual void _setRowLowerBound(int i, Value value) = 0;
203 /// The upper bound of a linear expression (row) have to be given by an
204 /// extended number of type Value, i.e. a finite number of type
206 virtual void _setRowUpperBound(int i, Value value) = 0;
209 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
213 ///\bug unimplemented!!!!
219 virtual ~LpSolverBase() {}
221 ///Add a new empty column (i.e a new variable) to the LP
222 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
223 ///\brief Fill the elements of a container with newly created columns
224 ///(i.e a new variables)
226 ///This magic function takes container as its argument
227 ///and fills its elements
228 ///with new columns (i.e. variables)
229 ///\param t can be either any standard STL iterable container with
230 ///\ref Col \c values_type or \c mapped_type
231 ///like <tt>std::vector<LpSolverBase::Col></tt>,
232 /// <tt>std::list<LpSolverBase::Col></tt> or
233 /// <tt>std::map<AnyType,LpSolverBase::Col></tt> or
234 ///it can be an iterable lemon map like
235 /// <tt>ListGraph::NodeMap<LpSolverBase::Col></tt>.
236 ///\return The number of the created column.
237 ///\bug Iterable nodemap hasn't been implemented yet.
240 int addColSet(T &t) { return 0;}
243 typename enable_if<typename T::value_type::LpSolverCol,int>::type
244 addColSet(T &t,dummy<0> = 0) {
246 for(typename T::iterator i=t.begin();i!=t.end();++i) {*i=addCol();s++;}
250 typename enable_if<typename T::value_type::second_type::LpSolverCol,
252 addColSet(T &t,dummy<1> = 1) {
254 for(typename T::iterator i=t.begin();i!=t.end();++i) {
261 ///Add a new empty row (i.e a new constaint) to the LP
262 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
264 ///Add a new row (i.e a new constaint) to the LP
266 ///\param l lower bound (-INF means no bound)
267 ///\param e a linear expression (see \ref Expr)
268 ///\param u upper bound (INF means no bound)
269 ///\bug This is a temportary function. The interface will change to
271 Row addRow(Value l,Expr e, Value u) {
273 std::vector<int> indices;
274 std::vector<Value> values;
275 indices.push_back(0);
277 for(Expr::iterator i=e.begin(); i!=e.end(); ++i)
278 if((*i).second!=0) { ///\bug EPSILON would be necessary here!!!
279 indices.push_back(cols.floatingId((*i).first.id));
280 values.push_back((*i).second);
282 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
283 &indices[0],&values[0]);
284 _setRowLowerBound(rows.floatingId(r.id),l-e.constComp());
285 _setRowUpperBound(rows.floatingId(r.id),u-e.constComp());
289 /// Set the lower bound of a column (i.e a variable)
291 /// The upper bound of a variable (column) have to be given by an
292 /// extended number of type Value, i.e. a finite number of type
294 virtual void setColLowerBound(Col c, Value value) {
295 _setColLowerBound(cols.floatingId(c.id),value);
297 /// Set the upper bound of a column (i.e a variable)
299 /// The upper bound of a variable (column) have to be given by an
300 /// extended number of type Value, i.e. a finite number of type
302 virtual void setColUpperBound(Col c, Value value) {
303 _setColUpperBound(cols.floatingId(c.id),value);
305 /// Set the lower bound of a row (i.e a constraint)
307 /// The lower bound of a linear expression (row) have to be given by an
308 /// extended number of type Value, i.e. a finite number of type
310 virtual void setRowLowerBound(Row r, Value value) {
311 _setRowLowerBound(rows.floatingId(r.id),value);
313 /// Set the upper bound of a row (i.e a constraint)
315 /// The upper bound of a linear expression (row) have to be given by an
316 /// extended number of type Value, i.e. a finite number of type
318 virtual void setRowUpperBound(Row r, Value value) {
319 _setRowUpperBound(rows.floatingId(r.id),value);
321 ///Set an element of the objective function
322 void setObjCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
323 ///Set the objective function
325 ///\param e is a linear expression of type \ref Expr.
326 ///\todo What to do with the constant component?
327 void setObj(Expr e) {
329 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
330 setObjCoeff((*i).first,(*i).second);
337 #endif //LEMON_LP_BASE_H