source: trunk/Cbc/examples/driver3.cpp

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

formatting

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