From a092c0586681bd6cb9934c60d96183b2b0ff13ce Mon Sep 17 00:00:00 2001 From: maxluli Date: Wed, 11 Jan 2023 14:10:30 +0100 Subject: [PATCH] Drastically reduced the processing between every frame (4X less actions) --- Caisses/Checkout.cs | 1 - Caisses/Form1.Designer.cs | 4 +- Caisses/Form1.cs | 6 +- Caisses/GraphicalStore.cs | 163 ++++++++++++++++++-------------------- 4 files changed, 81 insertions(+), 93 deletions(-) diff --git a/Caisses/Checkout.cs b/Caisses/Checkout.cs index ace3cb4..e65fc83 100644 --- a/Caisses/Checkout.cs +++ b/Caisses/Checkout.cs @@ -12,7 +12,6 @@ namespace Caisses private List clients; private bool open; - private int crossoverTime; public List Clients { get => clients; set => clients = value; } public bool Open { get => open; set => open = value; } diff --git a/Caisses/Form1.Designer.cs b/Caisses/Form1.Designer.cs index 0209d93..e87b929 100644 --- a/Caisses/Form1.Designer.cs +++ b/Caisses/Form1.Designer.cs @@ -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); diff --git a/Caisses/Form1.cs b/Caisses/Form1.cs index 2ed2f38..f92b93a 100644 --- a/Caisses/Form1.cs +++ b/Caisses/Form1.cs @@ -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; } } diff --git a/Caisses/GraphicalStore.cs b/Caisses/GraphicalStore.cs index 5c6ef0d..a830421 100644 --- a/Caisses/GraphicalStore.cs +++ b/Caisses/GraphicalStore.cs @@ -16,6 +16,22 @@ namespace Caisses private Bitmap _checkoutCorner; private Random rnd; + /// Stats + + private int _AverageWaitingTime; + private int _OpenCheckoutCount; + private int _PlacesLeftCount; + private List _NotFullCheckouts; + private List _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 NotFullCheckouts { get => _NotFullCheckouts; set => _NotFullCheckouts = value; } + public List 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(); + 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(); + 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 clientsToRemove = new List(); foreach (GraphicalClient client in Clients) { if (client.State == Client.ClientState.Waiting) { - if (openPlacesAmount > 0) - { - List 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 GetClientsWithoutCheckout() - { - List result = new List(); - foreach (GraphicalClient client in Clients) - { - if (client.State == Client.ClientState.Waiting) - result.Add(client); - } - return result; - } - public List GetNotFullCheckouts() - { - List result = new List(); - 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; - } + } } }