From 3c18e7e360c97f2e9890bd6b542be93d4728514d Mon Sep 17 00:00:00 2001 From: maxluli Date: Wed, 11 Jan 2023 15:25:19 +0100 Subject: [PATCH] Otptimised checkout behaviours --- Caisses/Checkout.cs | 6 +- Caisses/Form1.cs | 2 +- Caisses/GraphicalClient.cs | 2 +- Caisses/GraphicalStore.cs | 124 ++++++++++++++++++++----------------- 4 files changed, 72 insertions(+), 62 deletions(-) diff --git a/Caisses/Checkout.cs b/Caisses/Checkout.cs index e65fc83..8b8f742 100644 --- a/Caisses/Checkout.cs +++ b/Caisses/Checkout.cs @@ -21,13 +21,13 @@ namespace Caisses Clients = new List(); Open = false; } - public void Tick(int averageWaitingTime, int CrossoverTime, int openedCheckoutsCount) + public void Tick(GraphicalStore store) { - if (!Open && averageWaitingTime > CrossoverTime || openedCheckoutsCount == 0) + if (!Open && store.AverageWaitingTime > GraphicalStore.CROSSOVER_TIME && store.GetEmptyCheckoutCount() == 0 || store.OpenCheckoutCount == 0) { Open = true; } - if (Open && averageWaitingTime < 1 && Clients.Count == 0 && openedCheckoutsCount > 1) + if (Open && store.AverageWaitingTime < 1 && Clients.Count == 0 && store.OpenCheckoutCount > 0) { Open = false; } diff --git a/Caisses/Form1.cs b/Caisses/Form1.cs index f92b93a..2e52721 100644 --- a/Caisses/Form1.cs +++ b/Caisses/Form1.cs @@ -17,7 +17,7 @@ namespace Caisses { InitializeComponent(); DoubleBuffered = true; - store = new GraphicalStore(10,12,pbxRayons.Size,pbxCaisses.Size); + store = new GraphicalStore(20,12,pbxRayons.Size,pbxCaisses.Size); } private void btnStartStop_Click(object sender, EventArgs e) diff --git a/Caisses/GraphicalClient.cs b/Caisses/GraphicalClient.cs index b2e2236..3cacd93 100644 --- a/Caisses/GraphicalClient.cs +++ b/Caisses/GraphicalClient.cs @@ -31,7 +31,7 @@ namespace Caisses //Position = new Point(Random.Next(0, 100), Random.Next(0, 100)); Speed = new PointF(NextFloat(MIN_CLIENT_SPEED, MAX_CLIENT_SPEED), NextFloat(MIN_CLIENT_SPEED, MAX_CLIENT_SPEED)); Color = Color.FromArgb(ShoppingTime * 2, 0, 0); - int clientWidth = Random.Next(20, 30); + int clientWidth = Random.Next(25, 30); Size = new Size(clientWidth, clientWidth); } static float NextFloat(float min, float max) diff --git a/Caisses/GraphicalStore.cs b/Caisses/GraphicalStore.cs index a830421..336854e 100644 --- a/Caisses/GraphicalStore.cs +++ b/Caisses/GraphicalStore.cs @@ -9,6 +9,9 @@ namespace Caisses { internal class GraphicalStore { + 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 }; + public const int CROSSOVER_TIME = 10; + private List _checkouts; private List _clients; private int _timeOfTheDayInMinuts; @@ -32,9 +35,6 @@ namespace Caisses /// 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; - public int TimeOfTheDayInMinuts { get => _timeOfTheDayInMinuts; set => _timeOfTheDayInMinuts = value; } public List Clients { get => _clients; set => _clients = value; } internal List Checkouts { get => _checkouts; set => _checkouts = value; } @@ -46,13 +46,13 @@ namespace Caisses get { return _timeOfTheDayInMinuts / 60; } } - public GraphicalStore(int startingHour,int checkoutNumber, Size shelvesCornerSize,Size checkoutCornerSize) + public GraphicalStore(int startingHour, int checkoutNumber, Size shelvesCornerSize, Size checkoutCornerSize) { TimeOfTheDayInMinuts = startingHour * 60; Clients = new List(); Checkouts = new List(); - CheckoutCorner = new Bitmap(checkoutCornerSize.Width,checkoutCornerSize.Height); - ShelvesCorner = new Bitmap(shelvesCornerSize.Width,shelvesCornerSize.Height); + CheckoutCorner = new Bitmap(checkoutCornerSize.Width, checkoutCornerSize.Height); + ShelvesCorner = new Bitmap(shelvesCornerSize.Width, shelvesCornerSize.Height); rnd = new Random(); FillStore(ATTENDANCE[TimeOfTheDayInHours]); @@ -81,36 +81,70 @@ 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; + AverageWaitingTime = 0; WaitingClients = new List(); + if (NotFullCheckouts == null) + NotFullCheckouts = new List(); + + //Clients that will leave the store + List clientsToRemove = new List(); foreach (GraphicalClient client in Clients) { //Tick client.Tick(); + // Calculating average waiting time + if (Clients.Count == 0 || waitingClients == 0) + { + AverageWaitingTime = 0; + } + else + { + AverageWaitingTime = cumulatedWaitingTime / waitingClients; + } + //Waiting time incrementation if (Clients.Count != 0 && client.State == Client.ClientState.Waiting) { cumulatedWaitingTime += client.WaitingTime; waitingClients += 1; WaitingClients.Add(client); + + //Checkout process + if (TotalPlacesLeftCount > 0 && NotFullCheckouts.Count > 0) + { + //Now we select a random one to put our waiting client + int index = 0; + int count = NotFullCheckouts.Count; + if (count > 0) + { + index = rnd.Next(0, count - 1); + } + else + { + index = 0; + } + 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; + } + } } } - // Calculating average waiting time - if (Clients.Count == 0 || waitingClients == 0) + foreach (GraphicalClient client in clientsToRemove) { - AverageWaitingTime = 0; - } - else - { - AverageWaitingTime = cumulatedWaitingTime / waitingClients; + Clients.Remove(client); } OpenCheckoutCount = 0; @@ -120,7 +154,7 @@ namespace Caisses foreach (GraphicalCheckout checkout in Checkouts) { //Tick - checkout.Tick(AverageWaitingTime,CROSSOVER_TIME,OpenCheckoutCount); + checkout.Tick(this); if (checkout.Open) { @@ -137,43 +171,6 @@ namespace Caisses } } - //Processing the checkout mechanism - List clientsToRemove = new List(); - foreach (GraphicalClient client in Clients) - { - if (client.State == Client.ClientState.Waiting) - { - if (TotalPlacesLeftCount > 0) - { - if (NotFullCheckouts.Count > 0) - { - //Now we select a random one to put our waiting client - int index = 0; - int count = NotFullCheckouts.Count; - if (count > 0) - { - index = rnd.Next(0, NotFullCheckouts.Count - 1); - } - else - { - index = 0; - } - 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; - } - } - } - } - } - foreach (GraphicalClient client in clientsToRemove) - { - Clients.Remove(client); - } - foreach (GraphicalCheckout checkout in Checkouts) { if (checkout.Open && checkout.Clients.Count > 0) @@ -197,8 +194,8 @@ namespace Caisses } public List Draw() { - ShelvesCorner = new Bitmap(ShelvesCorner.Width,ShelvesCorner.Height); - CheckoutCorner = new Bitmap(CheckoutCorner.Width,CheckoutCorner.Height); + ShelvesCorner = new Bitmap(ShelvesCorner.Width, ShelvesCorner.Height); + CheckoutCorner = new Bitmap(CheckoutCorner.Width, CheckoutCorner.Height); foreach (GraphicalClient client in Clients) { client.Draw(ShelvesCorner); @@ -219,8 +216,21 @@ namespace Caisses { for (int i = 0; i < amountOfNewClients; i++) { - Clients.Add(new GraphicalClient(new Point(0,0),rnd)); + Clients.Add(new GraphicalClient(new Point(0, 0), rnd)); } - } + } + public int GetEmptyCheckoutCount() + { + int result = 0; + foreach (GraphicalCheckout checkout in Checkouts) + { + if (checkout.Open && checkout.Clients.Count == 0) + { + result += 1; + } + } + return result; + } } + }