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

Last change on this file since 931 was 931, checked in by forrest, 11 years ago

changes to try and improve performance

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