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

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

fix so will link on Windows

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