source: trunk/Samples/sample1.cpp @ 2

Last change on this file since 2 was 2, checked in by ladanyi, 16 years ago

Import of Coin Branch-and-Cut (formerly known as Sbb)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1// Copyright (C) 2002, 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
10// For Branch and bound
11#include "OsiSolverInterface.hpp"
12#include "CbcModel.hpp"
13
14#ifdef COIN_USE_CLP
15#include "OsiClpSolverInterface.hpp"
16#endif
17#include "ClpPresolve.hpp"
18#include "CbcCompareUser.hpp"
19#include "CglProbing.hpp"
20
21//#############################################################################
22
23#ifdef NDEBUG
24#undef NDEBUG
25#endif
26// Time
27
28#include  <time.h>
29#if !defined(_MSC_VER)
30#include <sys/times.h>
31#include <sys/resource.h>
32#include <unistd.h>
33#endif
34static double cpuTime()
35{
36  double cpu_temp;
37#if defined(_MSC_VER)
38  unsigned int ticksnow;        /* clock_t is same as int */
39 
40  ticksnow = (unsigned int)clock();
41 
42  cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
43#else
44  struct rusage usage;
45  getrusage(RUSAGE_SELF,&usage);
46  cpu_temp = usage.ru_utime.tv_sec;
47  cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
48#endif
49  return cpu_temp;
50}
51
52
53/************************************************************************
54
55This main program reads in an integer model from an mps file.
56
57It then sets up some Cgl cut generators and calls branch and cut.
58
59Branching is simple binary branching on integer variables.
60
61Node selection is depth first until first solution is found and then
62based on objective and number of unsatisfied integer variables.
63
64Variable branching selection is on maximum minimum-of-up-down change
65after strong branching on 5 variables closest to 0.5.
66
67A simple rounding heuristic is used.
68
69Any cut generators based on Cgl can be added in same way
70
71You may also wish to look at CbcModel.hpp
72
73
74************************************************************************/
75
76
77int main (int argc, const char *argv[])
78{
79
80  // Define your favorite OsiSolver
81 
82#ifdef COIN_USE_CLP
83  ClpSimplex simplex;
84#endif
85  double time0 = cpuTime();
86  double time1 = time0;
87  double time2;
88
89  // Read in model using argv[1]
90  // and assert that it is a clean model
91
92  assert(!simplex.readMps(argv[1],""));
93  time2 = cpuTime();
94  std::cout<<"Input took "<<time2-time1<<" seconds"<<std::endl;;
95  time1 = time2;
96  // Should work with OsiPresolve but not sure - so this is complicated
97  ClpPresolve pinfo;
98  ClpSimplex * simplex2 = pinfo.presolvedModel(simplex,1.0e-8);
99  time2 = cpuTime();
100  std::cout<<"Presolve took "<<time2-time1<<" seconds"<<std::endl;;
101  time1 = time2;
102  OsiClpSolverInterface solver1(simplex2);
103  solver1.writeMps("bad2");
104  // Do initial solve to continuous
105  solver1.initialSolve();
106  time2 = cpuTime();
107  std::cout<<"Continuous solve took "<<time2-time1<<" seconds"<<std::endl;;
108  time1 = time2;
109  solver1.messageHandler()->setLogLevel(0);
110  CbcModel model(solver1);
111  // Definition of node choice
112  CbcCompareUser compare;
113  model.setNodeComparison(compare);
114
115  // Maybe probing due to very large coefficients
116
117  CglProbing generator1;
118  generator1.setUsingObjective(true);
119  generator1.setMaxPass(3);
120  generator1.setMaxProbe(100);
121  generator1.setMaxLook(50);
122  generator1.setRowCuts(3);
123
124 
125  // Add in generators
126  // model.addCutGenerator(&generator1,-1,"Probing");
127  // Switch off strong branching if wanted
128  model.setNumberStrong(0);
129  model.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
130  //model.solver()->setHintParam(OsiDoScale,false,OsiHintTry);
131  // Switch off most output
132  if (model.getNumCols()<3000) {
133    model.messageHandler()->setLogLevel(1);
134    model.solver()->messageHandler()->setLogLevel(0);
135  } else {
136    model.messageHandler()->setLogLevel(2);
137    model.solver()->messageHandler()->setLogLevel(1);
138  }
139
140  // Do complete search
141 
142  model.branchAndBound();
143  time2 = cpuTime();
144  std::cout<<"Search took "<<time2-time1<<" seconds"<<std::endl;
145  // as we made such a mess of presolve lets be safe
146  OsiClpSolverInterface * clpSolver
147    = dynamic_cast<OsiClpSolverInterface *> (model.solver());
148  assert (clpSolver);
149  ClpSimplex * clp = clpSolver->getModelPtr();
150  *simplex2 = *clp;
151  pinfo.postsolve(true);
152  time1 = time2;
153  // Fix all integers
154  const int * original = pinfo.originalColumns();
155  double * lower2 = simplex2->columnLower();
156  double * upper2 = simplex2->columnUpper();
157  const char * info2 = simplex2->integerInformation();
158  double * lower = simplex.columnLower();
159  double * upper = simplex.columnUpper();
160  int i;
161  for (i=0;i<simplex2->numberColumns();i++) {
162    if (info2[i]) {
163      int iSeq = original[i];
164      upper[iSeq]=upper2[i];
165      lower[iSeq]=lower2[i];
166    }
167  }
168
169  simplex.primal();
170  time2 = cpuTime();
171  std::cout<<"Cleanup took "<<time2-time1<<" seconds"<<std::endl;;
172  std::cout<<"Total time "<<time2-time0<<" seconds"<<std::endl;;
173  return 0;
174}   
Note: See TracBrowser for help on using the repository browser.