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

Last change on this file since 1565 was 1468, checked in by stefan, 9 years ago

do not require CbcConfig?.h in example to decide whether sample or miplib3 is present - do this in makefile

File size: 8.8 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#include "CbcSolver.hpp"
16
17#include  "CoinTime.hpp"
18
19//#############################################################################
20
21
22/************************************************************************
23
24This main program shows how to take advantage of the standalone cbc in your program,
25while still making major modifications.
26First it reads in an integer model from an mps file
27Then it initializes the integer model with cbc defaults
28Then it calls CbcMain1 passing all parameters apart from first but with callBack to modify stuff
29Finally it prints solution
30
31************************************************************************/
32/* Meaning of whereFrom:
33   1 after initial solve by dualsimplex etc
34   2 after preprocessing
35   3 just before branchAndBound (so user can override)
36   4 just after branchAndBound (before postprocessing)
37   5 after postprocessing
38*/
39/* Meaning of model status is as normal
40   status
41      -1 before branchAndBound
42      0 finished - check isProvenOptimal or isProvenInfeasible to see if solution found
43      (or check value of best solution)
44      1 stopped - on maxnodes, maxsols, maxtime
45      2 difficulties so run was abandoned
46      (5 event user programmed event occurred)
47
48      cbc secondary status of problem
49        -1 unset (status_ will also be -1)
50        0 search completed with solution
51        1 linear relaxation not feasible (or worse than cutoff)
52        2 stopped on gap
53        3 stopped on nodes
54        4 stopped on time
55        5 stopped on user event
56        6 stopped on solutions
57        7 linear relaxation unbounded
58
59   but initially check if status is 0 and secondary status is 1 -> infeasible
60   or you can check solver status.
61*/
62/* Return non-zero to return quickly */   
63static int callBack(CbcModel * model, int whereFrom)
64{
65  int returnCode=0;
66  switch (whereFrom) {
67  case 1:
68  case 2:
69    if (!model->status()&&model->secondaryStatus())
70      returnCode=1;
71    break;
72  case 3:
73    {
74      CbcCompareUser compare;
75      model->setNodeComparison(compare);
76    }
77    break;
78  case 4:
79    // If not good enough could skip postprocessing
80    break;
81  case 5:
82    break;
83  default:
84    abort();
85  }
86  return returnCode;
87}
88#include "CbcEventHandler.hpp"
89/** This is so user can trap events and do useful stuff. 
90
91    CbcModel model_ is available as well as anything else you care
92    to pass in
93*/
94
95class MyEventHandler3 : public CbcEventHandler {
96 
97public:
98  /**@name Overrides */
99  //@{
100  virtual CbcAction event(CbcEvent whichEvent);
101  //@}
102
103  /**@name Constructors, destructor etc*/
104  //@{
105  /** Default constructor. */
106  MyEventHandler3();
107  /// Constructor with pointer to model (redundant as setEventHandler does)
108  MyEventHandler3(CbcModel * model);
109  /** Destructor */
110  virtual ~MyEventHandler3();
111  /** The copy constructor. */
112  MyEventHandler3(const MyEventHandler3 & rhs);
113  /// Assignment
114  MyEventHandler3& operator=(const MyEventHandler3 & rhs);
115  /// Clone
116  virtual CbcEventHandler * clone() const ;
117  //@}
118   
119   
120protected:
121  // data goes here
122};
123//-------------------------------------------------------------------
124// Default Constructor
125//-------------------------------------------------------------------
126MyEventHandler3::MyEventHandler3 () 
127  : CbcEventHandler()
128{
129}
130
131//-------------------------------------------------------------------
132// Copy constructor
133//-------------------------------------------------------------------
134MyEventHandler3::MyEventHandler3 (const MyEventHandler3 & rhs) 
135: CbcEventHandler(rhs)
136{ 
137}
138
139// Constructor with pointer to model
140MyEventHandler3::MyEventHandler3(CbcModel * model)
141  : CbcEventHandler(model)
142{
143}
144
145//-------------------------------------------------------------------
146// Destructor
147//-------------------------------------------------------------------
148MyEventHandler3::~MyEventHandler3 ()
149{
150}
151
152//----------------------------------------------------------------
153// Assignment operator
154//-------------------------------------------------------------------
155MyEventHandler3 &
156MyEventHandler3::operator=(const MyEventHandler3& rhs)
157{
158  if (this != &rhs) {
159    CbcEventHandler::operator=(rhs);
160  }
161  return *this;
162}
163//-------------------------------------------------------------------
164// Clone
165//-------------------------------------------------------------------
166CbcEventHandler * MyEventHandler3::clone() const
167{
168  return new MyEventHandler3(*this);
169}
170
171CbcEventHandler::CbcAction
172MyEventHandler3::event(CbcEvent whichEvent)
173{
174  // If in sub tree carry on
175  if (!model_->parentModel()) {
176    if (whichEvent==solution||whichEvent==heuristicSolution) {
177#ifdef STOP_EARLY
178      return stop; // say finished
179#else
180      // If preprocessing was done solution will be to processed model
181      int numberColumns = model_->getNumCols();
182      const double * bestSolution = model_->bestSolution();
183      assert (bestSolution);
184      printf("value of solution is %g\n",model_->getObjValue());
185      for (int i=0;i<numberColumns;i++) {
186        if (fabs(bestSolution[i])>1.0e-8)
187          printf("%d %g\n",i,bestSolution[i]);
188      }
189      return noAction; // carry on
190#endif
191    } else {
192      return noAction; // carry on
193    }
194  } else {
195      return noAction; // carry on
196  }
197}
198
199int main (int argc, const char *argv[])
200{
201
202  OsiClpSolverInterface solver1;
203  //#define USE_OSI_NAMES
204#ifdef USE_OSI_NAMES
205  // Say we are keeping names (a bit slower this way)
206  solver1.setIntParam(OsiNameDiscipline,1);
207#endif
208  // Read in model using argv[1]
209  // and assert that it is a clean model
210  std::string mpsFileName;
211#if defined(SAMPLEDIR)
212  mpsFileName = SAMPLEDIR "/p0033.mps";
213#else
214  if (argc < 2) {
215    fprintf(stderr, "Do not know where to find sample MPS files.\n");
216    exit(1);
217  }
218#endif
219  if (argc>=2) mpsFileName = argv[1];
220  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
221  assert(numMpsReadErrors==0);
222  // Tell solver to return fast if presolve or initial solve infeasible
223  solver1.getModelPtr()->setMoreSpecialOptions(3);
224
225  /* Two ways of doing this depending on whether NEW_STYLE_SOLVER defined.
226     So we need pointer to model.  Old way could use modelA. rather than model->
227   */
228  // Messy code below copied from CbcSolver.cpp
229#ifdef CLP_FAST_CODE
230// force new style solver
231#ifndef NEW_STYLE_SOLVER
232#define NEW_STYLE_SOLVER 1
233#endif
234#else
235// Not new style solver
236#ifndef NEW_STYLE_SOLVER
237#define NEW_STYLE_SOLVER 0
238#endif
239#endif
240#if NEW_STYLE_SOLVER==0
241  // Pass to Cbc initialize defaults
242  CbcModel modelA(solver1);
243  CbcModel * model = &modelA;
244  CbcMain0(modelA);
245  // Event handler
246  MyEventHandler3 eventHandler;
247  model->passInEventHandler(&eventHandler);
248  /* Now go into code for standalone solver
249     Could copy arguments and add -quit at end to be safe
250     but this will do
251  */
252  if (argc>2) {
253    CbcMain1(argc-1,argv+1,modelA,callBack);
254  } else {
255    const char * argv2[]={"driver4","-solve","-quit"};
256    CbcMain1(3,argv2,modelA,callBack);
257  }
258#else
259  CbcSolver control(solver1);
260  // initialize
261  control.fillValuesInSolver();
262  // Event handler
263  MyEventHandler3 eventHandler;
264  CbcModel * model = control.model();
265  model->passInEventHandler(&eventHandler);
266  control.solve (argc-1, argv+1, 1);
267#endif
268  // Solver was cloned so get current copy
269  OsiSolverInterface * solver = model->solver();
270  // Print solution if finished (could get from model->bestSolution() as well
271
272  if (model->bestSolution()) {
273   
274    const double * solution = solver->getColSolution();
275   
276    int iColumn;
277    int numberColumns = solver->getNumCols();
278    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
279   
280    std::cout<<"--------------------------------------"<<std::endl;
281#ifdef USE_OSI_NAMES
282   
283    for (iColumn=0;iColumn<numberColumns;iColumn++) {
284      double value=solution[iColumn];
285      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
286        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver->getColName(iColumn)
287                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
288    }
289#else
290    // names may not be in current solver - use original
291   
292    for (iColumn=0;iColumn<numberColumns;iColumn++) {
293      double value=solution[iColumn];
294      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
295        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()->columnName(iColumn)
296                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
297    }
298#endif
299    std::cout<<"--------------------------------------"<<std::endl;
300 
301    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
302  } else {
303    std::cout<<" No solution!"<<std::endl;
304  }
305  return 0;
306}   
Note: See TracBrowser for help on using the repository browser.