COMP26120
Academic Session: 2022-23
Lab Exercise 5: The 0/1 Knapsack Problem
Duration: 3 weeks
Q1703105484
You should do all your work in the lab5 directory of the COMP26120 2022 repository - see Blackboard for further details. You will need to make use of the existing code in the branch as a starting point.
Important: You submit this lab via a quiz on Blackboard. This will:
You can save your answers and submit later so we recommend filling in the questions for each part as you complete it, rather than entering everything at once at the end.
NB: We have made some changes to this lab since the start of semester 1 in the hopes it will be quicker to mark. If you want a helper script for generating input for experiments and a LaTeX template for the report that reflects how it should be structured now, please pull from upstream:
You can do this by typing the following commands in your gitlab directory:
git remote remove upstream
git remote add upstream https://gitlab.cs.man.ac.uk/t95229ld/comp26120_2022_base.git git fetch upstream
git merge upstream/master
You have the choice to complete the lab in C, Java or Python. Program stubs for this exercise exist for each language. Only one language solution will be marked.
Because people had a number of issues with GitLab last year we are going to take a multiple redundancy approach to submission of code. This involves both pushing and tagging a commit to GitLab and uploading a zip of your code to Blackboard. By preference we will mark the code you submitted to GitLab but if we can’t find it or it doesn’t check out properly then we will look in the zip file on Blackboard. Please do both to maximise the chance that one of them will work.
When you submit the assignment through Blackboard you will be asked for the hash and tag of the commit you want marked. This is to make sure the TAs can identify exactly which GitLab commit
Figure 1: Identifying the hash of your most recent commit in GitLab
igure 2: Identifying the hashes of previous commits in GitLab
you want marked. You tag a commit lab5 solution (we recommend you use this tag, but you do not have to) by typing the following at the command line:
git tag lab5_solution git push
git push origin lab5_solution
You can find the hash of your most recent commit by looking in your repository on GitLab as shown in figure 1.
You can also find the hash for a previous commit by clicking on the “commits” link and then iden- tifying the commit you are interested in. This is shown in figure 2.
Note that while the full hash for commits are quite long, we only need the first 8 characters (as shown in the screenshots) to identify for marking.
Reminder: It is bad practice to include automatically generated files in source control (e.g. your git repositories). This applies to object files (C), class files (Java), and compiled bytecode files (Python). It’s not fatal if you do this by mistake, but it can sometimes cause confusions while marking.
While it is fine to discuss this coursework with your friends and compare notes, the work submitted should be your own. In particular this means you should not have copied any of the source code, or the report. We will be using the turnitin tool to compare reports for similarities.
By the end of this lab you should be able to:
In this section we introduce two related ‘Knapsack’ problems.
Suppose an airline cargo company has 1 aeroplane which it flies from the UK to the US on a daily basis to transport some cargo. In advance of a flight, it receives bids for deliveries from (many) customers.
Customers state
The company must choose a subset of the packages (bids) to carry in order to make the maximum possible profit, given the total weight limit that the plane is allowed to carry.
In mathematical form the problem is: Given a set of N items each with weight wi and value vi, for i = 1 to N , choose a subset of items (e.g. to carry in a knapsack, or in this case an aeroplane) so that the total value carried is maximised, and the total weight carried is less than or equal to a given carrying capacity, C. As we are maximising a value given some constraints this is an optimisation problem.
This kind of problem is known as a 0/1 Knapsack problem. A Knapsack problem is any problem that involves packing things into limited space or a limited weight capacity. The problem above is “0/1” because we either do carry an item: “1”; or we don’t: “0”. Other problems allow that we can take more than 1 or less than 1 (a fraction) of an item. Below is a description of a fractional problem.
See the description in Algorithm Design and Applications, p. 498. or the briefer description in
Introduction to Algorithms, p. 417.
A straightforward method for solving any 0/1 Knapsack problem is to try out all possible ways of packing/leaving out the items. We can then choose the most valuable packing that is within the weight limit.
|
3 1 5 4 2 12 10 3 8 5 11 |
e first line gives the number of items; the last line gives the capacity of the knapsack; the remaining lines give the index, value and weight of each item e.g. item 2 has value 12 and weight 10.
The full enumeration of possible packings would be as follows:
Items Packed |
Value |
Weight |
Feasible? |
|
000 |
0 |
0 |
Yes |
|
001 |
8 |
5 |
Yes |
|
010 |
12 |
10 |
Yes |
|
011 |
20 |
15 |
No |
|
100 |
5 |
4 |
Yes |
|
101 |
13 |
9 |
Yes |
OPTIMAL |
110 |
17 |
14 |
No |
|
111 |
25 |
19 |
No |
The items packed column represents the packings as a binary string, where “1” in position i means pack item i, and 0 means do not pack it. Every combination of 0s and 1s has been tried. The one which is best is 101 (take items 1 and 3), which has weight 9 (so less than C = 11) and value 13. We can also represent a solution as an array of booleans (this approach is taken in the Java and Python stubs).
In this lab we will investigate some efficient ways of finding optimal solutions and approximate solu- tions.
This lab asks you to implement four different solutions to the 1/0 Knapsack problem over three weeks. We have provided partial solutions and it is your job to complete them.