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