CS302 -- Lab 6

Priority Queues


Lab Objective

This lab is designed to give you experience using priority queues and to give you experience writing simulations, which was the first application for object-oriented programming.


Setting Up

You should copy the following files from the /ruby/homes/ftp/pub/bvz/classes/cs302/labs/lab6 directory to your directory:


Problem Statement

In this lab you will implement a simple simulation of a factory using priority queues. The simulation will assume that you have a set of machines that can process an item into a product. The items arrive randomly and are put in a waiting queue until they can be assigned to a free machine. The items incur an inventory cost while they are waiting. The goal of the simulation is to determine the optimal number of machines that will minimize the total cost of both the machines and the inventory cost. Once you have completed the simulation program, you will be able to experimentally increase or decrease the number of machines in order to determine the optimal number of machines.

The program you will write will be named simulation and will take the following arguments:

simulation time_limit num_machines machine_cost_per_unit_time 
           inventory_cost_per_unit_time
The arguments have the following meaning:

  1. time_limit: How many time units the simulation should run.
  2. num_machines: The number of machines to be used in the simulation.
  3. machine_cost_per_unit_time: The cost of each machine per unit time. This argument should be an integer. You should assume that the machine costs the same amount per unit time, regardless of whether or not it is processing an item. For example, the cost per unit time might represent depreciation.
  4. inventory_cost_per_time_unit: The cost incurred by keeping an item waiting for a machine for one time unit. If an item waits 10 time units for a machine and the cost per time unit is 2, then the total inventory cost for this item will be 20. This argument should be an integer.

An example invocation of the simulation program would look as follows:

% simulation 1200 3 2 1
 
Number of items processed = 233
Maximum wait = 521
Number of items waiting to be processed = 215
 
Inventory cost: 93442
Machine cost:   7200
Total cost:     100642

The binary for simulation can be found in /ruby/homes/ftp/pub/bvz/classes/cs302/bin/simulation. If you have any questions about how your program should execute, the way the format of the output should look, or what the correct values for the output are, you should execute the simulation binary.


Output

Each execution of your simulation program should produce six outputs, formatted as shown above:

  1. Number of items finished: This count is the total number of items that were completely finished before the time limit was exceeded. This count will be equal to the number of departure events processed. When the time limit is exceeded, there may be one or more departure events still waiting to be processed because their time stamps exceed the time limit. Do not count the items associated with these departure events as finished.

  2. Maximum wait: This number is the maximum time a completely finished item spent waiting in the wait queue.

  3. Number of items waiting to be processed: This number is the number of items still in the wait queue when the time limit was exceeded.

  4. Inventory cost: This number is the number obtained by adding the inventory cost of each item. An item's inventory cost is its time spent waiting in the wait queue multiplied by the inventory cost per unit time. In computing this cost, you need to include the inventory cost of items left in the wait queue but not processed. For example, if an item arrives 20 time units before the time limit is exceeded and is never processed, then its 20 time units of waiting should be included in the inventory cost.

  5. Machine cost: This number is the cost of the machines and is obtained by multiplying the number of machines by the cost per machine.

  6. Total cost: This number is the total of the inventory and machine costs. It is the number you are looking to minimize.


Details

You should use the implementation scheme described in class for the bank teller problem (also see pages 224-225 of Weiss). In other words, you should have a priority queue that is ordered by timestamped events. If your simulation has k machines, then the priority queue will have up to k departure events and one arrival event (i.e., a maximum of k+1 events).

Arrival Events

An arrival event should perform the following actions:

  1. Record the arrival time in the item's record.

  2. Determine whether there is a free machine that can process this item. If there is a free machine, then the item should be assigned to the free machine and the start time should be noted in the item's record (it will be identical to the arrival time). If there is no free machine, then the item should be placed at the end of a waiting queue. The waiting queue can be implemented as an ordinary queue.

  3. Generate the next arrival event and add it to the priority queue.

Departure Events

A departure event should perform the following actions:

  1. Compute statistics for the item. These statistics are described later on.

  2. Determine if there are any items waiting to be processed and if so, assign the first item from the waiting queue to the now free machine. When the item is assigned to the machine, its start time should be noted (i.e., the current time should be noted). Otherwise the machine should be marked as free.


What Has Already Been Done For You

In order to help get you started, some code has already been prepared for you. This code can be found in the following files: