source: stable/2.4/Cbc/src/CoinSolve.cpp @ 1271

Last change on this file since 1271 was 1271, checked in by forrest, 10 years ago

Creating new stable branch 2.4 from trunk (rev 1270)

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