CbcHeuristic.hpp

Go to the documentation of this file.
00001 /* $Id: CbcHeuristic.hpp 2094 2014-11-18 11:15:36Z forrest $ */
00002 // Copyright (C) 2002, International Business Machines
00003 // Corporation and others.  All Rights Reserved.
00004 // This code is licensed under the terms of the Eclipse Public License (EPL).
00005 
00006 #ifndef CbcHeuristic_H
00007 #define CbcHeuristic_H
00008 
00009 #include <string>
00010 #include <vector>
00011 #include "CoinPackedMatrix.hpp"
00012 #include "OsiCuts.hpp"
00013 #include "CoinHelperFunctions.hpp"
00014 #include "OsiBranchingObject.hpp"
00015 
00016 class OsiSolverInterface;
00017 
00018 class CbcModel;
00019 
00020 //#############################################################################
00021 
00022 class CbcHeuristicNodeList;
00023 class CbcBranchingObject;
00024 
00028 class CbcHeuristicNode {
00029 private:
00030     void gutsOfConstructor(CbcModel& model);
00031     CbcHeuristicNode();
00032     CbcHeuristicNode& operator=(const CbcHeuristicNode&);
00033 private:
00035     int numObjects_;
00039     CbcBranchingObject** brObj_;
00040 public:
00041     CbcHeuristicNode(CbcModel& model);
00042 
00043     CbcHeuristicNode(const CbcHeuristicNode& rhs);
00044     ~CbcHeuristicNode();
00045     double distance(const CbcHeuristicNode* node) const;
00046     double minDistance(const CbcHeuristicNodeList& nodeList) const;
00047     bool minDistanceIsSmall(const CbcHeuristicNodeList& nodeList,
00048                             const double threshold) const;
00049     double avgDistance(const CbcHeuristicNodeList& nodeList) const;
00050 };
00051 
00052 class CbcHeuristicNodeList {
00053 private:
00054     void gutsOfDelete();
00055     void gutsOfCopy(const CbcHeuristicNodeList& rhs);
00056 private:
00057     std::vector<CbcHeuristicNode*> nodes_;
00058 public:
00059     CbcHeuristicNodeList() {}
00060     CbcHeuristicNodeList(const CbcHeuristicNodeList& rhs);
00061     CbcHeuristicNodeList& operator=(const CbcHeuristicNodeList& rhs);
00062     ~CbcHeuristicNodeList();
00063 
00064     void append(CbcHeuristicNode*& node);
00065     void append(const CbcHeuristicNodeList& nodes);
00066     inline const CbcHeuristicNode* node(int i) const {
00067         return nodes_[i];
00068     }
00069     inline int size() const {
00070         return static_cast<int>(nodes_.size());
00071     }
00072 };
00073 
00074 //#############################################################################
00077 class CbcHeuristic {
00078 private:
00079     void gutsOfDelete() {}
00080     void gutsOfCopy(const CbcHeuristic & rhs);
00081 
00082 public:
00083     // Default Constructor
00084     CbcHeuristic ();
00085 
00086     // Constructor with model - assumed before cuts
00087     CbcHeuristic (CbcModel & model);
00088 
00089     // Copy constructor
00090     CbcHeuristic ( const CbcHeuristic &);
00091 
00092     virtual ~CbcHeuristic();
00093 
00095     virtual CbcHeuristic * clone() const = 0;
00096 
00098     CbcHeuristic & operator=(const CbcHeuristic& rhs);
00099 
00101     virtual void setModel(CbcModel * model);
00102 
00104     virtual void resetModel(CbcModel * model) = 0;
00105 
00111     virtual int solution(double & objectiveValue,
00112                          double * newSolution) = 0;
00113 
00121     virtual int solution2(double & /*objectiveValue*/,
00122                           double * /*newSolution*/,
00123                           OsiCuts & /*cs*/) {
00124         return 0;
00125     }
00126 
00128     virtual void validate() {}
00129 
00134     inline void setWhen(int value) {
00135         when_ = value;
00136     }
00138     inline int when() const {
00139         return when_;
00140     }
00141 
00143     inline void setNumberNodes(int value) {
00144         numberNodes_ = value;
00145     }
00147     inline int numberNodes() const {
00148         return numberNodes_;
00149     }
00160     inline void setSwitches(int value) {
00161         switches_ = value;
00162     }
00174     inline int switches() const {
00175         return switches_;
00176     }
00178     bool exitNow(double bestObjective) const;
00180     inline void setFeasibilityPumpOptions(int value) {
00181         feasibilityPumpOptions_ = value;
00182     }
00184     inline int feasibilityPumpOptions() const {
00185         return feasibilityPumpOptions_;
00186     }
00188     inline void setModelOnly(CbcModel * model) {
00189         model_ = model;
00190     }
00191 
00192 
00194     inline void setFractionSmall(double value) {
00195         fractionSmall_ = value;
00196     }
00198     inline double fractionSmall() const {
00199         return fractionSmall_;
00200     }
00202     inline int numberSolutionsFound() const {
00203         return numberSolutionsFound_;
00204     }
00206     inline void incrementNumberSolutionsFound() {
00207         numberSolutionsFound_++;
00208     }
00209 
00219     int smallBranchAndBound(OsiSolverInterface * solver, int numberNodes,
00220                             double * newSolution, double & newSolutionValue,
00221                             double cutoff , std::string name) const;
00223     virtual void generateCpp( FILE * ) {}
00225     void generateCpp( FILE * fp, const char * heuristic) ;
00227     virtual bool canDealWithOdd() const {
00228         return false;
00229     }
00231     inline const char *heuristicName() const {
00232         return heuristicName_.c_str();
00233     }
00235     inline void setHeuristicName(const char *name) {
00236         heuristicName_ = name;
00237     }
00239     void setSeed(int value);
00241     int getSeed() const;
00243     inline void setDecayFactor(double value) {
00244         decayFactor_ = value;
00245     }
00247     void setInputSolution(const double * solution, double objValue);
00248     /* Runs if bit set
00249         0 - before cuts at root node (or from doHeuristics)
00250         1 - during cuts at root
00251         2 - after root node cuts
00252         3 - after cuts at other nodes
00253         4 - during cuts at other nodes
00254             8 added if previous heuristic in loop found solution
00255      */
00256     inline void setWhereFrom(int value) {
00257         whereFrom_ = value;
00258     }
00259     inline int whereFrom() const {
00260         return whereFrom_;
00261     }
00268     inline void setShallowDepth(int value) {
00269         shallowDepth_ = value;
00270     }
00272     inline void setHowOftenShallow(int value) {
00273         howOftenShallow_ = value;
00274     }
00278     inline void setMinDistanceToRun(int value) {
00279         minDistanceToRun_ = value;
00280     }
00281 
00290     virtual bool shouldHeurRun(int whereFrom);
00292     bool shouldHeurRun_randomChoice();
00293     void debugNodes();
00294     void printDistanceToNodes();
00296     inline int numRuns() const {
00297         return numRuns_;
00298     }
00299 
00301     inline int numCouldRun() const {
00302         return numCouldRun_;
00303     }
00312     OsiSolverInterface * cloneBut(int type);
00313 protected:
00314 
00316     CbcModel * model_;
00318     int when_;
00320     int numberNodes_;
00326     int feasibilityPumpOptions_;
00328     mutable double fractionSmall_;
00330     CoinThreadRandom randomNumberGenerator_;
00332     std::string heuristicName_;
00333 
00335     mutable int howOften_;
00337     double decayFactor_;
00348     mutable int switches_;
00349     /* Runs if bit set
00350         0 - before cuts at root node (or from doHeuristics)
00351         1 - during cuts at root
00352         2 - after root node cuts
00353         3 - after cuts at other nodes
00354         4 - during cuts at other nodes
00355             8 added if previous heuristic in loop found solution
00356      */
00357     int whereFrom_;
00364     int shallowDepth_;
00366     int howOftenShallow_;
00369     int numInvocationsInShallow_;
00372     int numInvocationsInDeep_;
00374     int lastRunDeep_;
00376     int numRuns_;
00380     int minDistanceToRun_;
00381 
00383     CbcHeuristicNodeList runNodes_;
00384 
00386     int numCouldRun_;
00387 
00389     int numberSolutionsFound_;
00390 
00392     mutable int numberNodesDone_;
00393 
00394     // Input solution - so can be used as seed
00395     double * inputSolution_;
00396 
00397 
00398 #ifdef JJF_ZERO
00400     double * lowerBoundLastNode_;
00402     double * upperBoundLastNode_;
00403 #endif
00404 };
00408 class CbcRounding : public CbcHeuristic {
00409 public:
00410 
00411     // Default Constructor
00412     CbcRounding ();
00413 
00414     // Constructor with model - assumed before cuts
00415     CbcRounding (CbcModel & model);
00416 
00417     // Copy constructor
00418     CbcRounding ( const CbcRounding &);
00419 
00420     // Destructor
00421     ~CbcRounding ();
00422 
00424     CbcRounding & operator=(const CbcRounding& rhs);
00425 
00427     virtual CbcHeuristic * clone() const;
00429     virtual void generateCpp( FILE * fp) ;
00430 
00432     virtual void resetModel(CbcModel * model);
00433 
00435     virtual void setModel(CbcModel * model);
00436 
00437     using CbcHeuristic::solution ;
00443     virtual int solution(double & objectiveValue,
00444                          double * newSolution);
00451     virtual int solution(double & objectiveValue,
00452                          double * newSolution,
00453                          double solutionValue);
00455     virtual void validate();
00456 
00457 
00459     void setSeed(int value) {
00460         seed_ = value;
00461     }
00470     virtual bool shouldHeurRun(int whereFrom);
00471 
00472 protected:
00473     // Data
00474 
00475     // Original matrix by column
00476     CoinPackedMatrix matrix_;
00477 
00478     // Original matrix by
00479     CoinPackedMatrix matrixByRow_;
00480 
00481     // Down locks
00482     unsigned short * down_;
00483 
00484     // Up locks
00485     unsigned short * up_;
00486 
00487     // Equality locks
00488     unsigned short * equal_;
00489 
00490     // Seed for random stuff
00491     int seed_;
00492 };
00493 
00499 class CbcHeuristicPartial : public CbcHeuristic {
00500 public:
00501 
00502     // Default Constructor
00503     CbcHeuristicPartial ();
00504 
00509     CbcHeuristicPartial (CbcModel & model, int fixPriority = 10000, int numberNodes = 200);
00510 
00511     // Copy constructor
00512     CbcHeuristicPartial ( const CbcHeuristicPartial &);
00513 
00514     // Destructor
00515     ~CbcHeuristicPartial ();
00516 
00518     CbcHeuristicPartial & operator=(const CbcHeuristicPartial& rhs);
00519 
00521     virtual CbcHeuristic * clone() const;
00523     virtual void generateCpp( FILE * fp) ;
00524 
00526     virtual void resetModel(CbcModel * model);
00527 
00529     virtual void setModel(CbcModel * model);
00530 
00531     using CbcHeuristic::solution ;
00537     virtual int solution(double & objectiveValue,
00538                          double * newSolution);
00540     virtual void validate();
00541 
00542 
00544     void setFixPriority(int value) {
00545         fixPriority_ = value;
00546     }
00547 
00549     virtual bool shouldHeurRun(int whereFrom);
00550 
00551 protected:
00552     // Data
00553 
00554     // All variables with abs priority <= this will be fixed
00555     int fixPriority_;
00556 };
00557 
00562 class CbcSerendipity : public CbcHeuristic {
00563 public:
00564 
00565     // Default Constructor
00566     CbcSerendipity ();
00567 
00568     /* Constructor with model
00569     */
00570     CbcSerendipity (CbcModel & model);
00571 
00572     // Copy constructor
00573     CbcSerendipity ( const CbcSerendipity &);
00574 
00575     // Destructor
00576     ~CbcSerendipity ();
00577 
00579     CbcSerendipity & operator=(const CbcSerendipity& rhs);
00580 
00582     virtual CbcHeuristic * clone() const;
00584     virtual void generateCpp( FILE * fp) ;
00585 
00587     virtual void setModel(CbcModel * model);
00588 
00589     using CbcHeuristic::solution ;
00600     virtual int solution(double & objectiveValue,
00601                          double * newSolution);
00603     virtual void resetModel(CbcModel * model);
00604 
00605 protected:
00606 };
00607 
00611 class CbcHeuristicJustOne : public CbcHeuristic {
00612 public:
00613 
00614     // Default Constructor
00615     CbcHeuristicJustOne ();
00616 
00617     // Constructor with model - assumed before cuts
00618     CbcHeuristicJustOne (CbcModel & model);
00619 
00620     // Copy constructor
00621     CbcHeuristicJustOne ( const CbcHeuristicJustOne &);
00622 
00623     // Destructor
00624     ~CbcHeuristicJustOne ();
00625 
00627     virtual CbcHeuristicJustOne * clone() const;
00628 
00630     CbcHeuristicJustOne & operator=(const CbcHeuristicJustOne& rhs);
00631 
00633     virtual void generateCpp( FILE * fp) ;
00634 
00641     virtual int solution(double & objectiveValue,
00642                          double * newSolution);
00644     virtual void resetModel(CbcModel * model);
00645 
00647     virtual void setModel(CbcModel * model);
00649 
00655     virtual bool selectVariableToBranch(OsiSolverInterface* /*solver*/,
00656                                         const double* /*newSolution*/,
00657                                         int& /*bestColumn*/,
00658                                         int& /*bestRound*/) {
00659         return true;
00660     }
00662     virtual void validate();
00664     void addHeuristic(const CbcHeuristic * heuristic, double probability);
00666     void normalizeProbabilities();
00667 protected:
00668     // Data
00669 
00670     // Probability of running a heuristic
00671     double * probabilities_;
00672 
00673     // Heuristics
00674     CbcHeuristic ** heuristic_;
00675 
00676     // Number of heuristics
00677     int numberHeuristics_;
00678 
00679 };
00680 
00681 #endif
00682 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 9 Feb 2015 for Cbc by  doxygen 1.6.1