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

Last change on this file since 857 was 857, checked in by forrest, 11 years ago

allow dense solver

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