Example #1
0
// burn each link independently (forward with FwdBurnProb, backward with BckBurnProb)
void TForestFire::BurnExpFire() {
  const double OldFwdBurnProb = FwdBurnProb;
  const double OldBckBurnProb = BckBurnProb;
  const int NInfect = InfectNIdV.Len();
  const TNGraph& G = *Graph;
  TIntH BurnedNIdH;               // burned nodes
  TIntV BurningNIdV = InfectNIdV; // currently burning nodes
  TIntV NewBurnedNIdV;            // nodes newly burned in current step
  bool HasAliveNbrs;              // has unburned neighbors
  int NBurned = NInfect, NDiedFire=0;
  for (int i = 0; i < InfectNIdV.Len(); i++) {
    BurnedNIdH.AddDat(InfectNIdV[i]); }
  NBurnedTmV.Clr(false);  NBurningTmV.Clr(false);  NewBurnedTmV.Clr(false);
  for (int time = 0; ; time++) {
    NewBurnedNIdV.Clr(false);
    // for each burning node
    for (int node = 0; node < BurningNIdV.Len(); node++) {
      const int& BurningNId = BurningNIdV[node];
      const TNGraph::TNodeI Node = G.GetNI(BurningNId);
      HasAliveNbrs = false;
      NDiedFire = 0;
      // burn forward links  (out-links)
      for (int e = 0; e < Node.GetOutDeg(); e++) {
        const int OutNId = Node.GetOutNId(e);
        if (! BurnedNIdH.IsKey(OutNId)) { // not yet burned
          HasAliveNbrs = true;
          if (Rnd.GetUniDev() < FwdBurnProb) {
            BurnedNIdH.AddDat(OutNId);  NewBurnedNIdV.Add(OutNId);  NBurned++; }
        }
      }
      // burn backward links (in-links)
      if (BckBurnProb > 0.0) {
        for (int e = 0; e < Node.GetInDeg(); e++) {
          const int InNId = Node.GetInNId(e);
          if (! BurnedNIdH.IsKey(InNId)) { // not yet burned
            HasAliveNbrs = true;
            if (Rnd.GetUniDev() < BckBurnProb) {
              BurnedNIdH.AddDat(InNId);  NewBurnedNIdV.Add(InNId);  NBurned++; }
          }
        }
      }
      if (! HasAliveNbrs) { NDiedFire++; }
    }
    NBurnedTmV.Add(NBurned);
    NBurningTmV.Add(BurningNIdV.Len() - NDiedFire);
    NewBurnedTmV.Add(NewBurnedNIdV.Len());
    //BurningNIdV.AddV(NewBurnedNIdV);   // node is burning eternally
    BurningNIdV.Swap(NewBurnedNIdV);    // node is burning just 1 time step
    if (BurningNIdV.Empty()) break;
    FwdBurnProb = FwdBurnProb * ProbDecay;
    BckBurnProb = BckBurnProb * ProbDecay;
  }
  BurnedNIdV.Gen(BurnedNIdH.Len(), 0);
  for (int i = 0; i < BurnedNIdH.Len(); i++) {
    BurnedNIdV.Add(BurnedNIdH.GetKey(i)); }
  FwdBurnProb = OldFwdBurnProb;
  BckBurnProb = OldBckBurnProb;
}
Example #2
0
// Node selects N~geometric(1.0-FwdBurnProb)-1 out-links and burns them. Then same for in-links.
// geometirc(p) has mean 1/(p), so for given FwdBurnProb, we burn 1/(1-FwdBurnProb)
void TForestFire::BurnGeoFire() {
	const double OldFwdBurnProb = FwdBurnProb;
	const double OldBckBurnProb = BckBurnProb;
	const int& NInfect = InfectNIdV.Len();
	const TNGraph& G = *Graph;
	TIntH BurnedNIdH;               // burned nodes
	TIntV BurningNIdV = InfectNIdV; // currently burning nodes
	TIntV NewBurnedNIdV;            // nodes newly burned in current step
	bool HasAliveInNbrs, HasAliveOutNbrs; // has unburned neighbors
	TIntV AliveNIdV;                // NIds of alive neighbors
	int NBurned = NInfect, time;
	for (int i = 0; i < InfectNIdV.Len(); i++) {
		BurnedNIdH.AddDat(InfectNIdV[i]);
	}
	NBurnedTmV.Clr(false);  NBurningTmV.Clr(false);  NewBurnedTmV.Clr(false);
	for (time = 0;; time++) {
		NewBurnedNIdV.Clr(false);
		for (int node = 0; node < BurningNIdV.Len(); node++) {
			const int& BurningNId = BurningNIdV[node];
			const TNGraph::TNodeI Node = G.GetNI(BurningNId);
			// find unburned links
			HasAliveOutNbrs = false;
			AliveNIdV.Clr(false); // unburned links
			for (int e = 0; e < Node.GetOutDeg(); e++) {
				const int OutNId = Node.GetOutNId(e);
				if (!BurnedNIdH.IsKey(OutNId)) {
					HasAliveOutNbrs = true;  AliveNIdV.Add(OutNId);
				}
			}
			// number of links to burn (geometric coin). Can also burn 0 links
			const int BurnNFwdLinks = Rnd.GetGeoDev(1.0 - FwdBurnProb) - 1;
			if (HasAliveOutNbrs && BurnNFwdLinks > 0) {
				AliveNIdV.Shuffle(Rnd);
				for (int i = 0; i < TMath::Mn(BurnNFwdLinks, AliveNIdV.Len()); i++) {
					BurnedNIdH.AddDat(AliveNIdV[i]);
					NewBurnedNIdV.Add(AliveNIdV[i]);  NBurned++;
				}
			}
			// backward links
			if (BckBurnProb > 0.0) {
				// find unburned links
				HasAliveInNbrs = false;
				AliveNIdV.Clr(false);
				for (int e = 0; e < Node.GetInDeg(); e++) {
					const int InNId = Node.GetInNId(e);
					if (!BurnedNIdH.IsKey(InNId)) {
						HasAliveInNbrs = true;  AliveNIdV.Add(InNId);
					}
				}
				// number of links to burn (geometric coin). Can also burn 0 links
				const int BurnNBckLinks = Rnd.GetGeoDev(1.0 - BckBurnProb) - 1;
				if (HasAliveInNbrs && BurnNBckLinks > 0) {
					AliveNIdV.Shuffle(Rnd);
					for (int i = 0; i < TMath::Mn(BurnNBckLinks, AliveNIdV.Len()); i++) {
						BurnedNIdH.AddDat(AliveNIdV[i]);
						NewBurnedNIdV.Add(AliveNIdV[i]);  NBurned++;
					}
				}
			}
		}
		NBurnedTmV.Add(NBurned);  NBurningTmV.Add(BurningNIdV.Len());  NewBurnedTmV.Add(NewBurnedNIdV.Len());
		// BurningNIdV.AddV(NewBurnedNIdV);   // node is burning eternally
		BurningNIdV.Swap(NewBurnedNIdV);   // node is burning just 1 time step
		if (BurningNIdV.Empty()) break;
		FwdBurnProb = FwdBurnProb * ProbDecay;
		BckBurnProb = BckBurnProb * ProbDecay;
	}
	BurnedNIdV.Gen(BurnedNIdH.Len(), 0);
	for (int i = 0; i < BurnedNIdH.Len(); i++) {
		BurnedNIdV.Add(BurnedNIdH.GetKey(i));
	}
	FwdBurnProb = OldFwdBurnProb;
	BckBurnProb = OldBckBurnProb;
}