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

Last change on this file since 1286 was 1286, checked in by EdwinStraver, 10 years ago

Changed formatting using AStyle -A4 -p

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