source: branches/sandbox/Cbc/src/CoinSolve.cpp @ 1389

Last change on this file since 1389 was 1387, checked in by lou, 10 years ago

This commit removes remaining code associated with NEW_STYLE_SOLVER. See the notes in CbcLinkedUtils? for why the remaining code remains.

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