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

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

chnages to try and make faster

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