source: trunk/Cbc/examples/crew.cpp

Last change on this file was 2469, checked in by unxusr, 2 weeks ago

formatting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1// $Id: crew.cpp 2469 2019-01-06 23:17:46Z 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#include "CoinPragma.hpp"
10// For Branch and bound
11#include "OsiClpSolverInterface.hpp"
12#include "CbcModel.hpp"
13#include "CbcBranchActual.hpp"
14#include "CbcCompareUser.hpp"
15
16#include "CoinTime.hpp"
17
18/************************************************************************
19
20This main program reads in an integer model from an mps file.
21It expects it to be unit coefficients and unit rhs.
22
23Branching is follow-on branching plus simple binary branching on integer variables.
24
25*/
26int main(int argc, const char *argv[])
27{
28
29  // Define a Solver for long thin problems
30
31  OsiClpSolverInterface solver1;
32
33  // Read in model using argv[1]
34  // and assert that it is a clean model
35  std::string mpsFileName;
36#if defined(SAMPLEDIR)
37  mpsFileName = SAMPLEDIR "/p0033.mps";
38#else
39  if (argc < 2) {
40    fprintf(stderr, "Do not know where to find sample MPS files.\n");
41    exit(1);
42  }
43#endif
44  if (argc >= 2)
45    mpsFileName = argv[1];
46  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
47  if (numMpsReadErrors != 0) {
48    printf("%d errors reading MPS file\n", numMpsReadErrors);
49    return numMpsReadErrors;
50  }
51  double time1 = CoinCpuTime();
52
53  solver1.initialSolve();
54  // Reduce printout
55  solver1.setHintParam(OsiDoReducePrint, true, OsiHintTry);
56
57  OsiSolverInterface *solver2 = &solver1;
58  CbcModel model(*solver2);
59  // Point to solver
60  OsiSolverInterface *solver3 = model.solver();
61  assert(dynamic_cast< OsiClpSolverInterface * >(solver3));
62
63  // Definition of node choice
64  CbcCompareUser compare;
65  model.setNodeComparison(compare);
66
67  int iColumn;
68  int numberColumns = solver3->getNumCols();
69  /* We are going to add a single follow on object but we
70     want to give low priority to existing integers.
71     As the default priority is 1000 we don't actually need to give
72     integer priorities but it is here to show how.
73  */
74  // Normal integer priorities
75  int *priority = new int[numberColumns];
76  int numberIntegers = 0;
77  for (iColumn = 0; iColumn < numberColumns; iColumn++) {
78    if (solver3->isInteger(iColumn)) {
79      priority[numberIntegers++] = 100; // low priority
80    }
81  }
82  /* Second parameter is true if we are adding objects,
83     false if integers.  So this does integers */
84  model.passInPriorities(priority, false);
85  delete[] priority;
86  /* Add in objects before we can give priority.
87     In this case just one - but this shows general method
88  */
89  CbcObject **objects = new CbcObject *[1];
90  objects[0] = new CbcFollowOn(&model);
91  model.addObjects(1, objects);
92  delete objects[0];
93  delete[] objects;
94  // High priority
95  int followPriority = 1;
96  model.passInPriorities(&followPriority, true);
97
98  // Do initial solve to continuous
99  model.initialSolve();
100
101  // Do more strong branching if small
102  // Switch off strong branching if wanted
103  model.setNumberStrong(0);
104
105  model.solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
106
107  // Switch off most output
108  if (model.getNumCols() < 3000) {
109    model.messageHandler()->setLogLevel(1);
110    //model.solver()->messageHandler()->setLogLevel(0);
111  } else {
112    model.messageHandler()->setLogLevel(2);
113    model.solver()->messageHandler()->setLogLevel(1);
114  }
115  //model.setPrintFrequency(50);
116
117  // Do complete search
118  try {
119    model.branchAndBound();
120  } catch (CoinError e) {
121    e.print();
122    if (e.lineNumber() >= 0)
123      std::cout << "This was from a CoinAssert" << std::endl;
124    exit(0);
125  }
126  //void printHowMany();
127  //printHowMany();
128  std::cout << mpsFileName << " took " << CoinCpuTime() - time1 << " seconds, "
129            << model.getNodeCount() << " nodes with objective "
130            << model.getObjValue()
131            << (!model.status() ? " Finished" : " Not finished")
132            << std::endl;
133
134  // Print solution if finished - we can't get names from Osi!
135
136  if (model.getMinimizationObjValue() < 1.0e50) {
137    int numberColumns = model.solver()->getNumCols();
138
139    const double *solution = model.solver()->getColSolution();
140
141    int iColumn;
142    std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
143
144    std::cout << "--------------------------------------" << std::endl;
145    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
146      double value = solution[iColumn];
147      if (fabs(value) > 1.0e-7 && model.solver()->isInteger(iColumn))
148        std::cout << std::setw(6) << iColumn << " " << value << std::endl;
149    }
150    std::cout << "--------------------------------------" << std::endl;
151
152    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
153  }
154  return 0;
155}
Note: See TracBrowser for help on using the repository browser.