source: trunk/Cbc/examples/driver4.cpp @ 739

Last change on this file since 739 was 739, checked in by forrest, 12 years ago

add example

File size: 4.9 KB
Line 
1// Copyright (C) 2007, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7
8#include <cassert>
9#include <iomanip>
10
11
12#include "CbcModel.hpp"
13#include "OsiClpSolverInterface.hpp"
14#include "CbcCompareUser.hpp"
15
16#include  "CoinTime.hpp"
17
18//#############################################################################
19
20
21/************************************************************************
22
23This main program shows how to take advantage of the standalone cbc in your program,
24while still making major modifications.
25First it reads in an integer model from an mps file
26Then it initializes the integer model with cbc defaults
27Then it calls CbcMain1 passing all parameters apart from first but with callBack to modify stuff
28Finally it prints solution
29
30************************************************************************/
31/* Meaning of whereFrom:
32   1 after initial solve by dualsimplex etc
33   2 after preprocessing
34   3 just before branchAndBound (so user can override)
35   4 just after branchAndBound (before postprocessing)
36   5 after postprocessing
37*/
38/* Meaning of model status is as normal
39   status
40      -1 before branchAndBound
41      0 finished - check isProvenOptimal or isProvenInfeasible to see if solution found
42      (or check value of best solution)
43      1 stopped - on maxnodes, maxsols, maxtime
44      2 difficulties so run was abandoned
45      (5 event user programmed event occurred)
46
47      cbc secondary status of problem
48        -1 unset (status_ will also be -1)
49        0 search completed with solution
50        1 linear relaxation not feasible (or worse than cutoff)
51        2 stopped on gap
52        3 stopped on nodes
53        4 stopped on time
54        5 stopped on user event
55        6 stopped on solutions
56        7 linear relaxation unbounded
57
58   but initially check if status is 0 and secondary status is 1 -> infeasible
59   or you can check solver status.
60*/
61/* Return non-zero to return quickly */   
62static int callBack(CbcModel * model, int whereFrom)
63{
64  int returnCode=0;
65  switch (whereFrom) {
66  case 1:
67  case 2:
68    if (!model->status()&&model->secondaryStatus())
69      returnCode=1;
70    break;
71  case 3:
72    {
73      CbcCompareUser compare;
74      model->setNodeComparison(compare);
75    }
76    break;
77  case 4:
78    // If not good enough could skip postprocessing
79    break;
80  case 5:
81    break;
82  default:
83    abort();
84  }
85  return returnCode;
86}
87
88int main (int argc, const char *argv[])
89{
90
91  OsiClpSolverInterface solver1;
92  //#define USE_OSI_NAMES
93#ifdef USE_OSI_NAMES
94  // Say we are keeping names (a bit slower this way)
95  solver1.setIntParam(OsiNameDiscipline,1);
96#endif
97  // Read in model using argv[1]
98  // and assert that it is a clean model
99  std::string mpsFileName = "../../Data/Sample/p0033.mps";
100  if (argc>=2) mpsFileName = argv[1];
101  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
102  assert(numMpsReadErrors==0);
103  // Tell solver to return fast if presolve or initial solve infeasible
104  solver1.getModelPtr()->setMoreSpecialOptions(3);
105
106  // Pass to Cbc initialize defaults
107  CbcModel model(solver1);   
108  CbcMain0(model);
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,callBack);
115  } else {
116    const char * argv2[]={"driver3","-solve","-quit"};
117    CbcMain1(3,argv2,model,callBack);
118  }
119
120  // Solver was cloned so get current copy
121  OsiSolverInterface * solver = model.solver();
122  // Print solution if finished (could get from model.bestSolution() as well
123
124  if (model.bestSolution()) {
125   
126    const double * solution = solver->getColSolution();
127   
128    int iColumn;
129    int numberColumns = solver->getNumCols();
130    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
131   
132    std::cout<<"--------------------------------------"<<std::endl;
133#ifdef USE_OSI_NAMES
134   
135    for (iColumn=0;iColumn<numberColumns;iColumn++) {
136      double value=solution[iColumn];
137      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
138        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver->getColName(iColumn)
139                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
140    }
141#else
142    // names may not be in current solver - use original
143   
144    for (iColumn=0;iColumn<numberColumns;iColumn++) {
145      double value=solution[iColumn];
146      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
147        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()->columnName(iColumn)
148                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
149    }
150#endif
151    std::cout<<"--------------------------------------"<<std::endl;
152 
153    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
154  } else {
155    std::cout<<" No solution!"<<std::endl;
156  }
157  return 0;
158}   
Note: See TracBrowser for help on using the repository browser.