CSCI 1321 (Principles of Algorithm Design II), Spring 2010:
Homework 7

Credit

Design 20 points; code 40 points.

Overview

By now your game should be playable, with all the major functionality implemented. This homework is something of a tangent; the goal is to write a second, more efficient priority queue and compare its performance with your first version. The needed code breaks down into two categories:

First, you are to write a second priority queue class that uses a heap (as discussed in class) to store elements rather than a sorted linked list.

Second, you are to add code to your game that will allow you to compare the performance of the two implementations as the number of game entities in the queue varies. To do this, you need to do several things: create a ``dummy'' game entity class so you can put arbitrarily many entities in the priority queue, change your main class to accept two command-line arguments that specify which implementation to use and how many of these dummy entities to add, and change your player class to print out some information that will allow you to see how performance varies based on the choice of priority queue implementation and the number of dummy entities.

Once you do all of this, you can run your game with different values of the two command-line arguments and see how performance changes. The final part of this assignment is to do this and plot the results.

Design

The design for this assignment will likely be simpler for this assignment than for previous assignments, since the number of new classes is smaller, and there will not be many changes to the descriptions of other classes.

Step-by-step instructions

Code

The class you need to add and/or modify are described in the next section (``Classes for this assignment''). This section describes the overall procedure.

Step-by-step instructions

Classes for this assignment

For this assignment the coding job consists of three main pieces -- writing a second PriorityQueue class, writing a class for dummy entities, and making changes to other classes to let you conduct the performance experiments described earlier.

Priority queue class

Like the priority queue class you wrote for Homework 5, the one for this assignment should implement the framework's PriorityQueue interface. You could call it HeapBasedQueue, or something else that distinguishes it from the previous class. It will store elements using a heap (as discussed in class) rather than a linked list. NOTE that you should not implement the heap using a tree-like representation. Instead, use an array (as discussed in class) or one of the Java library classes that can represent a resizable array (Vector or ArrayList). If you use a plain array your code is likely to be simpler, but you will have to include code to make it bigger if it fills up (by creating a bigger array and copying elements). A good strategy for that is to start out with some reasonable-seeming size, and double it if it fills up.

Dummy entity class

You also need a ``dummy'' game entity class you can use to put lots of entries in the priority queue. It will need all the methods of the GameEntity interface, but the only ones that have to do anything are update (store the time passed in) and getUpdateTime (return the time previously passed to update plus something nonzero). To fully test your priority queue, the time between updates should not be the same for all dummy entities. A simple way to make this happen is for each entity to have a timeBetweenUpdates variable, passed in via a constructor; its getUpdateTime method then returns the time of the last update plus timeBetweenUpdates.

Changes to other classes

Finally, you have to make some smaller changes to other classes to allow you to conduct performance experiments:

Performance experiments

Once you have written and tested the above code, I want you to collect and plot timing information that shows how, for each version of the priority queue, performance (time for 100 calls to player's update method) changes as the number of dummy entities increases. You will have to experiment a little to see what number of entities is needed to get meaningful results. Start with a small number (say 10), and then increase it by factors of 2 or 10 until you see performance differences. Then collect timing results for at least six different numbers of dummy entities. (You might start with the value at which you first notice an increase, and then double that repeatedly, or multiply by 10. What you should see is that for $ n$ entities the time for the linked-list version is roughly proportional to $ n^2$ , while for the heap version time is roughly proportional to $ n \log n$ .) I also suggest letting the game run long enough to print several values (at least three or four) for the ``time for 100 updates'' measurement, and then averaging them.

Plot your performance data in whatever way is easiest for you -- using whatever tool you prefer for making plots, or with pencil and paper if that's easier. I usually use gnuplot (that's what I've used in class), but it does take a bit of learning to use. Details on request.

Files and links



Berna Massingill
2010-04-23