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