source: trunk/Cbc/examples/pool.cpp @ 2496

Last change on this file since 2496 was 2469, checked in by unxusr, 7 months ago

formatting

File size: 3.4 KB
Line 
1// $Id: driver4.cpp 1898 2013-04-09 18:06:04Z stefan $
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 "CbcSolver.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,
21while still making major modifications.
22First it reads in an integer model from an mps file
23Then it initializes the integer model with cbc defaults
24Then it calls CbcMain1 passing all parameters apart from first but with callBack to modify stuff
25Finally it prints solution
26*/
27int main(int argc, const char *argv[])
28{
29
30  OsiClpSolverInterface solver1;
31  // Read in model using argv[1]
32  // and assert that it is a clean model
33  std::string mpsFileName;
34#if defined(SAMPLEDIR)
35  mpsFileName = SAMPLEDIR "/p0033.mps";
36#else
37  if (argc < 2) {
38    fprintf(stderr, "Do not know where to find sample MPS files.\n");
39    exit(1);
40  }
41#endif
42  if (argc >= 2)
43    mpsFileName = argv[1];
44  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
45  if (numMpsReadErrors != 0) {
46    printf("%d errors reading MPS file\n", numMpsReadErrors);
47    return numMpsReadErrors;
48  }
49  // Tell solver to return fast if presolve or initial solve infeasible
50  solver1.getModelPtr()->setMoreSpecialOptions(3);
51
52  // Pass to Cbc initialize defaults
53  CbcModel modelA(solver1);
54  CbcModel *model = &modelA;
55  CbcMain0(modelA);
56  modelA.setMaximumSavedSolutions(5);
57  /* Now go into code for standalone solver
58     Could copy arguments and add -quit at end to be safe
59     but this will do
60  */
61  if (argc > 2) {
62    CbcMain1(argc - 1, argv + 1, modelA);
63  } else {
64    const char *argv2[] = { "driver4", "-solve", "-quit" };
65    CbcMain1(3, argv2, modelA);
66  }
67  // Solver was cloned so get current copy
68  OsiSolverInterface *solver = model->solver();
69  // Print solution if finished (could get from model->bestSolution() as well
70
71  if (model->bestSolution()) {
72
73    int numberColumns = solver->getNumCols();
74    std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
75
76    // do solutions
77    for (int solutionNumber = 0; solutionNumber < modelA.numberSavedSolutions();
78         solutionNumber++) {
79      const double *solution = modelA.savedSolution(solutionNumber);
80      std::cout << "-------------------------------------- solution "
81                << solutionNumber + 1 << " objective " << modelA.savedSolutionObjective(solutionNumber)
82                << std::endl;
83      for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
84        double value = solution[iColumn];
85        if (fabs(value) > 1.0e-7 && solver1.isInteger(iColumn))
86          std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver1.getModelPtr()->columnName(iColumn)
87                    << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl;
88      }
89      std::cout << "--------------------------------------" << std::endl;
90    }
91    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
92  } else {
93    std::cout << " No solution!" << std::endl;
94  }
95  return 0;
96}
Note: See TracBrowser for help on using the repository browser.