source: trunk/Test/unitTest.cpp @ 107

Last change on this file since 107 was 107, checked in by forrest, 15 years ago

OsiCbc? conditional

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3// Test individual classes or groups of classes
4
5#if defined(_MSC_VER)
6// Turn off compiler warning about long names
7#  pragma warning(disable:4786)
8#endif
9
10#include <cstdlib>
11#include <cassert>
12#include <vector>
13#include <iostream>
14#include <cstdio>
15
16#ifdef COIN_USE_CBC
17#include "OsiCbcSolverInterface.hpp"
18#else
19//#define OsiCbcSolverInterface OsiClpSolverInterface
20#endif
21#ifdef COIN_USE_OSL
22#include "OsiOslSolverInterface.hpp"
23#endif
24#ifdef COIN_USE_SPX
25#include "OsiSpxSolverInterface.hpp"
26#endif
27#ifdef COIN_USE_DYLP
28#include "OsiDylpSolverInterface.hpp"
29#endif
30#ifdef COIN_USE_GLPK
31#include "OsiGlpkSolverInterface.hpp"
32#endif
33#ifdef COIN_USE_CLP
34#include "OsiClpSolverInterface.hpp"
35#endif
36#ifdef NDEBUG
37#undef NDEBUG
38#endif
39
40#include "CoinTime.hpp"
41// Function Prototypes. Function definitions is in this file.
42void testingMessage( const char * const msg );
43
44#ifdef COIN_USE_CBC
45void CbcUnitTest (const std::vector<OsiCbcSolverInterface*> & vecEmptySiP,
46                  const std::string & mpsDir)
47{
48  int i ;
49  unsigned int m ;
50  // See if files exist
51  FILE * fp;
52  bool doTest=false;
53  std::string test1 = mpsDir +"p0033";
54  fp=fopen(test1.c_str(),"r");
55  if (fp) {
56    doTest=true;
57    fclose(fp);
58  }
59#ifdef COIN_USE_ZLIB
60  test1 += ".gz";
61  fp=fopen(test1.c_str(),"r");
62  if (fp) {
63    doTest=true;
64    fclose(fp);
65  }
66#endif
67  if (!doTest)
68    return;
69  /*
70    Vectors to hold test problem names and characteristics. The objective value
71    after optimization (objValue) must agree to the specified tolerance
72    (objValueTol).
73  */
74  std::vector<std::string> mpsName ;
75  std::vector<int> nRows ;
76  std::vector<int> nCols ;
77  std::vector<double> objValueC ;
78  std::vector<double> objValue ;
79  std::vector<int> strategy ;
80  /*
81    And a macro to make the vector creation marginally readable.
82  */
83#define PUSH_MPS(zz_mpsName_zz,\
84                 zz_nRows_zz,zz_nCols_zz,zz_objValue_zz,zz_objValueC_zz, \
85                 zz_strategy_zz) \
86  mpsName.push_back(zz_mpsName_zz) ; \
87  nRows.push_back(zz_nRows_zz) ; \
88  nCols.push_back(zz_nCols_zz) ; \
89  objValueC.push_back(zz_objValueC_zz) ; \
90  strategy.push_back(zz_strategy_zz) ; \
91  objValue.push_back(zz_objValue_zz) ;
92 
93  /*
94    Load up the problem vector. Note that the row counts here include the
95    objective function.
96   
97  */
98  // 0 for no test, 1 for some, 2 for many, 3 for all
99#define HOWMANY 1
100#if HOWMANY
101#if HOWMANY>1
102  PUSH_MPS("10teams",230,2025,924,917,7)
103#endif
104    PUSH_MPS("air03",124,10757,340160,338864.25,7)
105#if HOWMANY==3
106    PUSH_MPS("air04",823,8904,56137,55535.436,8)
107    PUSH_MPS("air05",426,7195,26374,25877.609,8)
108#endif
109    //    PUSH_MPS("arki001",1048,1388,7580813.0459,7579599.80787,7)
110    PUSH_MPS("bell3a",123,133,878430.32,862578.64,7)
111#if HOWMANY>1
112    PUSH_MPS("bell5",91,104,8966406.49,8608417.95,7)
113#endif
114    PUSH_MPS("blend2",274,353,7.598985,6.9156751140,7)
115#if HOWMANY>1
116    PUSH_MPS("cap6000",2176,6000,-2451377,-2451537.325,7)
117#endif
118    //    PUSH_MPS("dano3mip",3202,13873,728.1111,576.23162474,7)
119    //PUSH_MPS("danoint",664,521,65.67,62.637280418,7)
120    PUSH_MPS("dcmulti",290,548,188182,183975.5397,7)
121    PUSH_MPS("dsbmip",1182,1886,-305.19817501,-305.19817501,7)
122    PUSH_MPS("egout",98,141,568.101,149.589,7)
123    PUSH_MPS("enigma",21,100,0.0,0.0,7)
124#if HOWMANY==3
125    PUSH_MPS("fast0507",507,63009,174,172.14556668,7)
126#endif
127    PUSH_MPS("fiber",363,1298,405935.18000,156082.51759,7)
128#if HOWMANY>1
129    PUSH_MPS("fixnet6",478,878,3983,1200.88,7)
130#endif
131    PUSH_MPS("flugpl",18,18,1201500,1167185.7,7)
132    PUSH_MPS("gen",780,870,112313,112130.0,7)
133#if HOWMANY>1
134    PUSH_MPS("gesa2",1392,1224,25779856.372,25476489.678,7)
135    PUSH_MPS("gesa2_o",1248,1224,25779856.372,25476489.678,7)
136#endif
137    PUSH_MPS("gesa3",1368,1152,27991042.648,27833632.451,7)
138    PUSH_MPS("gesa3_o",1224,1152,27991042.648,27833632.451,7)
139    PUSH_MPS("gt2",29,188,21166.000,13460.233074,7)
140#if HOWMANY==3
141    PUSH_MPS("harp2",112,2993,-73899798.00,-74353341.502,7)
142#endif
143    PUSH_MPS("khb05250",101,1350,106940226,95919464.0,7)
144#if HOWMANY>1
145    PUSH_MPS("l152lav",97,1989,4722,4656.36,7)
146#endif
147    PUSH_MPS("lseu",28,89,1120,834.68,7)
148    PUSH_MPS("misc03",96,160,3360,1910.,7)
149    PUSH_MPS("misc06",820,1808,12850.8607,12841.6,7)
150#if HOWMANY>1
151    PUSH_MPS("misc07",212,260,2810,1415.0,7)
152    PUSH_MPS("mitre",2054,10724,115155,114740.5184,7)
153#endif
154    PUSH_MPS("mod008",6,319,307,290.9,7)
155    PUSH_MPS("mod010",146,2655,6548,6532.08,7)
156#if HOWMANY==3
157    PUSH_MPS("mod011",4480,10958,-54558535,-62121982.55,7)
158    PUSH_MPS("modglob",291,422,20740508,20430947.,7)
159    PUSH_MPS("noswot",182,128,-43,-43.0,7)
160#endif
161#if HOWMANY>1
162    PUSH_MPS("nw04",36,87482,16862,16310.66667,7)
163#endif
164    PUSH_MPS("p0033",16,33,3089,2520.57,7)
165    PUSH_MPS("p0201",133,201,7615,6875.0,7)
166    PUSH_MPS("p0282",241,282,258411,176867.50,7)
167    PUSH_MPS("p0548",176,548,8691,315.29,7)
168    PUSH_MPS("p2756",755,2756,3124,2688.75,7)
169#if HOWMANY==3
170    PUSH_MPS("pk1",45,86,11.0,0.0,7)
171#endif
172#if HOWMANY>1
173    PUSH_MPS("pp08a",136,240,7350.0,2748.3452381,7)
174    PUSH_MPS("pp08aCUTS",246,240,7350.0,5480.6061563,7)
175#endif
176#if HOWMANY==3
177    PUSH_MPS("qiu",1192,840,-132.873137,-931.638857,7)
178#endif
179    PUSH_MPS("qnet1",503,1541,16029.692681,14274.102667,7)
180    PUSH_MPS("qnet1_o",456,1541,16029.692681,12095.571667,7)
181    PUSH_MPS("rentacar",6803,9557,30356761,28806137.644,7)
182    PUSH_MPS("rgn",24,180,82.1999,48.7999,7)
183#if HOWMANY==3
184    PUSH_MPS("rout",291,556,1077.56,981.86428571,7)
185    PUSH_MPS("set1ch",492,712,54537.75,32007.73,7)
186#endif
187    //    PUSH_MPS("seymour",4944,1372,423,403.84647413,7)
188    PUSH_MPS("stein27",118,27,18,13.0,7)
189#if HOWMANY>1
190    PUSH_MPS("stein45",331,45,30,22.0,7)
191#endif
192    PUSH_MPS("vpm1",234,378,20,15.4167,7)
193      PUSH_MPS("vpm2",234,378,13.75,9.8892645972,7)
194#endif
195#undef PUSH_MPS
196   
197    /*
198      Create a vector of solver interfaces that we can use to run the test
199      problems. The strategy is to create a fresh clone of the `empty' solvers
200      from vecEmptySiP for each problem, then proceed in stages: read the MPS
201      file, solve the problem, check the solution. If there are multiple
202      solvers in vecSiP, the results of each solver are compared with its
203      neighbors in the vector.
204    */
205    int numberSolvers=vecEmptySiP.size();
206  std::vector<OsiSolverInterface*> vecSiP(numberSolvers) ;
207
208  // Create vector to store a name for each solver interface
209  // and a count on the number of problems the solver intface solved.
210  std::vector<std::string> siName;
211  std::vector<int> numProbSolved;
212  std::vector<double> timeTaken;
213  for ( i=0; i<numberSolvers; i++ ) {
214    std::string name;
215    vecEmptySiP[i]->getStrParam(OsiSolverName,name);
216    siName.push_back(name);
217    numProbSolved.push_back(0);
218    timeTaken.push_back(0.0);
219  }
220 
221  /*
222    Open the main loop to step through the MPS problems.
223  */
224  for (m = 0 ; m < mpsName.size() ; m++)
225    { std::cerr << "  processing mps file: " << mpsName[m] 
226                << " (" << m+1 << " out of " << mpsName.size() << ")" << std::endl ;
227    /*
228      Stage 1: Read the MPS file into each solver interface.
229
230      Fill vecSiP with fresh clones of the solvers and read in the MPS file. As
231      a basic check, make sure the size of the constraint matrix is correct.
232    */
233    for (i = vecSiP.size()-1 ; i >= 0 ; --i)
234      { vecSiP[i] = vecEmptySiP[i]->clone() ;
235     
236      std::string fn = mpsDir+mpsName[m] ;
237      vecSiP[i]->readMps(fn.c_str(),"") ;
238     
239      vecSiP[i]->setObjSense(1.0) ;
240     
241      int nr = vecSiP[i]->getNumRows() ;
242      int nc = vecSiP[i]->getNumCols() ;
243      assert(nr == nRows[m]) ;
244      assert(nc == nCols[m]) ;
245    /*
246      Stage 2: Call each solver to solve the problem.
247     
248      We call each solver, then check the return code and objective.
249     
250    */
251
252      double startTime = CoinCpuTime();
253      OsiCbcSolverInterface * integerSolver =
254        dynamic_cast<OsiCbcSolverInterface *>(vecSiP[i]) ;
255      assert (integerSolver);
256      integerSolver->setMaximumNodes(50000);
257      integerSolver->branchAndBound();
258     
259      double timeOfSolution = CoinCpuTime()-startTime;
260      if (!integerSolver->status()) { 
261        double soln = integerSolver->getObjValue();       
262        CoinRelFltEq eq(1.0e-3) ;
263        if (eq(soln,objValue[m])) { 
264          std::cerr
265            <<siName[i]<<" "
266            << soln << " = " << objValue[m] << " ; okay";
267          numProbSolved[i]++;
268        } else  { 
269          std::cerr <<siName[i] <<" " <<soln << " != " <<objValue[m] << "; error=" ;
270          std::cerr <<fabs(objValue[m] - soln); 
271        }
272      } else {
273        std::cerr << "error; too many nodes" ;
274      }
275      std::cerr<<" - took " <<timeOfSolution<<" seconds."<<std::endl; 
276      timeTaken[i] += timeOfSolution;
277      delete integerSolver;
278    }
279  }
280
281  const int siName_size = siName.size();
282  for ( i=0; i<siName_size; i++ ) {
283    std::cerr
284      <<siName[i] 
285      <<" solved " 
286      <<numProbSolved[i]
287      <<" out of "
288      <<objValue.size()
289      <<" and took "
290      <<timeTaken[i]
291      <<" seconds."
292      <<std::endl;
293  } 
294}
295#endif
296//----------------------------------------------------------------
297// unitTest  [-miplibDir=V2]
298//
299// where:
300//   -miplibDir: directory containing miplib files
301//       Default value V2="./Samples/miplib3"
302//
303// All parameters are optional.
304//----------------------------------------------------------------
305
306int mainTest (int argc, const char *argv[])
307{
308  int i;
309
310
311  // define valid parameter keywords
312  std::set<std::string> definedKeyWords;
313  definedKeyWords.insert("-miplibDir");
314
315  // Create a map of parameter keys and associated data
316  std::map<std::string,std::string> parms;
317  for ( i=1; i<argc; i++ ) {
318    std::string parm(argv[i]);
319    std::string key,value;
320    unsigned int  eqPos = parm.find('=');
321
322    // Does parm contain and '='
323    if ( eqPos==std::string::npos ) {
324      //Parm does not contain '='
325      key = parm;
326    }
327    else {
328      key=parm.substr(0,eqPos);
329      value=parm.substr(eqPos+1);
330    }
331
332    // Is specifed key valid?
333    if ( definedKeyWords.find(key) == definedKeyWords.end() ) {
334      // invalid key word.
335      // Write help text
336      std::cerr <<"Undefined parameter \"" <<key <<"\".\n";
337      std::cerr <<"Correct usage: \n";
338      std::cerr <<"  unitTest [-miplibDir=V2] \n";
339      std::cerr <<"  where:\n";
340      std::cerr <<"    -miplibDir: directory containing miplib files\n";
341      std::cerr <<"        Default value V2=\"./Samples/miplib3\"\n";
342      return 1;
343    }
344    parms[key]=value;
345  }
346 
347  const char dirsep =  CoinFindDirSeparator();
348 
349  // Set directory containing miplib data files.
350  std::string miplibDir;
351  if (parms.find("-miplibDir") != parms.end())
352    miplibDir=parms["-miplibDir"] + dirsep;
353  else 
354    miplibDir = dirsep == '/' ? "./Samples/miplib3/" : ".\\Samples\\miplib3\\";
355#ifdef COIN_USE_CBC
356
357  {
358    // Create vector of solver interfaces
359    std::vector<OsiCbcSolverInterface*> vecSi;
360    CbcStrategyDefault strategy(false);
361#   if COIN_USE_OSL
362    OsiSolverInterface * oslSi = new OsiOslSolverInterface;
363    vecSi.push_back(new OsiCbcSolverInterface(oslSi,&strategy));
364#endif
365#   if COIN_USE_SPX
366    OsiSolverInterface * spxSi = new OsiSpxSolverInterface;
367    vecSi.push_back(new OsiCbcSolverInterface(spxSi,&strategy));
368#endif
369#   if COIN_USE_CLP
370    OsiSolverInterface * clpSi = new OsiClpSolverInterface;
371    vecSi.push_back(new OsiCbcSolverInterface(clpSi,&strategy));
372#endif
373#   if COIN_USE_DYLP
374    OsiSolverInterface * dylpSi = new OsiDylpSolverInterface;
375    vecSi.push_back(new OsiCbcSolverInterface(dylpSi,&strategy));
376#endif
377#   if COIN_USE_GLPK
378    OsiSolverInterface * glpkSi = new OsiGlpkSolverInterface;
379    vecSi.push_back(new OsiCbcSolverInterface(glpkSi,&strategy));
380#endif
381
382    testingMessage( "Testing some miplib stuff\n" );
383    CbcUnitTest(vecSi,miplibDir);
384
385    unsigned int i;
386    for (i=0; i<vecSi.size(); i++)
387      delete vecSi[i];
388  }
389#endif
390  testingMessage( "All tests completed successfully\n" );
391  return 0;
392}
393
394 
395// Display message on stdout and stderr
396void testingMessage( const char * const msg )
397{
398  std::cerr <<msg;
399  //cout <<endl <<"*****************************************"
400  //     <<endl <<msg <<endl;
401}
402
Note: See TracBrowser for help on using the repository browser.