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