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

Last change on this file since 846 was 834, checked in by forrest, 13 years ago

show how to grab solution

File size: 8.4 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 = "../../Data/Sample/p0033.mps";
211  if (argc>=2) mpsFileName = argv[1];
212  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
213  assert(numMpsReadErrors==0);
214  // Tell solver to return fast if presolve or initial solve infeasible
215  solver1.getModelPtr()->setMoreSpecialOptions(3);
216
217  /* Two ways of doing this depending on whether NEW_STYLE_SOLVER defined.
218     So we need pointer to model.  Old way could use modelA. rather than model->
219   */
220#ifndef NEW_STYLE_SOLVER
221  // Pass to Cbc initialize defaults
222  CbcModel modelA(solver1);
223  CbcModel * model = &modelA;
224  CbcMain0(modelA);
225  // Event handler
226  MyEventHandler3 eventHandler;
227  model->passInEventHandler(&eventHandler);
228  /* Now go into code for standalone solver
229     Could copy arguments and add -quit at end to be safe
230     but this will do
231  */
232  if (argc>2) {
233    CbcMain1(argc-1,argv+1,modelA,callBack);
234  } else {
235    const char * argv2[]={"driver3","-solve","-quit"};
236    CbcMain1(3,argv2,modelA,callBack);
237  }
238#else
239  CbcSolver control(solver1);
240  // initialize
241  control.fillValuesInSolver();
242  // Event handler
243  MyEventHandler3 eventHandler;
244  CbcModel * model = control.model();
245  model->passInEventHandler(&eventHandler);
246  control.solve (argc-1, argv+1, 1);
247#endif
248  // Solver was cloned so get current copy
249  OsiSolverInterface * solver = model->solver();
250  // Print solution if finished (could get from model->bestSolution() as well
251
252  if (model->bestSolution()) {
253   
254    const double * solution = solver->getColSolution();
255   
256    int iColumn;
257    int numberColumns = solver->getNumCols();
258    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
259   
260    std::cout<<"--------------------------------------"<<std::endl;
261#ifdef USE_OSI_NAMES
262   
263    for (iColumn=0;iColumn<numberColumns;iColumn++) {
264      double value=solution[iColumn];
265      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
266        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver->getColName(iColumn)
267                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
268    }
269#else
270    // names may not be in current solver - use original
271   
272    for (iColumn=0;iColumn<numberColumns;iColumn++) {
273      double value=solution[iColumn];
274      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
275        std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()->columnName(iColumn)
276                 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;
277    }
278#endif
279    std::cout<<"--------------------------------------"<<std::endl;
280 
281    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
282  } else {
283    std::cout<<" No solution!"<<std::endl;
284  }
285  return 0;
286}   
Note: See TracBrowser for help on using the repository browser.