author | ladanyi |
Tue, 14 Jun 2005 10:18:12 +0000 | |
changeset 1482 | b9818eea0550 |
parent 1473 | 876c7b7f4dae |
child 1484 | a3484f00a5f0 |
permissions | -rw-r--r-- |
1 #include<lemon/lp_skeleton.h>
2 #include "test_tools.h"
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
8 #ifdef HAVE_GLPK
9 #include <lemon/lp_glpk.h>
10 #endif
12 #ifdef HAVE_CPLEX
13 #include <lemon/lp_cplex.h>
14 #endif
16 using namespace lemon;
18 void lpTest(LpSolverBase & lp)
19 {
20 typedef LpSolverBase LP;
22 std::vector<LP::Col> x(10);
23 // for(int i=0;i<10;i++) x.push_back(lp.addCol());
24 lp.addColSet(x);
26 std::vector<LP::Col> y(10);
27 lp.addColSet(y);
29 std::map<int,LP::Col> z;
31 z.insert(std::make_pair(12,INVALID));
32 z.insert(std::make_pair(2,INVALID));
33 z.insert(std::make_pair(7,INVALID));
34 z.insert(std::make_pair(5,INVALID));
36 lp.addColSet(z);
38 {
39 LP::Expr e,f,g;
40 LP::Col p1,p2,p3,p4,p5;
41 LP::Constr c;
43 e[p1]=2;
44 e.constComp()=12;
45 e[p1]+=2;
46 e.constComp()+=12;
47 e[p1]-=2;
48 e.constComp()-=12;
50 e=2;
51 e=2.2;
52 e=p1;
53 e=f;
55 e+=2;
56 e+=2.2;
57 e+=p1;
58 e+=f;
60 e-=2;
61 e-=2.2;
62 e-=p1;
63 e-=f;
65 e*=2;
66 e*=2.2;
67 e/=2;
68 e/=2.2;
70 e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
71 (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
72 (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
73 2.2*f+f*2.2+f/2.2+
74 2*f+f*2+f/2+
75 2.2*p1+p1*2.2+p1/2.2+
76 2*p1+p1*2+p1/2
77 );
80 c = (e <= f );
81 c = (e <= 2.2);
82 c = (e <= 2 );
83 c = (e <= p1 );
84 c = (2.2<= f );
85 c = (2 <= f );
86 c = (p1 <= f );
87 c = (p1 <= p2 );
88 c = (p1 <= 2.2);
89 c = (p1 <= 2 );
90 c = (2.2<= p2 );
91 c = (2 <= p2 );
93 c = (e >= f );
94 c = (e >= 2.2);
95 c = (e >= 2 );
96 c = (e >= p1 );
97 c = (2.2>= f );
98 c = (2 >= f );
99 c = (p1 >= f );
100 c = (p1 >= p2 );
101 c = (p1 >= 2.2);
102 c = (p1 >= 2 );
103 c = (2.2>= p2 );
104 c = (2 >= p2 );
106 c = (e == f );
107 c = (e == 2.2);
108 c = (e == 2 );
109 c = (e == p1 );
110 c = (2.2== f );
111 c = (2 == f );
112 c = (p1 == f );
113 //c = (p1 == p2 );
114 c = (p1 == 2.2);
115 c = (p1 == 2 );
116 c = (2.2== p2 );
117 c = (2 == p2 );
119 c = (2 <= e <= 3);
120 c = (2 <= p1<= 3);
122 c = (2 >= e >= 3);
123 c = (2 >= p1>= 3);
125 e[x[3]]=2;
126 e[x[3]]=4;
127 e[x[3]]=1;
128 e.constComp()=12;
130 lp.addRow(LP::INF,e,23);
131 lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
132 lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
134 lp.addRow(x[1]+x[3]<=x[5]-3);
135 lp.addRow(-7<=x[1]+x[3]-12<=3);
136 lp.addRow(x[1]<=x[5]);
137 }
139 {
140 LP::DualExpr e,f,g;
141 LP::Row p1,p2,p3,p4,p5;
143 e[p1]=2;
144 e[p1]+=2;
145 e[p1]-=2;
147 e=p1;
148 e=f;
150 e+=p1;
151 e+=f;
153 e-=p1;
154 e-=f;
156 e*=2;
157 e*=2.2;
158 e/=2;
159 e/=2.2;
161 e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
162 (p1+f)+(f+p1)+(f+g)+
163 (p1-f)+(f-p1)+(f-g)+
164 2.2*f+f*2.2+f/2.2+
165 2*f+f*2+f/2+
166 2.2*p1+p1*2.2+p1/2.2+
167 2*p1+p1*2+p1/2
168 );
169 }
172 }
174 void aTest(LpSolverBase & lp)
175 {
176 typedef LpSolverBase LP;
178 //The following example is taken from the book by Gáspár and Temesi, page 39.
180 typedef LpSolverBase::Row Row;
181 typedef LpSolverBase::Col Col;
184 Col x1 = lp.addCol();
185 Col x2 = lp.addCol();
188 //Constraints
189 lp.addRow(3*x1+2*x2 >=6);
190 lp.addRow(-1*x1+x2<=4);
191 lp.addRow(5*x1+8*x2<=40);
192 lp.addRow(x1-2*x2<=4);
193 //Nonnegativity of the variables
194 lp.colLowerBound(x1, 0);
195 lp.colLowerBound(x2, 0);
196 //Objective function
197 lp.setObj(2*x1+x2);
199 lp.max();
200 lp.solve();
203 if (lp.primalStatus()==LpSolverBase::OPTIMAL){
204 printf("Z = %g; x1 = %g; x2 = %g\n",
205 lp.primalValue(),
206 lp.primal(x1), lp.primal(x2));
207 }
208 else{
209 std::cout<<"Optimal solution not found!"<<std::endl;
210 }
212 check(lp.primalStatus()==LpSolverBase::OPTIMAL,"Primalstatus should be OPTIMAL");
214 double opt=123/9;
215 check(lp.primalValue()==opt,"The optimum value is 122/9");
218 }
221 int main()
222 {
223 LpSkeleton lp_skel;
224 lpTest(lp_skel);
226 #ifdef HAVE_GLPK
227 LpGlpk lp_glpk;
228 lpTest(lp_glpk);
229 aTest(lp_glpk);
230 #endif
232 #ifdef HAVE_CPLEX
233 // LpCplex lp_cplex;
234 // lpTest(lp_cplex);
235 #endif
237 return 0;
238 }