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

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

on way to CbcSolver? class

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