Debian Codegraph @

Osi/test/unitTest.cpp

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 // Test individual classes or groups of classes
4 // This file is licensed under the terms of Eclipse Public License (EPL).
5 // $Id$
6
7 #include "CoinPragma.hpp"
8 #include "OsiConfig.h"
9
10 #include <iostream>
11
12 #include "OsiUnitTests.hpp"
13 #include "OsiSolverInterface.hpp"
14 #include "OsiTestSolverInterface.hpp"
15
16 using namespace OsiUnitTest;
17
18 /*
19 Currently the Osi unit test is configured to exercise only the external
20 solvers. The Osi interfaces for Coin solvers have been moved out to the
21 project's repository and each has its own private Osi unit test.
22
23 This unit test will include as many external solvers as are available. If
24 none of them are available, the OsiTestSolver (currently a clone of Vol)
25 will be used. If any other solver is available, its presence will disable
26 use of the test solver. You can disable it manually by undefining
27 USETESTSOLVER.
28
29 You may want to use the Osi unit test to compare two or more Coin solvers.
30 In particular, OsiSolverInterfaceMpsUnitTest, which runs the Netlib problem
31 set, is set up for exactly this sort of comparison. To take advantage of
32 it, you'll need to edit this file and Makefile in order to get it to work.
33 */
•34 #ifdef COIN_HAS_SAMPLE
35 #define USETESTSOLVER
•36 #endif
37
38 /*
39 Some convenient undef's, to make it easy to isolate a particular solver.
40 Uncomment to disable a solver that's included in the build. Leave them
41 commented if you're happy with running the unitTest for all solvers in
42 the build.
43 */
44 // #undef COIN_HAS_XPR
45 // #undef COIN_HAS_CPX
46 // #undef COIN_HAS_GLPK
47 // #undef COIN_HAS_MSK
48 // #undef COIN_HAS_GRB
49 // #undef COIN_HAS_SOPLEX
50
51 #ifdef COIN_HAS_XPR
52 #include "OsiXprSolverInterface.hpp"
53 #ifdef USETESTSOLVER
54 #undef USETESTSOLVER
55 #endif
56 #endif
57
58 #ifdef COIN_HAS_CPX
59 #include "OsiCpxSolverInterface.hpp"
60 #undef USETESTSOLVER
61 #ifdef USETESTSOLVER
62 #undef USETESTSOLVER
63 #endif
64 #endif
65
66 #ifdef COIN_HAS_GLPK
67 #include "OsiGlpkSolverInterface.hpp"
68 #ifdef USETESTSOLVER
69 #undef USETESTSOLVER
70 #endif
71 #endif
72
73 #ifdef COIN_HAS_MSK
74 #include "OsiMskSolverInterface.hpp"
75 #ifdef USETESTSOLVER
76 #undef USETESTSOLVER
77 #endif
78 #endif
79
80 #ifdef COIN_HAS_GRB
81 #include "OsiGrbSolverInterface.hpp"
82 #ifdef USETESTSOLVER
83 #undef USETESTSOLVER
84 #endif
85 #endif
86
87 #ifdef COIN_HAS_SOPLEX
88 #include "OsiSpxSolverInterface.hpp"
89 #ifdef USETESTSOLVER
90 #undef USETESTSOLVER
91 #endif
92 #endif
93
94 #ifdef USETESTSOLVER
95 #include "OsiTestSolverInterface.hpp"
96 #endif
97
98
99 //----------------------------------------------------------------
100 // to see parameter list, call unitTest -usage
101 //----------------------------------------------------------------
102
103 int main (int argc, const char *argv[])
104 {
105 /*
106 Start off with various bits of initialisation that don't really belong
107 anywhere else.
108
109 First off, synchronise C++ stream i/o with C stdio. This makes debugging
110 output a bit more comprehensible. It still suffers from interleave of cout
111 (stdout) and cerr (stderr), but -nobuf deals with that.
112 */
113 std::ios::sync_with_stdio() ;
114 /*
115 Suppress an popup window that Windows shows in response to a crash. See
116 note at head of file.
117 */
118 WindowsErrorPopupBlocker();
119 /*
120 Might as well make use of this convenient Xpress feature.
121 */
122 #ifdef COIN_HAS_XPR
123 OsiXprSolverInterface::setLogFileName("xprCallTrace.txt");
124 #endif
125
126 /*
127 Process command line parameters.
128 */
129 std::map<std::string,std::string> parms ;
130 if (processParameters(argc,argv,parms) == false)
131 return 1;
132
133 std::string mpsDir = parms["-mpsDir"] ;
134 std::string netlibDir = parms["-netlibDir"] ;
135
136 /*
137 Test Osi{Row,Col}Cut routines.
138 */
139 #ifdef COIN_HAS_XPR
140 {
141 OsiXprSolverInterface xprSi;
142 testingMessage( "Testing OsiRowCut with OsiXprSolverInterface\n" );
143 OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&xprSi,mpsDir), {}, xprSi, "rowcut unittest");
144 }
145 {
146 OsiXprSolverInterface xprSi;
147 testingMessage( "Testing OsiColCut with OsiXprSolverInterface\n" );
148 OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&xprSi,mpsDir), {}, xprSi, "colcut unittest");
149 }
150 {
151 OsiXprSolverInterface xprSi;
152 testingMessage( "Testing OsiRowCutDebugger with OsiXprSolverInterface\n" );
153 OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&xprSi,mpsDir), {}, xprSi, "rowcut debugger unittest");
154 }
155 #endif
156
157 #ifdef COIN_HAS_CPX
158 {
159 OsiCpxSolverInterface cpxSi;
160 testingMessage( "Testing OsiRowCut with OsiCpxSolverInterface\n" );
161 OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&cpxSi,mpsDir), {}, cpxSi, "rowcut unittest");
162 }
163 {
164 OsiCpxSolverInterface cpxSi;
165 testingMessage( "Testing OsiColCut with OsiCpxSolverInterface\n" );
166 OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&cpxSi,mpsDir), {}, cpxSi, "colcut unittest");
167 }
168 {
169 OsiCpxSolverInterface cpxSi;
170 testingMessage( "Testing OsiRowCutDebugger with OsiCpxSolverInterface\n" );
171 OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&cpxSi,mpsDir), {}, cpxSi, "rowcut debugger unittest");
172 }
173 #endif
174
175 #ifdef USETESTSOLVER
176 {
177 OsiTestSolverInterface testSi;
178 testingMessage( "Testing OsiRowCut with OsiTestSolverInterface\n" );
179 OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&testSi,mpsDir), {}, testSi, "rowcut unittest");
180 }
181 {
182 OsiTestSolverInterface testSi;
183 testingMessage( "Testing OsiColCut with OsiTestSolverInterface\n" );
184 OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&testSi,mpsDir), {}, testSi, "colcut unittest");
185 }
186 #endif
187
188 #ifdef COIN_HAS_GLPK
189 {
190 OsiGlpkSolverInterface glpkSi;
191 testingMessage( "Testing OsiRowCut with OsiGlpkSolverInterface\n" );
192 OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&glpkSi,mpsDir), {}, glpkSi, "rowcut unittest");
193 }
194 {
195 OsiGlpkSolverInterface glpkSi;
196 testingMessage( "Testing OsiColCut with OsiGlpkSolverInterface\n" );
197 OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&glpkSi,mpsDir), {}, glpkSi, "colcut unittest");
198 }
199 {
200 OsiGlpkSolverInterface glpkSi;
201 testingMessage( "Testing OsiRowCutDebugger with OsiGlpkSolverInterface\n" );
202 OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&glpkSi,mpsDir), {}, glpkSi, "rowcut debugger unittest");
203 }
204 #endif
205
206 #ifdef COIN_HAS_MSK
207 {
208 OsiMskSolverInterface MskSi;
209 testingMessage( "Testing OsiRowCut with OsiMskSolverInterface\n" );
210 OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&MskSi,mpsDir), {}, MskSi, "rowcut unittest");
211 }
212 {
213 OsiMskSolverInterface MskSi;
214 testingMessage( "Testing OsiColCut with OsiMskSolverInterface\n" );
215 OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&MskSi,mpsDir), {}, MskSi, "colcut unittest");
216 }
217 {
218 OsiMskSolverInterface MskSi;
219 testingMessage( "Testing OsiRowCutDebugger with OsiMskSolverInterface\n" );
220 OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&MskSi,mpsDir), {}, MskSi, "rowcut debugger unittest");
221 }
222 #endif
223
224 #ifdef COIN_HAS_GRB
225 {
226 OsiGrbSolverInterface grbSi;
227 testingMessage( "Testing OsiRowCut with OsiGrbSolverInterface\n" );
228 OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&grbSi,mpsDir), {}, grbSi, "rowcut unittest");
229 }
230 {
231 OsiGrbSolverInterface grbSi;
232 testingMessage( "Testing OsiColCut with OsiGrbSolverInterface\n" );
233 OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&grbSi,mpsDir), {}, grbSi, "colcut unittest");
234 }
235 {
236 OsiGrbSolverInterface grbSi;
237 testingMessage( "Testing OsiRowCutDebugger with OsiGrbSolverInterface\n" );
238 OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&grbSi,mpsDir), {}, grbSi, "rowcut debugger unittest");
239 }
240 #endif
241
242 #ifdef COIN_HAS_SOPLEX
243 {
244 OsiSpxSolverInterface spxSi;
245 testingMessage( "Testing OsiRowCut with OsiSpxSolverInterface\n" );
246 OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&spxSi,mpsDir), {}, spxSi, "rowcut unittest");
247 }
248 {
249 OsiSpxSolverInterface spxSi;
250 testingMessage( "Testing OsiColCut with OsiSpxSolverInterface\n" );
251 OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&spxSi,mpsDir), {}, spxSi, "colcut unittest");
252 }
253 {
254 OsiSpxSolverInterface spxSi;
255 testingMessage( "Testing OsiRowCutDebugger with OsiSpxSolverInterface\n" );
256 OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&spxSi,mpsDir), {}, spxSi, "rowcut debugger unittest");
257 }
258 #endif
259
260 testingMessage( "Testing OsiCuts\n" );
261 OSIUNITTEST_CATCH_ERROR(OsiCutsUnitTest(), {}, "osi", "osicuts unittest");
262
263 /*
264 Testing OsiCuts only? A useful option when doing memory access and leak
265 checks. Keeps the run time to something reasonable.
266 */
267 if (parms.find("-cutsOnly") != parms.end()) {
268 testingMessage( "Stopping after OsiCuts tests.\n" );
269 return 0;
270 }
271
272 /*
273 Run the OsiXXX class test for each solver. It's up to the solver implementor
274 to decide whether or not to run OsiSolverInterfaceCommonUnitTest. Arguably
275 this should be required.
276 */
277 #ifdef COIN_HAS_XPR
278 testingMessage( "Testing OsiXprSolverInterface\n" );
279 OSIUNITTEST_CATCH_ERROR(OsiXprSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "xpress", "osixpr unittest");
280 #endif
281
282 #ifdef COIN_HAS_CPX
283 testingMessage( "Testing OsiCpxSolverInterface\n" );
284 OSIUNITTEST_CATCH_ERROR(OsiCpxSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "cplex", "osicpx unittest");
285 #endif
286
287 #ifdef USETESTSOLVER
288 testingMessage( "Testing OsiTestSolverInterface\n" );
289 OSIUNITTEST_CATCH_ERROR(OsiTestSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "vol", "ositestsolver unittest");
290 #endif
291
292 #ifdef COIN_HAS_GLPK
293 testingMessage( "Testing OsiGlpkSolverInterface\n" );
294 OSIUNITTEST_CATCH_ERROR(OsiGlpkSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "glpk", "osiglpk unittest");
295 #endif
296
297 #ifdef COIN_HAS_MSK
298 testingMessage( "Testing OsiMskSolverInterface\n" );
299 OSIUNITTEST_CATCH_ERROR(OsiMskSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "mosek", "osimsk unittest");
300 #endif
301
302 #ifdef COIN_HAS_GRB
303 testingMessage( "Testing OsiGrbSolverInterface\n" );
304 OSIUNITTEST_CATCH_ERROR(OsiGrbSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "gurobi", "osigrb unittest");
305 #endif
306
307 #ifdef COIN_HAS_SOPLEX
308 testingMessage( "Testing OsiSpxSolverInterface\n" );
309 OSIUNITTEST_CATCH_ERROR(OsiSpxSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "soplex", "osispx unittest");
310 #endif
311
312 /*
313 Each solver has run its specialised unit test. Check now to see if we need to
314 run through the Netlib problems.
315 */
316 if (parms.find("-testOsiSolverInterface") != parms.end())
317 {
318 // Create vector of solver interfaces
319 std::vector<OsiSolverInterface*> vecSi;
320 # if COIN_HAS_XPR
321 OsiSolverInterface * xprSi = new OsiXprSolverInterface;
322 vecSi.push_back(xprSi);
323 # endif
324 # if COIN_HAS_CPX
325 OsiSolverInterface * cpxSi = new OsiCpxSolverInterface;
326 vecSi.push_back(cpxSi);
327 # endif
328 # if COIN_HAS_GLPK
329 OsiSolverInterface * glpkSi = new OsiGlpkSolverInterface;
330 glpkSi->setHintParam(OsiDoPresolveInInitial,true,OsiHintTry) ;
331 glpkSi->setHintParam(OsiDoReducePrint,true,OsiHintDo) ;
332 vecSi.push_back(glpkSi);
333 # endif
334 # if COIN_HAS_MSK
335 OsiSolverInterface * MskSi = new OsiMskSolverInterface;
336 vecSi.push_back(MskSi);
337 # endif
338 # if COIN_HAS_GRB
339 OsiSolverInterface * grbSi = new OsiGrbSolverInterface;
340 vecSi.push_back(grbSi);
341 # endif
342 # if COIN_HAS_SOPLEX
343 OsiSolverInterface * spxSi = new OsiSpxSolverInterface;
344 vecSi.push_back(spxSi);
345 # endif
346 # ifdef USETESTSOLVER
347 /*
348 The test solver is normally Vol, which can't do any of the netlib problems.
349 So let's not try.
350 */
351 {
352 std::string solverName ;
353 OsiSolverInterface * testSi = new OsiTestSolverInterface;
354 testSi->getStrParam(OsiSolverName,solverName) ;
355 if (solverName != "vol")
356 { vecSi.push_back(testSi); }
357 else
358 { testingMessage("Test solver vol cannot do Netlib. Skipping.\n") ; }
359 }
360 # endif
361
362 if (vecSi.size() > 0)
363 { testingMessage( "Testing OsiSolverInterface on Netlib problems.\n" );
364 OSIUNITTEST_CATCH_ERROR(OsiSolverInterfaceMpsUnitTest(vecSi,netlibDir), {}, "osi", "netlib unittest");
365 }
366
367 unsigned int i;
368 for (i=0; i<vecSi.size(); i++)
369 delete vecSi[i];
370 }
371 else {
372 testingMessage( "***Skipped Testing of OsiSolverInterface on Netlib problems***\n" );
373 testingMessage( "***use -testOsiSolverInterface to run them.***\n" );
374 }
375
376 /*
377 We're done. Report on the results.
378 */
379 std::cout.flush();
380 outcomes.print();
381
382 int nerrors;
383 int nerrors_expected;
384 outcomes.getCountBySeverity(TestOutcome::ERROR, nerrors, nerrors_expected);
385
386 if (nerrors > nerrors_expected)
387 std::cerr << "Tests completed with " << nerrors - nerrors_expected << " unexpected errors." << std::endl ;
388 else
389 std::cerr << "All tests completed successfully\n";
390
391 return nerrors - nerrors_expected;
392 }