source: trunk/Cbc/examples/clpdriver.cpp

Last change on this file was 2469, checked in by unxusr, 6 weeks ago

formatting

  • 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#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
124int main(int argc, const char *argv[])
125{
126
127  OsiClpSolverInterface solver1;
128  // Say we are keeping names (a bit slower this way)
129  solver1.setIntParam(OsiNameDiscipline, 1);
130  // Read in model using argv[1]
131  // and assert that it is a clean model
132  std::string mpsFileName;
133#if defined(SAMPLEDIR)
134  mpsFileName = SAMPLEDIR "/p0033.mps";
135#else
136  if (argc < 2) {
137    fprintf(stderr, "Do not know where to find sample MPS files.\n");
138    exit(1);
139  }
140#endif
141  if (argc >= 2)
142    mpsFileName = argv[1];
143  int numMpsReadErrors;
144  if (strstr(mpsFileName.c_str(), ".mps"))
145    numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), "");
146  else
147    numMpsReadErrors = solver1.readLp(mpsFileName.c_str());
148  if (numMpsReadErrors != 0) {
149    printf("%d errors reading MPS file\n", numMpsReadErrors);
150    return numMpsReadErrors;
151  }
152  ClpSimplex *model = solver1.getModelPtr();
153  // Event handler
154  MyEventHandler3 eventHandler(model);
155  model->passInEventHandler(&eventHandler);
156  // Messy code below copied from CbcSolver.cpp
157  // Pass to Cbc initialize defaults
158  CbcModel modelA(solver1);
159  OsiClpSolverInterface *solver = dynamic_cast< OsiClpSolverInterface * >(modelA.solver());
160  model = solver->getModelPtr();
161  CbcMain0(modelA);
162  /* Now go into code for standalone solver
163     Could copy arguments and add -quit at end to be safe
164     but this will do
165  */
166  if (argc > 2) {
167    CbcMain1(argc - 1, argv + 1, modelA);
168  } else {
169    const char *argv2[] = { "clpdriver", "-solve", "-quit" };
170    CbcMain1(3, argv2, modelA);
171  }
172
173  if (!model->problemStatus()) {
174    std::cout << "Objective value " << model->objectiveValue() << std::endl;
175  } else {
176    std::cout << "Infeasible!" << std::endl;
177  }
178  return 0;
179}
Note: See TracBrowser for help on using the repository browser.