10 IAssert(NInfect < Graph->GetNodes());
13 NIdV.
Add(NI.GetId()); }
16 for (
int i = 0; i < NInfect; i++) {
30 int NBurned = NInfect, NDiedFire=0;
34 for (
int time = 0; ; time++) {
35 NewBurnedNIdV.
Clr(
false);
37 for (
int node = 0; node < BurningNIdV.
Len(); node++) {
38 const int& BurningNId = BurningNIdV[node];
43 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
45 if (! BurnedNIdH.
IsKey(OutNId)) {
48 BurnedNIdH.
AddDat(OutNId); NewBurnedNIdV.
Add(OutNId); NBurned++; }
53 for (
int e = 0; e < Node.
GetInDeg(); e++) {
55 if (! BurnedNIdH.
IsKey(InNId)) {
58 BurnedNIdH.
AddDat(InNId); NewBurnedNIdV.
Add(InNId); NBurned++; }
62 if (! HasAliveNbrs) { NDiedFire++; }
68 BurningNIdV.
Swap(NewBurnedNIdV);
69 if (BurningNIdV.
Empty())
break;
74 for (
int i = 0; i < BurnedNIdH.
Len(); i++) {
90 bool HasAliveInNbrs, HasAliveOutNbrs;
92 int NBurned = NInfect, time;
96 for (time = 0; ; time++) {
97 NewBurnedNIdV.
Clr(
false);
98 for (
int node = 0; node < BurningNIdV.
Len(); node++) {
99 const int& BurningNId = BurningNIdV[node];
102 HasAliveOutNbrs =
false;
103 AliveNIdV.
Clr(
false);
104 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
106 if (! BurnedNIdH.
IsKey(OutNId)) {
107 HasAliveOutNbrs =
true; AliveNIdV.
Add(OutNId); }
111 if (HasAliveOutNbrs && BurnNFwdLinks > 0) {
113 for (
int i = 0; i <
TMath::Mn(BurnNFwdLinks, AliveNIdV.
Len()); i++) {
114 BurnedNIdH.
AddDat(AliveNIdV[i]);
115 NewBurnedNIdV.
Add(AliveNIdV[i]); NBurned++; }
120 HasAliveInNbrs =
false;
121 AliveNIdV.
Clr(
false);
122 for (
int e = 0; e < Node.
GetInDeg(); e++) {
124 if (! BurnedNIdH.
IsKey(InNId)) {
125 HasAliveInNbrs =
true; AliveNIdV.
Add(InNId); }
129 if (HasAliveInNbrs && BurnNBckLinks > 0) {
131 for (
int i = 0; i <
TMath::Mn(BurnNBckLinks, AliveNIdV.
Len()); i++) {
132 BurnedNIdH.
AddDat(AliveNIdV[i]);
133 NewBurnedNIdV.
Add(AliveNIdV[i]); NBurned++; }
139 BurningNIdV.
Swap(NewBurnedNIdV);
140 if (BurningNIdV.
Empty())
break;
145 for (
int i = 0; i < BurnedNIdH.
Len(); i++) {
241 TGnuPlot GnuPlot(FNmPref,
TStr::Fmt(
"%s. ForestFire. G(%d, %d). Fwd:%g Bck:%g NInfect:%d",
243 GnuPlot.
SetXYLabel(
"TIME EPOCH",
"Number of NODES");
251 TFfGGen Ff(
false, 1, FwdProb, BckProb, 1.0, 0.0, 0.0);
260 TFfGGen::TFfGGen(
const bool& BurnExpFireP,
const int& StartNNodes,
const double& ForwBurnProb,
261 const double& BackBurnProb,
const double& DecayProb,
const double& Take2AmbasPrb,
const double& OrphanPrb) :
262 Graph(), BurnExpFire(BurnExpFireP), StartNodes(StartNNodes), FwdBurnProb(ForwBurnProb),
263 BckBurnProb(BackBurnProb), ProbDecay(DecayProb), Take2AmbProb(Take2AmbasPrb), OrphanProb(OrphanPrb) {
268 return TStr::Fmt(
"%s FWD:%g BCK:%g, StartNds:%d, Take2:%g, Orphan:%g, ProbDecay:%g",
276 int Burned1=0, Burned2=0, Burned3=0;
287 for (
int NNodes =
Graph->
GetNodes()+1; NNodes <= GraphNodes; NNodes++) {
298 while (AmbassadorNId1 == AmbassadorNId2) {
306 for (
int e = 0; e < ForestFire.
GetBurned(); e++) {
310 Burned1=Burned2; Burned2=Burned3; Burned3=ForestFire.
GetBurned();
313 Burned1=Burned2; Burned2=Burned3; Burned3=0;
315 if (NNodes %
Kilo(1) == 0) {
316 printf(
"(%d, %d) burned: [%d,%d,%d] [%s]\n", NNodes, NEdges, Burned1, Burned2, Burned3, ExeTm.
GetStr()); }
317 if (FloodStop && NEdges>GraphNodes && (NEdges/
double(NNodes)>1000.0)) {
318 printf(
". FLOOD. G(%6d, %6d)\n", NNodes, NEdges);
return srFlood; }
329 return AddNodes(GraphNodes, FloodStop);
333 int GrowthStatNodes = 100;
338 SR =
AddNodes(GrowthStatNodes, FloodStop);
339 if (SR !=
srOk) {
return SR; }
341 GrowthStatNodes = int(1.5*GrowthStatNodes);
349 GnuPlot.
SetXYLabel(
"Vertex id (iterations)",
"Fire size (node out-degree)");
352 IdToOutDegV.
Add(
TFltPr(NI.GetId(), NI.GetOutDeg())); }
359 const int NRuns = 10;
360 const int NNodes = 10000;
369 TFfGGen FF(
false, 1, FProb, BProb, 1.0, 0, 0);
370 for (
int r = 0; r < NRuns; r++) {
373 for (
int i = 0; i < GV->Len(); i++) {
374 if (i == GAtTmV.
Len()) {
377 GAtTmV[i]->
Add(GV->At(i));
382 for (
int i = 0; i < GAtTmV.
Len(); i++) {
383 AvgStat->Add(GAtTmV[i]->GetAvgGStat(
false));
385 AvgStat->PlotAllVsX(
gsvNodes, FNm,
TStr::Fmt(
"Forest Fire: F:%g B:%g (%d runs)", FProb, BProb, NRuns));
386 AvgStat->Last()->PlotAll(FNm,
TStr::Fmt(
"Forest Fire: F:%g B:%g (%d runs)", FProb, BProb, NRuns));
760 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
785 printf(
"\n***Undirected GEO ForestFire: graph(%d,%d) add %d nodes, burn prob %.3f\n",
788 int Burned1=0, Burned2=0, Burned3=0;
795 for (
int NNodes =
Graph->
GetNodes()+1; NNodes <= GraphNodes; NNodes++) {
801 for (
int e = 0; e < NBurned; e++) {
804 Burned1=Burned2; Burned2=Burned3; Burned3=NBurned;
805 if (NNodes %
Kilo(1) == 0) {
806 printf(
"(%d, %d) burned: [%d,%d,%d] [%s]\n", NNodes, NEdges, Burned1, Burned2, Burned3, ExeTm.GetStr());
809 if (FloodStop && NEdges>1000 && NEdges/
double(NNodes)>100.0) {
810 printf(
"!!! FLOOD. G(%6d, %6d)\n", NNodes, NEdges);
return TFfGGen::srFlood; }
void PlotFire(const TStr &FNmPref, const TStr &Desc, const bool &PlotAllBurned=false)
void Clr(const bool &DoDel=true, const int &NoDelLim=-1)
static const T & Mn(const T &LVal, const T &RVal)
TPair< TInt, TInt > TIntPr
int GetBurnedNId(const int &NIdN) const
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
TStopReason GenGraph(const int &GraphNodes, const bool &FloodStop=true)
void SavePng(const int &SizeX=1000, const int &SizeY=800, const TStr &Comment=TStr())
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
int GetEdges() const
Returns the number of edges in the graph.
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
int GetEdges() const
Returns the number of edges in the graph.
TSizeTy Len() const
Returns the number of elements in the vector.
Node iterator. Only forward iteration (operator++) is supported.
void Infect(const int &NodeId)
TFfGGen::TStopReason AddNodes(const int &GraphNodes, const bool &FloodStop=true)
int GetNodes() const
Returns the number of nodes in the graph.
void SetXYLabel(const TStr &XLabel, const TStr &YLabel)
int BurnGeoFire(const int &StartNId)
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
void PlotFireSize(const TStr &FNmPref, const TStr &DescStr)
bool IsKey(const TKey &Key) const
int GetNodes() const
Returns the number of nodes in the graph.
bool Empty() const
Tests whether the vector is empty.
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
static TStr GetUniqueFNm(const TStr &FNm)
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
int AddKey(const TKey &Key)
TPair< TFlt, TFlt > TFltPr
void InfectRnd(const int &NInfect)
TStopReason AddNodes(const int &GraphNodes, const bool &FloodStop=true)
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
TFfGGen(const bool &BurnExpFireP, const int &StartNNodes, const double &ForwBurnProb, const double &BackBurnProb, const double &DecayProb, const double &Take2AmbasPrb, const double &OrphanPrb)
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
int GetOutDeg() const
Returns out-degree of the current node.
static TStr Fmt(const char *FmtStr,...)
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Node iterator. Only forward iteration (operator++) is supported.
int AddPlot(const TIntV &YValV, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
static void GenFFGraphs(const double &FProb, const double &BProb, const TStr &FNm)
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
int GetUniDevInt(const int &Range=0)
int GetGeoDev(const double &Prb)
static TVec< TInt, TSizeTy > GetV(const TInt &Val1)
Returns a vector on element Val1.
int GetInDeg() const
Returns in-degree of the current node.
const char * GetStr() const
bool IsKey(const TKey &Key) const
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
static PGStatVec New(const TTmUnit &_TmUnit=tmu1Sec)
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
static PNGraph GenGraph(const int &Nodes, const double &FwdProb, const double &BckProb)
TDat & AddDat(const TKey &Key)
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
const TKey & GetKey(const int &KeyId) const
int GetBurnedNId(const int &n) const