1 | #include<lemon/lp_skeleton.h> |
---|
2 | #include "test_tools.h" |
---|
3 | |
---|
4 | |
---|
5 | #ifdef HAVE_CONFIG_H |
---|
6 | #include <config.h> |
---|
7 | #endif |
---|
8 | |
---|
9 | #ifdef HAVE_GLPK |
---|
10 | #include <lemon/lp_glpk.h> |
---|
11 | #endif |
---|
12 | |
---|
13 | #ifdef HAVE_CPLEX |
---|
14 | #include <lemon/lp_cplex.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | using namespace lemon; |
---|
18 | |
---|
19 | void lpTest(LpSolverBase & lp) |
---|
20 | { |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | typedef LpSolverBase LP; |
---|
25 | |
---|
26 | std::vector<LP::Col> x(10); |
---|
27 | // for(int i=0;i<10;i++) x.push_back(lp.addCol()); |
---|
28 | lp.addColSet(x); |
---|
29 | |
---|
30 | #ifndef GYORSITAS |
---|
31 | |
---|
32 | std::vector<LP::Col> y(10); |
---|
33 | lp.addColSet(y); |
---|
34 | |
---|
35 | std::map<int,LP::Col> z; |
---|
36 | |
---|
37 | z.insert(std::make_pair(12,INVALID)); |
---|
38 | z.insert(std::make_pair(2,INVALID)); |
---|
39 | z.insert(std::make_pair(7,INVALID)); |
---|
40 | z.insert(std::make_pair(5,INVALID)); |
---|
41 | |
---|
42 | lp.addColSet(z); |
---|
43 | |
---|
44 | { |
---|
45 | LP::Expr e,f,g; |
---|
46 | LP::Col p1,p2,p3,p4,p5; |
---|
47 | LP::Constr c; |
---|
48 | |
---|
49 | p1=lp.addCol(); |
---|
50 | p2=lp.addCol(); |
---|
51 | p3=lp.addCol(); |
---|
52 | p4=lp.addCol(); |
---|
53 | p5=lp.addCol(); |
---|
54 | |
---|
55 | e[p1]=2; |
---|
56 | e.constComp()=12; |
---|
57 | e[p1]+=2; |
---|
58 | e.constComp()+=12; |
---|
59 | e[p1]-=2; |
---|
60 | e.constComp()-=12; |
---|
61 | |
---|
62 | e=2; |
---|
63 | e=2.2; |
---|
64 | e=p1; |
---|
65 | e=f; |
---|
66 | |
---|
67 | e+=2; |
---|
68 | e+=2.2; |
---|
69 | e+=p1; |
---|
70 | e+=f; |
---|
71 | |
---|
72 | e-=2; |
---|
73 | e-=2.2; |
---|
74 | e-=p1; |
---|
75 | e-=f; |
---|
76 | |
---|
77 | e*=2; |
---|
78 | e*=2.2; |
---|
79 | e/=2; |
---|
80 | e/=2.2; |
---|
81 | |
---|
82 | e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+ |
---|
83 | (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+ |
---|
84 | (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+ |
---|
85 | 2.2*f+f*2.2+f/2.2+ |
---|
86 | 2*f+f*2+f/2+ |
---|
87 | 2.2*p1+p1*2.2+p1/2.2+ |
---|
88 | 2*p1+p1*2+p1/2 |
---|
89 | ); |
---|
90 | |
---|
91 | |
---|
92 | c = (e <= f ); |
---|
93 | c = (e <= 2.2); |
---|
94 | c = (e <= 2 ); |
---|
95 | c = (e <= p1 ); |
---|
96 | c = (2.2<= f ); |
---|
97 | c = (2 <= f ); |
---|
98 | c = (p1 <= f ); |
---|
99 | c = (p1 <= p2 ); |
---|
100 | c = (p1 <= 2.2); |
---|
101 | c = (p1 <= 2 ); |
---|
102 | c = (2.2<= p2 ); |
---|
103 | c = (2 <= p2 ); |
---|
104 | |
---|
105 | c = (e >= f ); |
---|
106 | c = (e >= 2.2); |
---|
107 | c = (e >= 2 ); |
---|
108 | c = (e >= p1 ); |
---|
109 | c = (2.2>= f ); |
---|
110 | c = (2 >= f ); |
---|
111 | c = (p1 >= f ); |
---|
112 | c = (p1 >= p2 ); |
---|
113 | c = (p1 >= 2.2); |
---|
114 | c = (p1 >= 2 ); |
---|
115 | c = (2.2>= p2 ); |
---|
116 | c = (2 >= p2 ); |
---|
117 | |
---|
118 | c = (e == f ); |
---|
119 | c = (e == 2.2); |
---|
120 | c = (e == 2 ); |
---|
121 | c = (e == p1 ); |
---|
122 | c = (2.2== f ); |
---|
123 | c = (2 == f ); |
---|
124 | c = (p1 == f ); |
---|
125 | //c = (p1 == p2 ); |
---|
126 | c = (p1 == 2.2); |
---|
127 | c = (p1 == 2 ); |
---|
128 | c = (2.2== p2 ); |
---|
129 | c = (2 == p2 ); |
---|
130 | |
---|
131 | c = (2 <= e <= 3); |
---|
132 | c = (2 <= p1<= 3); |
---|
133 | |
---|
134 | c = (2 >= e >= 3); |
---|
135 | c = (2 >= p1>= 3); |
---|
136 | |
---|
137 | e[x[3]]=2; |
---|
138 | e[x[3]]=4; |
---|
139 | e[x[3]]=1; |
---|
140 | e.constComp()=12; |
---|
141 | |
---|
142 | lp.addRow(LP::INF,e,23); |
---|
143 | lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23); |
---|
144 | lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23); |
---|
145 | |
---|
146 | lp.addRow(x[1]+x[3]<=x[5]-3); |
---|
147 | lp.addRow(-7<=x[1]+x[3]-12<=3); |
---|
148 | lp.addRow(x[1]<=x[5]); |
---|
149 | } |
---|
150 | |
---|
151 | { |
---|
152 | LP::DualExpr e,f,g; |
---|
153 | LP::Row p1,p2,p3,p4,p5; |
---|
154 | |
---|
155 | e[p1]=2; |
---|
156 | e[p1]+=2; |
---|
157 | e[p1]-=2; |
---|
158 | |
---|
159 | e=p1; |
---|
160 | e=f; |
---|
161 | |
---|
162 | e+=p1; |
---|
163 | e+=f; |
---|
164 | |
---|
165 | e-=p1; |
---|
166 | e-=f; |
---|
167 | |
---|
168 | e*=2; |
---|
169 | e*=2.2; |
---|
170 | e/=2; |
---|
171 | e/=2.2; |
---|
172 | |
---|
173 | e=((p1+p2)+(p1-p2)+ |
---|
174 | (p1+f)+(f+p1)+(f+g)+ |
---|
175 | (p1-f)+(f-p1)+(f-g)+ |
---|
176 | 2.2*f+f*2.2+f/2.2+ |
---|
177 | 2*f+f*2+f/2+ |
---|
178 | 2.2*p1+p1*2.2+p1/2.2+ |
---|
179 | 2*p1+p1*2+p1/2 |
---|
180 | ); |
---|
181 | } |
---|
182 | |
---|
183 | #endif |
---|
184 | } |
---|
185 | |
---|
186 | void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat, |
---|
187 | double exp_opt){ |
---|
188 | lp.solve(); |
---|
189 | //int decimal,sign; |
---|
190 | std::string buf1; |
---|
191 | // itoa(stat,buf1, 10); |
---|
192 | check(lp.primalStatus()==stat,"Primalstatus should be "+buf1); |
---|
193 | |
---|
194 | if (stat == LpSolverBase::OPTIMAL){ |
---|
195 | check(std::abs(lp.primalValue()-exp_opt)<1e-3, |
---|
196 | "Wrong optimal value: the right optimum is "); |
---|
197 | //+ecvt(exp_opt,2) |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | void aTest(LpSolverBase & lp) |
---|
202 | { |
---|
203 | typedef LpSolverBase LP; |
---|
204 | |
---|
205 | //The following example is very simple |
---|
206 | |
---|
207 | typedef LpSolverBase::Row Row; |
---|
208 | typedef LpSolverBase::Col Col; |
---|
209 | |
---|
210 | |
---|
211 | Col x1 = lp.addCol(); |
---|
212 | Col x2 = lp.addCol(); |
---|
213 | |
---|
214 | |
---|
215 | //Constraints |
---|
216 | Row upright=lp.addRow(x1+x2 <=1); |
---|
217 | lp.addRow(x1+x2 >=-1); |
---|
218 | lp.addRow(x1-x2 <=1); |
---|
219 | lp.addRow(x1-x2 >=-1); |
---|
220 | //Nonnegativity of the variables |
---|
221 | lp.colLowerBound(x1, 0); |
---|
222 | lp.colLowerBound(x2, 0); |
---|
223 | //Objective function |
---|
224 | lp.setObj(x1+x2); |
---|
225 | |
---|
226 | lp.max(); |
---|
227 | |
---|
228 | //Maximization of x1+x2 |
---|
229 | //over the triangle with vertices (0,0) (0,1) (1,0) |
---|
230 | double expected_opt=1; |
---|
231 | solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); |
---|
232 | |
---|
233 | //Minimization |
---|
234 | lp.min(); |
---|
235 | expected_opt=0; |
---|
236 | solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); |
---|
237 | |
---|
238 | //Vertex (-1,0) instead of (0,0) |
---|
239 | lp.colLowerBound(x1, -LpSolverBase::INF); |
---|
240 | expected_opt=-1; |
---|
241 | solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt); |
---|
242 | |
---|
243 | //Erase one constraint and return to maximization |
---|
244 | lp.eraseRow(upright); |
---|
245 | lp.max(); |
---|
246 | expected_opt=LpSolverBase::INF; |
---|
247 | solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt); |
---|
248 | |
---|
249 | //Infeasibilty |
---|
250 | lp.addRow(x1+x2 <=-2); |
---|
251 | solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt); |
---|
252 | |
---|
253 | //Change problem and forget to solve |
---|
254 | lp.min(); |
---|
255 | check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED"); |
---|
256 | |
---|
257 | // lp.solve(); |
---|
258 | // if (lp.primalStatus()==LpSolverBase::OPTIMAL){ |
---|
259 | // std::cout<< "Z = "<<lp.primalValue() |
---|
260 | // << " (error = " << lp.primalValue()-expected_opt |
---|
261 | // << "); x1 = "<<lp.primal(x1) |
---|
262 | // << "; x2 = "<<lp.primal(x2) |
---|
263 | // <<std::endl; |
---|
264 | |
---|
265 | // } |
---|
266 | // else{ |
---|
267 | // std::cout<<lp.primalStatus()<<std::endl; |
---|
268 | // std::cout<<"Optimal solution not found!"<<std::endl; |
---|
269 | // } |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | int main() |
---|
277 | { |
---|
278 | LpSkeleton lp_skel; |
---|
279 | lpTest(lp_skel); |
---|
280 | |
---|
281 | #ifdef HAVE_GLPK |
---|
282 | LpGlpk lp_glpk1,lp_glpk2; |
---|
283 | lpTest(lp_glpk1); |
---|
284 | aTest(lp_glpk2); |
---|
285 | #endif |
---|
286 | |
---|
287 | #ifdef HAVE_CPLEX |
---|
288 | LpCplex lp_cplex; |
---|
289 | lpTest(lp_cplex); |
---|
290 | aTest(lp_cplex); |
---|
291 | #endif |
---|
292 | |
---|
293 | return 0; |
---|
294 | } |
---|