source: trunk/Cbc/examples/longthin.cpp @ 1464

Last change on this file since 1464 was 1464, checked in by stefan, 9 years ago

merge split branch into trunk; fix some examples

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