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