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
22 #include<lemon/error.h>
26 ///\brief The interface of the LP solver interface.
29 ///Internal data structure to convert floating id's to fix one's
31 ///\todo This might by implemented to be usable in other places.
34 std::vector<int> index;
35 std::vector<int> cross;
38 _FixId() : first_free(-1) {};
39 ///Convert a floating id to a fix one
41 ///\param n is a floating id
42 ///\return the corresponding fix id
43 int fixId(int n) {return cross[n];}
44 ///Convert a fix id to a floating one
46 ///\param n is a fix id
47 ///\return the corresponding floating id
48 int floatingId(int n) { return index[n];}
49 ///Add a new floating id.
51 ///\param n is a floating id
52 ///\return the fix id of the new value
53 ///\todo Multiple additions should also be handled.
56 if(n>=int(cross.size())) {
59 cross[n]=index.size();
64 int next=index[first_free];
69 else throw LogicError(); //floatingId-s must form a continuous range;
73 ///\param n is a fix id
80 for(int i=fl+1;i<int(cross.size());++i) {
86 ///An upper bound on the largest fix id.
88 ///\todo Do we need this?
90 std::size_t maxFixId() { return cross.size()-1; }
94 ///Common base class for LP solvers
100 typedef double Value;
102 static const Value INF;
105 class Col { protected: int id; friend class LpSolverBase; };
107 class Row { protected: int id; friend class LpSolverBase; };
109 typedef SparseLinExpr<Col, Value> Expr;
115 //MATRIX MANIPULATING FUNCTIONS
118 virtual int _addCol() = 0;
120 virtual int _addRow() = 0;
123 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
125 virtual void _setRowCoeffs(int i,
128 Value const * values ) = 0;
131 /// \warning Arrays are indexed from 1 (datum at index 0 is ignored)
133 virtual void _setColCoeffs(int i,
136 Value const * values ) = 0;
140 /// The lower bound of a variable (column) have to be given by an
141 /// extended number of type Value, i.e. a finite number of type
143 virtual void _setColLowerBound(int i, Value value) = 0;
146 /// The upper bound of a variable (column) have to be given by an
147 /// extended number of type Value, i.e. a finite number of type
149 virtual void _setColUpperBound(int i, Value value) = 0;
152 /// The lower bound of a linear expression (row) have to be given by an
153 /// extended number of type Value, i.e. a finite number of type
155 virtual void _setRowLowerBound(int i, Value value) = 0;
158 /// The upper bound of a linear expression (row) have to be given by an
159 /// extended number of type Value, i.e. a finite number of type
161 virtual void _setRowUpperBound(int i, Value value) = 0;
164 virtual void _setObjCoeff(int i, Value obj_coef) = 0;
168 ///\bug unimplemented!!!!
174 virtual ~LpSolverBase() {}
176 ///Add a new empty column (i.e a new variable) to the LP
177 Col addCol() { Col c; c.id=cols.insert(_addCol()); return c;}
178 ///Add a new empty row (i.e a new constaint) to the LP
179 Row addRow() { Row r; r.id=rows.insert(_addRow()); return r;}
181 ///Add a new row (i.e a new constaint) to the LP
183 ///\param l lower bound (-INF means no bound)
184 ///\param e a linear expression (see \ref Expr)
185 ///\param u upper bound (INF means no bound)
186 ///\bug This is a temportary function. The interface will change to
188 Row addRow(Value l,Expr e, Value u) {
190 std::vector<int> indices;
191 std::vector<Value> values;
192 indices.push_back(0);
194 for(Expr::iterator i=e.begin(); i!=e.end(); ++i) {
195 indices.push_back(cols.floatingId((*i).first.id));
196 values.push_back((*i).second);
198 _setRowCoeffs(rows.floatingId(r.id),indices.size()-1,
199 &indices[0],&values[0]);
200 _setRowLowerBound(rows.floatingId(r.id),l);
201 _setRowUpperBound(rows.floatingId(r.id),l);
205 /// Set the lower bound of a column (i.e a variable)
207 /// The upper bound of a variable (column) have to be given by an
208 /// extended number of type Value, i.e. a finite number of type
210 virtual void setColLowerBound(Col c, Value value) {
211 _setColLowerBound(cols.floatingId(c.id),value);
213 /// Set the upper bound of a column (i.e a variable)
215 /// The upper bound of a variable (column) have to be given by an
216 /// extended number of type Value, i.e. a finite number of type
218 virtual void setColUpperBound(Col c, Value value) {
219 _setColUpperBound(cols.floatingId(c.id),value);
221 /// Set the lower bound of a row (i.e a constraint)
223 /// The lower bound of a linear expression (row) have to be given by an
224 /// extended number of type Value, i.e. a finite number of type
226 virtual void setRowLowerBound(Row r, Value value) {
227 _setRowLowerBound(rows.floatingId(r.id),value);
229 /// Set the upper bound of a row (i.e a constraint)
231 /// The upper bound of a linear expression (row) have to be given by an
232 /// extended number of type Value, i.e. a finite number of type
234 virtual void setRowUpperBound(Row r, Value value) {
235 _setRowUpperBound(rows.floatingId(r.id),value);
237 ///Set an element of the objective function
238 void setObjCoeff(Col c, Value v) {_setObjCoeff(cols.floatingId(c.id),v); };
239 ///Set the objective function
241 ///\param e is a linear expression of type \ref Expr.
242 ///\todo What to do with the constant component?
243 void setObj(Expr e) {
245 for (Expr::iterator i=e.begin(); i!=e.end(); ++i)
246 setObjCoeff((*i).first,(*i).second);
253 #endif //LEMON_LP_BASE_H