Changeset 741 for trunk


Ignore:
Timestamp:
Aug 10, 2007 12:18:20 PM (12 years ago)
Author:
forrest
Message:

event handling

Location:
trunk/Cbc
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/examples/driver4.cpp

    r739 r741  
    8585  return returnCode;
    8686}
     87#include "CbcEventHandler.hpp"
     88/** This is so user can trap events and do useful stuff. 
     89
     90    CbcModel model_ is available as well as anything else you care
     91    to pass in
     92*/
     93
     94class MyEventHandler3 : public CbcEventHandler {
     95 
     96public:
     97  /**@name Overrides */
     98  //@{
     99  virtual CbcAction event(CbcEvent whichEvent);
     100  //@}
     101
     102  /**@name Constructors, destructor etc*/
     103  //@{
     104  /** Default constructor. */
     105  MyEventHandler3();
     106  /// Constructor with pointer to model (redundant as setEventHandler does)
     107  MyEventHandler3(CbcModel * model);
     108  /** Destructor */
     109  virtual ~MyEventHandler3();
     110  /** The copy constructor. */
     111  MyEventHandler3(const MyEventHandler3 & rhs);
     112  /// Assignment
     113  MyEventHandler3& operator=(const MyEventHandler3 & rhs);
     114  /// Clone
     115  virtual CbcEventHandler * clone() const ;
     116  //@}
     117   
     118   
     119protected:
     120  // data goes here
     121};
     122//-------------------------------------------------------------------
     123// Default Constructor
     124//-------------------------------------------------------------------
     125MyEventHandler3::MyEventHandler3 ()
     126  : CbcEventHandler()
     127{
     128}
     129
     130//-------------------------------------------------------------------
     131// Copy constructor
     132//-------------------------------------------------------------------
     133MyEventHandler3::MyEventHandler3 (const MyEventHandler3 & rhs)
     134: CbcEventHandler(rhs)
     135
     136}
     137
     138// Constructor with pointer to model
     139MyEventHandler3::MyEventHandler3(CbcModel * model)
     140  : CbcEventHandler(model)
     141{
     142}
     143
     144//-------------------------------------------------------------------
     145// Destructor
     146//-------------------------------------------------------------------
     147MyEventHandler3::~MyEventHandler3 ()
     148{
     149}
     150
     151//----------------------------------------------------------------
     152// Assignment operator
     153//-------------------------------------------------------------------
     154MyEventHandler3 &
     155MyEventHandler3::operator=(const MyEventHandler3& rhs)
     156{
     157  if (this != &rhs) {
     158    CbcEventHandler::operator=(rhs);
     159  }
     160  return *this;
     161}
     162//-------------------------------------------------------------------
     163// Clone
     164//-------------------------------------------------------------------
     165CbcEventHandler * MyEventHandler3::clone() const
     166{
     167  return new MyEventHandler3(*this);
     168}
     169
     170CbcEventHandler::CbcAction
     171MyEventHandler3::event(CbcEvent whichEvent)
     172{
     173  // If in sub tree carry on
     174  if (!model_->parentModel()) {
     175    if (whichEvent==solution||whichEvent==heuristicSolution)
     176      return stop; // say finished
     177    else
     178      return noAction; // carry on
     179  } else {
     180      return noAction; // carry on
     181  }
     182}
    87183
    88184int main (int argc, const char *argv[])
     
    107203  CbcModel model(solver1);   
    108204  CbcMain0(model);
     205  // Event handler
     206  MyEventHandler3 eventHandler;
     207  model.passInEventHandler(&eventHandler);
    109208  /* Now go into code for standalone solver
    110209     Could copy arguments and add -quit at end to be safe
  • trunk/Cbc/src/CbcEventHandler.hpp

    r706 r741  
    8888    treeStatus,
    8989    /*! A solution has been found. */
    90     solution
     90    solution,
     91    /*! A heuristic solution has been found. */
     92    heuristicSolution
    9193  } ;
    9294
  • trunk/Cbc/src/CbcHeuristic.cpp

    r724 r741  
    157157               solver->getNumRows(),solver->getNumCols(),
    158158               solver2->getNumRows(),solver2->getNumCols());
    159       if (after>fractionSmall_*before)
     159      if (after>fractionSmall_*before&&after>300)
    160160        return 0;
    161161      solver2->resolve();
  • trunk/Cbc/src/CbcModel.cpp

    r738 r741  
    691691  eventHappened_=false;
    692692  CbcEventHandler *eventHandler = getEventHandler() ;
     693  if (eventHandler)
     694    eventHandler->setModel(this);
    693695  // set up for probing
    694696  probingInfo_ = new CglTreeProbingInfo(solver_);
     
    11321134        }
    11331135      }
     1136      if (eventHandler) {
     1137        if (!eventHandler->event(CbcEventHandler::solution)) {
     1138          eventHappened_=true; // exit
     1139        }
     1140      }
    11341141    }
    11351142    delete [] newSolution ;
     
    11591166
    11601167      if (numberUnsatisfied)   {
    1161         feasible = solveWithCuts(cuts,maximumCutPassesAtRoot_,
    1162                                  NULL);
     1168        // User event
     1169        if (!eventHappened_)
     1170          feasible = solveWithCuts(cuts,maximumCutPassesAtRoot_,
     1171                                   NULL);
     1172        else
     1173          feasible=false;
    11631174      } else if (solverCharacteristics_->solutionAddsCuts()||
    11641175                 solverCharacteristics_->alwaysTryCutsAtRootNode()) {
     
    11901201    feasible = false;
    11911202  }
     1203  // User event
     1204  if (eventHappened_)
     1205    feasible=false;
    11921206/*
    11931207  We've taken the continuous relaxation as far as we can. Time to branch.
     
    32533267  lastHeuristic_ = NULL;
    32543268  if (rhs.eventHandler_)
    3255   { eventHandler_ = new CbcEventHandler(*rhs.eventHandler_) ; }
     3269    { eventHandler_ = rhs.eventHandler_->clone() ; }
    32563270  else
    32573271  { eventHandler_ = NULL ; }
     
    35743588      delete eventHandler_ ;
    35753589    if (rhs.eventHandler_)
    3576     { eventHandler_ = new CbcEventHandler(*rhs.eventHandler_) ; }
     3590      { eventHandler_ = rhs.eventHandler_->clone() ; }
    35773591    else
    35783592    { eventHandler_ = NULL ; }
Note: See TracChangeset for help on using the changeset viewer.