source: trunk/Samples/longthin.cpp @ 83

Last change on this file since 83 was 83, checked in by forrest, 16 years ago

samples

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1// Copyright (C) 2005, 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 "OsiSolverInterface.hpp"
14#include "CbcModel.hpp"
15#include "CbcBranchActual.hpp"
16#include "CbcBranchUser.hpp"
17#include "CbcCompareUser.hpp"
18#include "CbcCutGenerator.hpp"
19#include "CbcHeuristicGreedy.hpp"
20#include "CbcSolver2.hpp"
21#include "CoinModel.hpp"
22
23// Cuts
24
25#include "CglProbing.hpp"
26
27#include  "CoinTime.hpp"
28
29/************************************************************************
30
31This main program reads in an integer model from an mps file.
32It expects it to be unit coefficients and unit rhs and long and thin
33
34Branching is simple binary branching on integer variables.
35
36*/
37int main (int argc, const char *argv[])
38{
39
40  // Define a Solver for long thin problems
41 
42  CbcSolver2 solver1;
43
44  // Read in model using argv[1]
45  // and assert that it is a clean model
46  std::string mpsFileName = "../../Mps/Sample/p0033.mps";
47  if (argc>=2) mpsFileName = argv[1];
48  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
49  assert(numMpsReadErrors==0);
50  double time1 = CoinCpuTime();
51
52  solver1.initialSolve();
53  // Reduce printout
54  solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry);
55
56  OsiSolverInterface * solver2=&solver1;
57  CbcModel model(*solver2);
58  // Point to solver
59  OsiSolverInterface * solver3 = model.solver();
60  CbcSolver2 * osiclp = dynamic_cast< CbcSolver2*> (solver3);
61  assert (osiclp);
62  osiclp->initialize(&model,NULL);
63  osiclp->setAlgorithm(2);
64  osiclp->setMemory(1000);
65  // Set up some cut generators and defaults
66  // Probing first as gets tight bounds on continuous
67
68  CglProbing generator1;
69  generator1.setUsingObjective(true);
70  generator1.setMaxPass(3);
71  // Number of unsatisfied variables to look at
72  generator1.setMaxProbe(10);
73  // How far to follow the consequences
74  generator1.setMaxLook(50);
75  // Only look at rows with fewer than this number of elements
76  generator1.setMaxElements(200);
77  generator1.setRowCuts(3);
78
79 
80  // Add in generators
81  // Experiment with -1 and -99 etc
82  model.addCutGenerator(&generator1,-99,"Probing");
83  // Allow rounding heuristic
84
85  CbcRounding heuristic1(model);
86  model.addHeuristic(&heuristic1);
87
88  // And Greedy heuristic
89
90  CbcGreedyCover heuristic2(model);
91  // Use original upper and perturb more
92  heuristic2.setAlgorithm(11);
93  model.addHeuristic(&heuristic2);
94
95  // Redundant definition of default branching (as Default == User)
96  CbcBranchUserDecision branch;
97  model.setBranchingMethod(&branch);
98
99  // Definition of node choice
100  CbcCompareUser compare;
101  model.setNodeComparison(compare);
102
103  int iColumn;
104  int numberColumns = solver3->getNumCols();
105  // do pseudo costs
106  CbcObject ** objects = new CbcObject * [numberColumns];
107  const CoinPackedMatrix * matrix = solver3->getMatrixByCol();
108  // Column copy
109  const int * columnLength = matrix->getVectorLengths();
110  const double * objective = model.getObjCoefficients();
111  int numberIntegers=0;
112  for (iColumn=0;iColumn<numberColumns;iColumn++) {
113    if (solver3->isInteger(iColumn)) {
114      double costPer = objective[iColumn]/ ((double) columnLength[iColumn]);
115      CbcSimpleIntegerPseudoCost * newObject =
116        new CbcSimpleIntegerPseudoCost(&model,numberIntegers,iColumn,
117                                       costPer,costPer);
118      newObject->setMethod(3);
119      objects[numberIntegers++]= newObject;
120    }
121  }
122  model.addObjects(numberIntegers,objects);
123  for (iColumn=0;iColumn<numberIntegers;iColumn++)
124    delete objects[iColumn];
125  delete [] objects;
126
127  // Do initial solve to continuous
128  model.initialSolve();
129
130  // Do more strong branching if small
131  // Switch off strong branching if wanted
132  model.setNumberStrong(5);
133
134  // say use resolve for strong branching
135  osiclp->setSpecialOptions(16);
136  // We had better allow a lot
137  model.solver()->setIntParam(OsiMaxNumIterationHotStart,10000);
138  // So use strategy to keep rows
139  osiclp->setStrategy(1);
140
141  // Switch off most output
142  if (model.getNumCols()<3000) {
143    model.messageHandler()->setLogLevel(1);
144    //model.solver()->messageHandler()->setLogLevel(0);
145  } else {
146    model.messageHandler()->setLogLevel(2);
147    model.solver()->messageHandler()->setLogLevel(1);
148  }
149  //model.setPrintFrequency(50);
150
151  // Do complete search
152  try {
153    model.branchAndBound();
154  }
155  catch (CoinError e) {
156    e.print();
157    if (e.lineNumber()>=0)
158      std::cout<<"This was from a CoinAssert"<<std::endl;
159    exit(0);
160  }
161  //void printHowMany();
162  //printHowMany();
163  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
164           <<model.getNodeCount()<<" nodes with objective "
165           <<model.getObjValue()
166           <<(!model.status() ? " Finished" : " Not finished")
167           <<std::endl;
168
169  // Print solution if finished - we can't get names from Osi!
170
171  if (model.getMinimizationObjValue()<1.0e50) {
172    int numberColumns = model.solver()->getNumCols();
173   
174    const double * solution = model.solver()->getColSolution();
175   
176    int iColumn;
177    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
178   
179    std::cout<<"--------------------------------------"<<std::endl;
180    for (iColumn=0;iColumn<numberColumns;iColumn++) {
181      double value=solution[iColumn];
182      if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
183        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
184    }
185    std::cout<<"--------------------------------------"<<std::endl;
186 
187    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
188  }
189  return 0;
190}   
Note: See TracBrowser for help on using the repository browser.