1. Computing

How do I Write a SuperMarket Checkout Simulation?

Simple Simulation

By

Queue Simulation
The code listed below (zip file) is a simple simulation program written in C# 2.0 and using Winforms.

The simulation is about SuperMarket Checkout Tills. Supermarkets have to strike a balance between employing enough staff and keeping customers happy. Too many staff and tills lie unused, while too few staff just irritate shoppers as they have to wait ages. The optimum for the shop is the minimum needed to keep shoppers happy. If a store can reduce employee head count at each of hundreds of shops without affecting service then it can save a considerable sum.

Simulations model behavior. For example- how long does it take for a shopper to be checked out? A very detailed model might be based upon statistical evidence from supermarkets. It might reveal that a typical shopper has 27 items and the time taken varies according to operator alertness, fatigue, time of day etc, perhaps between two minutes twenty seconds and four minutes fifteen seconds. It might also show that the typical shopper time varies according to time of day and whether weekend or weekday. So a really complex simulation would take all of these parameters into account. This one, however does not, its purpose is to give a flavor of what a simulation offers and show how you would write one. It's more of a toy!

Running the Simulation

This simulation is easy to operate- run it and click the Checkout button as many times as you need tills. Now click Start Simulation and then occasionally click the Add 10 Customers button. You'll see customers added onto tills. Each customer is added to the queue with the least shoppers.

You can see the Queue Length for each queue, and this is updated every second. The speed is how fast the till operator works - this is an approximation (in minutes) so a 2 minute till is twice as fast as a 4 minute till. You can open new checkouts any time or close them by clicking the button. When you close a checkout, all shoppers in the queue defect to other queues.

The simulation has two classes plus one form. One class represents a single checkout queue. It has private variables for queue length, customer checking out speed and whether the queue is open or closed. Some checkouts work faster than others - this is the ProcessTime member. It represents time to checkout in minutes and each queue has a random value. You can see these when the simulation is run.

The SuperMarket class manages the number of customers and the checkout queues. The public method SuperMarket.AddCustomers() does much of the donkey work, searching all open queues and adding each customer to the shortest queue available. This is also called when a queue is closed.

That's really all it does. You can see the time in minutes ticking along, one simulated minute every second, driven by the Timer SimTimer. There is additional code to enable/disable buttons and switch the timer off when the last checkout is closed.

Features of the Application

I used a DataGridView to show the data. This is new in C# 2.0 and is very easy to use- you design the columns, specifying what each holds at design time then add or remove rows at runtime. I use an ArrayList to hold all the queues in the SuperMarket class.

The Tag property on controls is also new in C# 2.0 and can hold an object reference. I've used it to hold a reference to a OneQueue object held in the Supermarket Queues variable. This is assigned in the btnadd_Click() method.

 Qgrid.Rows[index].Tag = Shop.OpenQueue() ;
 
This line
 OneQueue Q = Qgrid.Rows[Index].Tag as OneQueue;
 
returns a reference to the appropriate Queue object held at index position. This uses the as to check the type of object in Tag and cast it to a OneQueue object. You'll see variations of this in the SuperMarket.ClockTick() method and other SuperMarket methods.

Download the Code

The project files are here. Download Project (Zip File)

Feel free to modify this as you wish. You may for instance want to have shoppers added randomly and see whether queues grow or shrink. Another variation would be have to fast queues for nine items or less and have 10-20% of shoppers use these queues. Then you have to start making assumptions- how long must a fast queue be before a shopper with 9 items or less queue at a regular queue?

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. C# / C Sharp
  5. How To Do Things in C#
  6. How do I Write a Simulation?

©2014 About.com. All rights reserved.