source: trunk/Cbc/src/unitTestClp.cpp @ 838

Last change on this file since 838 was 838, checked in by forrest, 13 years ago

for deterministic parallel

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include <cstdio>
5#include <string>
6#include <iostream>
7
8#include "CoinTime.hpp"
9#include "CbcModel.hpp"
10#include "CbcCutGenerator.hpp"
11#include "OsiClpSolverInterface.hpp"
12#include "OsiRowCutDebugger.hpp"
13
14//#############################################################################
15
16#ifdef NDEBUG
17#undef NDEBUG
18#endif
19
20//#############################################################################
21
22// Display message on stdout and stderr
23void testingMessage( const char * const msg )
24{
25  std::cout <<msg;
26  //cout <<endl <<"*****************************************"
27  //     <<endl <<msg <<endl;
28}
29
30//#############################################################################
31
32static inline bool CbcTestFile(const std::string name)
33{
34  FILE *fp = fopen(name.c_str(),"r");
35  if (fp) {
36    fclose(fp);
37    return true;
38  }
39  return false;
40}
41
42//#############################################################################
43
44bool CbcTestMpsFile(std::string& fname)
45{
46  if (CbcTestFile(fname)) {
47    return true;
48  }
49  if (CbcTestFile(fname+".mps")) {
50    fname += ".mps";
51    return true;
52  }
53  if (CbcTestFile(fname+".MPS")) {
54    fname += ".MPS";
55    return true;
56  }
57#ifdef COIN_HAS_ZLIB
58  if (CbcTestFile(fname+".gz")) {
59    return true;
60  }
61  if (CbcTestFile(fname+".mps.gz")) {
62    fname += ".mps";
63    return true;
64  }
65  if (CbcTestFile(fname+".MPS.gz")) {
66    fname += ".MPS";
67    return true;
68  }
69  if (CbcTestFile(fname+".MPS.GZ")) {
70    fname += ".MPS";
71    return true;
72  }
73#endif
74  return false;
75}
76
77//#############################################################################
78
79int CbcClpUnitTest (const CbcModel & saveModel, std::string& dirMiplib,
80                     bool unitTestOnly)
81{
82  unsigned int m ;
83
84  // Set directory containing miplib data files.
85  std::string test1 = dirMiplib +"p0033";
86  // See if files exist
87  bool doTest=CbcTestMpsFile(test1);
88
89  if (!doTest) {
90    printf("Not doing miplib run as can't find mps files - ? .gz without libz\n");
91    return -1;
92  }
93  /*
94    Vectors to hold test problem names and characteristics. The objective value
95    after optimization (objValue) must agree to the specified tolerance
96    (objValueTol).
97  */
98  std::vector<std::string> mpsName ;
99  std::vector<int> nRows ;
100  std::vector<int> nCols ;
101  std::vector<double> objValueC ;
102  std::vector<double> objValue ;
103  std::vector<int> strategy ;
104  /*
105    And a macro to make the vector creation marginally readable.
106  */
107#define PUSH_MPS(zz_mpsName_zz,\
108                 zz_nRows_zz,zz_nCols_zz,zz_objValue_zz,zz_objValueC_zz, \
109                 zz_strategy_zz) \
110  mpsName.push_back(zz_mpsName_zz) ; \
111  nRows.push_back(zz_nRows_zz) ; \
112  nCols.push_back(zz_nCols_zz) ; \
113  objValueC.push_back(zz_objValueC_zz) ; \
114  strategy.push_back(zz_strategy_zz) ; \
115  objValue.push_back(zz_objValue_zz) ;
116
117  if (unitTestOnly) {
118    PUSH_MPS("p0033",16,33,3089,2520.57,7);
119    PUSH_MPS("p0201",133,201,7615,6875.0,7);
120  } else {
121    /*
122      Load up the problem vector. Note that the row counts here include the
123      objective function.
124    */
125    // 0 for no test, 1 for some, 2 for many, 3 for all
126#define HOWMANY 2
127#if HOWMANY
128#if HOWMANY>1
129    PUSH_MPS("gen",780,870,112313,112130.0,7);
130    //PUSH_MPS("blend2",274,353,7.598985,6.9156751140,7);
131    PUSH_MPS("10teams",230,2025,924,917,7);
132#endif
133    PUSH_MPS("air03",124,10757,340160,338864.25,7);
134#if HOWMANY>2
135    PUSH_MPS("air04",823,8904,56137,55535.436,8);
136    PUSH_MPS("air05",426,7195,26374,25877.609,8);
137#endif
138  //    PUSH_MPS("arki001",1048,1388,7580813.0459,7579599.80787,7);
139    PUSH_MPS("bell3a",123,133,878430.32,862578.64,7);
140#if HOWMANY>1
141    PUSH_MPS("bell5",91,104,8966406.49,8608417.95,7);
142#endif
143    PUSH_MPS("blend2",274,353,7.598985,6.9156751140,7);
144#if HOWMANY>1
145    PUSH_MPS("cap6000",2176,6000,-2451377,-2451537.325,7);
146#endif
147    //    PUSH_MPS("dano3mip",3202,13873,728.1111,576.23162474,7);
148    //PUSH_MPS("danoint",664,521,65.67,62.637280418,7);
149    PUSH_MPS("dcmulti",290,548,188182,183975.5397,7);
150    PUSH_MPS("dsbmip",1182,1886,-305.19817501,-305.19817501,7);
151    PUSH_MPS("egout",98,141,568.101,149.589,7);
152    PUSH_MPS("enigma",21,100,0.0,0.0,7);
153#if HOWMANY>3
154    PUSH_MPS("fast0507",507,63009,174,172.14556668,7);
155#endif
156    PUSH_MPS("fiber",363,1298,405935.18000,156082.51759,7);
157#if HOWMANY>1
158    PUSH_MPS("fixnet6",478,878,3983,1200.88,7);
159#endif
160    PUSH_MPS("flugpl",18,18,1201500,1167185.7,7);
161    PUSH_MPS("gen",780,870,112313,112130.0,7);
162#if HOWMANY>1
163    PUSH_MPS("gesa2",1392,1224,25779856.372,25476489.678,7);
164    PUSH_MPS("gesa2_o",1248,1224,25779856.372,25476489.678,7);
165#endif
166    PUSH_MPS("gesa3",1368,1152,27991042.648,27833632.451,7);
167    PUSH_MPS("gesa3_o",1224,1152,27991042.648,27833632.451,7);
168    PUSH_MPS("gt2",29,188,21166.000,13460.233074,7);
169#if HOWMANY>2
170    PUSH_MPS("harp2",112,2993,-73899798.00,-74353341.502,7);
171#endif
172    PUSH_MPS("khb05250",101,1350,106940226,95919464.0,7);
173#if HOWMANY>1
174    PUSH_MPS("l152lav",97,1989,4722,4656.36,7);
175#endif
176    PUSH_MPS("lseu",28,89,1120,834.68,7);
177    PUSH_MPS("misc03",96,160,3360,1910.,7);
178    PUSH_MPS("misc06",820,1808,12850.8607,12841.6,7);
179#if HOWMANY>1
180    PUSH_MPS("misc07",212,260,2810,1415.0,7);
181    PUSH_MPS("mitre",2054,10724,115155,114740.5184,7);
182#endif
183    PUSH_MPS("mod008",6,319,307,290.9,7);
184    PUSH_MPS("mod010",146,2655,6548,6532.08,7);
185#if HOWMANY>2
186    PUSH_MPS("mod011",4480,10958,-54558535,-62121982.55,7);
187    PUSH_MPS("modglob",291,422,20740508,20430947.,7);
188#endif
189#if HOWMANY>3
190    PUSH_MPS("noswot",182,128,-43,-43.0,7);
191#endif
192#if HOWMANY>1
193    PUSH_MPS("nw04",36,87482,16862,16310.66667,7);
194#endif
195    PUSH_MPS("p0033",16,33,3089,2520.57,7);
196    PUSH_MPS("p0201",133,201,7615,6875.0,7);
197    PUSH_MPS("p0282",241,282,258411,176867.50,7);
198    PUSH_MPS("p0548",176,548,8691,315.29,7);
199    PUSH_MPS("p2756",755,2756,3124,2688.75,7);
200#if HOWMANY>2
201    PUSH_MPS("pk1",45,86,11.0,0.0,7);
202#endif
203#if HOWMANY>1
204    PUSH_MPS("pp08a",136,240,7350.0,2748.3452381,7);
205    PUSH_MPS("pp08aCUTS",246,240,7350.0,5480.6061563,7);
206#endif
207#if HOWMANY>2
208    PUSH_MPS("qiu",1192,840,-132.873137,-931.638857,7);
209#endif
210    PUSH_MPS("qnet1",503,1541,16029.692681,14274.102667,7);
211    PUSH_MPS("qnet1_o",456,1541,16029.692681,12095.571667,7);
212    PUSH_MPS("rentacar",6803,9557,30356761,28806137.644,7);
213    PUSH_MPS("rgn",24,180,82.1999,48.7999,7);
214#if HOWMANY>3
215    PUSH_MPS("rout",291,556,1077.56,981.86428571,7);
216    PUSH_MPS("set1ch",492,712,54537.75,32007.73,7);
217#endif
218    //    PUSH_MPS("seymour",4944,1372,423,403.84647413,7);
219    PUSH_MPS("stein27",118,27,18,13.0,7);
220#if HOWMANY>1
221    PUSH_MPS("stein45",331,45,30,22.0,7);
222#endif
223    PUSH_MPS("vpm1",234,378,20,15.4167,7);
224    PUSH_MPS("vpm2",234,378,13.75,9.8892645972,7);
225#endif
226  }
227#undef PUSH_MPS
228   
229  int numProbSolved = 0;
230  double timeTaken=0.0;
231  int numberFailures=0;
232 
233  /*
234    Open the main loop to step through the MPS problems.
235  */
236  for (m = 0 ; m < mpsName.size() ; m++) {
237    std::cout << "  processing mps file: " << mpsName[m] 
238              << " (" << m+1 << " out of " << mpsName.size() << ")\n";
239    /*
240      Stage 1: Read the MPS
241      and make sure the size of the constraint matrix is correct.
242    */
243    CbcModel * model = new CbcModel(saveModel);
244     
245    std::string fn = dirMiplib+mpsName[m] ;
246    if (!CbcTestMpsFile(fn)) {
247      std::cout << "ERROR: Cannot find MPS file " << fn << "\n";
248      continue;
249    }
250    model->solver()->readMps(fn.c_str(),"") ;
251    assert(model->getNumRows() == nRows[m]) ;
252    assert(model->getNumCols() == nCols[m]) ;
253
254    /*
255      Stage 2: Call solver to solve the problem.
256      then check the return code and objective.
257    */
258
259    double startTime = CoinCpuTime()+CoinCpuTimeJustChildren();
260    if (model->getMaximumNodes()>200000) {
261      model->setMaximumNodes(200000);
262    }
263    OsiClpSolverInterface * si =
264      dynamic_cast<OsiClpSolverInterface *>(model->solver()) ;
265    assert (si != NULL);
266    // get clp itself
267    ClpSimplex * modelC = si->getModelPtr();
268    modelC->tightenPrimalBounds();
269    model->initialSolve();
270    if (modelC->dualBound()==1.0e10) {
271      // user did not set - so modify
272      // get largest scaled away from bound
273      double largest=1.0e-12;
274      int numberRows = modelC->numberRows();
275      const double * rowPrimal = modelC->primalRowSolution();
276      const double * rowLower = modelC->rowLower();
277      const double * rowUpper = modelC->rowUpper();
278      const double * rowScale = modelC->rowScale();
279      int iRow;
280      for (iRow=0;iRow<numberRows;iRow++) {
281        double value = rowPrimal[iRow];
282        double above = value-rowLower[iRow];
283        double below = rowUpper[iRow]-value;
284        if (rowScale) {
285          double multiplier = rowScale[iRow];
286          above *= multiplier;
287          below *= multiplier;
288        }
289        if (above<1.0e12) {
290          largest = CoinMax(largest,above);
291        }
292        if (below<1.0e12) {
293          largest = CoinMax(largest,below);
294        }
295      }
296     
297      int numberColumns = modelC->numberColumns();
298      const double * columnPrimal = modelC->primalColumnSolution();
299      const double * columnLower = modelC->columnLower();
300      const double * columnUpper = modelC->columnUpper();
301      const double * columnScale = modelC->columnScale();
302      int iColumn;
303      for (iColumn=0;iColumn<numberColumns;iColumn++) {
304        double value = columnPrimal[iColumn];
305        double above = value-columnLower[iColumn];
306        double below = columnUpper[iColumn]-value;
307        if (columnScale) {
308          double multiplier = 1.0/columnScale[iColumn];
309          above *= multiplier;
310          below *= multiplier;
311        }
312        if (above<1.0e12) {
313          largest = CoinMax(largest,above);
314        }
315        if (below<1.0e12) {
316          largest = CoinMax(largest,below);
317        }
318      }
319      //std::cout<<"Largest (scaled) away from bound "<<largest<<std::endl;
320      modelC->setDualBound(CoinMax(1.0001e8,
321                                   CoinMin(1000.0*largest,1.00001e10)));
322    }
323    model->setMinimumDrop(min(5.0e-2,
324                              fabs(model->getMinimizationObjValue())*1.0e-3+1.0e-4));
325    if (model->getNumCols()<500) {
326      model->setMaximumCutPassesAtRoot(-100); // always do 100 if possible
327    } else if (model->getNumCols()<5000) {
328      model->setMaximumCutPassesAtRoot(100); // use minimum drop
329    } else {
330      model->setMaximumCutPassesAtRoot(20);
331    }
332    // If defaults then increase trust for small models
333    if (model->numberStrong()==5&&model->numberBeforeTrust()==10) {
334      int numberColumns = model->getNumCols();
335      if (numberColumns<=50) {
336        model->setNumberBeforeTrust(1000);
337      } else if (numberColumns<=100) {
338        model->setNumberBeforeTrust(100);
339      } else if (numberColumns<=300) {
340        model->setNumberBeforeTrust(50);
341      }
342    }
343    if (model->getNumCols()==-353) {
344      // blend2
345      std::string problemName ;
346      model->solver()->getStrParam(OsiProbName,problemName) ;
347      model->solver()->activateRowCutDebugger(problemName.c_str()) ;
348    }
349    model->branchAndBound();
350     
351    double timeOfSolution = CoinCpuTime()+CoinCpuTimeJustChildren()-startTime;
352    // Print more statistics
353    std::cout<<"Cuts at root node changed objective from "<<model->getContinuousObjective()
354             <<" to "<<model->rootObjectiveAfterCuts()<<std::endl;
355    int numberGenerators = model->numberCutGenerators();
356    for (int iGenerator=0;iGenerator<numberGenerators;iGenerator++) {
357      CbcCutGenerator * generator = model->cutGenerator(iGenerator);
358      std::cout<<generator->cutGeneratorName()<<" was tried "
359               <<generator->numberTimesEntered()<<" times and created "
360               <<generator->numberCutsInTotal()<<" cuts of which "
361               <<generator->numberCutsActive()<<" were active after adding rounds of cuts";
362      if (generator->timing())
363        std::cout<<" ( "<<generator->timeInCutGenerator()<<" seconds)"<<std::endl;
364      else
365        std::cout<<std::endl;
366    }
367    if (!model->status()) { 
368      double soln = model->getObjValue();       
369      CoinRelFltEq eq(1.0e-3) ;
370      if (eq(soln,objValue[m])) { 
371        std::cout
372          <<"cbc_clp"<<" "
373          << soln << " = " << objValue[m] << " ; okay";
374        numProbSolved++;
375      } else  { 
376        std::cout <<"cbc_clp" <<" " <<soln << " != " <<objValue[m]
377                  << "; error=" << fabs(objValue[m] - soln); 
378        numberFailures++;
379        abort();
380      }
381    } else {
382      std::cout << "error; too many nodes" ;
383    }
384    std::cout<<" - took " <<timeOfSolution<<" seconds.("<<
385      model->getNodeCount()<<" / "<<model->getIterationCount()<<
386      " )"<<std::endl;
387    timeTaken += timeOfSolution;
388    delete model;
389  }
390  int returnCode=0;
391  std::cout
392    <<"cbc_clp" 
393    <<" solved " 
394    <<numProbSolved
395    <<" out of "
396    <<objValue.size();
397  int numberOnNodes = objValue.size()-numProbSolved-numberFailures;
398  if (numberFailures||numberOnNodes) {
399    if (numberOnNodes) {
400      std::cout<<" ("<<numberOnNodes<<" stopped on nodes)";
401      returnCode = numberOnNodes;
402    }
403    if (numberFailures) {
404      std::cout<<" ("<<numberFailures<<" gave bad answer!)";
405      returnCode += 100*numberFailures;
406    }
407  }
408  std::cout<<" and took "
409    <<timeTaken
410    <<" seconds."
411    <<std::endl;
412  return returnCode;
413}
Note: See TracBrowser for help on using the repository browser.