source: trunk/Cbc/examples/sample1.cpp @ 1660

Last change on this file since 1660 was 1660, checked in by stefan, 8 years ago

change to new way of using and installing configuration header files

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