source: branches/devel/Cbc/examples/strategy.cpp @ 582

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

for playing with strategy

File size: 4.4 KB
Line 
1// Copyright (C) 2007, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7
8#include <cassert>
9#include <iomanip>
10
11
12// For Branch and bound
13#include "OsiClpSolverInterface.hpp"
14#include "CbcModel.hpp"
15#include "CbcStrategy.hpp"
16#include "CbcCutGenerator.hpp"
17#include "CoinTime.hpp"
18
19//#############################################################################
20
21
22/************************************************************************
23
24This main program reads in an integer model from an mps file.
25
26It then sets up a strategy and solves a problem
27
28Sensible users may wish to look at CbcStrategy.hpp to see what the default means and then tweak it
29using - say a subset of miplib to tune. 
30
31CbcStrategyDefault was there as an example - the more adventurous may wish to look at
32
33strategy2.cpp and try adding in other possibilities
34************************************************************************/
35
36int main (int argc, const char *argv[])
37{
38
39  // Define your favorite OsiSolver
40 
41  OsiClpSolverInterface solver1;
42
43  // Read in model using argv[1]
44  // and assert that it is a clean model
45  std::string mpsFileName = "../../Data/Sample/p0033.mps";
46  if (argc>=2) mpsFileName = argv[1];
47  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
48  assert(numMpsReadErrors==0);
49  double time1 = CoinCpuTime();
50
51  solver1.initialSolve();
52  // Reduce printout
53  solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry);
54  // See if we want preprocessing
55  OsiSolverInterface * solver2=&solver1;
56  CbcModel model(solver1);
57  // Stop after 20 minutes
58  int minutes=20;
59  std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl;
60  model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes);
61  ////////////////////////////////////////////////////////////
62
63  CbcStrategyDefault strategy;
64  // Default strategy will leave cut generators as they exist already
65  // so cutsOnlyAtRoot (1) true
66  // numberStrong (2) is 5 (default)
67  // numberBeforeTrust (3) is 0 (default)
68  // printLevel (4) defaults (0)
69  // So same as CbcStrategyDefault strategy(true,5,0,0);
70  // Set up pre-processing if wanted
71  //strategy.setupPreProcessing(1);
72  model.setStrategy(strategy);
73
74  ////////////////////////////////////////////////////////////
75  // Do complete search
76 
77  model.branchAndBound();
78
79  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
80           <<model.getNodeCount()<<" nodes with objective "
81           <<model.getObjValue()
82           <<(!model.status() ? " Finished" : " Not finished")
83           <<std::endl;
84
85  // Print more statistics
86  std::cout<<"Cuts at root node changed objective from "<<model.getContinuousObjective()
87           <<" to "<<model.rootObjectiveAfterCuts()<<std::endl;
88
89  for (int iGenerator=0;iGenerator<model.numberCutGenerators();iGenerator++) {
90    CbcCutGenerator * generator = model.cutGenerator(iGenerator);
91    std::cout<<generator->cutGeneratorName()<<" was tried "
92             <<generator->numberTimesEntered()<<" times and created "
93             <<generator->numberCutsInTotal()<<" cuts of which "
94             <<generator->numberCutsActive()<<" were active after adding rounds of cuts";
95    if (generator->timing())
96      std::cout<<" ( "<<generator->timeInCutGenerator()<<" seconds)"<<std::endl;
97    else
98      std::cout<<std::endl;
99  }
100  // Print solution if finished - we can't get names from Osi! - so get from OsiClp
101
102  if (model.getMinimizationObjValue()<1.0e50) {
103    OsiSolverInterface * solver = model.solver();
104    int numberColumns = solver->getNumCols();
105   
106    const double * solution = solver->getColSolution();
107
108    // Get names from solver1 (as OsiSolverInterface may lose)
109    std::vector<std::string> columnNames = *solver1.getModelPtr()->columnNames();
110   
111    int iColumn;
112    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
113   
114    std::cout<<"--------------------------------------"<<std::endl;
115    for (iColumn=0;iColumn<numberColumns;iColumn++) {
116      double value=solution[iColumn];
117      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
118        std::cout<<std::setw(6)<<iColumn<<" "
119                 <<columnNames[iColumn]<<" "
120                 <<value<<std::endl;
121    }
122    std::cout<<"--------------------------------------"<<std::endl;
123 
124    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
125  }
126  return 0;
127}   
Note: See TracBrowser for help on using the repository browser.