source: trunk/Cbc/examples/driver3.cpp

Last change on this file was 2518, checked in by stefan, 6 weeks ago

add missing callback argument to CbcMain1

  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1// $Id: driver3.cpp 2518 2019-03-06 20:48:02Z forrest $
2// Copyright (C) 2007, 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#include "CbcModel.hpp"
11#include "OsiClpSolverInterface.hpp"
12#include "CbcBranchDynamic.hpp"
13#include "CbcSolver.hpp"
14
15#include "CoinTime.hpp"
16
17//#############################################################################
18
19/************************************************************************
20
21This main program shows how to take advantage of the standalone cbc in your program.
22It should perform very nearly the same as cbc 
23First it reads in an integer model from an mps file and saves and strips off integer information.
24Then it initializes the integer model with cbc defaults
25Then it puts back integers - here you could do anything and also set parameters
26Then it calls CbcMain1 passing all parameters apart from first
27Finally it prints solution
28
29************************************************************************/
30
31static int dummyCallBack(CbcModel * /*model*/, int /*whereFrom*/)
32{
33  return 0;
34}
35
36int main(int argc, const char *argv[])
37{
38
39  OsiClpSolverInterface solver1;
40  //#define USE_OSI_NAMES
41#ifdef USE_OSI_NAMES
42  // Say we are keeping names (a bit slower this way)
43  solver1.setIntParam(OsiNameDiscipline, 1);
44#endif
45  // Read in model using argv[1]
46  // and assert that it is a clean model
47  std::string mpsFileName;
48#if defined(SAMPLEDIR)
49  mpsFileName = SAMPLEDIR "/p0033.mps";
50#else
51  if (argc < 2) {
52    fprintf(stderr, "Do not know where to find sample MPS files.\n");
53    exit(1);
54  }
55#endif
56  if (argc >= 2)
57    mpsFileName = argv[1];
58  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
59  if (numMpsReadErrors != 0) {
60    printf("%d errors reading MPS file\n", numMpsReadErrors);
61    return numMpsReadErrors;
62  }
63
64  // Strip off integer information and save
65  int numberColumns = solver1.getNumCols();
66  char *integer = new char[numberColumns];
67  int i;
68  for (i = 0; i < numberColumns; i++) {
69    if (solver1.isInteger(i)) {
70      integer[i] = 1;
71      solver1.setContinuous(i);
72    } else {
73      integer[i] = 0;
74    }
75  }
76  // Pass to Cbc initialize defaults
77  CbcSolverUsefulData cbcData;
78  CbcModel model(solver1);
79  CbcMain0(model, cbcData);
80
81  // Solve just to show there are no integers
82  model.branchAndBound();
83  // Set cutoff etc back in model and solver
84  model.resetModel();
85  // Solver was cloned so get it
86  OsiSolverInterface *solver = model.solver();
87  // Put back integers.  Here the user could do anything really
88#define ADD_DIRECTLY
89#ifndef ADD_DIRECTLY
90  for (i = 0; i < numberColumns; i++) {
91    if (integer[i])
92      solver->setInteger(i);
93  }
94#else
95  CbcObject **objects = new CbcObject *[numberColumns];
96  int n = 0;
97  for (i = 0; i < numberColumns; i++) {
98    if (integer[i]) {
99      CbcSimpleIntegerDynamicPseudoCost *newObject = new CbcSimpleIntegerDynamicPseudoCost(&model, i);
100      objects[n++] = newObject;
101    }
102  }
103  model.addObjects(n, objects);
104  for (i = 0; i < n; i++)
105    delete objects[i];
106  delete[] objects;
107#endif
108  delete[] integer;
109  /* Now go into code for standalone solver
110     Could copy arguments and add -quit at end to be safe
111     but this will do
112  */
113  if (argc > 2) {
114    CbcMain1(argc - 1, argv + 1, model, dummyCallBack, cbcData);
115  } else {
116    const char *argv2[] = { "driver3", "-solve", "-quit" };
117    CbcMain1(3, argv2, model, dummyCallBack, cbcData);
118  }
119
120  // Print solution if finished (could get from model.bestSolution() as well
121
122  if (solver->getObjValue() * solver->getObjSense() < 1.0e50) {
123
124    const double *solution = solver->getColSolution();
125
126    int iColumn;
127    std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
128
129    std::cout << "--------------------------------------" << std::endl;
130#ifdef USE_OSI_NAMES
131
132    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
133      double value = solution[iColumn];
134      if (fabs(value) > 1.0e-7 && solver->isInteger(iColumn))
135        std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver->getColName(iColumn)
136                  << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
137    }
138#else
139    // names may not be in current solver - use original
140
141    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
142      double value = solution[iColumn];
143      if (fabs(value) > 1.0e-7 && solver->isInteger(iColumn))
144        std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver1.getModelPtr()->columnName(iColumn)
145                  << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
146    }
147#endif
148    std::cout << "--------------------------------------" << std::endl;
149
150    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
151  }
152  return 0;
153}
Note: See TracBrowser for help on using the repository browser.