Changed a bit the core mechanics with a slight improvement in performance

This commit is contained in:
M.Rohmer
2022-03-05 17:14:35 +01:00
parent 3a561f2d0a
commit 8316c5204a
2 changed files with 92 additions and 62 deletions
+26 -24
View File
@@ -17,10 +17,10 @@ namespace PropagationRemasteredBeta
const int DEFAULT_INFECTION_RATE = 20;
const int DEFAULT_CURE_TIME = 25;
const int DEBUGGER_PLACEHOLDER = 86758;
private Human[,] _population;
private List<Human> _peopleToCheck;
private Size _terrainSize;
private int _infectedProportion;
private int _immuneProportion;
private int _deathRate;
@@ -28,9 +28,6 @@ namespace PropagationRemasteredBeta
private int _cureTime;
private Bitmap _render;
private Human[,] oldTerrain;
private Human[,] newTerrain;
private List<int> _sainCount;
private List<int> _infectedCount;
private List<int> _immuneCount;
@@ -56,6 +53,8 @@ namespace PropagationRemasteredBeta
public List<int> ImmuneCount { get => _immuneCount; set => _immuneCount = value; }
public List<int> DeadCount { get => _deadCount; set => _deadCount = value; }
public int SimulationDuration { get => _simulationDuration; set => _simulationDuration = value; }
public List<Human> PeopleToCheck { get => _peopleToCheck; set => _peopleToCheck = value; }
public Random Rnd { get => rnd; set => rnd = value; }
public Terrain(Size terrainSize, int infectedProportion, int resistantProportion, int deathRate, int infectionRate, int cureTime)
{
@@ -66,10 +65,7 @@ namespace PropagationRemasteredBeta
DeathRate = deathRate;
InfectionRate = infectionRate;
CureTime = cureTime;
rnd = new Random();
oldTerrain = null;
newTerrain = null;
Rnd = new Random();
//all of this is for stats purposes
@@ -96,13 +92,13 @@ namespace PropagationRemasteredBeta
state = Human.SAIN;
if (rnd.Next(0, 100) < InfectedProportion)
if (Rnd.Next(0, 100) < InfectedProportion)
{
state = Human.INFECTED;
}
else
{
if (rnd.Next(0, 100) < ImmuneProportion)
if (Rnd.Next(0, 100) < ImmuneProportion)
{
state = Human.IMMUNE;
}
@@ -123,7 +119,7 @@ namespace PropagationRemasteredBeta
state = Human.SAIN;
if (rnd.Next(0, 100) < ImmuneProportion)
if (Rnd.Next(0, 100) < ImmuneProportion)
{
state = Human.IMMUNE;
}
@@ -133,8 +129,8 @@ namespace PropagationRemasteredBeta
}
}
//we generate a single infected in a random location
int x = rnd.Next(0, TerrainSize.Width);
int y = rnd.Next(0, TerrainSize.Height);
int x = Rnd.Next(0, TerrainSize.Width);
int y = Rnd.Next(0, TerrainSize.Height);
Population[x, y].State = Human.INFECTED;
}
@@ -146,22 +142,24 @@ namespace PropagationRemasteredBeta
InfectedCount.Add(0);
DeadCount.Add(0);
oldTerrain = Population;
newTerrain = oldTerrain;
PeopleToCheck = new List<Human>();
for (int width = 0; width < TerrainSize.Width; width += 1)
{
//Parallel.For(0, TerrainSize.Height, height =>
for (int height = 0; height < TerrainSize.Height; height += 1)
{
//we also send the random so if one day we want to add a seed feature it will also affect the transmission
if (Population[width, height].State != Human.SAIN && Population[width, height].State != Human.DEAD && Population[width, height].State != Human.IMMUNE)
switch (Population[width, height].State)
{
Population[width, height].Propagate(oldTerrain, newTerrain, InfectionRate, DeathRate, CureTime, rnd);
}
else
{
newTerrain[width, height] = Population[width, height];
case Human.INFECTED:
Population[width, height] = Population[width, height].Propagate(this);
break;
default:
//nothing to do
break;
}
//only for stats purposes
switch (Population[width, height].State)
{
@@ -178,10 +176,14 @@ namespace PropagationRemasteredBeta
DeadCount[SimulationDuration] += 1;
break;
}
}
}//);
}
foreach (Human target in PeopleToCheck)
{
Population[target.Position.X,target.Position.Y] = target.CheckInfection(InfectionRate,Rnd);
}
Population = newTerrain;
}
public Bitmap Display()