1 | #if defined(_MSC_VER) |
---|
2 | // Turn off compiler warning about long names |
---|
3 | # pragma warning(disable:4786) |
---|
4 | #endif |
---|
5 | #include <cassert> |
---|
6 | #include <iostream> |
---|
7 | using namespace std; |
---|
8 | #include "CoinHelperFunctions.hpp" |
---|
9 | #include "CoinError.hpp" |
---|
10 | #include "CbcModel.hpp" |
---|
11 | #include "CbcBranchActual.hpp" //for CbcSOS |
---|
12 | #include "CbcBranchLotsize.hpp" //for CbcLotsize |
---|
13 | #include "OsiClpSolverInterface.hpp" |
---|
14 | #define testtol 1e-6 |
---|
15 | /** model sos1a from the GAMS test library |
---|
16 | * http://www.gams.com/testlib/libhtml/sos1a.htm */ |
---|
17 | void sos1a(int& error_count, int& warning_count); |
---|
18 | /** model sos2a from the GAMS test library |
---|
19 | * http://www.gams.com/testlib/libhtml/sos2a.htm */ |
---|
20 | void sos2a(int& error_count, int& warning_count); |
---|
21 | /** model semicon1 from the GAMS test library |
---|
22 | * http://www.gams.com/testlib/libhtml/semicon1.htm */ |
---|
23 | void semicon1(int& error_count, int& warning_count); |
---|
24 | /** model semiint1 from the GAMS test library |
---|
25 | * http://www.gams.com/testlib/libhtml/semiint1.htm */ |
---|
26 | void semiint1(int& error_count, int& warning_count); |
---|
27 | int main (int argc, const char *argv[]) |
---|
28 | { |
---|
29 | WindowsErrorPopupBlocker(); |
---|
30 | // only in CoinUtils/trunk: WindowsErrorPopupBlocker(); |
---|
31 | int error_count = 0; |
---|
32 | int warning_count = 0; |
---|
33 | |
---|
34 | sos1a(error_count, warning_count); |
---|
35 | cout << "\n***********************\n" << endl; |
---|
36 | sos2a(error_count, warning_count); |
---|
37 | cout << "\n***********************\n" << endl; |
---|
38 | semicon1(error_count, warning_count); |
---|
39 | cout << "\n***********************\n" << endl; |
---|
40 | semiint1(error_count, warning_count); |
---|
41 | |
---|
42 | cout << endl << "Finished - there have been " << error_count << " errors and " << warning_count << " warnings." << endl; |
---|
43 | return error_count; |
---|
44 | } |
---|
45 | void sos1a(int& error_count, int& warning_count) { |
---|
46 | OsiClpSolverInterface solver1; |
---|
47 | |
---|
48 | int numcols = 3; |
---|
49 | int numrows = 1; |
---|
50 | int nnz = 3; |
---|
51 | CoinBigIndex *start = new int[numcols+1]; |
---|
52 | int* index = new int[nnz]; |
---|
53 | double* value = new double[nnz]; |
---|
54 | double *collb = new double[numcols]; |
---|
55 | double *colub = new double[numcols]; |
---|
56 | double *obj = new double[numcols]; |
---|
57 | double *rowlb = new double[numrows]; |
---|
58 | double *rowub = new double[numrows]; |
---|
59 | // objective |
---|
60 | obj[0] = .9; obj[1] = 1.; obj[2] = 1.1; |
---|
61 | |
---|
62 | // column bounds |
---|
63 | collb[0] = 0.; colub[0] = .8; |
---|
64 | collb[1] = 0.; colub[1] = .6; |
---|
65 | collb[2] = 0.; colub[2] = .6; |
---|
66 | // matrix |
---|
67 | start[0] = 0; index[0] = 0; value[0] = 1.; |
---|
68 | start[1] = 1; index[1] = 0; value[1] = 1.; |
---|
69 | start[2] = 2; index[2] = 0; value[2] = 1.; |
---|
70 | start[3] = 3; |
---|
71 | |
---|
72 | // row bounds |
---|
73 | rowlb[0] = -solver1.getInfinity(); rowub[0] = 1.; |
---|
74 | solver1.loadProblem(numcols, numrows, start, index, value, collb, colub, obj, rowlb, rowub); |
---|
75 | solver1.setObjSense(-1); |
---|
76 | |
---|
77 | CbcModel model(solver1); |
---|
78 | CbcMain0(model); |
---|
79 | |
---|
80 | int which[3] = { 0, 1, 2 }; |
---|
81 | CbcObject* sosobject = new CbcSOS(&model, 3, which, NULL, 0, 1); |
---|
82 | model.addObjects(1, &sosobject); |
---|
83 | delete sosobject; |
---|
84 | |
---|
85 | const char * argv2[]={"gamstest_sos1a","-solve","-quit"}; |
---|
86 | CbcMain1(3,argv2,model); |
---|
87 | cout << endl; |
---|
88 | if (!model.isProvenOptimal()) { |
---|
89 | cerr << "Error: Model sos1a not solved to optimality." << endl; |
---|
90 | ++error_count; |
---|
91 | return; // other tests make no sense ---- memory leak here |
---|
92 | } |
---|
93 | |
---|
94 | OsiSolverInterface* solver = model.solver(); |
---|
95 | assert(solver); |
---|
96 | |
---|
97 | cout << "Objective value model: " << model.getObjValue() |
---|
98 | << "\t solver: " << solver->getObjValue() |
---|
99 | << "\t expected: 0.72" << endl; |
---|
100 | if (CoinAbs(model.getObjValue()-0.72)>testtol || CoinAbs(solver->getObjValue()-0.72)>testtol) { |
---|
101 | cerr << "Error: Objective value incorrect." << endl; |
---|
102 | ++error_count; |
---|
103 | } |
---|
104 | |
---|
105 | cout << "Primal value variable 0 in model: " << model.bestSolution()[0] |
---|
106 | << "\t in solver: " << solver->getColSolution()[0] |
---|
107 | << "\t expected: 0.8" << endl; |
---|
108 | if (CoinAbs(model.bestSolution()[0]-0.8)>testtol || CoinAbs(solver->getColSolution()[0]-0.8)>testtol) { |
---|
109 | cerr << "Error: Primal value incorrect." << endl; |
---|
110 | ++error_count; |
---|
111 | } |
---|
112 | cout << "Primal value variable 1 in model: " << model.bestSolution()[1] |
---|
113 | << "\t in solver: " << solver->getColSolution()[1] |
---|
114 | << "\t expected: 0.0" << endl; |
---|
115 | if (CoinAbs(model.bestSolution()[1])>testtol || CoinAbs(solver->getColSolution()[1])>testtol) { |
---|
116 | cerr << "Error: Primal value incorrect." << endl; |
---|
117 | ++error_count; |
---|
118 | } |
---|
119 | cout << "Primal value variable 2 in model: " << model.bestSolution()[2] |
---|
120 | << "\t in solver: " << solver->getColSolution()[2] |
---|
121 | << "\t expected: 0.0" << endl; |
---|
122 | if (CoinAbs(model.bestSolution()[2])>testtol || CoinAbs(solver->getColSolution()[2])>testtol) { |
---|
123 | cerr << "Error: Primal value incorrect." << endl; |
---|
124 | ++error_count; |
---|
125 | } |
---|
126 | delete[] start; |
---|
127 | delete[] index; |
---|
128 | delete[] value; |
---|
129 | delete[] collb; |
---|
130 | delete[] colub; |
---|
131 | delete[] obj; |
---|
132 | delete[] rowlb; |
---|
133 | delete[] rowub; |
---|
134 | } |
---|
135 | void sos2a(int& error_count, int& warning_count) { |
---|
136 | OsiClpSolverInterface solver1; |
---|
137 | |
---|
138 | int numcols = 7; // w1, w2, w3, x, fx, fplus, fminus |
---|
139 | int numrows = 5; // wsum, xdef, fxdef, gapplus, gapminus |
---|
140 | int nnz = 15; |
---|
141 | CoinBigIndex *start = new int[numcols+1]; |
---|
142 | int* index = new int[nnz]; |
---|
143 | double* value = new double[nnz]; |
---|
144 | double *collb = new double[numcols]; |
---|
145 | double *colub = new double[numcols]; |
---|
146 | double *obj = new double[numcols]; |
---|
147 | double *rowlb = new double[numrows]; |
---|
148 | double *rowub = new double[numrows]; |
---|
149 | // objective |
---|
150 | obj[0] = 0.; obj[1] = 0.; obj[2] = 0.; obj[3] = 0.; |
---|
151 | obj[4] = 0.; obj[5] = 1.; obj[6] = 1.; |
---|
152 | |
---|
153 | // column bounds |
---|
154 | collb[0] = 0.; colub[0] = solver1.getInfinity(); |
---|
155 | collb[1] = 0.; colub[1] = solver1.getInfinity(); |
---|
156 | collb[2] = 0.; colub[2] = solver1.getInfinity(); |
---|
157 | collb[3] = -solver1.getInfinity(); colub[3] = solver1.getInfinity(); |
---|
158 | collb[4] = -solver1.getInfinity(); colub[4] = solver1.getInfinity(); |
---|
159 | collb[5] = 0.; colub[5] = solver1.getInfinity(); |
---|
160 | collb[6] = 0.; colub[6] = solver1.getInfinity(); |
---|
161 | // matrix |
---|
162 | start[0] = 0; index[0] = 0; value[0] = 1.; |
---|
163 | index[1] = 1; value[1] = 1.; |
---|
164 | index[2] = 2; value[2] = 1.; |
---|
165 | start[1] = 3; index[3] = 0; value[3] = 1.; |
---|
166 | index[4] = 1; value[4] = 2.; |
---|
167 | index[5] = 2; value[5] = 2.; |
---|
168 | start[2] = 6; index[6] = 0; value[6] = 1.; |
---|
169 | index[7] = 1; value[7] = 3.; |
---|
170 | index[8] = 2; value[8] = 3.; |
---|
171 | start[3] = 9; index[9] = 1; value[9] =-1.; |
---|
172 | start[4] =10; index[10]= 2; value[10]=-1.; |
---|
173 | index[11]= 3; value[11]=-1.; |
---|
174 | index[12]= 4; value[12]= 1.; |
---|
175 | start[5] =13; index[13]= 3; value[13]= 1.; |
---|
176 | start[6] =14; index[14]= 4; value[14]= 1.; |
---|
177 | start[7] =15; |
---|
178 | |
---|
179 | // row bounds |
---|
180 | rowlb[0] = 1. ; rowub[0] = 1.; |
---|
181 | rowlb[1] = 0. ; rowub[1] = 0.; |
---|
182 | rowlb[2] = 0. ; rowub[2] = 0.; |
---|
183 | rowlb[3] = -1.3; rowub[3] = solver1.getInfinity(); |
---|
184 | rowlb[4] = 1.3; rowub[4] = solver1.getInfinity(); |
---|
185 | solver1.loadProblem(numcols, numrows, start, index, value, collb, colub, obj, rowlb, rowub); |
---|
186 | double* primalval = new double[numcols]; |
---|
187 | double* redcost = new double[numcols]; |
---|
188 | double optvalue = solver1.getInfinity(); |
---|
189 | for (int testcase = 0; testcase < 2; ++testcase) { |
---|
190 | switch(testcase) { |
---|
191 | case 0: |
---|
192 | solver1.setColLower(0, 0.); |
---|
193 | optvalue = 0.; |
---|
194 | primalval[0] = .7; redcost[0] = 0.; |
---|
195 | primalval[1] = .3; redcost[1] = 0.; |
---|
196 | primalval[2] =0. ; redcost[2] = 0.; |
---|
197 | primalval[3] =1.3; redcost[3] = 0.; |
---|
198 | primalval[4] =1.3; redcost[4] = 0.; |
---|
199 | primalval[5] =0. ; redcost[5] = 1.; |
---|
200 | primalval[6] =0. ; redcost[6] = 1.; |
---|
201 | break; |
---|
202 | case 1: |
---|
203 | solver1.setColLower(0, .8); |
---|
204 | optvalue = 0.1; |
---|
205 | primalval[0] = .8; redcost[0] = 1.; |
---|
206 | primalval[1] = .2; redcost[1] = 0.; |
---|
207 | primalval[2] =0. ; redcost[2] =-1.; |
---|
208 | primalval[3] =1.2; redcost[3] = 0.; |
---|
209 | primalval[4] =1.2; redcost[4] = 0.; |
---|
210 | primalval[5] =0. ; redcost[5] = 1.; |
---|
211 | primalval[6] =0.1; redcost[6] = 0.; |
---|
212 | break; |
---|
213 | } |
---|
214 | CbcModel model(solver1); |
---|
215 | CbcMain0(model); |
---|
216 | int which[3] = { 0, 1, 2 }; |
---|
217 | CbcObject* sosobject = new CbcSOS(&model, 3, which, NULL, 0, 2); |
---|
218 | model.addObjects(1, &sosobject); |
---|
219 | delete sosobject; |
---|
220 | const char * argv2[]={"gamstest_sos2a","-solve","-quit"}; |
---|
221 | cout << "\nSolving sos2a model with w1 having lower bound " << solver1.getColLower()[0] << endl; |
---|
222 | CbcMain1(3,argv2,model); |
---|
223 | cout << endl; |
---|
224 | if (!model.isProvenOptimal()) { |
---|
225 | cerr << "Error: Model sos2a not solved to optimality." << endl; |
---|
226 | ++error_count; |
---|
227 | continue; // other tests make no sense |
---|
228 | } |
---|
229 | OsiSolverInterface* solver = model.solver(); |
---|
230 | assert(solver); |
---|
231 | cout << "Objective value model: " << model.getObjValue() |
---|
232 | << "\t solver: " << solver->getObjValue() |
---|
233 | << "\t expected: " << optvalue << endl; |
---|
234 | if (CoinAbs(model.getObjValue()-optvalue)>testtol || CoinAbs(solver->getObjValue()-optvalue)>testtol) { |
---|
235 | cerr << "Error: Objective value incorrect." << endl; |
---|
236 | ++error_count; |
---|
237 | } |
---|
238 | for (int i=0; i<numcols; ++i) { |
---|
239 | cout << "Primal value variable " << i << " in model: " << model.bestSolution()[i] |
---|
240 | << "\t in solver: " << solver->getColSolution()[i] |
---|
241 | << "\t expected: " << primalval[i] |
---|
242 | << endl; |
---|
243 | if (CoinAbs(model.bestSolution()[i]-primalval[i])>testtol || CoinAbs(solver->getColSolution()[i]-primalval[i])>testtol) { |
---|
244 | cerr << "Error: Primal value incorrect." << endl; |
---|
245 | ++error_count; |
---|
246 | } |
---|
247 | } |
---|
248 | for (int i=0; i<numcols; ++i) { |
---|
249 | cout << "Reduced cost variable " << i << " in model: " << model.getReducedCost()[i] |
---|
250 | << "\t in solver: " << solver->getReducedCost()[i] |
---|
251 | << "\t expected: " << redcost[i] |
---|
252 | << endl; |
---|
253 | if (CoinAbs(model.getReducedCost()[i]-redcost[i])>testtol || CoinAbs(solver->getReducedCost()[i]-redcost[i])>testtol) { |
---|
254 | cerr << "Warning: Reduced cost incorrect." << endl; |
---|
255 | ++warning_count; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | delete[] start; |
---|
260 | delete[] index; |
---|
261 | delete[] value; |
---|
262 | delete[] collb; |
---|
263 | delete[] colub; |
---|
264 | delete[] obj; |
---|
265 | delete[] rowlb; |
---|
266 | delete[] rowub; |
---|
267 | delete[] primalval; |
---|
268 | delete[] redcost; |
---|
269 | } |
---|
270 | void semicon1(int& error_count, int& warning_count) { |
---|
271 | OsiClpSolverInterface solver1; |
---|
272 | |
---|
273 | int numcols = 4; // s, pup, plo, x |
---|
274 | int numrows = 3; // bigx, smallx, f |
---|
275 | int nnz = 6; |
---|
276 | CoinBigIndex *start = new int[numcols+1]; |
---|
277 | int* index = new int[nnz]; |
---|
278 | double* value = new double[nnz]; |
---|
279 | double *collb = new double[numcols]; |
---|
280 | double *colub = new double[numcols]; |
---|
281 | double *obj = new double[numcols]; |
---|
282 | double *rowlb = new double[numrows]; |
---|
283 | double *rowub = new double[numrows]; |
---|
284 | // objective |
---|
285 | obj[0] = 0; obj[1] = 1.; obj[2] = 1; obj[3] = 0; |
---|
286 | |
---|
287 | // column bounds |
---|
288 | collb[0] = 0.; colub[0] = 10.; |
---|
289 | collb[1] = 0.; colub[1] = solver1.getInfinity(); |
---|
290 | collb[2] = 0.; colub[2] = solver1.getInfinity(); |
---|
291 | collb[3] = 0.; colub[3] = solver1.getInfinity(); |
---|
292 | // matrix |
---|
293 | start[0] = 0; index[0] = 2; value[0] = 1.; |
---|
294 | start[1] = 1; index[1] = 0; value[1] = -1.; |
---|
295 | start[2] = 2; index[2] = 1; value[2] = 1.; |
---|
296 | start[3] = 3; index[3] = 0; value[3] = 1.; |
---|
297 | index[4] = 1; value[4] = 1.; |
---|
298 | index[5] = 2; value[5] = 1.; |
---|
299 | start[4] = nnz; |
---|
300 | |
---|
301 | // row bounds |
---|
302 | rowlb[0] = -solver1.getInfinity(); rowub[0] = 8.9; |
---|
303 | rowlb[1] = 8.9; rowub[1] = solver1.getInfinity(); |
---|
304 | rowlb[2] = 10.; rowub[2] = 10.; |
---|
305 | solver1.loadProblem(numcols, numrows, start, index, value, collb, colub, obj, rowlb, rowub); |
---|
306 | for (int testcase = 0; testcase < 5; ++testcase) { |
---|
307 | CbcModel model(solver1); |
---|
308 | CbcMain0(model); |
---|
309 | |
---|
310 | double points[4] = { 0., 0., 0., 10. }; |
---|
311 | double objval; |
---|
312 | double primalval[4]; |
---|
313 | double redcost[4]; |
---|
314 | double row2marg; |
---|
315 | redcost[1] = 1.0; |
---|
316 | redcost[2] = 1.0; |
---|
317 | redcost[3] = 0.0; |
---|
318 | switch(testcase) { |
---|
319 | case 0: |
---|
320 | points[2] = 0.; |
---|
321 | objval = 0.; |
---|
322 | primalval[0] = 1.1; |
---|
323 | primalval[1] = 0.0; |
---|
324 | primalval[2] = 0.0; |
---|
325 | primalval[3] = 8.9; |
---|
326 | redcost[0] = 0.0; |
---|
327 | row2marg = 0.0; |
---|
328 | break; |
---|
329 | case 1: |
---|
330 | points[2] = 1.; |
---|
331 | objval = 0.; |
---|
332 | primalval[0] = 1.1; |
---|
333 | primalval[1] = 0.0; |
---|
334 | primalval[2] = 0.0; |
---|
335 | primalval[3] = 8.9; |
---|
336 | redcost[0] = 0.0; |
---|
337 | row2marg = 0.0; |
---|
338 | break; |
---|
339 | case 2: |
---|
340 | points[2] = 1.5; |
---|
341 | objval = 0.4; |
---|
342 | primalval[0] = 1.5; |
---|
343 | primalval[1] = 0.0; |
---|
344 | primalval[2] = 0.4; |
---|
345 | primalval[3] = 8.5; |
---|
346 | redcost[0] = 1.0; |
---|
347 | row2marg = -1.0; |
---|
348 | break; |
---|
349 | case 3: |
---|
350 | points[2] = 2.1; |
---|
351 | objval = 1.0; |
---|
352 | primalval[0] = 2.1; |
---|
353 | primalval[1] = 0.0; |
---|
354 | primalval[2] = 1.0; |
---|
355 | primalval[3] = 7.9; |
---|
356 | redcost[0] = 1.0; |
---|
357 | row2marg = -1.0; |
---|
358 | break; |
---|
359 | case 4: |
---|
360 | points[2] = 2.8; |
---|
361 | objval = 1.1; |
---|
362 | primalval[0] = 0.0; |
---|
363 | primalval[1] = 1.1; |
---|
364 | primalval[2] = 0.0; |
---|
365 | primalval[3] = 10.0; |
---|
366 | redcost[0] = -1.0; |
---|
367 | row2marg = 1.0; |
---|
368 | break; |
---|
369 | default: // to please the compile |
---|
370 | redcost[0] = 0.; |
---|
371 | row2marg = 0.; |
---|
372 | objval = 0.; |
---|
373 | } |
---|
374 | |
---|
375 | CbcObject* semiconobject = new CbcLotsize(&model, 0, 2, points, true); |
---|
376 | model.addObjects(1, &semiconobject); |
---|
377 | delete semiconobject; |
---|
378 | |
---|
379 | cout << "\nSolving semicon1 model for lotsize variable being either 0 or between " << points[2] << " and 10.\n" << endl; |
---|
380 | const char * argv2[]={"gamstest_semicon1","-solve","-quit"}; |
---|
381 | CbcMain1(3,argv2,model); |
---|
382 | cout << endl; |
---|
383 | if (!model.isProvenOptimal()) { |
---|
384 | cerr << "Error: Model semicon1 not solved to optimality." << endl; |
---|
385 | ++error_count; |
---|
386 | continue; // other tests make no sense |
---|
387 | } |
---|
388 | OsiSolverInterface* solver = model.solver(); |
---|
389 | assert(solver); |
---|
390 | cout << "Objective value in model: " << model.getObjValue() |
---|
391 | << "\t in solver: " << solver->getObjValue() |
---|
392 | << "\t expected: " << objval << endl; |
---|
393 | if (CoinAbs(model.getObjValue()-objval)>testtol || CoinAbs(solver->getObjValue()-objval)>testtol) { |
---|
394 | cerr << "Error: Objective value incorrect." << endl; |
---|
395 | ++error_count; |
---|
396 | } |
---|
397 | for (int i=0; i<numcols; ++i) { |
---|
398 | cout << "Primal value variable " << i << " in model: " << model.bestSolution()[i] |
---|
399 | << "\t in solver: " << solver->getColSolution()[i] |
---|
400 | << "\t expected: " << primalval[i] |
---|
401 | << endl; |
---|
402 | if (CoinAbs(model.bestSolution()[i]-primalval[i])>testtol || CoinAbs(solver->getColSolution()[i]-primalval[i])>testtol) { |
---|
403 | cerr << "Error: Primal value incorrect." << endl; |
---|
404 | ++error_count; |
---|
405 | } |
---|
406 | } |
---|
407 | cout << "Reduced cost variable " << 0 << " in model: " << model.getReducedCost()[0] |
---|
408 | << "\t in solver: " << solver->getReducedCost()[0] |
---|
409 | << "\t expected: " << redcost[0] |
---|
410 | << endl; |
---|
411 | if (CoinAbs(model.getReducedCost()[0]-redcost[0])>testtol || CoinAbs(solver->getReducedCost()[0]-redcost[0])>testtol) { |
---|
412 | cerr << "Warning: Reduced cost incorrect." << endl; |
---|
413 | ++warning_count; |
---|
414 | } |
---|
415 | cout << "Reduced cost variable " << 3 << " in model: " << model.getReducedCost()[3] |
---|
416 | << "\t in solver: " << solver->getReducedCost()[3] |
---|
417 | << "\t expected: " << redcost[3] |
---|
418 | << endl; |
---|
419 | if (CoinAbs(model.getReducedCost()[3]-redcost[3])>testtol || CoinAbs(solver->getReducedCost()[3]-redcost[3])>testtol) { |
---|
420 | cerr << "Warning: Reduced cost incorrect." << endl; |
---|
421 | ++warning_count; |
---|
422 | } |
---|
423 | cout << "Reduced cost variable 1 plus - dual of row 0 in model: " << model.getReducedCost()[1]-model.getRowPrice()[0] |
---|
424 | << "\t expected: " << redcost[1] |
---|
425 | << endl; |
---|
426 | if (CoinAbs(model.getReducedCost()[1]-model.getRowPrice()[0]-redcost[1])>testtol) { |
---|
427 | cerr << "Warning: Reduced cost or row margin incorrect." << endl; |
---|
428 | ++warning_count; |
---|
429 | } |
---|
430 | cout << "Reduced cost variable 2 plus + dual of row 1 in model: " << model.getReducedCost()[2]+model.getRowPrice()[1] |
---|
431 | << "\t expected: " << redcost[2] |
---|
432 | << endl; |
---|
433 | if (CoinAbs(model.getReducedCost()[2]+model.getRowPrice()[1]-redcost[2])>testtol) { |
---|
434 | cerr << "Warning: Reduced cost or row margin incorrect." << endl; |
---|
435 | ++warning_count; |
---|
436 | } |
---|
437 | |
---|
438 | cout << "Row 2 marginal (price) in model: " << model.getRowPrice()[2] |
---|
439 | << "\t in solver: " << solver->getRowPrice()[2] |
---|
440 | << "\t expected: " << row2marg << endl; |
---|
441 | if (CoinAbs(model.getRowPrice()[2]-row2marg)>testtol || CoinAbs(solver->getRowPrice()[2]-row2marg)>testtol) { |
---|
442 | cerr << "Warning: Row price incorrect." << endl; |
---|
443 | ++warning_count; |
---|
444 | } |
---|
445 | |
---|
446 | } |
---|
447 | |
---|
448 | delete[] start; |
---|
449 | delete[] index; |
---|
450 | delete[] value; |
---|
451 | delete[] collb; |
---|
452 | delete[] colub; |
---|
453 | delete[] obj; |
---|
454 | delete[] rowlb; |
---|
455 | delete[] rowub; |
---|
456 | } |
---|
457 | void semiint1(int& error_count, int& warning_count) { |
---|
458 | OsiClpSolverInterface solver1; |
---|
459 | |
---|
460 | int numcols = 4; // s, pup, plo, x |
---|
461 | int numrows = 3; // bigx, smallx, f |
---|
462 | int nnz = 6; |
---|
463 | CoinBigIndex *start = new int[numcols+1]; |
---|
464 | int* index = new int[nnz]; |
---|
465 | double* value = new double[nnz]; |
---|
466 | double *collb = new double[numcols]; |
---|
467 | double *colub = new double[numcols]; |
---|
468 | double *obj = new double[numcols]; |
---|
469 | double *rowlb = new double[numrows]; |
---|
470 | double *rowub = new double[numrows]; |
---|
471 | // objective |
---|
472 | obj[0] = 0; obj[1] = 1.; obj[2] = 1; obj[3] = 0; |
---|
473 | |
---|
474 | // column bounds |
---|
475 | collb[0] = 0.; colub[0] = 10.; |
---|
476 | collb[1] = 0.; colub[1] = solver1.getInfinity(); |
---|
477 | collb[2] = 0.; colub[2] = solver1.getInfinity(); |
---|
478 | collb[3] = 0.; colub[3] = solver1.getInfinity(); |
---|
479 | // matrix |
---|
480 | start[0] = 0; index[0] = 2; value[0] = 1.; |
---|
481 | start[1] = 1; index[1] = 0; value[1] = -1.; |
---|
482 | start[2] = 2; index[2] = 1; value[2] = 1.; |
---|
483 | start[3] = 3; index[3] = 0; value[3] = 1.; |
---|
484 | index[4] = 1; value[4] = 1.; |
---|
485 | index[5] = 2; value[5] = 1.; |
---|
486 | start[4] = nnz; |
---|
487 | |
---|
488 | // row bounds |
---|
489 | rowlb[0] = -solver1.getInfinity(); rowub[0] = 7.9; |
---|
490 | rowlb[1] = 7.9; rowub[1] = solver1.getInfinity(); |
---|
491 | rowlb[2] = 10.; rowub[2] = 10.; |
---|
492 | solver1.loadProblem(numcols, numrows, start, index, value, collb, colub, obj, rowlb, rowub); |
---|
493 | solver1.setInteger(0); |
---|
494 | |
---|
495 | for (int testcase = 0; testcase < 6; ++testcase) { |
---|
496 | CbcModel model(solver1); |
---|
497 | CbcMain0(model); |
---|
498 | |
---|
499 | double points[10]; |
---|
500 | points[0]=0.; |
---|
501 | int nrpoints = 0; |
---|
502 | double objval; |
---|
503 | double primalval[4]; |
---|
504 | double redcost[4]; |
---|
505 | double row2marg; |
---|
506 | redcost[2] = 1.0; |
---|
507 | redcost[3] = 0.0; |
---|
508 | switch(testcase) { |
---|
509 | case 0: |
---|
510 | nrpoints = 0; // pure integer case |
---|
511 | objval = 0.1; |
---|
512 | primalval[0] = 2.0; |
---|
513 | primalval[1] = 0.1; |
---|
514 | primalval[2] = 0.0; |
---|
515 | primalval[3] = 8; |
---|
516 | redcost[0] = -1.0; |
---|
517 | redcost[1] = 0.0; |
---|
518 | row2marg = 1.0; |
---|
519 | break; |
---|
520 | case 1: |
---|
521 | nrpoints = 0; // pure integer case too |
---|
522 | objval = 0.1; |
---|
523 | primalval[0] = 2.0; |
---|
524 | primalval[1] = 0.1; |
---|
525 | primalval[2] = 0.0; |
---|
526 | primalval[3] = 8.0; |
---|
527 | redcost[0] = -1.0; |
---|
528 | redcost[1] = 0.0; |
---|
529 | row2marg = 1.0; |
---|
530 | break; |
---|
531 | case 2: |
---|
532 | for (nrpoints=1; nrpoints<10; ++nrpoints) |
---|
533 | points[nrpoints]=nrpoints+1; |
---|
534 | objval = 0.1; |
---|
535 | primalval[0] = 2.0; |
---|
536 | primalval[1] = 0.1; |
---|
537 | primalval[2] = 0.0; |
---|
538 | primalval[3] = 8.0; |
---|
539 | redcost[0] = -1.0; |
---|
540 | redcost[1] = 0.0; |
---|
541 | row2marg = 1.0; |
---|
542 | break; |
---|
543 | case 3: |
---|
544 | for (nrpoints=1; nrpoints<9; ++nrpoints) |
---|
545 | points[nrpoints]=nrpoints+2; |
---|
546 | objval = 0.9; |
---|
547 | primalval[0] = 3.0; |
---|
548 | primalval[1] = 0.0; |
---|
549 | primalval[2] = 0.9; |
---|
550 | primalval[3] = 7.0; |
---|
551 | redcost[0] = 1.0; |
---|
552 | redcost[1] = 1.0; |
---|
553 | row2marg = -1.0; |
---|
554 | break; |
---|
555 | case 4: |
---|
556 | for (nrpoints=1; nrpoints<8; ++nrpoints) |
---|
557 | points[nrpoints]=nrpoints+3; |
---|
558 | objval = 1.9; |
---|
559 | primalval[0] = 4.0; |
---|
560 | primalval[1] = 0.0; |
---|
561 | primalval[2] = 1.9; |
---|
562 | primalval[3] = 6.0; |
---|
563 | redcost[0] = 1.0; |
---|
564 | redcost[1] = 1.0; |
---|
565 | row2marg = -1.0; |
---|
566 | break; |
---|
567 | case 5: |
---|
568 | for (nrpoints=1; nrpoints<7; ++nrpoints) |
---|
569 | points[nrpoints]=nrpoints+4; |
---|
570 | objval = 2.1; |
---|
571 | primalval[0] = 0.0; |
---|
572 | primalval[1] = 2.1; |
---|
573 | primalval[2] = 0.0; |
---|
574 | primalval[3] = 10.0; |
---|
575 | redcost[0] = -1.0; |
---|
576 | redcost[1] = 0.0; |
---|
577 | row2marg = 1.0; |
---|
578 | break; |
---|
579 | default: // to please the compile |
---|
580 | redcost[0] = 0.; |
---|
581 | redcost[1] = 0.; |
---|
582 | row2marg = 0.; |
---|
583 | objval = 0.; |
---|
584 | } |
---|
585 | if (nrpoints) { |
---|
586 | CbcObject* semiintobject = new CbcLotsize(&model, 0, nrpoints, points); |
---|
587 | model.addObjects(1, &semiintobject); |
---|
588 | delete semiintobject; |
---|
589 | } |
---|
590 | |
---|
591 | cout << "\nSolving semiint1 model for integer lotsize variable being either 0 or between " << points[2] << " and 10.\n" << endl; |
---|
592 | const char * argv2[]={"gamstest_semiint1","-solve","-quit"}; |
---|
593 | CbcMain1(3,argv2,model); |
---|
594 | cout << endl; |
---|
595 | if (!model.isProvenOptimal()) { |
---|
596 | cerr << "Error: Model semiint1 not solved to optimality." << endl; |
---|
597 | ++error_count; |
---|
598 | continue; // other tests make no sense |
---|
599 | } |
---|
600 | OsiSolverInterface* solver = model.solver(); |
---|
601 | assert(solver); |
---|
602 | cout << "Objective value in model: " << model.getObjValue() |
---|
603 | << "\t in solver: " << solver->getObjValue() |
---|
604 | << "\t expected: " << objval << endl; |
---|
605 | if (CoinAbs(model.getObjValue()-objval)>testtol || CoinAbs(solver->getObjValue()-objval)>testtol) { |
---|
606 | cerr << "Error: Objective value incorrect." << endl; |
---|
607 | ++error_count; |
---|
608 | } |
---|
609 | for (int i=0; i<numcols; ++i) { |
---|
610 | cout << "Primal value variable " << i << " in model: " << model.bestSolution()[i] |
---|
611 | << "\t in solver: " << solver->getColSolution()[i] |
---|
612 | << "\t expected: " << primalval[i] |
---|
613 | << endl; |
---|
614 | if (CoinAbs(model.bestSolution()[i]-primalval[i])>testtol || CoinAbs(solver->getColSolution()[i]-primalval[i])>testtol) { |
---|
615 | cerr << "Error: Primal value incorrect." << endl; |
---|
616 | ++error_count; |
---|
617 | } |
---|
618 | } |
---|
619 | cout << "Reduced cost variable " << 0 << " in model: " << model.getReducedCost()[0] |
---|
620 | << "\t in solver: " << solver->getReducedCost()[0] |
---|
621 | << "\t expected: " << redcost[0] |
---|
622 | << endl; |
---|
623 | if (CoinAbs(model.getReducedCost()[0]-redcost[0])>testtol || CoinAbs(solver->getReducedCost()[0]-redcost[0])>testtol) { |
---|
624 | cerr << "Warning: Reduced cost incorrect." << endl; |
---|
625 | ++warning_count; |
---|
626 | } |
---|
627 | cout << "Reduced cost variable " << 3 << " in model: " << model.getReducedCost()[3] |
---|
628 | << "\t in solver: " << solver->getReducedCost()[3] |
---|
629 | << "\t expected: " << redcost[3] |
---|
630 | << endl; |
---|
631 | if (CoinAbs(model.getReducedCost()[3]-redcost[3])>testtol || CoinAbs(solver->getReducedCost()[3]-redcost[3])>testtol) { |
---|
632 | cerr << "Warning: Reduced cost incorrect." << endl; |
---|
633 | ++warning_count; |
---|
634 | } |
---|
635 | cout << "Row 2 marginal (price) in model: " << model.getRowPrice()[2] |
---|
636 | << "\t in solver: " << solver->getRowPrice()[2] |
---|
637 | << "\t expected: " << row2marg << endl; |
---|
638 | if (CoinAbs(model.getRowPrice()[2]-row2marg)>testtol || CoinAbs(solver->getRowPrice()[2]-row2marg)>testtol) { |
---|
639 | cerr << "Warning: Row price incorrect." << endl; |
---|
640 | ++warning_count; |
---|
641 | } |
---|
642 | |
---|
643 | cout << "Row 2 marginal (price) in model: " << model.getRowPrice()[2] |
---|
644 | << "\t in solver: " << solver->getRowPrice()[2] |
---|
645 | << "\t expected: " << row2marg << endl; |
---|
646 | if (CoinAbs(model.getRowPrice()[2]-row2marg)>testtol || CoinAbs(solver->getRowPrice()[2]-row2marg)>testtol) { |
---|
647 | cerr << "Warning: Row price incorrect." << endl; |
---|
648 | ++warning_count; |
---|
649 | } |
---|
650 | |
---|
651 | } |
---|
652 | |
---|
653 | delete[] start; |
---|
654 | delete[] index; |
---|
655 | delete[] value; |
---|
656 | delete[] collb; |
---|
657 | delete[] colub; |
---|
658 | delete[] obj; |
---|
659 | delete[] rowlb; |
---|
660 | delete[] rowub; |
---|
661 | } |
---|