source: trunk/Cbc/examples/pool.cpp

Last change on this file was 1947, checked in by forrest, 4 years ago

add multiple solution example

File size: 3.3 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
10#include "CoinPragma.hpp"
11#include "CbcModel.hpp"
12#include "OsiClpSolverInterface.hpp"
13#include "CbcSolver.hpp"
14
15#include "CoinTime.hpp"
16
17//#############################################################################
18
19
20/************************************************************************
21
22This main program shows how to take advantage of the standalone cbc in your program,
23while still making major modifications.
24First it reads in an integer model from an mps file
25Then it initializes the integer model with cbc defaults
26Then it calls CbcMain1 passing all parameters apart from first but with callBack to modify stuff
27Finally it prints solution
28*/
29int main (int argc, const char *argv[])
30{
31
32  OsiClpSolverInterface solver1;
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) mpsFileName = argv[1];
45  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
46  if( numMpsReadErrors != 0 )
47  {
48     printf("%d errors reading MPS file\n", numMpsReadErrors);
49     return numMpsReadErrors;
50  }
51  // Tell solver to return fast if presolve or initial solve infeasible
52  solver1.getModelPtr()->setMoreSpecialOptions(3);
53
54  // Pass to Cbc initialize defaults
55  CbcModel modelA(solver1);
56  CbcModel * model = &modelA;
57  CbcMain0(modelA);
58  modelA.setMaximumSavedSolutions(5);
59  /* Now go into code for standalone solver
60     Could copy arguments and add -quit at end to be safe
61     but this will do
62  */
63  if (argc>2) {
64    CbcMain1(argc-1,argv+1,modelA);
65  } else {
66    const char * argv2[]={"driver4","-solve","-quit"};
67    CbcMain1(3,argv2,modelA);
68  }
69  // Solver was cloned so get current copy
70  OsiSolverInterface * solver = model->solver();
71  // Print solution if finished (could get from model->bestSolution() as well
72
73  if (model->bestSolution()) {
74   
75    int numberColumns = solver->getNumCols();
76    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
77   
78    // do solutions
79    for (int solutionNumber=0;solutionNumber<modelA.numberSavedSolutions();
80         solutionNumber++) {
81      const double * solution = modelA.savedSolution(solutionNumber);
82      std::cout<<"-------------------------------------- solution "
83               <<solutionNumber+1<<" objective "<<modelA.savedSolutionObjective(solutionNumber)
84               <<std::endl;
85      for (int iColumn=0;iColumn<numberColumns;iColumn++) {
86        double value=solution[iColumn];
87        if (fabs(value)>1.0e-7&&solver1.isInteger(iColumn)) 
88          std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()->columnName(iColumn)
89                   <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
90      }
91      std::cout<<"--------------------------------------"<<std::endl;
92    }
93    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
94  } else {
95    std::cout<<" No solution!"<<std::endl;
96  }
97  return 0;
98}   
Note: See TracBrowser for help on using the repository browser.