source: trunk/Cbc/examples/clpdriver.cpp @ 2452

Last change on this file since 2452 was 2452, checked in by forrest, 5 months ago

example for ClpEventHandler?

  • Property svn:executable set to *
File size: 5.2 KB
Line 
1// $Id: clpdriver.cpp 2101 2014-12-03 17:43:20Z forrest $
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. 
24Clp has not got a similar function so there can be times when it is
25useful to use Cbc for Lps.  Normally if a postsolved problem has small
26infeasibilities then simplex is called to clean up.  For some problems
27you may wish to kip this step (e.g. to save memory).
28First it reads in a model from an mps file
29Then it initializes the model with cbc defaults
30Then it calls CbcMain1 passing all parameters apart from first but with
31an event handler to modify stuff.
32
33************************************************************************/
34#include "ClpEventHandler.hpp"
35/** This is so user can trap events and do useful stuff. 
36
37    ClpSimplex model_ is available as well as anything else you care
38    to pass in
39*/
40
41class MyEventHandler3 : public ClpEventHandler {
42 
43public:
44  /**@name Overrides */
45  //@{
46  virtual int event(Event whichEvent);
47  //@}
48
49  /**@name Constructors, destructor etc*/
50  //@{
51  /** Default constructor. */
52  MyEventHandler3();
53  /// Constructor with pointer to model (redundant as setEventHandler does)
54  MyEventHandler3(ClpSimplex * model);
55  /** Destructor */
56  virtual ~MyEventHandler3();
57  /** The copy constructor. */
58  MyEventHandler3(const MyEventHandler3 & rhs);
59  /// Assignment
60  MyEventHandler3& operator=(const MyEventHandler3 & rhs);
61  /// Clone
62  virtual ClpEventHandler * clone() const ;
63  //@}
64   
65   
66protected:
67  // data goes here
68};
69//-------------------------------------------------------------------
70// Default Constructor
71//-------------------------------------------------------------------
72MyEventHandler3::MyEventHandler3 () 
73  : ClpEventHandler()
74{
75}
76
77//-------------------------------------------------------------------
78// Copy constructor
79//-------------------------------------------------------------------
80MyEventHandler3::MyEventHandler3 (const MyEventHandler3 & rhs) 
81: ClpEventHandler(rhs)
82{ 
83}
84
85// Constructor with pointer to model
86MyEventHandler3::MyEventHandler3(ClpSimplex * model)
87  : ClpEventHandler(model)
88{
89}
90
91//-------------------------------------------------------------------
92// Destructor
93//-------------------------------------------------------------------
94MyEventHandler3::~MyEventHandler3 ()
95{
96}
97
98//----------------------------------------------------------------
99// Assignment operator
100//-------------------------------------------------------------------
101MyEventHandler3 &
102MyEventHandler3::operator=(const MyEventHandler3& rhs)
103{
104  if (this != &rhs) {
105    ClpEventHandler::operator=(rhs);
106  }
107  return *this;
108}
109//-------------------------------------------------------------------
110// Clone
111//-------------------------------------------------------------------
112ClpEventHandler * MyEventHandler3::clone() const
113{
114  return new MyEventHandler3(*this);
115}
116
117int
118MyEventHandler3::event(Event whichEvent)
119{
120  // See if just after postsolve
121  if (whichEvent==presolveAfterFirstSolve||
122      whichEvent==presolveAfterSolve) {
123    return -2; // skip clean up
124  } else {
125    return -1; // carry on
126  }
127}
128
129int main (int argc, const char *argv[])
130{
131
132  OsiClpSolverInterface solver1;
133  // Say we are keeping names (a bit slower this way)
134  solver1.setIntParam(OsiNameDiscipline,1);
135  // Read in model using argv[1]
136  // and assert that it is a clean model
137  std::string mpsFileName;
138#if defined(SAMPLEDIR)
139  mpsFileName = SAMPLEDIR "/p0033.mps";
140#else
141  if (argc < 2) {
142    fprintf(stderr, "Do not know where to find sample MPS files.\n");
143    exit(1);
144  }
145#endif
146  if (argc>=2) mpsFileName = argv[1];
147  int numMpsReadErrors;
148  if (strstr(mpsFileName.c_str(),".mps"))
149    numMpsReadErrors= solver1.readMps(mpsFileName.c_str(),"");
150  else
151    numMpsReadErrors= solver1.readLp(mpsFileName.c_str());
152  if( numMpsReadErrors != 0 )
153  {
154     printf("%d errors reading MPS file\n", numMpsReadErrors);
155     return numMpsReadErrors;
156  }
157  ClpSimplex * model = solver1.getModelPtr();
158  // Event handler
159  MyEventHandler3 eventHandler(model);
160  model->passInEventHandler(&eventHandler);
161  // Messy code below copied from CbcSolver.cpp
162  // Pass to Cbc initialize defaults
163  CbcModel modelA(solver1);
164  OsiClpSolverInterface * solver =
165    dynamic_cast<OsiClpSolverInterface *>(modelA.solver());
166  model = solver->getModelPtr();
167  CbcMain0(modelA);
168  /* Now go into code for standalone solver
169     Could copy arguments and add -quit at end to be safe
170     but this will do
171  */
172  if (argc>2) {
173    CbcMain1(argc-1,argv+1,modelA);
174  } else {
175    const char * argv2[]={"clpdriver","-solve","-quit"};
176    CbcMain1(3,argv2,modelA);
177  }
178
179  if (!model->problemStatus()) {
180    std::cout<<"Objective value "<<model->objectiveValue()<<std::endl;
181  } else {
182    std::cout<<"Infeasible!"<<std::endl;
183  }
184  return 0;
185}   
Note: See TracBrowser for help on using the repository browser.