00001
00002
00003 #ifndef CbcHeuristic_H
00004 #define CbcHeuristic_H
00005
00006 #include <string>
00007 #include <vector>
00008 #include "CoinPackedMatrix.hpp"
00009 #include "OsiCuts.hpp"
00010 #include "CoinHelperFunctions.hpp"
00011 #include "OsiBranchingObject.hpp"
00012
00013 class OsiSolverInterface;
00014
00015 class CbcModel;
00016
00017
00018
00019 class CbcHeuristicNodeList;
00020 class CbcBranchingObject;
00021
00025 class CbcHeuristicNode {
00026 private:
00027 void gutsOfConstructor(CbcModel& model);
00028 CbcHeuristicNode();
00029 CbcHeuristicNode& operator=(const CbcHeuristicNode&);
00030 private:
00032 int numObjects_;
00036 CbcBranchingObject** brObj_;
00037 public:
00038 CbcHeuristicNode(CbcModel& model);
00039
00040 CbcHeuristicNode(const CbcHeuristicNode& rhs);
00041 ~CbcHeuristicNode();
00042 double distance(const CbcHeuristicNode* node) const;
00043 double minDistance(const CbcHeuristicNodeList& nodeList) const;
00044 bool minDistanceIsSmall(const CbcHeuristicNodeList& nodeList,
00045 const double threshold) const;
00046 double avgDistance(const CbcHeuristicNodeList& nodeList) const;
00047 };
00048
00049 class CbcHeuristicNodeList {
00050 private:
00051 void gutsOfDelete();
00052 void gutsOfCopy(const CbcHeuristicNodeList& rhs);
00053 private:
00054 std::vector<CbcHeuristicNode*> nodes_;
00055 public:
00056 CbcHeuristicNodeList() {}
00057 CbcHeuristicNodeList(const CbcHeuristicNodeList& rhs);
00058 CbcHeuristicNodeList& operator=(const CbcHeuristicNodeList& rhs);
00059 ~CbcHeuristicNodeList();
00060
00061 void append(CbcHeuristicNode*& node);
00062 void append(const CbcHeuristicNodeList& nodes);
00063 inline const CbcHeuristicNode* node(int i) const { return nodes_[i]; }
00064 inline int size() const { return nodes_.size(); }
00065 };
00066
00067
00070 class CbcHeuristic {
00071 private:
00072 void gutsOfDelete() {}
00073 void gutsOfCopy(const CbcHeuristic & rhs);
00074
00075 public:
00076
00077 CbcHeuristic ();
00078
00079
00080 CbcHeuristic (CbcModel & model);
00081
00082
00083 CbcHeuristic ( const CbcHeuristic &);
00084
00085 virtual ~CbcHeuristic();
00086
00088 virtual CbcHeuristic * clone() const=0;
00089
00091 CbcHeuristic & operator=(const CbcHeuristic& rhs);
00092
00094 virtual void setModel(CbcModel * model);
00095
00097 virtual void resetModel(CbcModel * model)=0;
00098
00104 virtual int solution(double & objectiveValue,
00105 double * newSolution)=0;
00106
00114 virtual int solution(double & objectiveValue,
00115 double * newSolution,
00116 OsiCuts & cs) {return 0;}
00117
00119 virtual void validate() {}
00120
00125 inline void setWhen(int value)
00126 { when_=value;}
00128 inline int when() const
00129 { return when_;}
00130
00132 inline void setNumberNodes(int value)
00133 { numberNodes_=value;}
00135 inline int numberNodes() const
00136 { return numberNodes_;}
00138 inline void setFeasibilityPumpOptions(int value)
00139 { feasibilityPumpOptions_=value;}
00141 inline int feasibilityPumpOptions() const
00142 { return feasibilityPumpOptions_;}
00144 inline void setModelOnly(CbcModel * model)
00145 { model_ = model;}
00146
00147
00149 inline void setFractionSmall(double value)
00150 { fractionSmall_=value;}
00152 inline double fractionSmall() const
00153 { return fractionSmall_;}
00155 inline int numberSolutionsFound() const
00156 { return numberSolutionsFound_;}
00158 inline void incrementNumberSolutionsFound()
00159 { numberSolutionsFound_++;}
00160
00168 int smallBranchAndBound(OsiSolverInterface * solver,int numberNodes,
00169 double * newSolution, double & newSolutionValue,
00170 double cutoff , std::string name) const;
00172 virtual void generateCpp( FILE * fp) {}
00174 void generateCpp( FILE * fp,const char * heuristic) ;
00176 virtual bool canDealWithOdd() const
00177 { return false;}
00179 inline const char *heuristicName() const
00180 { return heuristicName_.c_str();}
00182 inline void setHeuristicName(const char *name)
00183 { heuristicName_ = name;}
00185 void setSeed(int value);
00187 void setInputSolution(const double * solution, double objValue);
00188
00190 virtual bool shouldHeurRun();
00192 bool shouldHeurRun_randomChoice();
00193 void debugNodes();
00194 void printDistanceToNodes();
00196 inline int numRuns() const
00197 { return numRuns_;}
00198
00200 inline int numCouldRun() const
00201 { return numCouldRun_;}
00202
00203 protected:
00204
00206 CbcModel * model_;
00208 int when_;
00210 int numberNodes_;
00212 int feasibilityPumpOptions_;
00214 mutable double fractionSmall_;
00216 CoinThreadRandom randomNumberGenerator_;
00218 std::string heuristicName_;
00219
00221 int howOften_;
00223 double decayFactor_;
00230 int shallowDepth_;
00232 int howOftenShallow_;
00235 int numInvocationsInShallow_;
00238 int numInvocationsInDeep_;
00240 int lastRunDeep_;
00242 int numRuns_;
00246 int minDistanceToRun_;
00247
00249 CbcHeuristicNodeList runNodes_;
00250
00252 int numCouldRun_;
00253
00255 int numberSolutionsFound_;
00256
00257
00258 double * inputSolution_;
00259
00260
00261 #if 0
00263 double * lowerBoundLastNode_;
00265 double * upperBoundLastNode_;
00266 #endif
00267 };
00271 class CbcRounding : public CbcHeuristic {
00272 public:
00273
00274
00275 CbcRounding ();
00276
00277
00278 CbcRounding (CbcModel & model);
00279
00280
00281 CbcRounding ( const CbcRounding &);
00282
00283
00284 ~CbcRounding ();
00285
00287 CbcRounding & operator=(const CbcRounding& rhs);
00288
00290 virtual CbcHeuristic * clone() const;
00292 virtual void generateCpp( FILE * fp) ;
00293
00295 virtual void resetModel(CbcModel * model);
00296
00298 virtual void setModel(CbcModel * model);
00299
00300 using CbcHeuristic::solution ;
00306 virtual int solution(double & objectiveValue,
00307 double * newSolution);
00314 virtual int solution(double & objectiveValue,
00315 double * newSolution,
00316 double solutionValue);
00318 virtual void validate();
00319
00320
00322 void setSeed(int value)
00323 { seed_ = value;}
00324
00325 protected:
00326
00327
00328
00329 CoinPackedMatrix matrix_;
00330
00331
00332 CoinPackedMatrix matrixByRow_;
00333
00334
00335 unsigned short * down_;
00336
00337
00338 unsigned short * up_;
00339
00340
00341 unsigned short * equal_;
00342
00343
00344 int seed_;
00345 };
00346
00352 class CbcHeuristicPartial : public CbcHeuristic {
00353 public:
00354
00355
00356 CbcHeuristicPartial ();
00357
00362 CbcHeuristicPartial (CbcModel & model, int fixPriority=10000, int numberNodes=200);
00363
00364
00365 CbcHeuristicPartial ( const CbcHeuristicPartial &);
00366
00367
00368 ~CbcHeuristicPartial ();
00369
00371 CbcHeuristicPartial & operator=(const CbcHeuristicPartial& rhs);
00372
00374 virtual CbcHeuristic * clone() const;
00376 virtual void generateCpp( FILE * fp) ;
00377
00379 virtual void resetModel(CbcModel * model);
00380
00382 virtual void setModel(CbcModel * model);
00383
00384 using CbcHeuristic::solution ;
00390 virtual int solution(double & objectiveValue,
00391 double * newSolution);
00393 virtual void validate();
00394
00395
00397 void setFixPriority(int value)
00398 { fixPriority_ = value;}
00399
00401 virtual bool shouldHeurRun();
00402
00403 protected:
00404
00405
00406
00407 int fixPriority_;
00408 };
00409
00414 class CbcSerendipity : public CbcHeuristic {
00415 public:
00416
00417
00418 CbcSerendipity ();
00419
00420
00421
00422 CbcSerendipity (CbcModel & model);
00423
00424
00425 CbcSerendipity ( const CbcSerendipity &);
00426
00427
00428 ~CbcSerendipity ();
00429
00431 CbcSerendipity & operator=(const CbcSerendipity& rhs);
00432
00434 virtual CbcHeuristic * clone() const;
00436 virtual void generateCpp( FILE * fp) ;
00437
00439 virtual void setModel(CbcModel * model);
00440
00441 using CbcHeuristic::solution ;
00452 virtual int solution(double & objectiveValue,
00453 double * newSolution);
00455 virtual void resetModel(CbcModel * model);
00456
00457 protected:
00458 };
00459
00463 class CbcHeuristicJustOne : public CbcHeuristic {
00464 public:
00465
00466
00467 CbcHeuristicJustOne ();
00468
00469
00470 CbcHeuristicJustOne (CbcModel & model);
00471
00472
00473 CbcHeuristicJustOne ( const CbcHeuristicJustOne &);
00474
00475
00476 ~CbcHeuristicJustOne ();
00477
00479 virtual CbcHeuristicJustOne * clone() const;
00480
00482 CbcHeuristicJustOne & operator=(const CbcHeuristicJustOne& rhs);
00483
00485 virtual void generateCpp( FILE * fp) ;
00486
00493 virtual int solution(double & objectiveValue,
00494 double * newSolution);
00496 virtual void resetModel(CbcModel * model);
00497
00499 virtual void setModel(CbcModel * model);
00501
00507 virtual bool selectVariableToBranch(OsiSolverInterface* solver,
00508 const double* newSolution,
00509 int& bestColumn,
00510 int& bestRound)
00511 { return true;}
00513 virtual void validate();
00515 void addHeuristic(const CbcHeuristic * heuristic, double probability);
00517 void normalizeProbabilities();
00518 protected:
00519
00520
00521
00522 double * probabilities_;
00523
00524
00525 CbcHeuristic ** heuristic_;
00526
00527
00528 int numberHeuristics_;
00529
00530 };
00531
00532 #endif