source: trunk/Cbc/examples/longthin.cpp

Last change on this file was 1898, checked in by stefan, 4 years ago

fixup examples

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