Debian Codegraph @

Osi/src/OsiCommonTest/OsiUnitTestUtils.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2000 -- 2010, Lou Hafer, International Business Machines,
3 and others. All Rights Reserved.
4
5 This code is licensed under the terms of the Eclipse Public License (EPL).
6 */
7
8 #include "CoinPragma.hpp"
9
10 #include "OsiUnitTests.hpp"
11
12 #include "OsiConfig.h"
13
14 /*
15 #include "CoinTime.hpp"
16 #include <cstdlib>
17 #include <cassert>
18 #include <vector>
19 #include <iostream>
20 #include <iomanip>
21 #include <sstream>
22 #include <cstdio>
23 */
24
25 #include "OsiSolverInterface.hpp"
26 #include "CoinFloatEqual.hpp"
27 #include "CoinHelperFunctions.hpp"
28 #include "CoinPackedMatrix.hpp"
29 /*
30 #include "CoinPackedVector.hpp"
31 #include "CoinWarmStartBasis.hpp"
32 #include "OsiRowCut.hpp"
33 #include "OsiCuts.hpp"
34 #include "OsiPresolve.hpp"
35 */
36
37 namespace OsiUnitTest {
38
39 unsigned int verbosity = 0;
40
41 unsigned int haltonerror = 0;
42
43 TestOutcomes outcomes;
44
45 //#############################################################################
46 // Helper routines for messages.
47 //#############################################################################
48
49 /*
50 If anyone is feeling ambitious, it'd be a really good idea to handle i/o for
51 the unittest by way of a standard CoinMessageHandler. Might require a bit of
52 tweaking in CoinMessageHandler.
53 */
54
55 // A helper function to write out a message about a test failure
56 void failureMessage(const std::string &solverName,
57 const std::string &message)
58 {
59 std::string messageText;
60 messageText = "*** ";
61 messageText += solverName + "SolverInterface testing issue: ";
62 messageText += message;
63 // flush stdout so that error messages are properly interleaved.
64 std::cout.flush();
65 std::cerr << messageText.c_str() << std::endl;
66 }
67
68 void failureMessage(const OsiSolverInterface &si,
69 const std::string &message)
70 {
71 std::string solverName;
72 si.getStrParam(OsiSolverName, solverName);
73 failureMessage(solverName, message);
74 }
75
76 void failureMessage(const std::string &solverName, const std::string &testname, const std::string &testcond)
77 {
78 std::string messageText;
79 messageText = "*** ";
80 messageText += solverName + "SolverInterface testing issue: ";
81 messageText += testname + " failed: " + testcond;
82 // flush stdout so that error messages are properly interleaved.
83 std::cout.flush();
84 std::cerr << messageText.c_str() << std::endl;
85 }
86
87 void failureMessage(const OsiSolverInterface &si, const std::string &testname, const std::string &testcond)
88 {
89 std::string solverName;
90 si.getStrParam(OsiSolverName, solverName);
91 failureMessage(solverName, testname, testcond);
92 }
93
94 /*
95 Display message on stderr. Flush cout buffer before printing the message,
96 so that output comes out in order in spite of buffered cout.
97 */
98 void testingMessage(const char *const msg)
99 {
100 std::cout.flush();
101 std::cerr << msg;
102 }
103 //#############################################################################
104 // Vector comparison utility.
105 //#############################################################################
106
107 // A helper function to compare the equivalence of two vectors
108 bool equivalentVectors(const OsiSolverInterface *si1,
109 const OsiSolverInterface *si2,
110 double tol,
111 const double *v1,
112 const double *v2,
113 int size)
114 {
115 bool retVal = true;
116 double infty1 = si1->getInfinity();
117 double infty2 = si2->getInfinity();
118 CoinRelFltEq eq(tol);
119 int i;
120 /*
121 Both values must be the same infinity or equal within the specified
122 tolerance. Otherwise we have failure.
123 */
124 for (i = 0; i < size; i++) {
125 if (!(v1[i] <= -infty1 && v2[i] <= -infty2) && !(v1[i] >= infty1 && v2[i] >= infty2) && !eq(v1[i], v2[i])) {
126 std::cout.flush();
127 std::cerr << "eq " << i << " " << v1[i] << " " << v2[i] << std::endl;
128 retVal = false;
129 break;
130 }
131 }
132 return retVal;
133 }
134
135 /*
136 Check a packed vector for equality with a full vector. The algorithm is to
137 first confirm that the elements of the packed vector are present in the full
138 vector, then scan the full vector to make sure there are no additional
139 nonzeros.
140 */
141 bool isEquivalent(const CoinPackedVectorBase &pv, int n, const double *fv)
142 {
143 int pvCnt = pv.getNumElements();
144 const int *indices = pv.getIndices();
145 const double *elems = pv.getElements();
146
147 bool retval = true;
148 CoinRelFltEq eq;
149
150 for (int v = 0; v < pvCnt; v++) {
151 int k = indices[v];
152 if (!eq(elems[v], fv[k])) {
153 retval = false;
154 break;
155 }
156 }
157 if (retval == true) {
158 int fvCnt = 0;
159 for (int k = 0; k < n; k++) {
160 if (!eq(fv[k], 0.0))
161 fvCnt++;
162 }
163 if (fvCnt != pvCnt)
164 retval = false;
165 }
166
167 return (retval);
168 }
169
170 /*
171 Method to compare the problem representation held by a pair of solver
172 interfaces.
173 */
174 bool compareProblems(OsiSolverInterface *osi1, OsiSolverInterface *osi2)
175 {
176
177 bool areEquiv = true;
178 std::string si1Name, si2Name;
179 osi1->getStrParam(OsiSolverName, si1Name);
180 osi2->getStrParam(OsiSolverName, si2Name);
181
182 // Compare row and column counts
183 int colCnt = 0;
184 if (osi1->getNumCols() != osi2->getNumCols()) {
185 std::cerr
186 << " Unequal column count, "
187 << si1Name << " vs. " << si2Name << std::endl;
188 return (false);
189 } else {
190 colCnt = osi1->getNumCols();
191 }
192
193 int rowCnt = 0;
194 if (osi1->getNumRows() != osi2->getNumRows()) {
195 std::cerr
196 << " Unequal row count, "
197 << si1Name << " vs. " << si2Name << std::endl;
198 return (false);
199 } else {
200 rowCnt = osi1->getNumRows();
201 }
202
203 // Compare column bounds
204 areEquiv = equivalentVectors(osi1, osi2, 1.e-10,
205 osi1->getColLower(), osi2->getColLower(), colCnt);
206 if (areEquiv == false) {
207 std::cerr
208 << " Unequal column lower bounds, "
209 << si1Name << " vs. " << si2Name << std::endl;
210 return (false);
211 }
212 areEquiv = equivalentVectors(osi1, osi2, 1.e-10,
213 osi1->getColUpper(), osi2->getColUpper(), colCnt);
214 if (areEquiv == false) {
215 std::cerr
216 << " Unequal column upper bounds, "
217 << si1Name << " vs. " << si2Name << std::endl;
218 return (false);
219 }
220
221 // Compare row bounds
222 areEquiv = equivalentVectors(osi1, osi2, 1.e-10,
223 osi1->getRowLower(), osi2->getRowLower(), rowCnt);
224 if (areEquiv == false) {
225 std::cerr
226 << " Unequal row lower bounds, "
227 << si1Name << " vs. " << si2Name << std::endl;
228 return (false);
229 }
230 areEquiv = equivalentVectors(osi1, osi2, 1.e-10,
231 osi1->getRowUpper(), osi2->getRowUpper(), rowCnt);
232 if (areEquiv == false) {
233 std::cerr
234 << " Unequal row lower bounds, "
235 << si1Name << " vs. " << si2Name << std::endl;
236 return (false);
237 }
238
239 // Compare row sense
240 {
241 const char *rowSense1 = osi1->getRowSense();
242 const char *rowSense2 = osi2->getRowSense();
243 areEquiv = true;
244 for (int r = 0; r < rowCnt && areEquiv == true; r++) {
245 if (rowSense1[r] != rowSense2[r]) {
246 areEquiv = false;
247 }
248 }
249 if (areEquiv == false) {
250 std::cerr
251 << " Unequal row sense, "
252 << si1Name << " vs. " << si2Name << std::endl;
253 return (false);
254 }
255 }
256
257 // Compare row rhs
258 areEquiv = equivalentVectors(osi1, osi2, 1.e-10,
259 osi1->getRightHandSide(), osi2->getRightHandSide(), rowCnt);
260 if (areEquiv == false) {
261 std::cerr
262 << " Unequal right-hand-side, "
263 << si1Name << " vs. " << si2Name << std::endl;
264 return (false);
265 }
266
267 // Compare range
268 areEquiv = equivalentVectors(osi1, osi2, 1.e-10,
269 osi1->getRowRange(), osi2->getRowRange(), rowCnt);
270 if (areEquiv == false) {
271 std::cerr
272 << " Unequal row range, "
273 << si1Name << " vs. " << si2Name << std::endl;
274 return (false);
275 }
276
277 // Compare objective sense
278 if (osi1->getObjSense() != osi2->getObjSense()) {
279 std::cerr
280 << " Unequal objective sense, "
281 << si1Name << " vs. " << si2Name << std::endl;
282 return (false);
283 }
284
285 // Compare objective coefficients
286 areEquiv = equivalentVectors(osi1, osi2, 1.e-10,
287 osi1->getObjCoefficients(), osi2->getObjCoefficients(), colCnt);
288 if (areEquiv == false) {
289 std::cerr
290 << " Unequal objective coefficients, "
291 << si1Name << " vs. " << si2Name << std::endl;
292 return (false);
293 }
294
295 // Compare number of elements
296 if (osi1->getNumElements() != osi2->getNumElements()) {
297 std::cerr
298 << " Unequal number of constraint matrix coefficients, "
299 << si1Name << " vs. " << si2Name << std::endl;
300 return (false);
301 }
302
303 // Compare constraint matrix, for both row-major and column-major orderings
304 {
305 const CoinPackedMatrix *rmm1 = osi1->getMatrixByRow();
306 const CoinPackedMatrix *rm = osi2->getMatrixByRow();
307 if (!rmm1->isEquivalent(*rm)) {
308 std::cerr
309 << " Unequal constraint matrix, row-major ordering, "
310 << si1Name << " vs. " << si2Name << std::endl;
311 return (false);
312 }
313 const CoinPackedMatrix *cmm1 = osi1->getMatrixByCol();
314 const CoinPackedMatrix *cm = osi2->getMatrixByCol();
315 if (!cmm1->isEquivalent(*cm)) {
316 std::cerr
317 << " Unequal constraint matrix, column-major ordering, "
318 << si1Name << " vs. " << si2Name << std::endl;
319 return (false);
320 }
321 }
322 // Check column types
323 {
324 areEquiv = true;
325 for (int j = 0; j < colCnt && areEquiv == true; j++) {
326 if (osi1->isContinuous(j) != osi2->isContinuous(j))
327 areEquiv = false;
328 if (osi1->isBinary(j) != osi2->isBinary(j))
329 areEquiv = false;
330 if (osi1->isIntegerNonBinary(j) != osi2->isIntegerNonBinary(j))
331 areEquiv = false;
332 if (osi1->isFreeBinary(j) != osi2->isFreeBinary(j))
333 areEquiv = false;
334 if (osi1->isInteger(j) != osi2->isInteger(j))
335 areEquiv = false;
336 }
337 if (areEquiv == false) {
338 std::cerr
339 << " Unequal variable type, "
340 << si1Name << " vs. " << si2Name << std::endl;
341 return (false);
342 }
343 }
344 return (true);
345 }
346
347 bool processParameters(int argc, const char **argv, std::map< std::string, std::string > &parms, const std::map< std::string, int > &ignorekeywords)
348 {
349 /*
350 Initialise the parameter keywords.
351 */
352 std::set< std::string > definedKeyWords;
353 definedKeyWords.insert("-cerr2cout");
354 definedKeyWords.insert("-mpsDir");
355 definedKeyWords.insert("-netlibDir");
356 definedKeyWords.insert("-miplib3Dir");
357 definedKeyWords.insert("-testOsiSolverInterface");
358 definedKeyWords.insert("-nobuf");
359 definedKeyWords.insert("-cutsOnly");
360 definedKeyWords.insert("-verbosity");
361 definedKeyWords.insert("-onerror");
362
363 /*
364 Set default values for data directories.
365 */
366 const char dirsep = CoinFindDirSeparator();
367 std::string pathTmp;
368 pathTmp = "..";
369 pathTmp += dirsep;
370 pathTmp += "..";
371 pathTmp += dirsep;
372 pathTmp += "Data";
373 pathTmp += dirsep;
374 #ifdef COIN_MSVS
375 // Visual Studio builds are deeper
376 pathTmp = "..\\..\\" + pathTmp;
377 #endif
378
379 parms["-mpsDir"] = pathTmp + "Sample";
380 parms["-netlibDir"] = pathTmp + "Netlib";
381 parms["-miplib3Dir"] = pathTmp + "miplib3";
382
383 /*
384 Read the command line parameters and fill a map of parameter keys and
385 associated data. The parser allows for parameters which are only a keyword,
386 or parameters of the form keyword=value (no spaces).
387 */
388 for (int i = 1; i < argc; i++) {
389 std::string parm(argv[i]);
390 std::string key, value;
391 std::string::size_type eqPos = parm.find('=');
392
393 if (eqPos == std::string::npos) {
394 key = parm;
395 } else {
396 key = parm.substr(0, eqPos);
397 value = parm.substr(eqPos + 1);
398 }
399
400 /*
401 * Should the specified key be ignored?
402 */
403 if (ignorekeywords.find(key) != ignorekeywords.end()) {
404 assert(ignorekeywords.find(key)->second >= 0);
405 i += ignorekeywords.find(key)->second;
406 continue;
407 }
408
409 /*
410 Is the specified key valid?
411 */
412 if (definedKeyWords.find(key) == definedKeyWords.end()) {
413 if (key != "-usage" && key != "-help")
414 std::cerr << "Undefined parameter \"" << key << "\"." << std::endl;
415 std::cerr << "Usage: unitTest [-nobuf] [-mpsDir=V1] [-netlibDir=V2] [-miplibDir=V3] [-testOsiSolverInterface] [-cutsOnly] [-verbosity=num]" << std::endl;
416 std::cerr << " where:" << std::endl;
417 std::cerr << " -cerr2cout: redirect cerr to cout; sometimes useful to synchronise cout & cerr." << std::endl;
418 std::cerr << " -mpsDir: directory containing mps test files." << std::endl
419 << " Default value V1=\"../../Data/Sample\"" << std::endl;
420 std::cerr << " -netlibDir: directory containing netlib files." << std::endl
421 << " Default value V2=\"../../Data/Netlib\"" << std::endl;
422 std::cerr << " -miplib3Dir: directory containing miplib3 files." << std::endl
423 << " Default value V3=\"../../Data/miplib3\"" << std::endl;
424 std::cerr << " -testOsiSolverInterface: run each OSI on the netlib problem set." << std::endl
425 << " Default is to not run the netlib problem set." << std::endl;
426 std::cerr << " -cutsOnly: If specified, only OsiCut tests are run." << std::endl;
427 std::cerr << " -nobuf: use unbuffered output." << std::endl
428 << " Default is buffered output." << std::endl;
429 std::cerr << " -verbosity: verbosity level of tests output (0-2)." << std::endl
430 << " Default is 0 (minimal output)." << std::endl;
431 std::cerr << " -onerror: behaviour in case of failing test (continue, wait, stop)." << std::endl
432 << " Default is continue." << std::endl;
433 return false;
434 }
435
436 /*
437 Valid keyword; stash the value for later reference.
438 */
439 parms[key] = value;
440 }
441
442 /*
443 Tack the directory separator onto the data directories so we don't have to
444 worry about it later.
445 */
446 if (parms["-mpsDir"].length() > 0)
447 parms["-mpsDir"] += dirsep;
448 if (parms["-netlibDir"].length() > 0)
449 parms["-netlibDir"] += dirsep;
450 if (parms["-miplib3"].length() > 0)
451 parms["-miplib3Dir"] += dirsep;
452
453 /*
454 Did the user request unbuffered i/o? It seems we need to go after this
455 through stdio --- using pubsetbuf(0,0) on the C++ streams has no
456 discernible affect. Nor, for that matter, did setting the unitbuf flag on
457 the streams. Why? At a guess, sync_with_stdio connects the streams to the
458 stdio buffers, and the C++ side isn't programmed to change them?
459 */
460 if (parms.find("-nobuf") != parms.end()) {
461 // std::streambuf *coutBuf, *cerrBuf ;
462 // coutBuf = std::cout.rdbuf() ;
463 // coutBuf->pubsetbuf(0,0) ;
464 // cerrBuf = std::cerr.rdbuf() ;
465 // cerrBuf->pubsetbuf(0,0) ;
466 setbuf(stderr, 0);
467 setbuf(stdout, 0);
468 }
469
470 /*
471 Did the user request a redirect for cerr? This must occur before any i/o is
472 performed.
473 */
474 if (parms.find("-cerr2cout") != parms.end())
475 std::cerr.rdbuf(std::cout.rdbuf());
476
477 /*
478 * Did the user set a verbosity level?
479 */
480 if (parms.find("-verbosity") != parms.end()) {
481 char *endptr;
482 std::string verbstring = parms["-verbosity"];
483 unsigned long verblevel = strtoul(verbstring.c_str(), &endptr, 10);
484
485 if (*endptr != '\0' || verblevel < 0) {
486 std::cerr << "verbosity level must be a nonnegative number" << std::endl;
487 return false;
488 }
489
490 OsiUnitTest::verbosity = static_cast< unsigned int >(verblevel);
491 }
492
493 /*
494 * Did the user specify what to do in case of a failure?
495 */
496 if (parms.find("-onerror") != parms.end()) {
497 std::string onerror = parms["-onerror"];
498
499 if (onerror == "continue")
500 OsiUnitTest::haltonerror = 0;
501 else if (onerror == "wait")
502 OsiUnitTest::haltonerror = 1;
503 else if (onerror == "stop")
504 OsiUnitTest::haltonerror = 2;
505 else {
506 std::cerr << "-onerror must be specified with either 'continue', 'wait', or 'stop'" << std::endl;
507 return false;
508 }
509 }
510
511 return true;
512 }
513
514 std::string TestOutcome::SeverityLevelName[LAST] = {
515 "NOTE", "PASSED", "WARNING", "ERROR"
516 };
517
518 void TestOutcome::print() const
519 {
520 printf("%-10s", SeverityLevelName[severity].c_str());
521 printf("%-10s", component.c_str());
522 printf("%s", testname.c_str());
523 printf("\n");
524
525 if (expected)
526 printf(" (expected) ");
527 else
528 printf(" ");
529 printf("%s\n", testcond.c_str());
530
531 printf(" ");
532 printf("%s:%d\n", filename.c_str(), linenumber);
533
534 // printf("\n");
535 }
536
537 void TestOutcomes::add(const OsiSolverInterface &si, std::string tst, const char *cond, TestOutcome::SeverityLevel sev, const char *file, int line, bool exp)
538 {
539 std::string solverName;
540 si.getStrParam(OsiSolverName, solverName);
541 push_back(TestOutcome(solverName, tst, cond, sev, file, line, exp));
542 }
543
544 void TestOutcomes::print() const
545 {
546 int count[TestOutcome::LAST];
547 int expected[TestOutcome::LAST];
548 for (int i = 0; i < TestOutcome::LAST; ++i) {
549 count[i] = 0;
550 expected[i] = 0;
551 }
552 /*
553 Walk the list, counting entries at each severity level. Print if verbosity
554 level is high enough.
555 */
556 for (const_iterator it(begin()); it != end(); ++it) {
557 ++count[it->severity];
558 if (it->expected)
559 ++expected[it->severity];
560 if ((it->severity != TestOutcome::PASSED || OsiUnitTest::verbosity >= 2) && (it->severity != TestOutcome::NOTE || OsiUnitTest::verbosity >= 1))
561 it->print();
562 }
563 /// Print summary counts
564 for (int i = 0; i < TestOutcome::LAST; ++i)
565 printf("Severity %-10s: %4d thereof expected: %4d\n",
566 TestOutcome::SeverityLevelName[i].c_str(), count[i], expected[i]);
567 }
568
569 void TestOutcomes::getCountBySeverity(TestOutcome::SeverityLevel sev,
570 int &total, int &expected) const
571 {
572 assert(sev >= 0);
573 assert(sev < TestOutcome::LAST);
574
575 total = 0;
576 expected = 0;
577
578 for (const_iterator it(begin()); it != end(); ++it) {
579 if (it->severity != sev)
580 continue;
581 ++total;
582 if (it->expected)
583 ++expected;
584 }
585 }
586
587 } // end OsiUnitTest namespace
588
589 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
590 */