CS 231: Data Structures and Algorithms (Lab Page)

Title image Project 6
Fall 2016

Spatial Simulation: Which Checkout Line is Best?

The main purpose of this project is to give you the opportunity to use your Queue implementation to simulate shoppers picking which check-out line to use. We'll use the simulation to compare the performance of three different algorithms for selecting a queue. For example, if you are checking out at a big box store, there may be 20 queues open. How do you optimize your choice?

The optimal process would be to select the queue with the shortest line. However, in practice that takes a lot of work, and by the time you're done picking, the situation may have changed.

The simplest strategy is to pick a line at random. While it takes no time, following a random strategy means you may end up at the end of the longest line, standing next to a short line.

It turns out that a much better strategy than picking randomly is to pick two queues randomly and then select the shorter queue (Mitzenmacher, The Power of Two Choices in Randomized Load Balancing, TPDS, 2001).

Simulation Overview

Design and implement a simulation of a multi-station checkout situation. Once you have your simulation working, test out the three strategies given below for customers selecting a checkout line.


You can design this simulation however you like. The tasks are simply hints as to one design approach within the general framework.

  1. This simulation involves adding and removing agents each turn. One method of adding agents to a simulation is to have a Spawner class that has no visual representation but that adds a certain number of agents to the Landscape each iteration. When you set up the simulation, adding a single Spawner object takes care of adding whatever other kinds of agents are needed for the simulation. In this case, the Spawner would be creating Customer agents.
  2. The simulation obviously needs a Customer agent. The Customer agent needs to know how many items it has and also needs to keep track of what it is doing each iteration. When starting, give each Customer just one item.

    The trajectory a Customer agent should follow is the following. These should be implemented as part of the Customer's updateState method.

    1. After spawning, the Customer should be in the selection phase. Depending on the strategy it is using, the selection phase should take 1 time step (random), 2 time steps (best of 2 random choices), or 4 time steps (check all counters). An extension is to design the class so the wait time for the check-all strategy is proportional to the number of checkout counters.
    2. At the end of the selection phase, the Customer should join a CheckoutQueue, entering the wait phase.
    3. When the Customer is at the head of a CheckoutQueue, the Checkout agent should decrement the number of items in the Customer's basket. When there is nothing left in the Customer's basket, the Customer should remove itself from the simulation.

    To achieve the above trajectory, the Customer agent must have fields for its current phase, how much time is left until it can select a queue, and how many items fit has in its basket. The Customer should also keep track of how many turns it takes from spawning until the customer reaches the head of the checkout queue. For flexibility, it may also be useful to have a field that determines the agent's selection strategy, although this could be a global class variable.

    The Customer must have methods to allow a Checkout agent to both test and decrement the number of items in its basket.

  3. The simulation will also need a Checkout agent. The checkout agent needs to maintain a queue of Customers. For this project, use the MyQueue class you wrote yourself in the lab. You may not use a Java library type to implement the queue (except during debugging, if you wish).

    To enable the customer to make decisions, the Checkout agent must be able to tell a Customer agent the length of its queue. You can decide whether the length of a queue is modified by the number of items in each Customer's basket.

    In its updateState method, the Checkout agent needs to set the spatial position of each Customer agent in its queue on each iteration since each Customer agent will always be situated relative to the (non-moving) Checkout agents. The Checkout agent's updateState method should implement the following rules.

    • There is no one in line: do nothing.
    • There is a Customer at the head of the line with more than zero items in their basket: decrement the number of items in the Customer's basket.
    • There is a Customer at the head of the line with zero items in their basket: remove this Customer from the checkout queue and update the positions of all other Customer agents in the queue.

    The Checkout agent class needs to have methods so a Customer can join a checkout queue.

  4. Create a new CheckoutSimulation class that initializes a Landscape with several Checkout agents and a single Spawner agent. Place all of the Checkout agents at the bottom of the Landscape, regularly spaced, with the lines growing upwards. The simulation should continue until the application is closed. Make sure to balance the number of Customers being spawned with the number of items per customer and the number of Checkout agents. On the first turn it is reasonable to create extra Customer agents to populate the store.
  5. Once your simulation is working, run lots of customers (at least 2000) and calculate the average and standard deviation of the time to check out for the three different strategies (to compute the time to check out, compute the number of time steps it takes from the moment the Customer enters the Selection Phase until his basket is empty). You may want to add some type of list to the Landscape to either hang on to all Customers removed from the simulation, or store just the time to checkout for each Customer that completes the process. Have the statistics periodically print out during the simulation, such as once every 100 customers.


Each assignment will have a set of suggested extensions. The required tasks constitute about 85% of the assignment, and if you do only the required tasks and do them well you will earn a B+. To earn a higher grade, you need to undertake at least one extension. The difficulty and quality of the extension or extensions will determine your final grade for the assignment. One significant extension, or 2-3 smaller ones, done well, is typical.

  1. Try other strategies for choosing a line. Keep track of how complex they are.
  2. Make the simulation visually interesting.
  3. Allow each agent to choose one of the three strategies randomly. Then calculate the statistics for each strategy in the mixed situation. Which strategy does the best?
  4. For any assignment, a good extension will be to implement a Java class yourself and demonstrate that it has the same functionality as the Java class. For example, you could implement your own ArrayList class for this assignment.
  5. For any assignment, a good extension will be to annotate your code to indicate all places where memory is "lost" (in other words, each place where the last remaining reference to an object is either destroyed or is given a new value). If you do this extension, please indicate so in your write-up.


Make your writeup for the project a wiki page in your personal space. If you have questions about making a wiki page, stop by my office or ask in lab.

Your writeup should have a simple format.

Once you have written up your assignment, give the page the label:


You can give any wiki page a label using the label field at the bottom of the page. The label is different from the title.

Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, put it in your folder on the Courses fileserver. Create a directory for each project inside the private folder inside your username folder.