Minor performance improvement but started changes to get faster processing
This commit is contained in:
@@ -48,8 +48,9 @@ namespace SharpPropagation
|
|||||||
Rnd = rnd;
|
Rnd = rnd;
|
||||||
Age = 0;
|
Age = 0;
|
||||||
Humans = new Human[dimensions.Width, dimensions.Height];
|
Humans = new Human[dimensions.Width, dimensions.Height];
|
||||||
|
Console.WriteLine("Starting Generation");
|
||||||
Generate();
|
Generate();
|
||||||
|
Console.WriteLine("Generation Finished");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetDisease(Disease plague)
|
public void SetDisease(Disease plague)
|
||||||
@@ -68,27 +69,39 @@ namespace SharpPropagation
|
|||||||
//Other thing, there will always be more of the last one because someone who is already infected for example could be then put to immune or dead
|
//Other thing, there will always be more of the last one because someone who is already infected for example could be then put to immune or dead
|
||||||
//One solution to this issue would be to have if else else statements but in this case for example 20% would be lower with the last because its 20% on the remaining population and not of the all
|
//One solution to this issue would be to have if else else statements but in this case for example 20% would be lower with the last because its 20% on the remaining population and not of the all
|
||||||
//In other words I did it that way but it can be changed just its not the right method to have perfect ratios
|
//In other words I did it that way but it can be changed just its not the right method to have perfect ratios
|
||||||
|
if (StartInfectedRatio != 0)
|
||||||
|
{
|
||||||
|
if (StartInfectedRatio == 100 || Roll(Rnd,StartInfectedRatio))
|
||||||
|
{
|
||||||
|
Humans[x, y] = new Human(new Point(x, y), Human.State.Infected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (StartImmuneRatio != 0)
|
||||||
|
{
|
||||||
|
if (StartImmuneRatio == 100 || Roll(Rnd,StartImmuneRatio))
|
||||||
|
{
|
||||||
|
Humans[x, y] = new Human(new Point(x, y), Human.State.Immune);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (StartDeadRatio != 0)
|
||||||
|
{
|
||||||
|
if (StartDeadRatio == 100 || Roll(Rnd,StartDeadRatio))
|
||||||
|
{
|
||||||
|
Humans[x, y] = new Human(new Point(x, y), Human.State.Dead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Humans[x,y] is null)
|
||||||
|
{
|
||||||
Humans[x, y] = new Human(new Point(x, y), Human.State.Normal);
|
Humans[x, y] = new Human(new Point(x, y), Human.State.Normal);
|
||||||
|
}
|
||||||
|
|
||||||
if (Rnd.Next(0, CORRECTED_PERCENTAGE) < StartInfectedRatio)
|
|
||||||
{
|
|
||||||
Humans[x, y].PresentState = Human.State.Infected;
|
|
||||||
}
|
|
||||||
if (Rnd.Next(0, CORRECTED_PERCENTAGE) < StartImmuneRatio)
|
|
||||||
{
|
|
||||||
Humans[x, y].PresentState = Human.State.Immune;
|
|
||||||
}
|
|
||||||
if (Rnd.Next(0, CORRECTED_PERCENTAGE) < StartDeadRatio)
|
|
||||||
{
|
|
||||||
Humans[x, y].PresentState = Human.State.Dead;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public int[] Propagate()
|
public int[] Propagate()
|
||||||
{
|
{
|
||||||
List<Human> peopleToCheck = new List<Human>();
|
List<Point> peopleToCheck = new List<Point>();
|
||||||
int[] stats = new int[] { 0, 0, 0, 0 };
|
int[] stats = new int[] { 0, 0, 0, 0 };
|
||||||
// 0 Normal, 1 Infected, 2 Immune, 3 Dead
|
// 0 Normal, 1 Infected, 2 Immune, 3 Dead
|
||||||
|
|
||||||
@@ -104,7 +117,7 @@ namespace SharpPropagation
|
|||||||
stats[0]+=1;
|
stats[0]+=1;
|
||||||
break;
|
break;
|
||||||
case Human.State.Infected:
|
case Human.State.Infected:
|
||||||
peopleToCheck.Add(Humans[x, y]);
|
peopleToCheck.Add(new Point(x,y));
|
||||||
stats[1]+=1;
|
stats[1]+=1;
|
||||||
break;
|
break;
|
||||||
case Human.State.Immune:
|
case Human.State.Immune:
|
||||||
@@ -117,52 +130,52 @@ namespace SharpPropagation
|
|||||||
}//);
|
}//);
|
||||||
}//);
|
}//);
|
||||||
|
|
||||||
List<Human> peopleToInfect = new List<Human>();
|
List<Point> peopleToInfect = new List<Point>();
|
||||||
List<Human> peopleToCure = new List<Human>();
|
List<Point> peopleToCure = new List<Point>();
|
||||||
List<Human> peopleToKill = new List<Human>();
|
List<Point> peopleToKill = new List<Point>();
|
||||||
|
|
||||||
//Parallel.ForEach(peopleToCheck, person =>
|
//Parallel.ForEach(peopleToCheck, person =>
|
||||||
foreach (Human person in peopleToCheck)
|
foreach (Point person in peopleToCheck)
|
||||||
{
|
{
|
||||||
//I am assuming that if the Human is Curing or dying on this tick, he cant infect anyone
|
//I am assuming that if the Human is Curing or dying on this tick, he cant infect anyone
|
||||||
if (wouldCure(Rnd))
|
if (Roll(Rnd,Plague.CuringRate))
|
||||||
{
|
{
|
||||||
peopleToCure.Add(person);
|
peopleToCure.Add(person);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (wouldDie(Rnd))
|
if (Roll(Rnd,Plague.DeathRate))
|
||||||
{
|
{
|
||||||
peopleToKill.Add(person);
|
peopleToKill.Add(person);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//now we can start to check if people would be infected or not
|
//now we can start to check if people would be infected or not
|
||||||
if (person.Position.X > 0 && person.Position.X < Dimensions.Width - 1 && person.Position.Y > 0 && person.Position.Y < Dimensions.Height - 1)
|
if (person.X > 0 && person.X < Dimensions.Width - 1 && person.Y > 0 && person.Y < Dimensions.Height - 1)
|
||||||
{
|
{
|
||||||
/// ###
|
/// ###
|
||||||
/// #@#
|
/// #@#
|
||||||
/// ###
|
/// ###
|
||||||
|
|
||||||
List<Human> potentialInfections = new List<Human>();
|
List<Point> potentialInfections = new List<Point>();
|
||||||
//potentialInfections.Add(Humans[person.Position.X,person.Position.Y]);
|
//potentialInfections.Add(Humans[person.Position.X,person.Position.Y]);
|
||||||
|
|
||||||
potentialInfections.Add(Humans[person.Position.X - 1, person.Position.Y - 1]); //Top Left
|
potentialInfections.Add(new Point(person.X - 1, person.Y - 1)); //Top Left
|
||||||
potentialInfections.Add(Humans[person.Position.X, person.Position.Y - 1]); //Top
|
potentialInfections.Add(new Point(person.X, person.Y - 1)); //Top
|
||||||
potentialInfections.Add(Humans[person.Position.X + 1, person.Position.Y - 1]); //Top Right
|
potentialInfections.Add(new Point(person.X + 1, person.Y - 1)); //Top Right
|
||||||
|
|
||||||
potentialInfections.Add(Humans[person.Position.X - 1, person.Position.Y]); //Left
|
potentialInfections.Add(new Point(person.X - 1, person.Y)); //Left
|
||||||
potentialInfections.Add(Humans[person.Position.X + 1, person.Position.Y]); //Right
|
potentialInfections.Add(new Point(person.X + 1, person.Y)); //Right
|
||||||
|
|
||||||
potentialInfections.Add(Humans[person.Position.X - 1, person.Position.Y + 1]); //Bottom Left
|
potentialInfections.Add(new Point(person.X - 1, person.Y + 1)); //Bottom Left
|
||||||
potentialInfections.Add(Humans[person.Position.X, person.Position.Y + 1]); //Bottom
|
potentialInfections.Add(new Point(person.X, person.Y + 1)); //Bottom
|
||||||
potentialInfections.Add(Humans[person.Position.X + 1, person.Position.Y + 1]); //Bottom Right
|
potentialInfections.Add(new Point(person.X + 1, person.Y + 1)); //Bottom Right
|
||||||
|
|
||||||
foreach (Human potentialInfected in potentialInfections)
|
foreach (Point potentialInfected in potentialInfections)
|
||||||
{
|
{
|
||||||
if (potentialInfected.PresentState == Human.State.Normal)
|
if (Humans[potentialInfected.X,potentialInfected.Y].PresentState == Human.State.Normal)
|
||||||
{
|
{
|
||||||
if (wouldBeInfected(Rnd))
|
if (Roll(Rnd,Plague.InfectionRate))
|
||||||
{
|
{
|
||||||
peopleToInfect.Add(potentialInfected);
|
peopleToInfect.Add(potentialInfected);
|
||||||
}
|
}
|
||||||
@@ -216,42 +229,32 @@ namespace SharpPropagation
|
|||||||
if (peopleToInfect.Count > 0)
|
if (peopleToInfect.Count > 0)
|
||||||
{
|
{
|
||||||
//Parallel.ForEach(peopleToInfect, infected =>
|
//Parallel.ForEach(peopleToInfect, infected =>
|
||||||
foreach (Human infected in peopleToInfect)
|
foreach (Point infected in peopleToInfect)
|
||||||
{
|
{
|
||||||
Humans[infected.Position.X, infected.Position.Y].PresentState = Human.State.Infected;
|
Humans[infected.X, infected.Y].PresentState = Human.State.Infected;
|
||||||
}//);
|
}//);
|
||||||
}
|
}
|
||||||
if (peopleToCure.Count > 0)
|
if (peopleToCure.Count > 0)
|
||||||
{
|
{
|
||||||
//Parallel.ForEach(peopleToCure, cured =>
|
//Parallel.ForEach(peopleToCure, cured =>
|
||||||
foreach (Human cured in peopleToCure)
|
foreach (Point cured in peopleToCure)
|
||||||
{
|
{
|
||||||
Humans[cured.Position.X, cured.Position.Y].PresentState = Human.State.Immune;
|
Humans[cured.X, cured.Y].PresentState = Human.State.Immune;
|
||||||
}//);
|
}//);
|
||||||
}
|
}
|
||||||
if (peopleToKill.Count > 0)
|
if (peopleToKill.Count > 0)
|
||||||
{
|
{
|
||||||
//Parallel.ForEach(peopleToKill, dead =>
|
//Parallel.ForEach(peopleToKill, dead =>
|
||||||
foreach (Human dead in peopleToKill)
|
foreach (Point dead in peopleToKill)
|
||||||
{
|
{
|
||||||
Humans[dead.Position.X, dead.Position.Y].PresentState = Human.State.Dead;
|
Humans[dead.X, dead.Y].PresentState = Human.State.Dead;
|
||||||
}//);
|
}//);
|
||||||
}
|
}
|
||||||
return stats;
|
return stats;
|
||||||
}
|
}
|
||||||
public bool wouldBeInfected(Random rnd)
|
public bool Roll(Random rnd,int rate)
|
||||||
{
|
{
|
||||||
return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.InfectionRate;
|
return rnd.Next(0, CORRECTED_PERCENTAGE) < rate;
|
||||||
}
|
|
||||||
public bool wouldCure(Random rnd)
|
|
||||||
{
|
|
||||||
//You could add a modifier that increases the curing chance with the age of the disease in the Human
|
|
||||||
//Like after 2 weeks with it it would be at least 5 times more likely for him to cure
|
|
||||||
return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.CuringRate;
|
|
||||||
}
|
|
||||||
public bool wouldDie(Random rnd)
|
|
||||||
{
|
|
||||||
return rnd.Next(0, CORRECTED_PERCENTAGE) < Plague.DeathRate;
|
|
||||||
}
|
}
|
||||||
public void Display()
|
public void Display()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ namespace SharpPropagation
|
|||||||
{
|
{
|
||||||
Console.WriteLine("********** Sharp Propagation (Console) 2022 **********");
|
Console.WriteLine("********** Sharp Propagation (Console) 2022 **********");
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
Console.WriteLine("Initialisation");
|
Size populationSize = new Size(1000,1000);
|
||||||
Population peoples = new Population(new Size(1000,1000),random,10,10,5);
|
Population peoples = new Population(populationSize,random,10,10,5);
|
||||||
peoples.SetDisease(new Disease(random,20,10,5,"Chick Chicken"));
|
peoples.SetDisease(new Disease(random,20,10,5,"Chick Chicken"));
|
||||||
//peoples.Display();
|
//peoples.Display();
|
||||||
Console.WriteLine("Propagation :");
|
Console.WriteLine($"Propagation started with {populationSize.Width} x {populationSize.Height} humans ({populationSize.Width*populationSize.Height})");
|
||||||
int[] stats;
|
int[] stats;
|
||||||
int steps = 0;
|
int steps = 0;
|
||||||
// 0:normal 1:infected
|
// 0:normal 1:infected
|
||||||
@@ -34,7 +34,7 @@ namespace SharpPropagation
|
|||||||
}
|
}
|
||||||
//peoples.Display();
|
//peoples.Display();
|
||||||
Console.WriteLine($"Propagation finished in {steps}");
|
Console.WriteLine($"Propagation finished in {steps}");
|
||||||
Console.ReadKey();
|
//Console.ReadKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user