SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
ff.h
Go to the documentation of this file.
1 #include "bd.h"
2 
3 //#//////////////////////////////////////////////
6 class TForestFire {
7 private:
11  TIntV InfectNIdV; // nodes to start fire
12  TIntV BurnedNIdV; // nodes that got burned (FF model creates edges to them)
13  // statistics
14  TIntV NBurnedTmV, NBurningTmV, NewBurnedTmV; // total burned, currently burning, newly burned in current time step
15 private:
17 public:
18  TForestFire() : Rnd(1), Graph(), FwdBurnProb(0.0), BckBurnProb(0.0), ProbDecay(1.0) { }
19  TForestFire(const PNGraph& GraphPt, const double& ForwBurnProb, const double& BackBurnProb, const double& DecayProb=1.0, const int& RndSeed=1) :
20  Rnd(RndSeed), Graph(GraphPt), FwdBurnProb(ForwBurnProb), BckBurnProb(BackBurnProb), ProbDecay(DecayProb) { }
21 
22  void SetGraph(const PNGraph& GraphPt) { Graph = GraphPt; }
23  PNGraph GetGraph() const { return Graph; }
24  void SetBurnProb(const double& ForwBurnProb, const double& BackBurnProb) { FwdBurnProb=ForwBurnProb; BckBurnProb=BackBurnProb; }
25  void SetProbDecay(const double& DecayProb) { ProbDecay = DecayProb; }
26 
27  void Infect(const int& NodeId) { InfectNIdV.Gen(1,1); InfectNIdV[0] = NodeId; }
28  void Infect(const TIntV& InfectedNIdV) { InfectNIdV = InfectedNIdV; }
29  void InfectAll();
30  void InfectRnd(const int& NInfect);
31 
32  void BurnExpFire(); // burn each link independently with prob BurnProb (burn fixed percentage of links)
33  void BurnGeoFire(); // burn fixed number of links
34 
35  int GetFireTm() const { return NBurnedTmV.Len(); } // time of fire
36  int GetBurned() const { return BurnedNIdV.Len(); }
37  int GetBurnedNId(const int& NIdN) const { return BurnedNIdV[NIdN]; }
38  const TIntV& GetBurnedNIdV() const { return BurnedNIdV; }
39  void GetBurnedNIdV(TIntV& NIdV) const { NIdV = BurnedNIdV; }
40  void PlotFire(const TStr& FNmPref, const TStr& Desc, const bool& PlotAllBurned=false);
41 
42  static PNGraph GenGraph(const int& Nodes, const double& FwdProb, const double& BckProb);
43 };
44 
45 
46 //#//////////////////////////////////////////////
49 class TFfGGen {
50 public:
52  static int TimeLimitSec;
53 private:
55  // parameters
56  TBool BurnExpFire; // burn Exponential or Geometric fire
57  TInt StartNodes; // start a graph with N isolated nodes
58  TFlt FwdBurnProb, BckBurnProb, ProbDecay; // Forest Fire parameters
60 public:
61  TFfGGen(const bool& BurnExpFireP, const int& StartNNodes, const double& ForwBurnProb,
62  const double& BackBurnProb, const double& DecayProb, const double& Take2AmbasPrb, const double& OrphanPrb);
63 
64  PNGraph GetGraph() const { return Graph; }
65  void SetGraph(const PNGraph& NGraph) { Graph = NGraph; }
66  void Clr() { Graph->Clr(); }
67  TStr GetParamStr() const;
68 
69  TStopReason AddNodes(const int& GraphNodes, const bool& FloodStop = true);
70  TStopReason GenGraph(const int& GraphNodes, const bool& FloodStop = true);
71  TStopReason GenGraph(const int& GraphNodes, PGStatVec& EvolStat, const bool& FloodStop=true);
72  void PlotFireSize(const TStr& FNmPref, const TStr& DescStr);
73  static void GenFFGraphs(const double& FProb, const double& BProb, const TStr& FNm);
74 };
75 
76 /*/////////////////////////////////////////////////
77 // Forest Fire Phase Transition
78 ClassTP(TFfPhaseTrans, PFfPhaseTrans)// {
79 private:
80  TBool BurExpFire;
81  TInt NNodes, StartNodes, NRuns;
82  TFlt Take2AmbProb, OrphanProb, ProbInc;
83  THash<TFltPr, PGrowthSet> FBPrGSetH; // (Fwd, Bck) -> GrowthSet
84  THash<TFltPr, PGrowthStat> FBPrGStatH; // (Fwd, Bck) -> AvgGrowthStat
85  TFSet TakeStatSet;
86 public:
87  TFfPhaseTrans(const int& NNds, const int& StartNds, const double& Take2AmbPr,
88  const double& ProbOrphan, const double& ProbIncrement, const int& NRunsPerFB);
89  static PFfPhaseTrans New(const int& NNds, const int& StartNds, const double& Take2AmbPr,
90  const double& ProbOrphan, const double& ProbIncrement, const int& NRunsPerFB);
91  TFfPhaseTrans(TSIn& SIn);
92  static PFfPhaseTrans Load(TSIn& SIn);
93  static PFfPhaseTrans Load(const TStr& InFNm) { TFIn FIn(InFNm); return TFfPhaseTrans::Load(FIn); }
94  void Save(TFOut& SOut) const;
95 
96  int Len() const { return FBPrGSetH.Len(); }
97  void TakeStat(const TFSet& TakeStatFSet) { TakeStatSet = TakeStatFSet; }
98  TStr GetTitleStr(const int& ValN) const;
99  TStr GetFNm(const TStr& FNmPref) const;
100 
101  double GetFProb(const int& ValN) const { return FBPrGSetH.GetKey(ValN).Val1(); }
102  double GetBProb(const int& ValN) const { return FBPrGSetH.GetKey(ValN).Val2(); }
103  TFltPr GetFBProb(const int& ValN) const { return FBPrGSetH.GetKey(ValN); }
104  PGrowthSet GetGSet(const int& ValN) const { return FBPrGSetH[ValN]; }
105  PGrowthStat GetAvgGStat(const int& ValN) const { return FBPrGStatH[ValN]; }
106  TFltPr GetGplCf(const int& ValN) const;
107  TFltPr GetDecDiam(const int& ValN) const;
108  TFltPr GetEffDiam(const int& ValN, const int& AtTick) const;
109  TFltPr LastGplCf() const { return GetGplCf(Len()-1); }
110  TFltPr LastDecDiam() const { return GetDecDiam(Len()-1); }
111 
112  void GetFProbV(TFltV& FProbV) const;
113  TFltPr RunForestFire(double FwdProb, double BckProb, const bool& Plot=true);
114 
115  void FwdProbSteps(const double& MinFwdProb, const double& MaxFwdProb, const double& BckProb);
116  void FwdProbStepsFact(const double& MinFwdProb, const double& MaxFwdProb, const double& BckFact);
117  void FwdBckPhasePlot(const double& MinFwdProb, const double& MaxFwdProb,
118  const double& MinBckFact, const double& MaxBckFact, const int& TimeLimitSec);
119 
120  void FindGplPhaseTr(const double& StartFProb, const double& FollowGplCf, const TStr& FNmPref, const TStr& Desc=TStr());
121  void SaveGplPhaseTr(const double& FollowGplCf, const TStr& FNmPref, const TStr& Desc=TStr());
122  void FindDiamPhaseTr(const double& StartFProb, const double& FollowDiamCf, const TStr& FNmPref, const TStr& Desc=TStr());
123  void SaveDiamPhaseTr(const double& FollowDiamCf, const TStr& FNmPref, const TStr& Desc=TStr());
124 
125  void Merge(const PFfPhaseTrans& FfPhaseTrans);
126  void Merge(const TFfPhaseTrans& FfPhaseTrans);
127 };
128 //*/
129 
130 //#//////////////////////////////////////////////
132 class TUndirFFire {
133 private:
136  double BurnProb;
137  TIntSet BurnedSet; // all burned nodes in the current iteration
139 public:
140  TUndirFFire(const double& _BurnProb=0.3) : Graph(TUNGraph::New()), BurnProb(_BurnProb) { }
141  void SetGraph(const PUNGraph& GraphPt) { Graph = GraphPt; }
142  PUNGraph GetGraph() const { return Graph; }
143  int GetNBurned() const { return BurnedSet.Len(); }
144  int GetBurnedNId(const int& n) const { return BurnedSet[n]; }
145  int BurnGeoFire(const int& StartNId);
146  TFfGGen::TStopReason AddNodes(const int& GraphNodes, const bool& FloodStop=true);
147 };
148 
void PlotFire(const TStr &FNmPref, const TStr &Desc, const bool &PlotAllBurned=false)
Definition: ff.cpp:240
void BurnExpFire()
Definition: ff.cpp:21
PNGraph Graph
Definition: ff.h:54
TBool BurnExpFire
Definition: ff.h:56
int GetBurnedNId(const int &NIdN) const
Definition: ff.h:37
TIntV InfectNIdV
Definition: ff.h:11
int GetFireTm() const
Definition: ff.h:35
Simulates a single Forest Fire on a undirected graph starting for a given starting node (does not den...
Definition: ff.h:132
Definition: dt.h:11
TStopReason GenGraph(const int &GraphNodes, const bool &FloodStop=true)
Definition: ff.cpp:327
void SetBurnProb(const double &ForwBurnProb, const double &BackBurnProb)
Definition: ff.h:24
PUNGraph GetGraph() const
Definition: ff.h:142
TFlt BckBurnProb
Definition: ff.h:58
TIntSet BurnedSet
Definition: ff.h:137
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Infect(const int &NodeId)
Definition: ff.h:27
TFlt OrphanProb
Definition: ff.h:59
TIntV NBurnedTmV
Definition: ff.h:14
TFfGGen::TStopReason AddNodes(const int &GraphNodes, const bool &FloodStop=true)
Definition: ff.cpp:784
double BurnProb
Definition: ff.h:136
const TIntV & GetBurnedNIdV() const
Definition: ff.h:38
int BurnGeoFire(const int &StartNId)
Definition: ff.cpp:745
TIntV NewBurnedNIdV
Definition: ff.h:138
void Clr()
Definition: ff.h:66
void PlotFireSize(const TStr &FNmPref, const TStr &DescStr)
Definition: ff.cpp:346
TIntV NBurningTmV
Definition: ff.h:14
TFlt ProbDecay
Definition: ff.h:58
Definition: ff.h:6
void Infect(const TIntV &InfectedNIdV)
Definition: ff.h:28
static int TimeLimitSec
Definition: ff.h:52
TStopReason
Definition: ff.h:51
Definition: dt.h:1386
void InfectAll()
Definition: ff.cpp:3
TStr GetParamStr() const
Definition: ff.cpp:267
void SetProbDecay(const double &DecayProb)
Definition: ff.h:25
Undirected graph.
Definition: graph.h:32
PUNGraph Graph
Definition: ff.h:135
TUndirFFire(const double &_BurnProb=0.3)
Definition: ff.h:140
TIntV AliveNIdV
Definition: ff.h:138
TIntV BurningNIdV
Definition: ff.h:138
TRnd Rnd
Definition: ff.h:8
TForestFire()
Definition: ff.h:18
void InfectRnd(const int &NInfect)
Definition: ff.cpp:9
TStopReason AddNodes(const int &GraphNodes, const bool &FloodStop=true)
Definition: ff.cpp:272
void SetGraph(const PNGraph &NGraph)
Definition: ff.h:65
void Clr()
Deletes all nodes and edges from the graph.
Definition: graph.h:612
TFfGGen(const bool &BurnExpFireP, const int &StartNNodes, const double &ForwBurnProb, const double &BackBurnProb, const double &DecayProb, const double &Take2AmbasPrb, const double &OrphanPrb)
Definition: ff.cpp:260
TFlt BckBurnProb
Definition: ff.h:10
Definition: dt.h:1137
UndefCopyAssign(TForestFire)
void SetGraph(const PUNGraph &GraphPt)
Definition: ff.h:141
void BurnGeoFire()
Definition: ff.cpp:82
int Len() const
Definition: shash.h:1121
TFlt ProbDecay
Definition: ff.h:10
TRnd Rnd
Definition: ff.h:134
TFlt FwdBurnProb
Definition: ff.h:10
Definition: dt.h:412
void SetGraph(const PNGraph &GraphPt)
Definition: ff.h:22
PNGraph GetGraph() const
Definition: ff.h:64
TFlt FwdBurnProb
Definition: ff.h:58
int GetBurned() const
Definition: ff.h:36
static void GenFFGraphs(const double &FProb, const double &BProb, const TStr &FNm)
Definition: ff.cpp:358
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TForestFire(const PNGraph &GraphPt, const double &ForwBurnProb, const double &BackBurnProb, const double &DecayProb=1.0, const int &RndSeed=1)
Definition: ff.h:19
int GetNBurned() const
Definition: ff.h:143
TFlt Take2AmbProb
Definition: ff.h:59
TInt StartNodes
Definition: ff.h:57
static PNGraph GenGraph(const int &Nodes, const double &FwdProb, const double &BckProb)
Definition: ff.cpp:250
TIntV BurnedNIdV
Definition: ff.h:12
Definition: dt.h:974
PNGraph Graph
Definition: ff.h:9
TIntV NewBurnedTmV
Definition: ff.h:14
Definition: ff.h:49
PNGraph GetGraph() const
Definition: ff.h:23
int GetBurnedNId(const int &n) const
Definition: ff.h:144
void GetBurnedNIdV(TIntV &NIdV) const
Definition: ff.h:39