Changeset 885 for branches/heur


Ignore:
Timestamp:
Feb 29, 2008 11:06:25 PM (11 years ago)
Author:
jpgoncal
Message:

Cleaned up a little bit.

Location:
branches/heur/Cbc/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/heur/Cbc/src/CbcHeuristic.cpp

    r884 r885  
    3333   heuristicName_("Unknown"),
    3434   howOften_(100),
    35    decayFactor_(0.5)
     35   decayFactor_(0.5),
     36   lastRun_(0),
     37   runNodes_(NULL)
    3638{
    3739  // As CbcHeuristic virtual need to modify .cpp if above change
     
    4850  heuristicName_("Unknown"),
    4951  howOften_(100),
    50   decayFactor_(0.5)
     52  decayFactor_(0.5),
     53  lastRun_(0),
     54  runNodes_(NULL)
    5155{}
    5256
     
    6367  howOften_(rhs.howOften_),
    6468  decayFactor_(rhs.howOften_),
     69  lastRun_(rhs.lastRun_),
    6570  runNodes_(rhs.runNodes_)
    6671{}
     
    8085    howOften_ = rhs.howOften_;
    8186    decayFactor_ = rhs.howOften_;
     87    lastRun_ = rhs.lastRun_;
    8288    runNodes_ = rhs.runNodes_;
    8389  }
     
    497503CbcHeuristicNode::CbcHeuristicNode(CbcModel& model)
    498504{
    499   CbcNode* node = model->currentNode();
     505  CbcNode* node = model.currentNode();
    500506  int depth = node->depth();
    501507  numObjects_ = depth; //??
    502   brObj_ = new int[numObjects_];
     508  brObj_ = new OsiBranchingObject*[numObjects_];
    503509  CbcNodeInfo* nodeInfo = node->nodeInfo();
    504   int depth = 0;
     510  depth = 0;
    505511  while (nodeInfo) {
    506     brObj_[depth++] = nodeInfo->branchingObject()->clone();
     512    brObj_[depth++] = node->branchingObject()->clone();
    507513    nodeInfo = nodeInfo->parent();
    508514  }
    509515  std::sort(brObj_, brObj_+depth, compareBranchingObjects);
    510   cnt = 0;
     516  int cnt = 0;
    511517  OsiBranchingObject* br;
    512518  for (int i = 1; i < depth; ++i) {
     
    548554  int i = 0;
    549555  int j = 0;
    550   double dist = 0.0;
    551   while( i < numObjects_ && j < node.numObjects_) {
     556  double distance = 0.0;
     557  while( i < numObjects_ && j < node->numObjects_) {
    552558    const OsiBranchingObject* br0 = brObj_[i];
    553     const OsiBranchingObject* br1 = node.brObj_[j];
     559    const OsiBranchingObject* br1 = node->brObj_[j];
    554560    const int brComp = compare3BranchingObjects(br0, br1);
    555     switch (brcomp) {
     561    switch (brComp) {
    556562    case -1:
    557563      distance += subsetWeight;
     
    581587    }
    582588  }
    583   distance += subsetWeight * (numObjects_ - i + node.numObjects_ - j);
     589  distance += subsetWeight * (numObjects_ - i + node->numObjects_ - j);
    584590  return distance;
    585591}
     
    592598    delete brObj_[i];
    593599  }
     600  delete [] brObj_;
     601}
     602
     603//=======================================================================================
     604
     605CbcHeuristicNodeList::CbcHeuristicNodeList(const CbcHeuristicNodeList& rhs)
     606{
     607  nodes_ = rhs.nodes_;
     608}
     609
     610//=======================================================================================
     611
     612CbcHeuristicNodeList&
     613CbcHeuristicNodeList::operator=(const CbcHeuristicNodeList& rhs)
     614{
     615  if (this != &rhs)
     616    nodes_ = rhs.nodes_;
     617
     618  return *this;
    594619}
    595620
     
    599624CbcHeuristicNodeList::farFrom(const CbcHeuristicNode* node)
    600625{
     626  const double minDistance = 10.0;
     627
     628  double distance = 0.0;
     629  for (int i = 0; i < (int) nodes_.size(); i++) {
     630    if(nodes_[i] != NULL)
     631      distance += nodes_[i]->distance(node);
     632  }
    601633 
    602  
    603   // Get Hamming distance to last node where a solution was found
    604   double
    605     CbcHeuristic::getHammingDistance(const OsiSolverInterface* solver) const
    606   {
    607     double hammingDistance = 0.0;
    608     int numberIntegers = model_->numberIntegers();
    609     const double * lower = solver->getColLower();
    610     const double * upper = solver->getColUpper();
    611     const int * integerVariable = model_->integerVariable();
    612     for (int i=0; i<numberIntegers; i++) {
    613       int iColumn = integerVariable[i];
    614       if(lowerBoundLastNode_[i] != lower[iColumn] ||
    615          upperBoundLastNode_[i] != upper[iColumn])
    616         hammingDistance += 1.0;
    617     }
    618     return hammingDistance;
    619   }
     634  if(distance > minDistance)
     635    return true;
     636
     637  return false;
    620638}
    621639
  • branches/heur/Cbc/src/CbcHeuristic.hpp

    r884 r885  
    99#include "OsiCuts.hpp"
    1010#include "CoinHelperFunctions.hpp"
     11#include "OsiBranchingObject.hpp"
    1112
    1213class OsiSolverInterface;
     
    2021
    2122class CbcHeuristicNode {
    22 private::
     23private:
    2324  /// The number of branching decisions made
    2425  int numObjects_;
     
    2829  OsiBranchingObject** brObj_;
    2930public:
     31  CbcHeuristicNode() {}
     32  CbcHeuristicNode(CbcModel& model);
     33  ~CbcHeuristicNode();
     34
     35  double distance(const CbcHeuristicNode* node) const;
     36#if 0
    3037  inline swap(CbcHeuristicNode& node) {
    3138    ::swap(numObjects_, node.numObjects_);
    3239    ::swap(objects_, node.objects_);
    33     ::swap(bounds_, node.bounds_);
    3440  }
     41#endif
    3542};
    3643
     
    4855  }
    4956
    50   bool farFrom(const CbcHeuristicNode& node);
     57  bool farFrom(const CbcHeuristicNode* node);
    5158  void append(CbcHeuristicNode*& node) {
    5259    nodes_.push_back(node);
     
    5461  }
    5562  void append(CbcHeuristicNodeList& nodes) {
    56     nodes_.insert(nodes_.end(), nodes.begin(), nodes.end());
    57     nodes.clear();
     63    nodes_.insert(nodes_.end(), nodes.nodes_.begin(), nodes.nodes_.end());
     64    nodes.nodes_.clear();
    5865  }
    5966};
     
    93100  */
    94101  virtual int solution(double & objectiveValue,
    95                        double * newSolution,
    96                        CbcHeuristicInfo* info = NULL)=0;
     102                       double * newSolution)=0;
    97103
    98104  /** returns 0 if no solution, 1 if valid solution, -1 if just
     
    105111  virtual int solution(double & objectiveValue,
    106112                       double * newSolution,
    107                        OsiCuts & cs,
    108                        CbcHeuristicInfo* info = NULL) {return 0;}
     113                       OsiCuts & cs) {return 0;}
    109114
    110115  /// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
     
    191196  /// How much to increase how often
    192197  double decayFactor_;
     198  /// Last node count where the heuristic was applied
     199  int lastRun_;
    193200  /// The description of the nodes where this heuristic has been applied
    194   CbcHeuristicNode* runNodes_;
     201  CbcHeuristicNodeList* runNodes_;
    195202#if 0
    196203  /// Lower bounds of last node where the heuristic found a solution
  • branches/heur/Cbc/src/CbcHeuristicDiveCoefficient.cpp

    r884 r885  
    156156
    157157    // Get where we are and create the appropriate CbcHeuristicNode object
    158     nodeDesc = new CbcHeuristicNode(model_);
    159     if (!nodes_.farFrom(nodeDesc)) {
     158    nodeDesc = new CbcHeuristicNode(*model_);
     159    if (!runNodes_->farFrom(nodeDesc)) {
    160160      delete nodeDesc;
    161161      return NULL;
    162162    }
    163163  }
     164
     165  if(nodeDesc != NULL)
     166    runNodes_->append(nodeDesc);
     167 
     168  lastRun_ = nodeCount;
    164169
    165170#if 0
     
    444449      returnCode=1;
    445450
    446       setCurrentNode(model_->solver());
     451      //      setCurrentNode(model_->solver());
    447452    }
    448453  }
Note: See TracChangeset for help on using the changeset viewer.