File: sample1.cpp

package info (click to toggle)
coinor-cbc 2.9.9%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (180 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// $Id: sample1.cpp 1898 2013-04-09 18:06:04Z stefan $
// Copyright (C) 2002, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#include <cassert>

#include "CbcConfig.h"
#include "CoinPragma.hpp"

// For Branch and bound
#include "OsiSolverInterface.hpp"
#include "CbcModel.hpp"

#include "OsiClpSolverInterface.hpp"
#include "ClpPresolve.hpp"
#include "CbcCompareUser.hpp"
#include "CglProbing.hpp"

//#############################################################################

#ifdef NDEBUG
#undef NDEBUG
#endif
// Time

#include  <time.h>
#if !defined(_MSC_VER)
#include <sys/times.h>
#include <sys/resource.h>
#include <unistd.h>
#endif
static double cpuTime()
{
  double cpu_temp;
#if defined(_MSC_VER)
  unsigned int ticksnow;        /* clock_t is same as int */
  
  ticksnow = (unsigned int)clock();
  
  cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
#else
  struct rusage usage;
  getrusage(RUSAGE_SELF,&usage);
  cpu_temp = (double)usage.ru_utime.tv_sec;
  cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
#endif
  return cpu_temp;
}


/************************************************************************

This main program reads in an integer model from an mps file.

It then sets up some Cgl cut generators and calls branch and cut.

Branching is simple binary branching on integer variables.

Node selection is depth first until first solution is found and then
based on objective and number of unsatisfied integer variables.

Variable branching selection is on maximum minimum-of-up-down change
after strong branching on 5 variables closest to 0.5.

A simple rounding heuristic is used.

Any cut generators based on Cgl can be added in same way

You may also wish to look at CbcModel.hpp


************************************************************************/


int main (int argc, const char *argv[])
{

  // Define your favorite OsiSolver
  
  ClpSimplex simplex;
  double time0 = cpuTime();
  double time1 = time0;
  double time2;

  // Read in model using argv[1]
  // and assert that it is a clean model

  if (argc <= 1) {
     printf("using %s <modelfile>\n", argv[0]);
     return 1;
  }
  int numMpsReadErrors = simplex.readMps(argv[1],"");
  if( numMpsReadErrors != 0 )
  {
     printf("%d errors reading MPS file\n", numMpsReadErrors);
     return numMpsReadErrors;
  }
  time2 = cpuTime();
  std::cout<<"Input took "<<time2-time1<<" seconds"<<std::endl;;
  time1 = time2;
  // Should work with OsiPresolve but not sure - so this is complicated
  ClpPresolve pinfo;
  ClpSimplex * simplex2 = pinfo.presolvedModel(simplex,1.0e-8);
  time2 = cpuTime();
  std::cout<<"Presolve took "<<time2-time1<<" seconds"<<std::endl;;
  time1 = time2;
  OsiClpSolverInterface solver1(simplex2);
  solver1.writeMps("bad2");
  // Do initial solve to continuous
  solver1.initialSolve();
  time2 = cpuTime();
  std::cout<<"Continuous solve took "<<time2-time1<<" seconds"<<std::endl;;
  time1 = time2;
  solver1.messageHandler()->setLogLevel(0);
  CbcModel model(solver1);
  // Definition of node choice
  CbcCompareUser compare;
  model.setNodeComparison(compare);

  // Maybe probing due to very large coefficients

  CglProbing generator1;
  generator1.setUsingObjective(true);
  generator1.setMaxPass(3);
  generator1.setMaxProbe(100);
  generator1.setMaxLook(50);
  generator1.setRowCuts(3);

  
  // Add in generators
  // model.addCutGenerator(&generator1,-1,"Probing");
  // Switch off strong branching if wanted
  model.setNumberStrong(0);
  model.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
  //model.solver()->setHintParam(OsiDoScale,false,OsiHintTry);
  // Switch off most output
  if (model.getNumCols()<3000) {
    model.messageHandler()->setLogLevel(1);
    model.solver()->messageHandler()->setLogLevel(0);
  } else {
    model.messageHandler()->setLogLevel(2);
    model.solver()->messageHandler()->setLogLevel(1);
  }

  // Do complete search
  
  model.branchAndBound();
  time2 = cpuTime();
  std::cout<<"Search took "<<time2-time1<<" seconds"<<std::endl;
  // as we made such a mess of presolve lets be safe
  OsiClpSolverInterface * clpSolver 
    = dynamic_cast<OsiClpSolverInterface *> (model.solver());
  assert (clpSolver);
  ClpSimplex * clp = clpSolver->getModelPtr();
  *simplex2 = *clp;
  pinfo.postsolve(true);
  time1 = time2;
  // Fix all integers
  const int * original = pinfo.originalColumns();
  double * lower2 = simplex2->columnLower();
  double * upper2 = simplex2->columnUpper();
  const char * info2 = simplex2->integerInformation();
  double * lower = simplex.columnLower();
  double * upper = simplex.columnUpper();
  int i;
  for (i=0;i<simplex2->numberColumns();i++) {
    if (info2[i]) {
      int iSeq = original[i];
      upper[iSeq]=upper2[i];
      lower[iSeq]=lower2[i];
    }
  }

  simplex.primal();
  time2 = cpuTime();
  std::cout<<"Cleanup took "<<time2-time1<<" seconds"<<std::endl;;
  std::cout<<"Total time "<<time2-time0<<" seconds"<<std::endl;;
  return 0;
}