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

Last change on this file since 1574 was 1574, checked in by lou, 8 years ago

Change to EPL license notice.

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