1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_LP_BASE_H |
20 | 20 |
#define LEMON_LP_BASE_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<vector> |
24 | 24 |
#include<map> |
25 | 25 |
#include<limits> |
26 | 26 |
#include<lemon/math.h> |
27 | 27 |
|
28 | 28 |
#include<lemon/error.h> |
29 | 29 |
#include<lemon/assert.h> |
30 | 30 |
|
31 | 31 |
#include<lemon/core.h> |
32 | 32 |
#include<lemon/bits/solver_bits.h> |
33 | 33 |
|
34 | 34 |
///\file |
35 | 35 |
///\brief The interface of the LP solver interface. |
36 | 36 |
///\ingroup lp_group |
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
///Common base class for LP and MIP solvers |
40 | 40 |
|
41 | 41 |
///Usually this class is not used directly, please use one of the concrete |
42 | 42 |
///implementations of the solver interface. |
43 | 43 |
///\ingroup lp_group |
44 | 44 |
class LpBase { |
45 | 45 |
|
46 | 46 |
protected: |
47 | 47 |
|
48 | 48 |
_solver_bits::VarIndex rows; |
49 | 49 |
_solver_bits::VarIndex cols; |
50 | 50 |
|
51 | 51 |
public: |
52 | 52 |
|
53 | 53 |
///Possible outcomes of an LP solving procedure |
54 | 54 |
enum SolveExitStatus { |
55 | 55 |
/// = 0. It means that the problem has been successfully solved: either |
56 | 56 |
///an optimal solution has been found or infeasibility/unboundedness |
57 | 57 |
///has been proved. |
58 | 58 |
SOLVED = 0, |
59 | 59 |
/// = 1. Any other case (including the case when some user specified |
60 | 60 |
///limit has been exceeded). |
61 | 61 |
UNSOLVED = 1 |
62 | 62 |
}; |
63 | 63 |
|
64 | 64 |
///Direction of the optimization |
65 | 65 |
enum Sense { |
66 | 66 |
/// Minimization |
67 | 67 |
MIN, |
68 | 68 |
/// Maximization |
69 | 69 |
MAX |
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
///Enum for \c messageLevel() parameter |
73 | 73 |
enum MessageLevel { |
74 | 74 |
/// No output (default value). |
75 | 75 |
MESSAGE_NOTHING, |
76 | 76 |
/// Error messages only. |
77 | 77 |
MESSAGE_ERROR, |
78 | 78 |
/// Warnings. |
79 | 79 |
MESSAGE_WARNING, |
80 | 80 |
/// Normal output. |
81 | 81 |
MESSAGE_NORMAL, |
82 | 82 |
/// Verbose output. |
83 | 83 |
MESSAGE_VERBOSE |
84 | 84 |
}; |
85 | 85 |
|
86 | 86 |
|
87 | 87 |
///The floating point type used by the solver |
88 | 88 |
typedef double Value; |
89 | 89 |
///The infinity constant |
90 | 90 |
static const Value INF; |
91 | 91 |
///The not a number constant |
92 | 92 |
static const Value NaN; |
93 | 93 |
|
94 | 94 |
friend class Col; |
95 | 95 |
friend class ColIt; |
96 | 96 |
friend class Row; |
97 | 97 |
friend class RowIt; |
98 | 98 |
|
99 | 99 |
///Refer to a column of the LP. |
100 | 100 |
|
101 | 101 |
///This type is used to refer to a column of the LP. |
102 | 102 |
/// |
103 | 103 |
///Its value remains valid and correct even after the addition or erase of |
104 | 104 |
///other columns. |
105 | 105 |
/// |
106 | 106 |
///\note This class is similar to other Item types in LEMON, like |
107 | 107 |
///Node and Arc types in digraph. |
108 | 108 |
class Col { |
109 | 109 |
friend class LpBase; |
110 | 110 |
protected: |
111 | 111 |
int _id; |
112 | 112 |
explicit Col(int id) : _id(id) {} |
113 | 113 |
public: |
114 | 114 |
typedef Value ExprValue; |
115 | 115 |
typedef True LpCol; |
116 | 116 |
/// Default constructor |
117 | 117 |
|
118 | 118 |
/// \warning The default constructor sets the Col to an |
119 | 119 |
/// undefined value. |
120 | 120 |
Col() {} |
121 | 121 |
/// Invalid constructor \& conversion. |
122 | 122 |
|
123 | 123 |
/// This constructor initializes the Col to be invalid. |
124 | 124 |
/// \sa Invalid for more details. |
125 | 125 |
Col(const Invalid&) : _id(-1) {} |
126 | 126 |
/// Equality operator |
127 | 127 |
|
128 | 128 |
/// Two \ref Col "Col"s are equal if and only if they point to |
129 | 129 |
/// the same LP column or both are invalid. |
130 | 130 |
bool operator==(Col c) const {return _id == c._id;} |
131 | 131 |
/// Inequality operator |
132 | 132 |
|
133 | 133 |
/// \sa operator==(Col c) |
134 | 134 |
/// |
135 | 135 |
bool operator!=(Col c) const {return _id != c._id;} |
136 | 136 |
/// Artificial ordering operator. |
137 | 137 |
|
138 | 138 |
/// To allow the use of this object in std::map or similar |
139 | 139 |
/// associative container we require this. |
140 | 140 |
/// |
141 | 141 |
/// \note This operator only have to define some strict ordering of |
142 | 142 |
/// the items; this order has nothing to do with the iteration |
143 | 143 |
/// ordering of the items. |
144 | 144 |
bool operator<(Col c) const {return _id < c._id;} |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
///Iterator for iterate over the columns of an LP problem |
148 | 148 |
|
149 | 149 |
/// Its usage is quite simple, for example, you can count the number |
150 | 150 |
/// of columns in an LP \c lp: |
151 | 151 |
///\code |
152 | 152 |
/// int count=0; |
153 | 153 |
/// for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count; |
154 | 154 |
///\endcode |
155 | 155 |
class ColIt : public Col { |
156 | 156 |
const LpBase *_solver; |
157 | 157 |
public: |
158 | 158 |
/// Default constructor |
159 | 159 |
|
160 | 160 |
/// \warning The default constructor sets the iterator |
161 | 161 |
/// to an undefined value. |
162 | 162 |
ColIt() {} |
163 | 163 |
/// Sets the iterator to the first Col |
164 | 164 |
|
165 | 165 |
/// Sets the iterator to the first Col. |
166 | 166 |
/// |
167 | 167 |
ColIt(const LpBase &solver) : _solver(&solver) |
168 | 168 |
{ |
169 | 169 |
_solver->cols.firstItem(_id); |
170 | 170 |
} |
171 | 171 |
/// Invalid constructor \& conversion |
172 | 172 |
|
173 | 173 |
/// Initialize the iterator to be invalid. |
174 | 174 |
/// \sa Invalid for more details. |
175 | 175 |
ColIt(const Invalid&) : Col(INVALID) {} |
176 | 176 |
/// Next column |
177 | 177 |
|
178 | 178 |
/// Assign the iterator to the next column. |
179 | 179 |
/// |
180 | 180 |
ColIt &operator++() |
181 | 181 |
{ |
182 | 182 |
_solver->cols.nextItem(_id); |
183 | 183 |
return *this; |
184 | 184 |
} |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
/// \brief Returns the ID of the column. |
188 | 188 |
static int id(const Col& col) { return col._id; } |
189 | 189 |
/// \brief Returns the column with the given ID. |
190 | 190 |
/// |
191 | 191 |
/// \pre The argument should be a valid column ID in the LP problem. |
192 | 192 |
static Col colFromId(int id) { return Col(id); } |
193 | 193 |
|
194 | 194 |
///Refer to a row of the LP. |
195 | 195 |
|
196 | 196 |
///This type is used to refer to a row of the LP. |
197 | 197 |
/// |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <sstream> |
20 | 20 |
#include <lemon/lp_skeleton.h> |
21 | 21 |
#include "test_tools.h" |
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
|
24 | 24 |
#include <lemon/config.h> |
25 | 25 |
|
26 | 26 |
#ifdef LEMON_HAVE_GLPK |
27 | 27 |
#include <lemon/glpk.h> |
28 | 28 |
#endif |
29 | 29 |
|
30 | 30 |
#ifdef LEMON_HAVE_CPLEX |
31 | 31 |
#include <lemon/cplex.h> |
32 | 32 |
#endif |
33 | 33 |
|
34 | 34 |
#ifdef LEMON_HAVE_SOPLEX |
35 | 35 |
#include <lemon/soplex.h> |
36 | 36 |
#endif |
37 | 37 |
|
38 | 38 |
#ifdef LEMON_HAVE_CLP |
39 | 39 |
#include <lemon/clp.h> |
40 | 40 |
#endif |
41 | 41 |
|
42 | 42 |
using namespace lemon; |
43 | 43 |
|
44 | 44 |
void lpTest(LpSolver& lp) |
45 | 45 |
{ |
46 | 46 |
|
47 | 47 |
typedef LpSolver LP; |
48 | 48 |
|
49 | 49 |
std::vector<LP::Col> x(10); |
50 | 50 |
// for(int i=0;i<10;i++) x.push_back(lp.addCol()); |
51 | 51 |
lp.addColSet(x); |
52 | 52 |
lp.colLowerBound(x,1); |
53 | 53 |
lp.colUpperBound(x,1); |
54 | 54 |
lp.colBounds(x,1,2); |
55 | 55 |
|
56 | 56 |
std::vector<LP::Col> y(10); |
57 | 57 |
lp.addColSet(y); |
58 | 58 |
|
59 | 59 |
lp.colLowerBound(y,1); |
60 | 60 |
lp.colUpperBound(y,1); |
61 | 61 |
lp.colBounds(y,1,2); |
62 | 62 |
|
63 | 63 |
std::map<int,LP::Col> z; |
64 | 64 |
|
65 | 65 |
z.insert(std::make_pair(12,INVALID)); |
66 | 66 |
z.insert(std::make_pair(2,INVALID)); |
67 | 67 |
z.insert(std::make_pair(7,INVALID)); |
68 | 68 |
z.insert(std::make_pair(5,INVALID)); |
69 | 69 |
|
70 | 70 |
lp.addColSet(z); |
71 | 71 |
|
72 | 72 |
lp.colLowerBound(z,1); |
73 | 73 |
lp.colUpperBound(z,1); |
74 | 74 |
lp.colBounds(z,1,2); |
75 | 75 |
|
76 | 76 |
{ |
77 | 77 |
LP::Expr e,f,g; |
78 | 78 |
LP::Col p1,p2,p3,p4,p5; |
79 | 79 |
LP::Constr c; |
80 | 80 |
|
81 | 81 |
p1=lp.addCol(); |
82 | 82 |
p2=lp.addCol(); |
83 | 83 |
p3=lp.addCol(); |
84 | 84 |
p4=lp.addCol(); |
85 | 85 |
p5=lp.addCol(); |
86 | 86 |
|
87 | 87 |
e[p1]=2; |
88 | 88 |
*e=12; |
89 | 89 |
e[p1]+=2; |
90 | 90 |
*e+=12; |
91 | 91 |
e[p1]-=2; |
92 | 92 |
*e-=12; |
93 | 93 |
|
94 | 94 |
e=2; |
95 | 95 |
e=2.2; |
96 | 96 |
e=p1; |
97 | 97 |
e=f; |
98 | 98 |
|
99 | 99 |
e+=2; |
100 | 100 |
e+=2.2; |
101 | 101 |
e+=p1; |
102 | 102 |
e+=f; |
103 | 103 |
|
104 | 104 |
e-=2; |
105 | 105 |
e-=2.2; |
106 | 106 |
e-=p1; |
107 | 107 |
e-=f; |
108 | 108 |
|
109 | 109 |
e*=2; |
110 | 110 |
e*=2.2; |
111 | 111 |
e/=2; |
112 | 112 |
e/=2.2; |
113 | 113 |
|
114 | 114 |
e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ |
115 | 115 |
(f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ |
116 | 116 |
(f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ |
117 | 117 |
2.2*f+f*2.2+f/2.2+ |
118 | 118 |
2*f+f*2+f/2+ |
119 | 119 |
2.2*p1+p1*2.2+p1/2.2+ |
120 | 120 |
2*p1+p1*2+p1/2 |
121 | 121 |
); |
122 | 122 |
|
123 | 123 |
|
124 | 124 |
c = (e <= f ); |
125 | 125 |
c = (e <= 2.2); |
126 | 126 |
c = (e <= 2 ); |
127 | 127 |
c = (e <= p1 ); |
128 | 128 |
c = (2.2<= f ); |
129 | 129 |
c = (2 <= f ); |
130 | 130 |
c = (p1 <= f ); |
131 | 131 |
c = (p1 <= p2 ); |
132 | 132 |
c = (p1 <= 2.2); |
133 | 133 |
c = (p1 <= 2 ); |
134 | 134 |
c = (2.2<= p2 ); |
135 | 135 |
c = (2 <= p2 ); |
136 | 136 |
|
137 | 137 |
c = (e >= f ); |
138 | 138 |
c = (e >= 2.2); |
139 | 139 |
c = (e >= 2 ); |
140 | 140 |
c = (e >= p1 ); |
141 | 141 |
c = (2.2>= f ); |
142 | 142 |
c = (2 >= f ); |
143 | 143 |
c = (p1 >= f ); |
144 | 144 |
c = (p1 >= p2 ); |
145 | 145 |
c = (p1 >= 2.2); |
146 | 146 |
c = (p1 >= 2 ); |
147 | 147 |
c = (2.2>= p2 ); |
148 | 148 |
c = (2 >= p2 ); |
149 | 149 |
|
150 | 150 |
c = (e == f ); |
151 | 151 |
c = (e == 2.2); |
152 | 152 |
c = (e == 2 ); |
153 | 153 |
c = (e == p1 ); |
154 | 154 |
c = (2.2== f ); |
155 | 155 |
c = (2 == f ); |
156 | 156 |
c = (p1 == f ); |
157 | 157 |
//c = (p1 == p2 ); |
158 | 158 |
c = (p1 == 2.2); |
159 | 159 |
c = (p1 == 2 ); |
160 | 160 |
c = (2.2== p2 ); |
161 | 161 |
c = (2 == p2 ); |
162 | 162 |
|
163 | 163 |
c = ((2 <= e) <= 3); |
164 | 164 |
c = ((2 <= p1) <= 3); |
165 | 165 |
|
166 | 166 |
c = ((2 >= e) >= 3); |
167 | 167 |
c = ((2 >= p1) >= 3); |
168 | 168 |
|
169 | 169 |
{ //Tests for #430 |
170 | 170 |
LP::Col v=lp.addCol(); |
171 | 171 |
LP::Constr c = v >= -3; |
172 | 172 |
c = c <= 4; |
173 | 173 |
LP::Constr c2; |
174 | 174 |
c2 = -3 <= v <= 4; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
e[x[3]]=2; |
178 | 178 |
e[x[3]]=4; |
179 | 179 |
e[x[3]]=1; |
180 | 180 |
*e=12; |
181 | 181 |
|
182 | 182 |
lp.addRow(-LP::INF,e,23); |
183 | 183 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); |
184 | 184 |
lp.addRow(-LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); |
185 | 185 |
|
186 | 186 |
lp.addRow(x[1]+x[3]<=x[5]-3); |
187 | 187 |
lp.addRow((-7<=x[1]+x[3]-12)<=3); |
188 | 188 |
lp.addRow(x[1]<=x[5]); |
189 | 189 |
|
190 | 190 |
std::ostringstream buf; |
191 | 191 |
|
192 | 192 |
|
193 | 193 |
e=((p1+p2)+(p1-0.99*p2)); |
194 | 194 |
//e.prettyPrint(std::cout); |
195 | 195 |
//(e<=2).prettyPrint(std::cout); |
196 | 196 |
double tolerance=0.001; |
197 | 197 |
e.simplify(tolerance); |
0 comments (0 inline)