Debian Codegraph @

Osi/test/OsiGlpkSolverInterfaceTest.cpp

raw
1 // Copyright (C) 2002, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 // Copyright (C) 2004 University of Pittsburgh
4 // University of Pittsburgh coding done by Brady Hunsaker
5 // This file is licensed under the terms of Eclipse Public License (EPL).
6
7 // OsiGlpkSolverInterfaceTest.cpp adapted from OsiClpSolverInterfaceTest.cpp
8 // on 2004/10/16
9 // Check for ??? to see tests that aren't working correctly.
10 // Also note that OsiPresolve doesn't appear to work with OsiGlpk. This
11 // needs to be examined.
12
13 #include "CoinPragma.hpp"
14 #include "OsiConfig.h"
15
16 //#include <cassert>
17 //#include <cstdlib>
18 //#include <cstdio>
19 //#include <iostream>
20
21 #include "OsiUnitTests.hpp"
22 #include "OsiGlpkSolverInterface.hpp"
23 #include "OsiRowCut.hpp"
24 #include "OsiCuts.hpp"
25 #include "CoinFloatEqual.hpp"
26
27 // Added so windows build with dsp files works,
28 // when not building with glpk.
29 #ifdef COIN_HAS_GLPK
30
31 void OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir)
32 {
33 // Test default constructor
34 {
35 OsiGlpkSolverInterface m;
36 OSIUNITTEST_ASSERT_ERROR(m.obj_ == NULL, {}, "glpk", "default constructor");
37 OSIUNITTEST_ASSERT_ERROR(m.collower_ == NULL, {}, "glpk", "default constructor");
38 OSIUNITTEST_ASSERT_ERROR(m.colupper_ == NULL, {}, "glpk", "default constructor");
39 OSIUNITTEST_ASSERT_ERROR(m.ctype_ == NULL, {}, "glpk", "default constructor");
40 OSIUNITTEST_ASSERT_ERROR(m.rowsense_ == NULL, {}, "glpk", "default constructor");
41 OSIUNITTEST_ASSERT_ERROR(m.rhs_ == NULL, {}, "glpk", "default constructor");
42 OSIUNITTEST_ASSERT_ERROR(m.rowrange_ == NULL, {}, "glpk", "default constructor");
43 OSIUNITTEST_ASSERT_ERROR(m.rowlower_ == NULL, {}, "glpk", "default constructor");
44 OSIUNITTEST_ASSERT_ERROR(m.rowupper_ == NULL, {}, "glpk", "default constructor");
45 OSIUNITTEST_ASSERT_ERROR(m.colsol_ == NULL, {}, "glpk", "default constructor");
46 OSIUNITTEST_ASSERT_ERROR(m.rowsol_ == NULL, {}, "glpk", "default constructor");
47 OSIUNITTEST_ASSERT_ERROR(m.matrixByRow_ == NULL, {}, "glpk", "default constructor");
48 OSIUNITTEST_ASSERT_ERROR(m.matrixByCol_ == NULL, {}, "glpk", "default constructor");
49 OSIUNITTEST_ASSERT_ERROR(m.getApplicationData() == NULL, {}, "glpk", "default constructor");
50 int i=2346;
51 m.setApplicationData(&i);
52 OSIUNITTEST_ASSERT_ERROR(*((int *)(m.getApplicationData())) == i, {}, "glpk", "default constructor");
53 }
54
55
56 {
57 CoinRelFltEq eq;
58 OsiGlpkSolverInterface m;
59 std::string fn = mpsDir+"exmip1";
60 m.readMps(fn.c_str(),"mps");
61
62 {
63 OsiGlpkSolverInterface im;
64
65 OSIUNITTEST_ASSERT_ERROR(im.getNumCols() == 0, {}, "glpk", "default constructor");
66 OSIUNITTEST_ASSERT_ERROR(im.getModelPtr() != NULL, {}, "glpk", "default constructor");
67
68 // Test reset
69 im.reset();
70 OSIUNITTEST_ASSERT_ERROR(m.obj_ == NULL, {}, "glpk", "reset");
71 OSIUNITTEST_ASSERT_ERROR(m.collower_ == NULL, {}, "glpk", "reset");
72 OSIUNITTEST_ASSERT_ERROR(m.colupper_ == NULL, {}, "glpk", "reset");
73 OSIUNITTEST_ASSERT_ERROR(m.ctype_ == NULL, {}, "glpk", "reset");
74 OSIUNITTEST_ASSERT_ERROR(m.rowsense_ == NULL, {}, "glpk", "reset");
75 OSIUNITTEST_ASSERT_ERROR(m.rhs_ == NULL, {}, "glpk", "reset");
76 OSIUNITTEST_ASSERT_ERROR(m.rowrange_ == NULL, {}, "glpk", "reset");
77 OSIUNITTEST_ASSERT_ERROR(m.rowlower_ == NULL, {}, "glpk", "reset");
78 OSIUNITTEST_ASSERT_ERROR(m.rowupper_ == NULL, {}, "glpk", "reset");
79 OSIUNITTEST_ASSERT_ERROR(m.colsol_ == NULL, {}, "glpk", "reset");
80 OSIUNITTEST_ASSERT_ERROR(m.rowsol_ == NULL, {}, "glpk", "reset");
81 OSIUNITTEST_ASSERT_ERROR(m.matrixByRow_ == NULL, {}, "glpk", "reset");
82 OSIUNITTEST_ASSERT_ERROR(m.matrixByCol_ == NULL, {}, "glpk", "reset");
83 OSIUNITTEST_ASSERT_ERROR(m.getApplicationData() == NULL, {}, "glpk", "reset");
84 }
85
86 // Test copy constructor and assignment operator
87 {
88 OsiGlpkSolverInterface lhs;
89 {
90 OsiGlpkSolverInterface im(m);
91
92 OsiGlpkSolverInterface imC1(im);
93 OSIUNITTEST_ASSERT_ERROR(imC1.getModelPtr() != im.getModelPtr(), {}, "glpk", "copy constructor");
94 OSIUNITTEST_ASSERT_ERROR(imC1.getNumCols() == im.getNumCols(), {}, "glpk", "copy constructor");
95 OSIUNITTEST_ASSERT_ERROR(imC1.getNumRows() == im.getNumRows(), {}, "glpk", "copy constructor");
96
97 OsiGlpkSolverInterface imC2(im);
98 OSIUNITTEST_ASSERT_ERROR(imC2.getModelPtr() != im.getModelPtr(), {}, "glpk", "copy constructor");
99 OSIUNITTEST_ASSERT_ERROR(imC2.getNumCols() == im.getNumCols(), {}, "glpk", "copy constructor");
100 OSIUNITTEST_ASSERT_ERROR(imC2.getNumRows() == im.getNumRows(), {}, "glpk", "copy constructor");
101
102 OSIUNITTEST_ASSERT_ERROR(imC1.getModelPtr() != imC2.getModelPtr(), {}, "glpk", "copy constructor");
103
104 lhs = imC2;
105 }
106
107 // Test that lhs has correct values even though rhs has gone out of scope
108 OSIUNITTEST_ASSERT_ERROR(lhs.getModelPtr() != m.getModelPtr(), {}, "glpk", "assignment operator");
109 OSIUNITTEST_ASSERT_ERROR(lhs.getNumCols() == m.getNumCols(), {}, "glpk", "copy constructor");
110 OSIUNITTEST_ASSERT_ERROR(lhs.getNumRows() == m.getNumRows(), {}, "glpk", "copy constructor");
111 }
112
113 // Test clone
114 {
115 OsiGlpkSolverInterface glpkSi(m);
116 OsiSolverInterface * siPtr = &glpkSi;
117 OsiSolverInterface * siClone = siPtr->clone();
118 OsiGlpkSolverInterface * glpkClone = dynamic_cast<OsiGlpkSolverInterface*>(siClone);
119 OSIUNITTEST_ASSERT_ERROR(glpkClone != NULL, {}, "glpk", "clone");
120 OSIUNITTEST_ASSERT_ERROR(glpkClone->getModelPtr() != glpkSi.getModelPtr(), {}, "glpk", "clone");
121 OSIUNITTEST_ASSERT_ERROR(glpkClone->getNumRows() == glpkSi.getNumRows(), {}, "glpk", "clone");
122 OSIUNITTEST_ASSERT_ERROR(glpkClone->getNumCols() == glpkSi.getNumCols(), {}, "glpk", "clone");
123
124 delete siClone;
125 }
126
127 // test infinity
128 {
129 OsiGlpkSolverInterface si;
130 OSIUNITTEST_ASSERT_ERROR(si.getInfinity() == COIN_DBL_MAX, {}, "glpk", "infinity");
131 }
132
133 #if 0
134 // ??? These index error 'throw's aren't in OsiGlpk
135
136 // Test some catches
137 {
138 OsiGlpkSolverInterface solver;
139 try {
140 solver.setObjCoeff(0,0.0);
141 }
142 catch (CoinError e) {
143 std::cout<<"Correct throw"<<std::endl;
144 }
145 std::string fn = mpsDir+"exmip1";
146 solver.readMps(fn.c_str(),"mps");
147 try {
148 solver.setObjCoeff(0,0.0);
149 }
150 catch (CoinError e) {
151 std::cout<<"** Incorrect throw"<<std::endl;
152 abort();
153 }
154 try {
155 int index[]={0,20};
156 double value[]={0.0,0.0,0.0,0.0};
157 solver.setColSetBounds(index,index+2,value);
158 }
159 catch (CoinError e) {
160 std::cout<<"Correct throw"<<std::endl;
161 }
162 }
163 #endif
164
165 {
166 OsiGlpkSolverInterface glpkSi(m);
167 int nc = glpkSi.getNumCols();
168 int nr = glpkSi.getNumRows();
169 const double * cl = glpkSi.getColLower();
170 const double * cu = glpkSi.getColUpper();
171 const double * rl = glpkSi.getRowLower();
172 const double * ru = glpkSi.getRowUpper();
173 OSIUNITTEST_ASSERT_ERROR(nc == 8, return, "glpk", "read and copy exmip1");
174 OSIUNITTEST_ASSERT_ERROR(nr == 5, return, "glpk", "read and copy exmip1");
175 OSIUNITTEST_ASSERT_ERROR(eq(cl[0],2.5), {}, "glpk", "read and copy exmip1");
176 OSIUNITTEST_ASSERT_ERROR(eq(cl[1],0.0), {}, "glpk", "read and copy exmip1");
177 OSIUNITTEST_ASSERT_ERROR(eq(cu[1],4.1), {}, "glpk", "read and copy exmip1");
178 OSIUNITTEST_ASSERT_ERROR(eq(cu[2],1.0), {}, "glpk", "read and copy exmip1");
179 OSIUNITTEST_ASSERT_ERROR(eq(rl[0],2.5), {}, "glpk", "read and copy exmip1");
180 OSIUNITTEST_ASSERT_ERROR(eq(rl[4],3.0), {}, "glpk", "read and copy exmip1");
181 OSIUNITTEST_ASSERT_ERROR(eq(ru[1],2.1), {}, "glpk", "read and copy exmip1");
182 OSIUNITTEST_ASSERT_ERROR(eq(ru[4],15.), {}, "glpk", "read and copy exmip1");
183
184 const double * cs = glpkSi.getColSolution();
185 OSIUNITTEST_ASSERT_ERROR(eq(cs[0],2.5), {}, "glpk", "read and copy exmip1");
186 OSIUNITTEST_ASSERT_ERROR(eq(cs[7],0.0), {}, "glpk", "read and copy exmip1");
187
188 OSIUNITTEST_ASSERT_ERROR(!eq(cl[3],1.2345), {}, "glpk", "set col lower");
189 glpkSi.setColLower( 3, 1.2345 );
190 OSIUNITTEST_ASSERT_ERROR( eq(cl[3],1.2345), {}, "glpk", "set col lower");
191
192 OSIUNITTEST_ASSERT_ERROR(!eq(glpkSi.getColUpper()[4],10.2345), {}, "glpk", "set col upper");
193 glpkSi.setColUpper( 4, 10.2345 );
194 OSIUNITTEST_ASSERT_ERROR( eq(glpkSi.getColUpper()[4],10.2345), {}, "glpk", "set col upper");
195
196 double objValue = glpkSi.getObjValue();
197 OSIUNITTEST_ASSERT_ERROR(eq(objValue,3.5), {}, "glpk", "getObjValue() before solve");
198
199 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[0], 1.0), {}, "glpk", "read and copy exmip1");
200 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[1], 0.0), {}, "glpk", "read and copy exmip1");
201 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[2], 0.0), {}, "glpk", "read and copy exmip1");
202 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[3], 0.0), {}, "glpk", "read and copy exmip1");
203 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[4], 2.0), {}, "glpk", "read and copy exmip1");
204 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[5], 0.0), {}, "glpk", "read and copy exmip1");
205 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[6], 0.0), {}, "glpk", "read and copy exmip1");
206 OSIUNITTEST_ASSERT_ERROR(eq(glpkSi.getObjCoefficients()[7],-1.0), {}, "glpk", "read and copy exmip1");
207 }
208
209 // Test matrixByRow method
210 {
211 const OsiGlpkSolverInterface si(m);
212 const CoinPackedMatrix * smP = si.getMatrixByRow();
213
214 OSIUNITTEST_ASSERT_ERROR(smP->getMajorDim() == 5, return, "glpk", "getMatrixByRow: major dim");
215 OSIUNITTEST_ASSERT_ERROR(smP->getNumElements() == 14, return, "glpk", "getMatrixByRow: num elements");
216
217 CoinRelFltEq eq;
218 const double * ev = smP->getElements();
219
220 // GLPK returns each row in reverse order. This is consistent with
221 // the sparse matrix format but is not what most solvers do. That's
222 // why this section is different.
223 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 4], 3.0), {}, "glpk", "getMatrixByRow: elements");
224 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 3], 1.0), {}, "glpk", "getMatrixByRow: elements");
225 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 2],-2.0), {}, "glpk", "getMatrixByRow: elements");
226 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 1],-1.0), {}, "glpk", "getMatrixByRow: elements");
227 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 0],-1.0), {}, "glpk", "getMatrixByRow: elements");
228 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 6], 2.0), {}, "glpk", "getMatrixByRow: elements");
229 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 5], 1.1), {}, "glpk", "getMatrixByRow: elements");
230 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 8], 1.0), {}, "glpk", "getMatrixByRow: elements");
231 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 7], 1.0), {}, "glpk", "getMatrixByRow: elements");
232 OSIUNITTEST_ASSERT_ERROR(eq(ev[10], 2.8), {}, "glpk", "getMatrixByRow: elements");
233 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 9],-1.2), {}, "glpk", "getMatrixByRow: elements");
234 OSIUNITTEST_ASSERT_ERROR(eq(ev[13], 5.6), {}, "glpk", "getMatrixByRow: elements");
235 OSIUNITTEST_ASSERT_ERROR(eq(ev[12], 1.0), {}, "glpk", "getMatrixByRow: elements");
236 OSIUNITTEST_ASSERT_ERROR(eq(ev[11], 1.9), {}, "glpk", "getMatrixByRow: elements");
237
238 const CoinBigIndex * mi = smP->getVectorStarts();
239 OSIUNITTEST_ASSERT_ERROR(mi[0] == 0, {}, "glpk", "getMatrixByRow: vector starts");
240 OSIUNITTEST_ASSERT_ERROR(mi[1] == 5, {}, "glpk", "getMatrixByRow: vector starts");
241 OSIUNITTEST_ASSERT_ERROR(mi[2] == 7, {}, "glpk", "getMatrixByRow: vector starts");
242 OSIUNITTEST_ASSERT_ERROR(mi[3] == 9, {}, "glpk", "getMatrixByRow: vector starts");
243 OSIUNITTEST_ASSERT_ERROR(mi[4] == 11, {}, "glpk", "getMatrixByRow: vector starts");
244 OSIUNITTEST_ASSERT_ERROR(mi[5] == 14, {}, "glpk", "getMatrixByRow: vector starts");
245
246 const int * ei = smP->getIndices();
247 OSIUNITTEST_ASSERT_ERROR(ei[ 4] == 0, {}, "glpk", "getMatrixByRow: indices");
248 OSIUNITTEST_ASSERT_ERROR(ei[ 3] == 1, {}, "glpk", "getMatrixByRow: indices");
249 OSIUNITTEST_ASSERT_ERROR(ei[ 2] == 3, {}, "glpk", "getMatrixByRow: indices");
250 OSIUNITTEST_ASSERT_ERROR(ei[ 1] == 4, {}, "glpk", "getMatrixByRow: indices");
251 OSIUNITTEST_ASSERT_ERROR(ei[ 0] == 7, {}, "glpk", "getMatrixByRow: indices");
252 OSIUNITTEST_ASSERT_ERROR(ei[ 6] == 1, {}, "glpk", "getMatrixByRow: indices");
253 OSIUNITTEST_ASSERT_ERROR(ei[ 5] == 2, {}, "glpk", "getMatrixByRow: indices");
254 OSIUNITTEST_ASSERT_ERROR(ei[ 8] == 2, {}, "glpk", "getMatrixByRow: indices");
255 OSIUNITTEST_ASSERT_ERROR(ei[ 7] == 5, {}, "glpk", "getMatrixByRow: indices");
256 OSIUNITTEST_ASSERT_ERROR(ei[10] == 3, {}, "glpk", "getMatrixByRow: indices");
257 OSIUNITTEST_ASSERT_ERROR(ei[ 9] == 6, {}, "glpk", "getMatrixByRow: indices");
258 OSIUNITTEST_ASSERT_ERROR(ei[13] == 0, {}, "glpk", "getMatrixByRow: indices");
259 OSIUNITTEST_ASSERT_ERROR(ei[12] == 4, {}, "glpk", "getMatrixByRow: indices");
260 OSIUNITTEST_ASSERT_ERROR(ei[11] == 7, {}, "glpk", "getMatrixByRow: indices");
261 }
262
263 // Test adding several cuts
264 {
265 OsiGlpkSolverInterface fim;
266 std::string fn = mpsDir+"exmip1";
267 fim.readMps(fn.c_str(),"mps");
268 // exmip1.mps has 2 integer variables with index 2 & 3
269 fim.initialSolve();
270 OsiRowCut cuts[3];
271
272 // Generate one ineffective cut plus two trivial cuts
273 int c;
274 int nc = fim.getNumCols();
275 int *inx = new int[nc];
276 for (c=0;c<nc;c++) inx[c]=c;
277 double *el = new double[nc];
278 for (c=0;c<nc;c++) el[c]=1.0e-50+((double)c)*((double)c);
279
280 cuts[0].setRow(nc,inx,el);
281 cuts[0].setLb(-100.);
282 cuts[0].setUb(500.);
283 cuts[0].setEffectiveness(22);
284 el[4]=0.0; // to get inf later
285
286 for (c=2;c<4;c++) {
287 el[0]=1.0;
288 inx[0]=c;
289 cuts[c-1].setRow(1,inx,el);
290 cuts[c-1].setLb(1.);
291 cuts[c-1].setUb(100.);
292 cuts[c-1].setEffectiveness(c);
293 }
294 fim.writeMps("x1.mps");
295 fim.applyRowCuts(3,cuts);
296 fim.writeMps("x2.mps");
297 // resolve - should get message about zero elements
298 fim.resolve();
299 fim.writeMps("x3.mps");
300 // check integer solution
301 const double * cs = fim.getColSolution();
302 CoinRelFltEq eq;
303 OSIUNITTEST_ASSERT_ERROR(eq(cs[2], 1.0), {}, "glpk", "add cuts");
304 OSIUNITTEST_ASSERT_ERROR(eq(cs[3], 1.0), {}, "glpk", "add cuts");
305 #if 0 // ??? Not working for some reason.
306 // check will find invalid matrix
307 el[0]=1.0/el[4];
308 inx[0]=0;
309 cuts[0].setRow(nc,inx,el);
310 cuts[0].setLb(-100.);
311 cuts[0].setUb(500.);
312 cuts[0].setEffectiveness(22);
313 fim.applyRowCut(cuts[0]);
314 // resolve - should get message about zero elements
315 fim.resolve();
316 OSIUNITTEST_ASSERT_WARNING(fim.isAbandoned(), {}, "glpk", "add cuts");
317 #endif
318 delete[]el;
319 delete[]inx;
320 }
321
322 // Test matrixByCol method
323 {
324 const OsiGlpkSolverInterface si(m);
325 const CoinPackedMatrix * smP = si.getMatrixByCol();
326
327 OSIUNITTEST_ASSERT_ERROR(smP->getMajorDim() == 8, return, "glpk", "getMatrixByCol: major dim");
328 OSIUNITTEST_ASSERT_ERROR(smP->getMinorDim() == 5, return, "glpk", "getMatrixByCol: minor dim");
329 OSIUNITTEST_ASSERT_ERROR(smP->getNumElements() == 14, return, "glpk", "getMatrixByCol: number of elements");
330 OSIUNITTEST_ASSERT_ERROR(smP->getSizeVectorStarts() == 9, return, "glpk", "getMatrixByCol: vector starts size");
331
332 CoinRelFltEq eq;
333 const double * ev = smP->getElements();
334 // Unlike row-ordered matrices, GLPK does column-ordered the "normal" way
335 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 0], 3.0), {}, "glpk", "getMatrixByCol: elements");
336 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 1], 5.6), {}, "glpk", "getMatrixByCol: elements");
337 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 2], 1.0), {}, "glpk", "getMatrixByCol: elements");
338 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 3], 2.0), {}, "glpk", "getMatrixByCol: elements");
339 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 4], 1.1), {}, "glpk", "getMatrixByCol: elements");
340 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 5], 1.0), {}, "glpk", "getMatrixByCol: elements");
341 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 6],-2.0), {}, "glpk", "getMatrixByCol: elements");
342 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 7], 2.8), {}, "glpk", "getMatrixByCol: elements");
343 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 8],-1.0), {}, "glpk", "getMatrixByCol: elements");
344 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 9], 1.0), {}, "glpk", "getMatrixByCol: elements");
345 OSIUNITTEST_ASSERT_ERROR(eq(ev[10], 1.0), {}, "glpk", "getMatrixByCol: elements");
346 OSIUNITTEST_ASSERT_ERROR(eq(ev[11],-1.2), {}, "glpk", "getMatrixByCol: elements");
347 OSIUNITTEST_ASSERT_ERROR(eq(ev[12],-1.0), {}, "glpk", "getMatrixByCol: elements");
348 OSIUNITTEST_ASSERT_ERROR(eq(ev[13], 1.9), {}, "glpk", "getMatrixByCol: elements");
349
350 const CoinBigIndex * mi = smP->getVectorStarts();
351 OSIUNITTEST_ASSERT_ERROR(mi[0] == 0, {}, "glpk", "getMatrixByCol: vector starts");
352 OSIUNITTEST_ASSERT_ERROR(mi[1] == 2, {}, "glpk", "getMatrixByCol: vector starts");
353 OSIUNITTEST_ASSERT_ERROR(mi[2] == 4, {}, "glpk", "getMatrixByCol: vector starts");
354 OSIUNITTEST_ASSERT_ERROR(mi[3] == 6, {}, "glpk", "getMatrixByCol: vector starts");
355 OSIUNITTEST_ASSERT_ERROR(mi[4] == 8, {}, "glpk", "getMatrixByCol: vector starts");
356 OSIUNITTEST_ASSERT_ERROR(mi[5] == 10, {}, "glpk", "getMatrixByCol: vector starts");
357 OSIUNITTEST_ASSERT_ERROR(mi[6] == 11, {}, "glpk", "getMatrixByCol: vector starts");
358 OSIUNITTEST_ASSERT_ERROR(mi[7] == 12, {}, "glpk", "getMatrixByCol: vector starts");
359 OSIUNITTEST_ASSERT_ERROR(mi[8] == 14, {}, "glpk", "getMatrixByCol: vector starts");
360
361 const int * ei = smP->getIndices();
362 OSIUNITTEST_ASSERT_ERROR(ei[ 0] == 0, {}, "glpk", "getMatrixByCol: indices");
363 OSIUNITTEST_ASSERT_ERROR(ei[ 1] == 4, {}, "glpk", "getMatrixByCol: indices");
364 OSIUNITTEST_ASSERT_ERROR(ei[ 2] == 0, {}, "glpk", "getMatrixByCol: indices");
365 OSIUNITTEST_ASSERT_ERROR(ei[ 3] == 1, {}, "glpk", "getMatrixByCol: indices");
366 OSIUNITTEST_ASSERT_ERROR(ei[ 4] == 1, {}, "glpk", "getMatrixByCol: indices");
367 OSIUNITTEST_ASSERT_ERROR(ei[ 5] == 2, {}, "glpk", "getMatrixByCol: indices");
368 OSIUNITTEST_ASSERT_ERROR(ei[ 6] == 0, {}, "glpk", "getMatrixByCol: indices");
369 OSIUNITTEST_ASSERT_ERROR(ei[ 7] == 3, {}, "glpk", "getMatrixByCol: indices");
370 OSIUNITTEST_ASSERT_ERROR(ei[ 8] == 0, {}, "glpk", "getMatrixByCol: indices");
371 OSIUNITTEST_ASSERT_ERROR(ei[ 9] == 4, {}, "glpk", "getMatrixByCol: indices");
372 OSIUNITTEST_ASSERT_ERROR(ei[10] == 2, {}, "glpk", "getMatrixByCol: indices");
373 OSIUNITTEST_ASSERT_ERROR(ei[11] == 3, {}, "glpk", "getMatrixByCol: indices");
374 OSIUNITTEST_ASSERT_ERROR(ei[12] == 0, {}, "glpk", "getMatrixByCol: indices");
375 OSIUNITTEST_ASSERT_ERROR(ei[13] == 4, {}, "glpk", "getMatrixByCol: indices");
376 }
377 //--------------
378 // Test rowsense, rhs, rowrange, matrixByRow
379 {
380 OsiGlpkSolverInterface lhs;
381 {
382 #if 0 // FIXME ??? these won't work because the copy constructor changes the values in m
383 OSIUNITTEST_ASSERT_ERROR(m.rowrange_ == NULL, {}, "glpk", "???");
384 OSIUNITTEST_ASSERT_ERROR(m.rowsense_ == NULL, {}, "glpk", "???");
385 OSIUNITTEST_ASSERT_ERROR(m.rhs_ == NULL, {}, "glpk", "???");
386 OSIUNITTEST_ASSERT_ERROR(m.matrixByRow_ == NULL, {}, "glpk", "???");
387 #endif
388
389 OsiGlpkSolverInterface siC1(m);
390 OSIUNITTEST_ASSERT_WARNING(siC1.rowrange_ == NULL, {}, "glpk", "row range");
391 OSIUNITTEST_ASSERT_WARNING(siC1.rowsense_ == NULL, {}, "glpk", "row sense");
392 OSIUNITTEST_ASSERT_WARNING(siC1.rhs_ == NULL, {}, "glpk", "right hand side");
393 OSIUNITTEST_ASSERT_WARNING(siC1.matrixByRow_ == NULL, {}, "glpk", "matrix by row");
394
395 const char * siC1rs = siC1.getRowSense();
396 OSIUNITTEST_ASSERT_ERROR(siC1rs[0] == 'G', {}, "glpk", "row sense");
397 OSIUNITTEST_ASSERT_ERROR(siC1rs[1] == 'L', {}, "glpk", "row sense");
398 OSIUNITTEST_ASSERT_ERROR(siC1rs[2] == 'E', {}, "glpk", "row sense");
399 OSIUNITTEST_ASSERT_ERROR(siC1rs[3] == 'R', {}, "glpk", "row sense");
400 OSIUNITTEST_ASSERT_ERROR(siC1rs[4] == 'R', {}, "glpk", "row sense");
401
402 const double * siC1rhs = siC1.getRightHandSide();
403 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[0],2.5), {}, "glpk", "right hand side");
404 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[1],2.1), {}, "glpk", "right hand side");
405 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[2],4.0), {}, "glpk", "right hand side");
406 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[3],5.0), {}, "glpk", "right hand side");
407 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[4],15.), {}, "glpk", "right hand side");
408
409 const double * siC1rr = siC1.getRowRange();
410 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[0],0.0), {}, "glpk", "row range");
411 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[1],0.0), {}, "glpk", "row range");
412 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[2],0.0), {}, "glpk", "row range");
413 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[3],5.0-1.8), {}, "glpk", "row range");
414 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[4],15.0-3.0), {}, "glpk", "row range");
415
416 const CoinPackedMatrix * siC1mbr = siC1.getMatrixByRow();
417 OSIUNITTEST_ASSERT_ERROR(siC1mbr != NULL, {}, "glpk", "matrix by row");
418 OSIUNITTEST_ASSERT_ERROR(siC1mbr->getMajorDim() == 5, return, "glpk", "matrix by row: major dim");
419 OSIUNITTEST_ASSERT_ERROR(siC1mbr->getNumElements() == 14, return, "glpk", "matrix by row: num elements");
420
421 const double * ev = siC1mbr->getElements();
422 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 4], 3.0), {}, "glpk", "matrix by row: elements");
423 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 3], 1.0), {}, "glpk", "matrix by row: elements");
424 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 2],-2.0), {}, "glpk", "matrix by row: elements");
425 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 1],-1.0), {}, "glpk", "matrix by row: elements");
426 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 0],-1.0), {}, "glpk", "matrix by row: elements");
427 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 6], 2.0), {}, "glpk", "matrix by row: elements");
428 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 5], 1.1), {}, "glpk", "matrix by row: elements");
429 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 8], 1.0), {}, "glpk", "matrix by row: elements");
430 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 7], 1.0), {}, "glpk", "matrix by row: elements");
431 OSIUNITTEST_ASSERT_ERROR(eq(ev[10], 2.8), {}, "glpk", "matrix by row: elements");
432 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 9],-1.2), {}, "glpk", "matrix by row: elements");
433 OSIUNITTEST_ASSERT_ERROR(eq(ev[13], 5.6), {}, "glpk", "matrix by row: elements");
434 OSIUNITTEST_ASSERT_ERROR(eq(ev[12], 1.0), {}, "glpk", "matrix by row: elements");
435 OSIUNITTEST_ASSERT_ERROR(eq(ev[11], 1.9), {}, "glpk", "matrix by row: elements");
436
437 const CoinBigIndex * mi = siC1mbr->getVectorStarts();
438 OSIUNITTEST_ASSERT_ERROR(mi[0] == 0, {}, "glpk", "matrix by row: vector starts");
439 OSIUNITTEST_ASSERT_ERROR(mi[1] == 5, {}, "glpk", "matrix by row: vector starts");
440 OSIUNITTEST_ASSERT_ERROR(mi[2] == 7, {}, "glpk", "matrix by row: vector starts");
441 OSIUNITTEST_ASSERT_ERROR(mi[3] == 9, {}, "glpk", "matrix by row: vector starts");
442 OSIUNITTEST_ASSERT_ERROR(mi[4] == 11, {}, "glpk", "matrix by row: vector starts");
443 OSIUNITTEST_ASSERT_ERROR(mi[5] == 14, {}, "glpk", "matrix by row: vector starts");
444
445 const int * ei = siC1mbr->getIndices();
446 OSIUNITTEST_ASSERT_ERROR(ei[ 4] == 0, {}, "glpk", "matrix by row: indices");
447 OSIUNITTEST_ASSERT_ERROR(ei[ 3] == 1, {}, "glpk", "matrix by row: indices");
448 OSIUNITTEST_ASSERT_ERROR(ei[ 2] == 3, {}, "glpk", "matrix by row: indices");
449 OSIUNITTEST_ASSERT_ERROR(ei[ 1] == 4, {}, "glpk", "matrix by row: indices");
450 OSIUNITTEST_ASSERT_ERROR(ei[ 0] == 7, {}, "glpk", "matrix by row: indices");
451 OSIUNITTEST_ASSERT_ERROR(ei[ 6] == 1, {}, "glpk", "matrix by row: indices");
452 OSIUNITTEST_ASSERT_ERROR(ei[ 5] == 2, {}, "glpk", "matrix by row: indices");
453 OSIUNITTEST_ASSERT_ERROR(ei[ 8] == 2, {}, "glpk", "matrix by row: indices");
454 OSIUNITTEST_ASSERT_ERROR(ei[ 7] == 5, {}, "glpk", "matrix by row: indices");
455 OSIUNITTEST_ASSERT_ERROR(ei[10] == 3, {}, "glpk", "matrix by row: indices");
456 OSIUNITTEST_ASSERT_ERROR(ei[ 9] == 6, {}, "glpk", "matrix by row: indices");
457 OSIUNITTEST_ASSERT_ERROR(ei[13] == 0, {}, "glpk", "matrix by row: indices");
458 OSIUNITTEST_ASSERT_ERROR(ei[12] == 4, {}, "glpk", "matrix by row: indices");
459 OSIUNITTEST_ASSERT_ERROR(ei[11] == 7, {}, "glpk", "matrix by row: indices");
460
461 OSIUNITTEST_ASSERT_WARNING(siC1rs == siC1.getRowSense(), {}, "glpk", "row sense");
462 OSIUNITTEST_ASSERT_WARNING(siC1rhs == siC1.getRightHandSide(), {}, "glpk", "right hand side");
463 OSIUNITTEST_ASSERT_WARNING(siC1rr == siC1.getRowRange(), {}, "glpk", "row range");
464
465 // Change glpk Model by adding free row
466 OsiRowCut rc;
467 rc.setLb(-COIN_DBL_MAX);
468 rc.setUb( COIN_DBL_MAX);
469 OsiCuts cuts;
470 cuts.insert(rc);
471 siC1.applyCuts(cuts);
472
473 // Since model was changed, test that cached data is now freed.
474 OSIUNITTEST_ASSERT_ERROR(siC1.rowrange_ == NULL, {}, "glpk", "free cached data after adding row");
475 OSIUNITTEST_ASSERT_ERROR(siC1.rowsense_ == NULL, {}, "glpk", "free cached data after adding row");
476 OSIUNITTEST_ASSERT_ERROR(siC1.rhs_ == NULL, {}, "glpk", "free cached data after adding row");
477 OSIUNITTEST_ASSERT_ERROR(siC1.matrixByRow_ == NULL, {}, "glpk", "free cached data after adding row");
478
479 siC1rs = siC1.getRowSense();
480 OSIUNITTEST_ASSERT_ERROR(siC1rs[0] == 'G', {}, "glpk", "row sense after adding row");
481 OSIUNITTEST_ASSERT_ERROR(siC1rs[1] == 'L', {}, "glpk", "row sense after adding row");
482 OSIUNITTEST_ASSERT_ERROR(siC1rs[2] == 'E', {}, "glpk", "row sense after adding row");
483 OSIUNITTEST_ASSERT_ERROR(siC1rs[3] == 'R', {}, "glpk", "row sense after adding row");
484 OSIUNITTEST_ASSERT_ERROR(siC1rs[4] == 'R', {}, "glpk", "row sense after adding row");
485 OSIUNITTEST_ASSERT_ERROR(siC1rs[5] == 'N', {}, "glpk", "row sense after adding row");
486
487 siC1rhs = siC1.getRightHandSide();
488 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[0],2.5), {}, "glpk", "right hand side after adding row");
489 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[1],2.1), {}, "glpk", "right hand side after adding row");
490 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[2],4.0), {}, "glpk", "right hand side after adding row");
491 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[3],5.0), {}, "glpk", "right hand side after adding row");
492 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[4],15.), {}, "glpk", "right hand side after adding row");
493 OSIUNITTEST_ASSERT_ERROR(eq(siC1rhs[5],0.0), {}, "glpk", "right hand side after adding row");
494
495 siC1rr = siC1.getRowRange();
496 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[0],0.0), {}, "glpk", "row range after adding row");
497 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[1],0.0), {}, "glpk", "row range after adding row");
498 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[2],0.0), {}, "glpk", "row range after adding row");
499 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[3],5.0-1.8), {}, "glpk", "row range after adding row");
500 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[4],15.0-3.0), {}, "glpk", "row range after adding row");
501 OSIUNITTEST_ASSERT_ERROR(eq(siC1rr[5],0.0), {}, "glpk", "row range after adding row");
502
503 lhs = siC1;
504 }
505 // Test that lhs has correct values even though siC1 has gone out of scope
506 OSIUNITTEST_ASSERT_ERROR(lhs.rowrange_ == NULL, {}, "glpk", "freed origin after assignment");
507 OSIUNITTEST_ASSERT_ERROR(lhs.rowsense_ == NULL, {}, "glpk", "freed origin after assignment");
508 OSIUNITTEST_ASSERT_ERROR(lhs.rhs_ == NULL, {}, "glpk", "freed origin after assignment");
509 OSIUNITTEST_ASSERT_ERROR(lhs.matrixByRow_ == NULL, {}, "glpk", "freed origin after assignment");
510
511 const char * lhsrs = lhs.getRowSense();
512 OSIUNITTEST_ASSERT_ERROR(lhsrs[0] == 'G', {}, "glpk", "row sense after assignment");
513 OSIUNITTEST_ASSERT_ERROR(lhsrs[1] == 'L', {}, "glpk", "row sense after assignment");
514 OSIUNITTEST_ASSERT_ERROR(lhsrs[2] == 'E', {}, "glpk", "row sense after assignment");
515 OSIUNITTEST_ASSERT_ERROR(lhsrs[3] == 'R', {}, "glpk", "row sense after assignment");
516 OSIUNITTEST_ASSERT_ERROR(lhsrs[4] == 'R', {}, "glpk", "row sense after assignment");
517 OSIUNITTEST_ASSERT_ERROR(lhsrs[5] == 'N', {}, "glpk", "row sense after assignment");
518
519 const double * lhsrhs = lhs.getRightHandSide();
520 OSIUNITTEST_ASSERT_ERROR(eq(lhsrhs[0],2.5), {}, "glpk", "right hand side after assignment");
521 OSIUNITTEST_ASSERT_ERROR(eq(lhsrhs[1],2.1), {}, "glpk", "right hand side after assignment");
522 OSIUNITTEST_ASSERT_ERROR(eq(lhsrhs[2],4.0), {}, "glpk", "right hand side after assignment");
523 OSIUNITTEST_ASSERT_ERROR(eq(lhsrhs[3],5.0), {}, "glpk", "right hand side after assignment");
524 OSIUNITTEST_ASSERT_ERROR(eq(lhsrhs[4],15.), {}, "glpk", "right hand side after assignment");
525 OSIUNITTEST_ASSERT_ERROR(eq(lhsrhs[5],0.0), {}, "glpk", "right hand side after assignment");
526
527 const double *lhsrr = lhs.getRowRange();
528 OSIUNITTEST_ASSERT_ERROR(eq(lhsrr[0],0.0), {}, "glpk", "row range after assignment");
529 OSIUNITTEST_ASSERT_ERROR(eq(lhsrr[1],0.0), {}, "glpk", "row range after assignment");
530 OSIUNITTEST_ASSERT_ERROR(eq(lhsrr[2],0.0), {}, "glpk", "row range after assignment");
531 OSIUNITTEST_ASSERT_ERROR(eq(lhsrr[3],5.0-1.8), {}, "glpk", "row range after assignment");
532 OSIUNITTEST_ASSERT_ERROR(eq(lhsrr[4],15.0-3.0), {}, "glpk", "row range after assignment");
533 OSIUNITTEST_ASSERT_ERROR(eq(lhsrr[5],0.0), {}, "glpk", "row range after assignment");
534
535 const CoinPackedMatrix * lhsmbr = lhs.getMatrixByRow();
536 OSIUNITTEST_ASSERT_ERROR(lhsmbr != NULL, return, "glpk", "matrix by row after assignment");
537 OSIUNITTEST_ASSERT_ERROR(lhsmbr->getMajorDim() == 6, return, "glpk", "matrix by row after assignment: major dim");
538 OSIUNITTEST_ASSERT_ERROR(lhsmbr->getNumElements() == 14, return, "glpk", "matrix by row after assignment: num elements");
539
540 const double * ev = lhsmbr->getElements();
541 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 4], 3.0), {}, "glpk", "matrix by row after assignment: elements");
542 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 3], 1.0), {}, "glpk", "matrix by row after assignment: elements");
543 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 2],-2.0), {}, "glpk", "matrix by row after assignment: elements");
544 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 1],-1.0), {}, "glpk", "matrix by row after assignment: elements");
545 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 0],-1.0), {}, "glpk", "matrix by row after assignment: elements");
546 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 6], 2.0), {}, "glpk", "matrix by row after assignment: elements");
547 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 5], 1.1), {}, "glpk", "matrix by row after assignment: elements");
548 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 8], 1.0), {}, "glpk", "matrix by row after assignment: elements");
549 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 7], 1.0), {}, "glpk", "matrix by row after assignment: elements");
550 OSIUNITTEST_ASSERT_ERROR(eq(ev[10], 2.8), {}, "glpk", "matrix by row after assignment: elements");
551 OSIUNITTEST_ASSERT_ERROR(eq(ev[ 9],-1.2), {}, "glpk", "matrix by row after assignment: elements");
552 OSIUNITTEST_ASSERT_ERROR(eq(ev[13], 5.6), {}, "glpk", "matrix by row after assignment: elements");
553 OSIUNITTEST_ASSERT_ERROR(eq(ev[12], 1.0), {}, "glpk", "matrix by row after assignment: elements");
554 OSIUNITTEST_ASSERT_ERROR(eq(ev[11], 1.9), {}, "glpk", "matrix by row after assignment: elements");
555
556 const CoinBigIndex * mi = lhsmbr->getVectorStarts();
557 OSIUNITTEST_ASSERT_ERROR(mi[0] == 0, {}, "glpk", "matrix by row after assignment: vector starts");
558 OSIUNITTEST_ASSERT_ERROR(mi[1] == 5, {}, "glpk", "matrix by row after assignment: vector starts");
559 OSIUNITTEST_ASSERT_ERROR(mi[2] == 7, {}, "glpk", "matrix by row after assignment: vector starts");
560 OSIUNITTEST_ASSERT_ERROR(mi[3] == 9, {}, "glpk", "matrix by row after assignment: vector starts");
561 OSIUNITTEST_ASSERT_ERROR(mi[4] == 11, {}, "glpk", "matrix by row after assignment: vector starts");
562 OSIUNITTEST_ASSERT_ERROR(mi[5] == 14, {}, "glpk", "matrix by row after assignment: vector starts");
563
564 const int * ei = lhsmbr->getIndices();
565 OSIUNITTEST_ASSERT_ERROR(ei[ 4] == 0, {}, "glpk", "matrix by row after assignment: indices");
566 OSIUNITTEST_ASSERT_ERROR(ei[ 3] == 1, {}, "glpk", "matrix by row after assignment: indices");
567 OSIUNITTEST_ASSERT_ERROR(ei[ 2] == 3, {}, "glpk", "matrix by row after assignment: indices");
568 OSIUNITTEST_ASSERT_ERROR(ei[ 1] == 4, {}, "glpk", "matrix by row after assignment: indices");
569 OSIUNITTEST_ASSERT_ERROR(ei[ 0] == 7, {}, "glpk", "matrix by row after assignment: indices");
570 OSIUNITTEST_ASSERT_ERROR(ei[ 6] == 1, {}, "glpk", "matrix by row after assignment: indices");
571 OSIUNITTEST_ASSERT_ERROR(ei[ 5] == 2, {}, "glpk", "matrix by row after assignment: indices");
572 OSIUNITTEST_ASSERT_ERROR(ei[ 8] == 2, {}, "glpk", "matrix by row after assignment: indices");
573 OSIUNITTEST_ASSERT_ERROR(ei[ 7] == 5, {}, "glpk", "matrix by row after assignment: indices");
574 OSIUNITTEST_ASSERT_ERROR(ei[10] == 3, {}, "glpk", "matrix by row after assignment: indices");
575 OSIUNITTEST_ASSERT_ERROR(ei[ 9] == 6, {}, "glpk", "matrix by row after assignment: indices");
576 OSIUNITTEST_ASSERT_ERROR(ei[13] == 0, {}, "glpk", "matrix by row after assignment: indices");
577 OSIUNITTEST_ASSERT_ERROR(ei[12] == 4, {}, "glpk", "matrix by row after assignment: indices");
578 OSIUNITTEST_ASSERT_ERROR(ei[11] == 7, {}, "glpk", "matrix by row after assignment: indices");
579 }
580
581 }
582
583 // Test add/delete columns
584 {
585 OsiGlpkSolverInterface m;
586 std::string fn = mpsDir+"p0033";
587 m.readMps(fn.c_str(),"mps");
588 double inf = m.getInfinity();
589
590 CoinPackedVector c0;
591 c0.insert(0, 4);
592 c0.insert(1, 1);
593 m.addCol(c0, 0, inf, 3);
594 m.initialSolve();
595 double objValue = m.getObjValue();
596 CoinRelFltEq eq(1.0e-2);
597 OSIUNITTEST_ASSERT_ERROR(eq(objValue,2520.57), {}, "glpk", "add/delete columns: first optimal value");
598 // Try deleting first column that's nonbasic at lower bound (0).
599 int * d = new int[1];
600 CoinWarmStartBasis *cwsb = dynamic_cast<CoinWarmStartBasis *>(m.getWarmStart()) ;
601 OSIUNITTEST_ASSERT_ERROR(cwsb != NULL, {}, "glpk", "add/delete columns: have warm start basis");
602 CoinWarmStartBasis::Status stati ;
603 int iCol ;
604 for (iCol = 0 ; iCol < cwsb->getNumStructural() ; iCol++)
605 { stati = cwsb->getStructStatus(iCol) ;
606 if (stati == CoinWarmStartBasis::atLowerBound) break ; }
607 d[0]=iCol;
608 m.deleteCols(1,d);
609 delete [] d;
610 delete cwsb;
611 d=NULL;
612 m.resolve();
613 objValue = m.getObjValue();
614 OSIUNITTEST_ASSERT_ERROR(eq(objValue,2520.57), {}, "glpk", "add/delete columns: optimal value after deleting nonbasic column");
615 // Try deleting column we added. If basic, go to initialSolve as deleting
616 // basic variable trashes basis required for warm start.
617 iCol = m.getNumCols()-1;
618 cwsb = dynamic_cast<CoinWarmStartBasis *>(m.getWarmStart()) ;
619 stati = cwsb->getStructStatus(iCol) ;
620 delete cwsb;
621 m.deleteCols(1,&iCol);
622 if (stati == CoinWarmStartBasis::basic)
623 { m.initialSolve() ; }
624 else
625 { m.resolve(); }
626 objValue = m.getObjValue();
627 OSIUNITTEST_ASSERT_ERROR(eq(objValue,2520.57), {}, "glpk", "add/delete columns: optimal value after deleting added column");
628 }
629
630 #if 0
631 // ??? Simplex routines not adapted to OsiGlpk yet
632
633 // Solve an lp by hand
634 {
635 OsiGlpkSolverInterface m;
636 std::string fn = mpsDir+"p0033";
637 m.readMps(fn.c_str(),"mps");
638 m.setObjSense(-1.0);
639 m.getModelPtr()->messageHandler()->setLogLevel(4);
640 m.initialSolve();
641 m.getModelPtr()->factorization()->maximumPivots(5);
642 m.setObjSense(1.0);
643 // enable special mode
644 m.enableSimplexInterface(true);
645 // we happen to know that variables are 0-1 and rows are L
646 int numberIterations=0;
647 int numberColumns = m.getNumCols();
648 int numberRows = m.getNumRows();
649 double * fakeCost = new double[numberColumns];
650 double * duals = new double [numberRows];
651 double * djs = new double [numberColumns];
652 const double * solution = m.getColSolution();
653 memcpy(fakeCost,m.getObjCoefficients(),numberColumns*sizeof(double));
654 while (1) {
655 const double * dj;
656 const double * dual;
657 if ((numberIterations&1)==0) {
658 // use given ones
659 dj = m.getReducedCost();
660 dual = m.getRowPrice();
661 } else {
662 // create
663 dj = djs;
664 dual = duals;
665 m.getReducedGradient(djs,duals,fakeCost);
666 }
667 int i;
668 int colIn=9999;
669 int direction=1;
670 double best=1.0e-6;
671 // find most negative reduced cost
672 // Should check basic - but should be okay on this problem
673 for (i=0;i<numberRows;i++) {
674 double value=dual[i];
675 if (value>best) {
676 direction=-1;
677 best=value;
678 colIn=-i-1;
679 }
680 }
681 for (i=0;i<numberColumns;i++) {
682 double value=dj[i];
683 if (value<-best&&solution[i]<1.0e-6) {
684 direction=1;
685 best=-value;
686 colIn=i;
687 } else if (value>best&&solution[i]>1.0-1.0e-6) {
688 direction=-1;
689 best=value;
690 colIn=i;
691 }
692 }
693 if (colIn==9999)
694 break; // should be optimal
695 int colOut;
696 int outStatus;
697 double theta;
698 OSIUNITTEST_ASSERT_ERROR(m.primalPivotResult(colIn,direction,colOut,outStatus,theta,NULL) == 0, {}, "glpk", "simplex routines");
699 printf("out %d, direction %d theta %g\n", colOut,outStatus,theta);
700 numberIterations++;
701 }
702 delete [] fakeCost;
703 delete [] duals;
704 delete [] djs;
705 // exit special mode
706 m.disableSimplexInterface();
707 m.getModelPtr()->messageHandler()->setLogLevel(4);
708 m.resolve();
709 OSIUNITTEST_ASSERT_ERROR(m.getIterationCount() == 0, {}, "glpk", "simplex routines");
710 m.setObjSense(-1.0);
711 m.initialSolve();
712 }
713 // Solve an lp when interface is on
714 {
715 OsiGlpkSolverInterface m;
716 std::string fn = mpsDir+"p0033";
717 m.readMps(fn.c_str(),"mps");
718 // enable special mode
719 m.setHintParam(OsiDoScale,false,OsiHintDo);
720 m.setHintParam(OsiDoPresolveInInitial,false,OsiHintDo);
721 m.setHintParam(OsiDoDualInInitial,false,OsiHintDo);
722 m.setHintParam(OsiDoPresolveInResolve,false,OsiHintDo);
723 m.setHintParam(OsiDoDualInResolve,false,OsiHintDo);
724 m.enableSimplexInterface(true);
725 m.initialSolve();
726 }
727 // Check tableau stuff when simplex interface is on
728 {
729 OsiGlpkSolverInterface m;
730 /*
731 Wolsey : Page 130
732 max 4x1 - x2
733 7x1 - 2x2 <= 14
734 x2 <= 3
735 2x1 - 2x2 <= 3
736 x1 in Z+, x2 >= 0
737 */
738
739 double inf_ = m.getInfinity();
740 int n_cols = 2;
741 int n_rows = 3;
742
743 double obj[2] = {-4.0, 1.0};
744 double collb[2] = {0.0, 0.0};
745 double colub[2] = {inf_, inf_};
746 double rowlb[3] = {-inf_, -inf_, -inf_};
747 double rowub[3] = {14.0, 3.0, 3.0};
748
749 int rowIndices[5] = {0, 2, 0, 1, 2};
750 int colIndices[5] = {0, 0, 1, 1, 1};
751 double elements[5] = {7.0, 2.0, -2.0, 1.0, -2.0};
752 CoinPackedMatrix M(true, rowIndices, colIndices, elements, 5);
753
754 m.loadProblem(M, collb, colub, obj, rowlb, rowub);
755 m.enableSimplexInterface(true);
756
757 m.initialSolve();
758
759 //check that the tableau matches wolsey (B-1 A)
760 // slacks in second part of binvA
761 double * binvA = (double*) malloc((n_cols+n_rows) * sizeof(double));
762
763 printf("B-1 A");
764 for(int i = 0; i < n_rows; i++){
765 m.getBInvARow(i, binvA,binvA+n_cols);
766 printf("\nrow: %d -> ",i);
767 for(int j=0; j < n_cols+n_rows; j++){
768 printf("%g, ", binvA[j]);
769 }
770 }
771 printf("\n");
772 m.disableSimplexInterface();
773 free(binvA);
774 }
775 #endif
776
777 /*
778 Read in exmip1 and solve it with verbose output setting.
779 */
780 { OsiGlpkSolverInterface osi ;
781 std::cout << "Boosting verbosity.\n" ;
782 osi.setHintParam(OsiDoReducePrint,false,OsiForceDo) ;
783
784 std::string exmpsfile = mpsDir+"exmip1" ;
785 std::string probname ;
786 std::cout << "Reading mps file \"" << exmpsfile << "\"\n" ;
787 osi.readMps(exmpsfile.c_str(), "mps") ;
788 OSIUNITTEST_ASSERT_ERROR(osi.getStrParam(OsiProbName,probname), {}, "glpk", "get problem name");
789 std::cout << "Solving " << probname << " ... \n" ;
790 osi.initialSolve() ;
791 double val = osi.getObjValue() ;
792 std::cout << "And the answer is " << val << ".\n" ;
793 OSIUNITTEST_ASSERT_ERROR(fabs(val - 3.23) < 0.01, {}, "glpk", "solve exmip1");
794 }
795
796 // Do common solverInterface testing
797 {
798 OsiGlpkSolverInterface m;
799 OsiSolverInterfaceCommonUnitTest(&m, mpsDir,netlibDir);
800 }
801
802 }
803 #endif