alpar@1390
|
1 |
#include<lemon/lp_skeleton.h>
|
athos@1473
|
2 |
#include "test_tools.h"
|
alpar@1390
|
3 |
|
ladanyi@1387
|
4 |
#ifdef HAVE_CONFIG_H
|
ladanyi@1387
|
5 |
#include <config.h>
|
ladanyi@1387
|
6 |
#endif
|
ladanyi@1387
|
7 |
|
ladanyi@1387
|
8 |
#ifdef HAVE_GLPK
|
ladanyi@1387
|
9 |
#include <lemon/lp_glpk.h>
|
ladanyi@1437
|
10 |
#endif
|
ladanyi@1437
|
11 |
|
ladanyi@1437
|
12 |
#ifdef HAVE_CPLEX
|
ladanyi@1387
|
13 |
#include <lemon/lp_cplex.h>
|
ladanyi@1387
|
14 |
#endif
|
alpar@1254
|
15 |
|
alpar@1254
|
16 |
using namespace lemon;
|
alpar@1254
|
17 |
|
alpar@1263
|
18 |
void lpTest(LpSolverBase & lp)
|
alpar@1254
|
19 |
{
|
alpar@1263
|
20 |
typedef LpSolverBase LP;
|
alpar@1256
|
21 |
|
alpar@1309
|
22 |
std::vector<LP::Col> x(10);
|
alpar@1309
|
23 |
// for(int i=0;i<10;i++) x.push_back(lp.addCol());
|
alpar@1309
|
24 |
lp.addColSet(x);
|
alpar@1256
|
25 |
|
alpar@1256
|
26 |
std::vector<LP::Col> y(10);
|
alpar@1256
|
27 |
lp.addColSet(y);
|
alpar@1256
|
28 |
|
alpar@1256
|
29 |
std::map<int,LP::Col> z;
|
alpar@1256
|
30 |
|
alpar@1256
|
31 |
z.insert(std::make_pair(12,INVALID));
|
alpar@1256
|
32 |
z.insert(std::make_pair(2,INVALID));
|
alpar@1256
|
33 |
z.insert(std::make_pair(7,INVALID));
|
alpar@1256
|
34 |
z.insert(std::make_pair(5,INVALID));
|
alpar@1256
|
35 |
|
alpar@1256
|
36 |
lp.addColSet(z);
|
alpar@1256
|
37 |
|
alpar@1445
|
38 |
{
|
alpar@1445
|
39 |
LP::Expr e,f,g;
|
alpar@1445
|
40 |
LP::Col p1,p2,p3,p4,p5;
|
alpar@1445
|
41 |
LP::Constr c;
|
alpar@1445
|
42 |
|
alpar@1445
|
43 |
e[p1]=2;
|
alpar@1445
|
44 |
e.constComp()=12;
|
alpar@1445
|
45 |
e[p1]+=2;
|
alpar@1445
|
46 |
e.constComp()+=12;
|
alpar@1445
|
47 |
e[p1]-=2;
|
alpar@1445
|
48 |
e.constComp()-=12;
|
alpar@1445
|
49 |
|
alpar@1445
|
50 |
e=2;
|
alpar@1445
|
51 |
e=2.2;
|
alpar@1445
|
52 |
e=p1;
|
alpar@1445
|
53 |
e=f;
|
alpar@1445
|
54 |
|
alpar@1445
|
55 |
e+=2;
|
alpar@1445
|
56 |
e+=2.2;
|
alpar@1445
|
57 |
e+=p1;
|
alpar@1445
|
58 |
e+=f;
|
alpar@1445
|
59 |
|
alpar@1445
|
60 |
e-=2;
|
alpar@1445
|
61 |
e-=2.2;
|
alpar@1445
|
62 |
e-=p1;
|
alpar@1445
|
63 |
e-=f;
|
alpar@1445
|
64 |
|
alpar@1445
|
65 |
e*=2;
|
alpar@1445
|
66 |
e*=2.2;
|
alpar@1445
|
67 |
e/=2;
|
alpar@1445
|
68 |
e/=2.2;
|
alpar@1445
|
69 |
|
alpar@1445
|
70 |
e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
|
alpar@1445
|
71 |
(f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
|
alpar@1445
|
72 |
(f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
|
alpar@1445
|
73 |
2.2*f+f*2.2+f/2.2+
|
alpar@1445
|
74 |
2*f+f*2+f/2+
|
alpar@1445
|
75 |
2.2*p1+p1*2.2+p1/2.2+
|
alpar@1445
|
76 |
2*p1+p1*2+p1/2
|
alpar@1445
|
77 |
);
|
alpar@1256
|
78 |
|
alpar@1445
|
79 |
|
alpar@1445
|
80 |
c = (e <= f );
|
alpar@1445
|
81 |
c = (e <= 2.2);
|
alpar@1445
|
82 |
c = (e <= 2 );
|
alpar@1445
|
83 |
c = (e <= p1 );
|
alpar@1445
|
84 |
c = (2.2<= f );
|
alpar@1445
|
85 |
c = (2 <= f );
|
alpar@1445
|
86 |
c = (p1 <= f );
|
alpar@1445
|
87 |
c = (p1 <= p2 );
|
alpar@1445
|
88 |
c = (p1 <= 2.2);
|
alpar@1445
|
89 |
c = (p1 <= 2 );
|
alpar@1445
|
90 |
c = (2.2<= p2 );
|
alpar@1445
|
91 |
c = (2 <= p2 );
|
alpar@1445
|
92 |
|
alpar@1445
|
93 |
c = (e >= f );
|
alpar@1445
|
94 |
c = (e >= 2.2);
|
alpar@1445
|
95 |
c = (e >= 2 );
|
alpar@1445
|
96 |
c = (e >= p1 );
|
alpar@1445
|
97 |
c = (2.2>= f );
|
alpar@1445
|
98 |
c = (2 >= f );
|
alpar@1445
|
99 |
c = (p1 >= f );
|
alpar@1445
|
100 |
c = (p1 >= p2 );
|
alpar@1445
|
101 |
c = (p1 >= 2.2);
|
alpar@1445
|
102 |
c = (p1 >= 2 );
|
alpar@1445
|
103 |
c = (2.2>= p2 );
|
alpar@1445
|
104 |
c = (2 >= p2 );
|
alpar@1445
|
105 |
|
alpar@1445
|
106 |
c = (e == f );
|
alpar@1445
|
107 |
c = (e == 2.2);
|
alpar@1445
|
108 |
c = (e == 2 );
|
alpar@1445
|
109 |
c = (e == p1 );
|
alpar@1445
|
110 |
c = (2.2== f );
|
alpar@1445
|
111 |
c = (2 == f );
|
alpar@1445
|
112 |
c = (p1 == f );
|
alpar@1445
|
113 |
//c = (p1 == p2 );
|
alpar@1445
|
114 |
c = (p1 == 2.2);
|
alpar@1445
|
115 |
c = (p1 == 2 );
|
alpar@1445
|
116 |
c = (2.2== p2 );
|
alpar@1445
|
117 |
c = (2 == p2 );
|
alpar@1445
|
118 |
|
alpar@1445
|
119 |
c = (2 <= e <= 3);
|
alpar@1445
|
120 |
c = (2 <= p1<= 3);
|
alpar@1445
|
121 |
|
alpar@1445
|
122 |
c = (2 >= e >= 3);
|
alpar@1445
|
123 |
c = (2 >= p1>= 3);
|
alpar@1445
|
124 |
|
alpar@1445
|
125 |
e[x[3]]=2;
|
alpar@1445
|
126 |
e[x[3]]=4;
|
alpar@1445
|
127 |
e[x[3]]=1;
|
alpar@1445
|
128 |
e.constComp()=12;
|
alpar@1445
|
129 |
|
alpar@1445
|
130 |
lp.addRow(LP::INF,e,23);
|
alpar@1445
|
131 |
lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
|
alpar@1445
|
132 |
lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
|
alpar@1445
|
133 |
|
alpar@1445
|
134 |
lp.addRow(x[1]+x[3]<=x[5]-3);
|
alpar@1445
|
135 |
lp.addRow(-7<=x[1]+x[3]-12<=3);
|
alpar@1445
|
136 |
lp.addRow(x[1]<=x[5]);
|
alpar@1445
|
137 |
}
|
alpar@1272
|
138 |
|
alpar@1445
|
139 |
{
|
alpar@1445
|
140 |
LP::DualExpr e,f,g;
|
alpar@1445
|
141 |
LP::Row p1,p2,p3,p4,p5;
|
alpar@1445
|
142 |
|
alpar@1445
|
143 |
e[p1]=2;
|
alpar@1445
|
144 |
e[p1]+=2;
|
alpar@1445
|
145 |
e[p1]-=2;
|
alpar@1445
|
146 |
|
alpar@1445
|
147 |
e=p1;
|
alpar@1445
|
148 |
e=f;
|
alpar@1445
|
149 |
|
alpar@1445
|
150 |
e+=p1;
|
alpar@1445
|
151 |
e+=f;
|
alpar@1445
|
152 |
|
alpar@1445
|
153 |
e-=p1;
|
alpar@1445
|
154 |
e-=f;
|
alpar@1445
|
155 |
|
alpar@1445
|
156 |
e*=2;
|
alpar@1445
|
157 |
e*=2.2;
|
alpar@1445
|
158 |
e/=2;
|
alpar@1445
|
159 |
e/=2.2;
|
alpar@1445
|
160 |
|
alpar@1445
|
161 |
e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
|
alpar@1445
|
162 |
(p1+f)+(f+p1)+(f+g)+
|
alpar@1445
|
163 |
(p1-f)+(f-p1)+(f-g)+
|
alpar@1445
|
164 |
2.2*f+f*2.2+f/2.2+
|
alpar@1445
|
165 |
2*f+f*2+f/2+
|
alpar@1445
|
166 |
2.2*p1+p1*2.2+p1/2.2+
|
alpar@1445
|
167 |
2*p1+p1*2+p1/2
|
alpar@1445
|
168 |
);
|
alpar@1445
|
169 |
}
|
alpar@1272
|
170 |
|
alpar@1272
|
171 |
|
alpar@1264
|
172 |
}
|
alpar@1264
|
173 |
|
athos@1473
|
174 |
void aTest(LpSolverBase & lp)
|
athos@1473
|
175 |
{
|
athos@1473
|
176 |
typedef LpSolverBase LP;
|
athos@1473
|
177 |
|
athos@1473
|
178 |
//The following example is taken from the book by Gáspár and Temesi, page 39.
|
athos@1473
|
179 |
|
athos@1473
|
180 |
typedef LpSolverBase::Row Row;
|
athos@1473
|
181 |
typedef LpSolverBase::Col Col;
|
athos@1473
|
182 |
|
athos@1473
|
183 |
|
athos@1473
|
184 |
Col x1 = lp.addCol();
|
athos@1473
|
185 |
Col x2 = lp.addCol();
|
athos@1473
|
186 |
|
athos@1473
|
187 |
|
athos@1473
|
188 |
//Constraints
|
athos@1473
|
189 |
lp.addRow(3*x1+2*x2 >=6);
|
athos@1473
|
190 |
lp.addRow(-1*x1+x2<=4);
|
athos@1473
|
191 |
lp.addRow(5*x1+8*x2<=40);
|
athos@1473
|
192 |
lp.addRow(x1-2*x2<=4);
|
athos@1473
|
193 |
//Nonnegativity of the variables
|
athos@1473
|
194 |
lp.colLowerBound(x1, 0);
|
athos@1473
|
195 |
lp.colLowerBound(x2, 0);
|
athos@1473
|
196 |
//Objective function
|
athos@1473
|
197 |
lp.setObj(2*x1+x2);
|
athos@1473
|
198 |
|
athos@1473
|
199 |
lp.max();
|
athos@1473
|
200 |
lp.solve();
|
athos@1473
|
201 |
|
athos@1473
|
202 |
|
athos@1473
|
203 |
if (lp.primalStatus()==LpSolverBase::OPTIMAL){
|
athos@1473
|
204 |
printf("Z = %g; x1 = %g; x2 = %g\n",
|
athos@1473
|
205 |
lp.primalValue(),
|
athos@1473
|
206 |
lp.primal(x1), lp.primal(x2));
|
athos@1473
|
207 |
}
|
athos@1473
|
208 |
else{
|
athos@1473
|
209 |
std::cout<<"Optimal solution not found!"<<std::endl;
|
athos@1473
|
210 |
}
|
athos@1473
|
211 |
|
athos@1473
|
212 |
check(lp.primalStatus()==LpSolverBase::OPTIMAL,"Primalstatus should be OPTIMAL");
|
athos@1473
|
213 |
|
athos@1473
|
214 |
double opt=123/9;
|
athos@1473
|
215 |
check(lp.primalValue()==opt,"The optimum value is 122/9");
|
athos@1473
|
216 |
|
athos@1473
|
217 |
|
athos@1473
|
218 |
}
|
athos@1473
|
219 |
|
athos@1473
|
220 |
|
alpar@1263
|
221 |
int main()
|
alpar@1263
|
222 |
{
|
alpar@1390
|
223 |
LpSkeleton lp_skel;
|
alpar@1390
|
224 |
lpTest(lp_skel);
|
alpar@1390
|
225 |
|
ladanyi@1437
|
226 |
#ifdef HAVE_GLPK
|
ladanyi@1437
|
227 |
LpGlpk lp_glpk;
|
ladanyi@1437
|
228 |
lpTest(lp_glpk);
|
athos@1473
|
229 |
aTest(lp_glpk);
|
ladanyi@1437
|
230 |
#endif
|
alpar@1263
|
231 |
|
ladanyi@1437
|
232 |
#ifdef HAVE_CPLEX
|
athos@1475
|
233 |
// LpCplex lp_cplex;
|
athos@1475
|
234 |
// lpTest(lp_cplex);
|
ladanyi@1437
|
235 |
#endif
|
alpar@1264
|
236 |
|
alpar@1309
|
237 |
return 0;
|
alpar@1263
|
238 |
}
|