Drastically reduced the processing between every frame (4X less actions)

This commit is contained in:
2023-01-11 14:10:30 +01:00
parent 435193b59f
commit a092c05866
4 changed files with 81 additions and 93 deletions
-1
View File
@@ -12,7 +12,6 @@ namespace Caisses
private List<Client> clients;
private bool open;
private int crossoverTime;
public List<Client> Clients { get => clients; set => clients = value; }
public bool Open { get => open; set => open = value; }
+2 -2
View File
@@ -62,7 +62,7 @@
//
this.pbxCaisses.Location = new System.Drawing.Point(12, 375);
this.pbxCaisses.Name = "pbxCaisses";
this.pbxCaisses.Size = new System.Drawing.Size(838, 246);
this.pbxCaisses.Size = new System.Drawing.Size(838, 387);
this.pbxCaisses.TabIndex = 1;
this.pbxCaisses.TabStop = false;
//
@@ -189,7 +189,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1062, 630);
this.ClientSize = new System.Drawing.Size(1062, 768);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.pbxCaisses);
+3 -3
View File
@@ -57,9 +57,9 @@ namespace Caisses
pbxCaisses.Image = result[1];
lblClients.Text = "Clients : " + store.Clients.Count();
lblClientsWithoutCheckout.Text = "Clients without checkout : "+ store.GetClientsWithoutCheckout().Count();
lblAvaiblePlaces.Text = "Avaible places = " + store.GetAmountOfPlacesLeft();
lblAvgWaitingTime.Text = "AverageWaitingTime = " + store.GetAverageWaitingTime();
lblClientsWithoutCheckout.Text = "Clients without checkout : "+ store.WaitingClients.Count();
lblAvaiblePlaces.Text = "Avaible places = " + store.TotalPlacesLeftCount;
lblAvgWaitingTime.Text = "AverageWaitingTime = " + store.AverageWaitingTime;
lblTime.Text = "Time of the day : "+store.TimeOfTheDayInHours + ":"+store.TimeOfTheDayInMinuts % 60;
}
}
+76 -87
View File
@@ -16,6 +16,22 @@ namespace Caisses
private Bitmap _checkoutCorner;
private Random rnd;
/// Stats
private int _AverageWaitingTime;
private int _OpenCheckoutCount;
private int _PlacesLeftCount;
private List<GraphicalCheckout> _NotFullCheckouts;
private List<GraphicalClient> _WaitingClients;
public int AverageWaitingTime { get => _AverageWaitingTime; set => _AverageWaitingTime = value; }
public int OpenCheckoutCount { get => _OpenCheckoutCount; set => _OpenCheckoutCount = value; }
public int TotalPlacesLeftCount { get => _PlacesLeftCount; set => _PlacesLeftCount = value; }
internal List<GraphicalCheckout> NotFullCheckouts { get => _NotFullCheckouts; set => _NotFullCheckouts = value; }
public List<GraphicalClient> WaitingClients { get => _WaitingClients; set => _WaitingClients = value; }
/// Stats
public static readonly int[] ATTENDANCE = { 0, 0, 0, 0, 0, 0, 0, 30, 30, 40, 50, 60, 100, 80, 50, 30, 80, 100, 50, 50, 80, 0, 0, 0 };
const int CROSSOVER_TIME = 10;
@@ -65,45 +81,90 @@ namespace Caisses
//Its a new Hour so we can send the new clients
int amountOfNewCLients = ATTENDANCE[TimeOfTheDayInHours];
FillStore(amountOfNewCLients);
}
}
int cumulatedWaitingTime = 0;
int waitingClients = 0;
AverageWaitingTime = 1;
WaitingClients = new List<GraphicalClient>();
foreach (GraphicalClient client in Clients)
{
//Tick
client.Tick();
//Waiting time incrementation
if (Clients.Count != 0 && client.State == Client.ClientState.Waiting)
{
cumulatedWaitingTime += client.WaitingTime;
waitingClients += 1;
WaitingClients.Add(client);
}
}
// Calculating average waiting time
if (Clients.Count == 0 || waitingClients == 0)
{
AverageWaitingTime = 0;
}
else
{
AverageWaitingTime = cumulatedWaitingTime / waitingClients;
}
OpenCheckoutCount = 0;
TotalPlacesLeftCount = 0;
NotFullCheckouts = new List<GraphicalCheckout>();
foreach (GraphicalCheckout checkout in Checkouts)
{
checkout.Tick(GetAverageWaitingTime(),CROSSOVER_TIME,GetAmountOfOpenCheckouts());
//Tick
checkout.Tick(AverageWaitingTime,CROSSOVER_TIME,OpenCheckoutCount);
if (checkout.Open)
{
//Calculate amount of open Checkouts
OpenCheckoutCount++;
//Calculate amount of places left
int CheckoutPlacesLeft = Checkout.MAX_CAPACITY - checkout.Clients.Count();
TotalPlacesLeftCount += CheckoutPlacesLeft;
//Collecting every non full checkout
if (CheckoutPlacesLeft > 0)
{
NotFullCheckouts.Add(checkout);
}
}
}
//Processing the checkout mechanism
int openPlacesAmount = GetAmountOfPlacesLeft();
List<GraphicalClient> clientsToRemove = new List<GraphicalClient>();
foreach (GraphicalClient client in Clients)
{
if (client.State == Client.ClientState.Waiting)
{
if (openPlacesAmount > 0)
{
List<GraphicalCheckout> checkoutsWithFreeSpace = GetNotFullCheckouts();
if (checkoutsWithFreeSpace.Count > 0)
if (TotalPlacesLeftCount > 0)
{
if (NotFullCheckouts.Count > 0)
{
//Now we select a random one to put our waiting client
int index = 0;
int count = checkoutsWithFreeSpace.Count - 1;
int count = NotFullCheckouts.Count;
if (count > 0)
{
index = rnd.Next(0, checkoutsWithFreeSpace.Count - 1);
index = rnd.Next(0, NotFullCheckouts.Count - 1);
}
else
{
index = 0;
}
checkoutsWithFreeSpace[index].Clients.Add(client);
//Clients.Remove(client);
clientsToRemove.Add(client);
client.State = Client.ClientState.Inline;
if (NotFullCheckouts[index].Clients.Count < Checkout.MAX_CAPACITY){
//We need to check that because a client could have been already added and so the checkout could not have any places left even tho she is listed as not full
NotFullCheckouts[index].Clients.Add(client);
//Clients.Remove(client);
clientsToRemove.Add(client);
client.State = Client.ClientState.Inline;
}
}
}
}
@@ -160,78 +221,6 @@ namespace Caisses
{
Clients.Add(new GraphicalClient(new Point(0,0),rnd));
}
}
public List<GraphicalClient> GetClientsWithoutCheckout()
{
List<GraphicalClient> result = new List<GraphicalClient>();
foreach (GraphicalClient client in Clients)
{
if (client.State == Client.ClientState.Waiting)
result.Add(client);
}
return result;
}
public List<GraphicalCheckout> GetNotFullCheckouts()
{
List<GraphicalCheckout> result = new List<GraphicalCheckout>();
foreach (GraphicalCheckout checkout in Checkouts)
{
if (checkout.Clients.Count < Checkout.MAX_CAPACITY && checkout.Open)
{
result.Add(checkout);
}
}
return result;
}
public int GetAmountOfPlacesLeft()
{
int result = 0;
foreach(GraphicalCheckout checkout in Checkouts)
{
if (checkout.Open)
{
result += Checkout.MAX_CAPACITY - checkout.Clients.Count();
}
}
return result;
}
public int GetAmountOfOpenCheckouts()
{
int result = 0;
foreach (GraphicalCheckout checkout in Checkouts)
{
if (checkout.Open)
{
result++;
}
}
return result;
}
public int GetAverageWaitingTime()
{
int cumulatedWaitingTime = 0;
int averageWaitingTime = 0;
int waitingClients = 0;
foreach (GraphicalClient client in Clients)
{
if (client.State == Client.ClientState.Waiting)
{
cumulatedWaitingTime += client.WaitingTime;
waitingClients += 1;
}
}
if (Clients.Count == 0 || waitingClients == 0)
{
averageWaitingTime = 0;
}
else
{
averageWaitingTime = cumulatedWaitingTime / waitingClients;
}
return averageWaitingTime;
}
}
}
}