1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others.  All Rights Reserved.
3 // This code is licensed under the terms of the Eclipse Public License (EPL).
4 
5 #include "CoinPragma.hpp"
6 
7 #include "OsiUnitTests.hpp"
8 
9 #include "OsiRowCutDebugger.hpp"
10 
11 //--------------------------------------------------------------------------
12 // test cut debugger methods.
OsiRowCutDebuggerUnitTest(const OsiSolverInterface * baseSiP,const std::string & mpsDir)13 void OsiRowCutDebuggerUnitTest(const OsiSolverInterface *baseSiP, const std::string &mpsDir)
14 {
15 
16   CoinRelFltEq eq;
17 
18   // Test default constructor
19   {
20     OsiRowCutDebugger r;
21     OSIUNITTEST_ASSERT_ERROR(r.integerVariable_ == NULL, {}, "osirowcutdebugger", "default constructor");
22     OSIUNITTEST_ASSERT_ERROR(r.knownSolution_ == NULL, {}, "osirowcutdebugger", "default constructor");
23     OSIUNITTEST_ASSERT_ERROR(r.numberColumns_ == 0, {}, "osirowcutdebugger", "default constructor");
24   }
25 
26   {
27     // Get non trivial instance
28     OsiSolverInterface *imP = baseSiP->clone();
29     std::string fn = mpsDir + "exmip1";
30     imP->readMps(fn.c_str(), "mps");
31     OSIUNITTEST_ASSERT_ERROR(imP->getNumRows() == 5, {}, "osirowcutdebugger", "read exmip1");
32 
33     /*
34       Activate the debugger. The garbled name here is deliberate; the
35       debugger should isolate the portion of the string between '/' and
36       '.' (in normal use, this would be the base file name, stripped of
37       the prefix and extension).
38     */
39     imP->activateRowCutDebugger("ab cd /x/ /exmip1.asc");
40 
41     int i;
42 
43     // return debugger
44     const OsiRowCutDebugger *debugger = imP->getRowCutDebugger();
45     OSIUNITTEST_ASSERT_ERROR(debugger != NULL, {}, "osirowcutdebugger", "return debugger");
46     OSIUNITTEST_ASSERT_ERROR(debugger->numberColumns_ == 8, {}, "osirowcutdebugger", "return debugger");
47 
48     const bool type[] = { 0, 0, 1, 1, 0, 0, 0, 0 };
49     const double values[] = { 2.5, 0, 1, 1, 0.5, 3, 0, 0.26315789473684253 };
50     CoinPackedVector objCoefs(8, imP->getObjCoefficients());
51 
52     bool type_ok = true;
53 #if 0
54     for (i=0;i<8;i++)
55       type_ok &= type[i] == debugger->integerVariable_[i];
56     OSIUNITTEST_ASSERT_ERROR(type_ok, {}, "osirowcutdebugger", "???");
57 #endif
58 
59     double objValue = objCoefs.dotProduct(values);
60     double debuggerObjValue = objCoefs.dotProduct(debugger->knownSolution_);
61     OSIUNITTEST_ASSERT_ERROR(eq(objValue, debuggerObjValue), {}, "osirowcutdebugger", "objective value");
62 
63     OsiRowCutDebugger rhs;
64     {
65       OsiRowCutDebugger rC1(*debugger);
66 
67       OSIUNITTEST_ASSERT_ERROR(rC1.numberColumns_ == 8, {}, "osirowcutdebugger", "copy constructor");
68       type_ok = true;
69       for (i = 0; i < 8; i++)
70         type_ok &= type[i] == rC1.integerVariable_[i];
71       OSIUNITTEST_ASSERT_ERROR(type_ok, {}, "osirowcutdebugger", "copy constructor");
72       OSIUNITTEST_ASSERT_ERROR(eq(objValue, objCoefs.dotProduct(rC1.knownSolution_)), {}, "osirowcutdebugger", "copy constructor");
73 
74       rhs = rC1;
75       OSIUNITTEST_ASSERT_ERROR(rhs.numberColumns_ == 8, {}, "osirowcutdebugger", "assignment operator");
76       type_ok = true;
77       for (i = 0; i < 8; i++)
78         type_ok &= type[i] == rhs.integerVariable_[i];
79       OSIUNITTEST_ASSERT_ERROR(type_ok, {}, "osirowcutdebugger", "assignment operator");
80       OSIUNITTEST_ASSERT_ERROR(eq(objValue, objCoefs.dotProduct(rhs.knownSolution_)), {}, "osirowcutdebugger", "assignment operator");
81     }
82     // Test that rhs has correct values even though lhs has gone out of scope
83     OSIUNITTEST_ASSERT_ERROR(rhs.numberColumns_ == 8, {}, "osirowcutdebugger", "assignment operator");
84     type_ok = true;
85     for (i = 0; i < 8; i++)
86       type_ok &= type[i] == rhs.integerVariable_[i];
87     OSIUNITTEST_ASSERT_ERROR(type_ok, {}, "osirowcutdebugger", "assignment operator");
88     OSIUNITTEST_ASSERT_ERROR(eq(objValue, objCoefs.dotProduct(rhs.knownSolution_)), {}, "osirowcutdebugger", "assignment operator");
89 
90     OsiRowCut cut[2];
91 
92     const int ne = 3;
93     int inx[ne] = { 0, 2, 3 };
94     double el[ne] = { 1., 1., 1. };
95     cut[0].setRow(ne, inx, el);
96     cut[0].setUb(5.);
97 
98     el[1] = 5;
99     cut[1].setRow(ne, inx, el);
100     cut[1].setUb(5);
101     OsiCuts cs;
102     cs.insert(cut[0]);
103     cs.insert(cut[1]);
104     OSIUNITTEST_ASSERT_ERROR(!debugger->invalidCut(cut[0]), {}, "osirowcutdebugger", "recognize (in)valid cut");
105     OSIUNITTEST_ASSERT_ERROR(debugger->invalidCut(cut[1]), {}, "osirowcutdebugger", "recognize (in)valid cut");
106     OSIUNITTEST_ASSERT_ERROR(debugger->validateCuts(cs, 0, 2) == 1, {}, "osirowcutdebugger", "recognize (in)valid cut");
107     OSIUNITTEST_ASSERT_ERROR(debugger->validateCuts(cs, 0, 1) == 0, {}, "osirowcutdebugger", "recognize (in)valid cut");
108     delete imP;
109   }
110 }
111 
112 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
113 */
114