SNAP Library, Developer Reference
2012-10-15 15:06:59
SNAP, a general purpose network analysis and graph mining library
|
00001 00002 00003 00004 class TForestFire { 00005 private: 00006 TRnd Rnd; 00007 PNGraph Graph; 00008 TFlt FwdBurnProb, BckBurnProb, ProbDecay; 00009 TIntV InfectNIdV; // nodes to start fire 00010 TIntV BurnedNIdV; // nodes that got burned (FF model creates edges to them) 00011 // statistics 00012 TIntV NBurnedTmV, NBurningTmV, NewBurnedTmV; // total burned, currently burning, newly burned in current time step 00013 private: 00014 UndefCopyAssign(TForestFire); 00015 public: 00016 TForestFire() : Rnd(1), Graph(), FwdBurnProb(0.0), BckBurnProb(0.0), ProbDecay(1.0) { } 00017 TForestFire(const PNGraph& GraphPt, const double& ForwBurnProb, const double& BackBurnProb, const double& DecayProb=1.0, const int& RndSeed=1) : 00018 Rnd(RndSeed), Graph(GraphPt), FwdBurnProb(ForwBurnProb), BckBurnProb(BackBurnProb), ProbDecay(DecayProb) { } 00019 00020 void SetGraph(const PNGraph& GraphPt) { Graph = GraphPt; } 00021 PNGraph GetGraph() const { return Graph; } 00022 void SetBurnProb(const double& ForwBurnProb, const double& BackBurnProb) { FwdBurnProb=ForwBurnProb; BckBurnProb=BackBurnProb; } 00023 void SetProbDecay(const double& DecayProb) { ProbDecay = DecayProb; } 00024 00025 void Infect(const int& NodeId) { InfectNIdV.Gen(1,1); InfectNIdV[0] = NodeId; } 00026 void Infect(const TIntV& InfectedNIdV) { InfectNIdV = InfectedNIdV; } 00027 void InfectAll(); 00028 void InfectRnd(const int& NInfect); 00029 00030 void BurnExpFire(); // burn each link independently with prob BurnProb (burn fixed percentage of links) 00031 void BurnGeoFire(); // burn fixed number of links 00032 00033 int GetFireTm() const { return NBurnedTmV.Len(); } // time of fire 00034 int GetBurned() const { return BurnedNIdV.Len(); } 00035 int GetBurnedNId(const int& NIdN) const { return BurnedNIdV[NIdN]; } 00036 const TIntV& GetBurnedNIdV() const { return BurnedNIdV; } 00037 void GetBurnedNIdV(TIntV& NIdV) const { NIdV = BurnedNIdV; } 00038 void PlotFire(const TStr& FNmPref, const TStr& Desc, const bool& PlotAllBurned=false); 00039 00040 static PNGraph GenGraph(const int& Nodes, const double& FwdProb, const double& BckProb); 00041 }; 00042 00043 00047 class TFfGGen { 00048 public: 00049 typedef enum { srUndef, srOk, srFlood, srTimeLimit } TStopReason; 00050 static int TimeLimitSec; 00051 private: 00052 PNGraph Graph; 00053 // parameters 00054 TBool BurnExpFire; // burn Exponential or Geometric fire 00055 TInt StartNodes; // start a graph with N isolated nodes 00056 TFlt FwdBurnProb, BckBurnProb, ProbDecay; // Forest Fire parameters 00057 TFlt Take2AmbProb, OrphanProb; 00058 public: 00059 TFfGGen(const bool& BurnExpFireP, const int& StartNNodes, const double& ForwBurnProb, 00060 const double& BackBurnProb, const double& DecayProb, const double& Take2AmbasPrb, const double& OrphanPrb); 00061 00062 PNGraph GetGraph() const { return Graph; } 00063 void SetGraph(const PNGraph& NGraph) { Graph = NGraph; } 00064 void Clr() { Graph->Clr(); } 00065 TStr GetParamStr() const; 00066 00067 TStopReason AddNodes(const int& GraphNodes, const bool& FloodStop = true); 00068 TStopReason GenGraph(const int& GraphNodes, const bool& FloodStop = true); 00069 TStopReason GenGraph(const int& GraphNodes, PGStatVec& EvolStat, const bool& FloodStop=true); 00070 void PlotFireSize(const TStr& FNmPref, const TStr& DescStr); 00071 static void GenFFGraphs(const double& FProb, const double& BProb, const TStr& FNm); 00072 }; 00073 00074 /*///////////////////////////////////////////////// 00075 // Forest Fire Phase Transition 00076 ClassTP(TFfPhaseTrans, PFfPhaseTrans)// { 00077 private: 00078 TBool BurExpFire; 00079 TInt NNodes, StartNodes, NRuns; 00080 TFlt Take2AmbProb, OrphanProb, ProbInc; 00081 THash<TFltPr, PGrowthSet> FBPrGSetH; // (Fwd, Bck) -> GrowthSet 00082 THash<TFltPr, PGrowthStat> FBPrGStatH; // (Fwd, Bck) -> AvgGrowthStat 00083 TFSet TakeStatSet; 00084 public: 00085 TFfPhaseTrans(const int& NNds, const int& StartNds, const double& Take2AmbPr, 00086 const double& ProbOrphan, const double& ProbIncrement, const int& NRunsPerFB); 00087 static PFfPhaseTrans New(const int& NNds, const int& StartNds, const double& Take2AmbPr, 00088 const double& ProbOrphan, const double& ProbIncrement, const int& NRunsPerFB); 00089 TFfPhaseTrans(TSIn& SIn); 00090 static PFfPhaseTrans Load(TSIn& SIn); 00091 static PFfPhaseTrans Load(const TStr& InFNm) { TFIn FIn(InFNm); return TFfPhaseTrans::Load(FIn); } 00092 void Save(TFOut& SOut) const; 00093 00094 int Len() const { return FBPrGSetH.Len(); } 00095 void TakeStat(const TFSet& TakeStatFSet) { TakeStatSet = TakeStatFSet; } 00096 TStr GetTitleStr(const int& ValN) const; 00097 TStr GetFNm(const TStr& FNmPref) const; 00098 00099 double GetFProb(const int& ValN) const { return FBPrGSetH.GetKey(ValN).Val1(); } 00100 double GetBProb(const int& ValN) const { return FBPrGSetH.GetKey(ValN).Val2(); } 00101 TFltPr GetFBProb(const int& ValN) const { return FBPrGSetH.GetKey(ValN); } 00102 PGrowthSet GetGSet(const int& ValN) const { return FBPrGSetH[ValN]; } 00103 PGrowthStat GetAvgGStat(const int& ValN) const { return FBPrGStatH[ValN]; } 00104 TFltPr GetGplCf(const int& ValN) const; 00105 TFltPr GetDecDiam(const int& ValN) const; 00106 TFltPr GetEffDiam(const int& ValN, const int& AtTick) const; 00107 TFltPr LastGplCf() const { return GetGplCf(Len()-1); } 00108 TFltPr LastDecDiam() const { return GetDecDiam(Len()-1); } 00109 00110 void GetFProbV(TFltV& FProbV) const; 00111 TFltPr RunForestFire(double FwdProb, double BckProb, const bool& Plot=true); 00112 00113 void FwdProbSteps(const double& MinFwdProb, const double& MaxFwdProb, const double& BckProb); 00114 void FwdProbStepsFact(const double& MinFwdProb, const double& MaxFwdProb, const double& BckFact); 00115 void FwdBckPhasePlot(const double& MinFwdProb, const double& MaxFwdProb, 00116 const double& MinBckFact, const double& MaxBckFact, const int& TimeLimitSec); 00117 00118 void FindGplPhaseTr(const double& StartFProb, const double& FollowGplCf, const TStr& FNmPref, const TStr& Desc=TStr()); 00119 void SaveGplPhaseTr(const double& FollowGplCf, const TStr& FNmPref, const TStr& Desc=TStr()); 00120 void FindDiamPhaseTr(const double& StartFProb, const double& FollowDiamCf, const TStr& FNmPref, const TStr& Desc=TStr()); 00121 void SaveDiamPhaseTr(const double& FollowDiamCf, const TStr& FNmPref, const TStr& Desc=TStr()); 00122 00123 void Merge(const PFfPhaseTrans& FfPhaseTrans); 00124 void Merge(const TFfPhaseTrans& FfPhaseTrans); 00125 }; 00126 //*/ 00127 00130 class TUndirFFire { 00131 private: 00132 TRnd Rnd; 00133 PUNGraph Graph; 00134 double BurnProb; 00135 TIntSet BurnedSet; // all burned nodes in the current iteration 00136 TIntV BurningNIdV, NewBurnedNIdV, AliveNIdV; // temporary 00137 public: 00138 TUndirFFire(const double& _BurnProb=0.3) : Graph(TUNGraph::New()), BurnProb(_BurnProb) { } 00139 void SetGraph(const PUNGraph& GraphPt) { Graph = GraphPt; } 00140 PUNGraph GetGraph() const { return Graph; } 00141 int GetNBurned() const { return BurnedSet.Len(); } 00142 int GetBurnedNId(const int& n) const { return BurnedSet[n]; } 00143 int BurnGeoFire(const int& StartNId); 00144 TFfGGen::TStopReason AddNodes(const int& GraphNodes, const bool& FloodStop=true); 00145 }; 00146