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

Last change on this file since 862 was 857, checked in by forrest, 12 years ago

allow dense solver

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