source: trunk/Cbc/src/CoinSolve.cpp @ 716

Last change on this file since 716 was 716, checked in by forrest, 12 years ago

message handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1// Copyright (C) 2007, International Business Machines
2// Corporation and others.  All Rights Reserved.
3   
4#include "CbcConfig.h"
5#include "CoinPragma.hpp"
6#include "CbcModel.hpp"
7#include "OsiClpSolverInterface.hpp"
8
9#include <cassert>
10#include <cstdio>
11#include <cmath>
12#include <cfloat>
13#include <cstring>
14#include <iostream>
15// This driver shows how to trap messages - this is just as in unitTest.cpp
16// ****** THis code is similar to MyMessageHandler.hpp and MyMessagehandler.cpp
17#include "CoinMessageHandler.hpp"
18
19/** This just adds a model to CoinMessage and a void pointer so
20    user can trap messages and do useful stuff. 
21    This is used in Clp/Test/unitTest.cpp
22
23    The file pointer is just there as an example of user stuff.
24
25*/
26class CbcModel;
27
28class MyMessageHandler : public CoinMessageHandler {
29 
30public:
31  /**@name Overrides */
32  //@{
33  virtual int print();
34  //@}
35  /**@name set and get */
36  //@{
37  /// Model
38  const CbcModel * model() const;
39  void setModel(CbcModel * model);
40  //@}
41
42  /**@name Constructors, destructor */
43  //@{
44  /** Default constructor. */
45  MyMessageHandler();
46  /// Constructor with pointer to model
47  MyMessageHandler(CbcModel * model,
48                           FILE * userPointer=NULL);
49  /** Destructor */
50  virtual ~MyMessageHandler();
51  //@}
52
53  /**@name Copy method */
54  //@{
55  /** The copy constructor. */
56  MyMessageHandler(const MyMessageHandler&);
57  /** The copy constructor from an CoinSimplexMessageHandler. */
58  MyMessageHandler(const CoinMessageHandler&);
59 
60  MyMessageHandler& operator=(const MyMessageHandler&);
61  /// Clone
62  virtual CoinMessageHandler * clone() const ;
63  //@}
64   
65   
66protected:
67  /**@name Data members
68     The data members are protected to allow access for derived classes. */
69  //@{
70  /// Pointer back to model
71  CbcModel * model_;
72  //@}
73};
74
75
76//#############################################################################
77// Constructors / Destructor / Assignment
78//#############################################################################
79
80//-------------------------------------------------------------------
81// Default Constructor
82//-------------------------------------------------------------------
83MyMessageHandler::MyMessageHandler () 
84  : CoinMessageHandler(),
85    model_(NULL)
86{
87}
88
89//-------------------------------------------------------------------
90// Copy constructor
91//-------------------------------------------------------------------
92MyMessageHandler::MyMessageHandler (const MyMessageHandler & rhs) 
93: CoinMessageHandler(rhs),
94    model_(rhs.model_)
95{ 
96}
97
98MyMessageHandler::MyMessageHandler (const CoinMessageHandler & rhs) 
99  : CoinMessageHandler(),
100    model_(NULL)
101{ 
102}
103
104// Constructor with pointer to model
105MyMessageHandler::MyMessageHandler(CbcModel * model,
106               FILE * userPointer)
107  : CoinMessageHandler(),
108    model_(model)
109{
110}
111
112//-------------------------------------------------------------------
113// Destructor
114//-------------------------------------------------------------------
115MyMessageHandler::~MyMessageHandler ()
116{
117}
118
119//----------------------------------------------------------------
120// Assignment operator
121//-------------------------------------------------------------------
122MyMessageHandler &
123MyMessageHandler::operator=(const MyMessageHandler& rhs)
124{
125  if (this != &rhs) {
126    CoinMessageHandler::operator=(rhs);
127    model_ = rhs.model_;
128  }
129  return *this;
130}
131//-------------------------------------------------------------------
132// Clone
133//-------------------------------------------------------------------
134CoinMessageHandler * MyMessageHandler::clone() const
135{
136  return new MyMessageHandler(*this);
137}
138int 
139MyMessageHandler::print()
140{
141  // Just add ==
142  fprintf(fp_," == ");
143  fprintf(fp_,"%s\n",messageBuffer_);
144  return 0;
145}
146const CbcModel *
147MyMessageHandler::model() const
148{
149  return model_;
150}
151void 
152MyMessageHandler::setModel(CbcModel * model)
153{
154  model_ = model;
155}
156//#############################################################################
157// To use USERCBC or USERCLP change 0 to 1 in defines and add in your fake main program(s) and any other code
158//#define USER_HAS_FAKE_CBC
159//#define USER_HAS_FAKE_CLP
160#ifdef USER_HAS_FAKE_CBC
161#endif
162void fakeMain (ClpSimplex & model,OsiSolverInterface & osiSolver, CbcModel & babSolver)
163{
164#ifdef USER_HAS_FAKE_CBC
165#else
166  printf("Dummy user cbc code - model has %d rows and %d columns\n",
167         model.getNumRows(),model.getNumCols());
168  // Reduce printout
169  babSolver.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
170  // Do complete search
171  babSolver.branchAndBound();
172#endif
173}
174// Clp stuff
175#ifdef USER_HAS_FAKE_CLP
176#endif
177void fakeMain2 (ClpSimplex & model,OsiClpSolverInterface & osiSolver,int options) {
178#ifdef USER_HAS_FAKE_CLP
179#else
180  ClpSimplex * lpSolver = osiSolver.getModelPtr();
181  printf("Dummy user clp code - model has %d rows and %d columns\n",
182         lpSolver->numberRows(),lpSolver->numberColumns());
183  osiSolver.initialSolve();
184#endif
185}
186//  End any fake main program
187//#############################################################################
188void CbcClpUnitTest (const CbcModel & saveModel);
189int main (int argc, const char *argv[])
190{
191  OsiClpSolverInterface solver1;
192  CbcModel model(solver1);
193  // define TEST_MESSAGE_HANDLER to check works on all messages
194  //#define TEST_MESSAGE_HANDLER
195#ifdef TEST_MESSAGE_HANDLER
196  MyMessageHandler messageHandler(&model);
197  std::cout<<"Testing derived message handler"<<std::endl;
198  model.passInMessageHandler(&messageHandler);
199  OsiClpSolverInterface * clpSolver = dynamic_cast< OsiClpSolverInterface*> (model.solver());
200  // Could use different handlers (if different log levels)
201  clpSolver->passInMessageHandler(&messageHandler);
202  //clpSolver->getModelPtr()->passInMessageHandler(&messageHandler);
203#endif
204  // initialize
205  CbcMain0(model);
206#ifdef TEST_MESSAGE_HANDLER
207  // Set log levels same so can use one message handler
208  clpSolver->messageHandler()->setLogLevel(1) ;
209  model.messageHandler()->setLogLevel(1);
210#endif
211  int returnCode = CbcMain1 (argc, argv,model);
212  if (returnCode!=777) {
213    return returnCode;
214  } else {
215    // do miplib
216    CbcClpUnitTest(model);
217    return 0;
218  }
219}
220/*
221  Version 1.00.00 November 16 2005.
222  This is to stop me (JJF) messing about too much.
223  Tuning changes should be noted here.
224  The testing next version may be activated by CBC_NEXT_VERSION
225  This applies to OsiClp, Clp etc
226  Version 1.00.01 November 24 2005
227  Added several classes for advanced users.  This can't affect code (if you don't use it)
228  Made some tiny changes (for N way branching) which should not change anything.
229  CbcNWay object class - for N way branching this also allows use of CbcConsequence class.
230  CbcBranchAllDifferent object class - for branching on general integer variables
231  to stop them having same value so branches are x >= y+1 and x <= y-1.
232  Added two new Cgl classes - CglAllDifferent which does column fixing (too slowly)
233  and CglStored which just has a list of cuts which can be activated.
234  Modified preprocess option to SOS
235  Version 1.00.02 December 9 2005
236  Added use of CbcStrategy to do clean preprocessing
237  Added use of referenceSolver for cleaner repetition of Cbc
238  Version 1.01.00 February 2 2006
239  Added first try at Ampl interface
240  Made dummy program so real main can be called from other programs
241*/
Note: See TracBrowser for help on using the repository browser.