Please make sure you adhere to the policies on academic honesty.
Please see the general programming webpage for details about the programming environment for this course, and specifically for directions in how to submit your programming assignment electronically.
The files you need for this assignment can be downloaded here.
Also, you will need to use the vector template class which is part of the standard C++ libraries. We will highlight details of that class below.
This setup can actually be used in modeling a variety of different applications, and it has been given the name "Bin Packing" in the literature. Unfortunately, minimizing the number of bins appears to be a very difficult problem to solve efficiently (there are too many possibilities!). Fortunately a number of heuristics have been studied, and some of these seem to get quite good results.
A second heuristic is known as "worst-fit-decreasing." This heuristics considers items one-at-a-time, again placing each considered item into the bin with the most remaining space (if such a bin will hold the item). The difference between this heuristic and the last is that items are considered in order of decreasing weight, rather than in the original order. To do this, all items are initially placed into a priority queue and at each step the item with largest remaining weight is removed from this queue and then placed into a bin. (note that for this to work, you will have to adjust your view of priorities so that the "minKey" is the one with the largest weight). Looking at our previous example, the items would get considered in the order .81, .68, .29 and .11, and thus packed into two bins. By no means are these the only two approaches to bin packing. If anyone is interested in the history of the problem, we will be happy to provide references. But for this assignment, we will focus on these two heuristics.
The driver accepts the following four command line arguments, as specified by the user. The first three of these are required; the fourth is optional.
Although generating random data is useful for experiments, it is troublesome when developing and testing your program. The difficulty is that when a bug arises, you generally want to fix the bug and then re-test on the same data. But if data is generated at random, you will likely get a different data set when you re-test. By explicitly setting the "seed" you can later re-test on the identical data set.
After reading the parameters, our driver calls a routine (to be written by you) which performs the worst-fit heuristic, then another call is made to perform the worst-fit-decreasing heuristic.
As we mentioned, your primary task is to correctly implement both of these heuristics. Specifically, you are to implement the following routine in file Binpack.cc:
int worstFit(double* w, int N, bool decreasing, bool verbose)The int return value should be the total number of bins which were used. The parameters are to be interpreted as follows:
We ask that you perform some experiments which will be reported in your 'readme' file. This will allow us to compare the quality of the solutions produced by the two heuristics as well as the efficiency of the two priority queue implementations.
You must submit a plain-text readme file with the following information:
You have a choice in how to gather this information. One way is to sit at the computer for a while and gather piece after piece of information using our driver. A wise student might feel this is a mindless use of time and surely can be automated by a new or modified driver. For your benefit, we will tell you that our driver relies internally on a procedure runTrial(int N, double u, bool verbose, long seed) You could modify our driver adding a loop to call this method repeatedly with various parameter values.
Our generic Priority Queue template requires that you specify the type for the key, the type for the element, and a class which is a valid comparator for the specified key type.
To help out, we have written a comparator class for you, DoubleCompare. This class is based on the assumption that keys are of type double. This should suffice for the assignment, though you will still need to carefully consider what values to use for keys to get the desired behavior.
This program might seem intimidating because of the complexity. Here is our suggestion for getting going:
Specifically, your routine should start by inserting all of the grocery weights into a priority queue, which will be used in the next stage for processing these items one at a time. By setting the key for each item appropriately, you should be able to process the set of items either in the original order (for worst-fit) or in decreasing order of weight (for worst-fit-decreasing).
To test your progress thus far, you might start removing items from the queue, printing each item before it is removed (you can eventually convert this loop to the loop which processes each item; the extraneous print statements should eventaully be commented out).
The assignment is worth 10 points. In general, we will try to apportion the points based on the following break down (though we will use some of our own judgment in the end):
#include <vector>
using namespace std;
As with our priority queue, a vector can hold an arbitrary type of objects, but you must declare the type when creating a vector. Therefore, a vector V of double's can be declared as:
vector<double> V;
Very detailed documentation for the vector class is provided by SGI, however you will only need to use a limited subset of the functionality.
| method | description |
|---|---|
| unsigned int size() const | The current number of elements in the vector |
| reference at(int r) const | Returns reference to element at rank r in vector |
| void push_back(e) | Inserts element e at the end of the vector |
| void pop_back() | Removes the last element from the vector |
| void clear() | Erases all of the elements, thereby resetting vector to empty |
Vector's also allow you to access an element directly via indexing. For example, if V is a vector, than you can access the item at rank r as V[r].