source: trunk/Cbc/examples/clpdriver.cpp

Last change on this file was 2496, checked in by forrest, 2 months ago

add parameters to example

  • Property svn:executable set to *
File size: 5.5 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#include "CoinPragma.hpp"
10#include "CbcModel.hpp"
11#include "OsiClpSolverInterface.hpp"
12#include "CbcSolver.hpp"
13
14#include "CoinTime.hpp"
15
16//#############################################################################
17
18/************************************************************************
19
20This main program shows how to take advantage of the standalone cbc in your program,
21while still making major modifications. 
22Clp has not got a similar function so there can be times when it is
23useful to use Cbc for Lps.  Normally if a postsolved problem has small
24infeasibilities then simplex is called to clean up.  For some problems
25you may wish to kip this step (e.g. to save memory).
26First it reads in a model from an mps file
27Then it initializes the model with cbc defaults
28Then it calls CbcMain1 passing all parameters apart from first but with
29an event handler to modify stuff.
30
31************************************************************************/
32#include "ClpEventHandler.hpp"
33/** This is so user can trap events and do useful stuff. 
34
35    ClpSimplex model_ is available as well as anything else you care
36    to pass in
37*/
38
39class MyEventHandler3 : public ClpEventHandler {
40
41public:
42  /**@name Overrides */
43  //@{
44  virtual int event(Event whichEvent);
45  //@}
46
47  /**@name Constructors, destructor etc*/
48  //@{
49  /** Default constructor. */
50  MyEventHandler3();
51  /// Constructor with pointer to model (redundant as setEventHandler does)
52  MyEventHandler3(ClpSimplex *model);
53  /** Destructor */
54  virtual ~MyEventHandler3();
55  /** The copy constructor. */
56  MyEventHandler3(const MyEventHandler3 &rhs);
57  /// Assignment
58  MyEventHandler3 &operator=(const MyEventHandler3 &rhs);
59  /// Clone
60  virtual ClpEventHandler *clone() const;
61  //@}
62
63protected:
64  // data goes here
65};
66//-------------------------------------------------------------------
67// Default Constructor
68//-------------------------------------------------------------------
69MyEventHandler3::MyEventHandler3()
70  : ClpEventHandler()
71{
72}
73
74//-------------------------------------------------------------------
75// Copy constructor
76//-------------------------------------------------------------------
77MyEventHandler3::MyEventHandler3(const MyEventHandler3 &rhs)
78  : ClpEventHandler(rhs)
79{
80}
81
82// Constructor with pointer to model
83MyEventHandler3::MyEventHandler3(ClpSimplex *model)
84  : ClpEventHandler(model)
85{
86}
87
88//-------------------------------------------------------------------
89// Destructor
90//-------------------------------------------------------------------
91MyEventHandler3::~MyEventHandler3()
92{
93}
94
95//----------------------------------------------------------------
96// Assignment operator
97//-------------------------------------------------------------------
98MyEventHandler3 &
99MyEventHandler3::operator=(const MyEventHandler3 &rhs)
100{
101  if (this != &rhs) {
102    ClpEventHandler::operator=(rhs);
103  }
104  return *this;
105}
106//-------------------------------------------------------------------
107// Clone
108//-------------------------------------------------------------------
109ClpEventHandler *MyEventHandler3::clone() const
110{
111  return new MyEventHandler3(*this);
112}
113
114int MyEventHandler3::event(Event whichEvent)
115{
116  // See if just after postsolve
117  if (whichEvent == presolveAfterFirstSolve || whichEvent == presolveAfterSolve) {
118    return -2; // skip clean up
119  } else {
120    return -1; // carry on
121  }
122}
123
124static int dummyCallBack(CbcModel * /*model*/, int /*whereFrom*/)
125{
126  return 0;
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)
147    mpsFileName = argv[1];
148  int numMpsReadErrors;
149  if (strstr(mpsFileName.c_str(), ".mps"))
150    numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
151  else
152    numMpsReadErrors = solver1.readLp(mpsFileName.c_str());
153  if (numMpsReadErrors != 0) {
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 = dynamic_cast< OsiClpSolverInterface * >(modelA.solver());
165  model = solver->getModelPtr();
166  CbcSolverUsefulData cbcData;
167#ifndef CBC_NO_INTERRUPT
168  cbcData.useSignalHandler_ = true;
169#endif
170  cbcData.noPrinting_ = false;
171  // initialize
172  CbcMain0(modelA, cbcData);
173  /* Now go into code for standalone solver
174     Could copy arguments and add -quit at end to be safe
175     but this will do
176  */
177  if (argc > 2) {
178    CbcMain1(argc - 1, argv + 1, modelA, dummyCallBack , cbcData);
179  } else {
180    const char *argv2[] = { "clpdriver", "-solve", "-quit" };
181    CbcMain1(3, argv2, modelA, dummyCallBack, cbcData);
182  }
183
184  if (!model->problemStatus()) {
185    std::cout << "Objective value " << model->objectiveValue() << std::endl;
186  } else {
187    std::cout << "Infeasible!" << std::endl;
188  }
189  return 0;
190}
Note: See TracBrowser for help on using the repository browser.