Program #1

Due March 11

The KNAPSACK problem is defined as follows: You are given a collection of N objects. Each object has a specified weight and a specified value. You are given a capacity, which is the maximum total weight you can carry, and a quota, which is the minimum total value that you want to get. The problem is to find a subset of the objects whose total weight is at most equal to the capacity and whose total value is at least equal to the quota.

For example, suppose that you are given three objects:

Object Weight Value
A 70 80
B 50 50
C 50 50

and you are given a capacity of 110 and a quota of 90. Then a solution to the problem is the set (B C). Note that there is no solution involving object A, because once you have put A in the knapsack, there is no room for the other two. You are not allowed to choose a fraction of an object.

Problem #1[20]: For your language of choice, devise data structures and functions for: a state, a node in the search tree, the initial state, a goal test function, and a function that expands the current node.

Problem #2 [40]: Implement a program that solves the KNAPSACK problem using iterative deepening, a blind search method. Demonstrate it as described below.

Problem #3 [40]: Choose an admissable heuristic and solve the same set of problems using an appropriate informed method. Please compare and briefly discuss the performance of this solution with the previous one (see the output we request below).

 

Input format

The program should take its input from a file. The input file will contain a number of problems in sequence. Each problem has the following format. Each object will parse as a Lisp list, but I've put linefeeds and a comment delimiter in so that it shouldbe easy to parse in other languages.

;;;
(
capacity
quota
number of objects for non-Lisp languages
(name1 weight1 value1)
(name2 weight2 value2)
...
)

For example, our example at the top would be:

;;;
(
110
90
3
(A 70 80)
(B 50 50)
(C 50 50)
)

You may assume that: There are at most 20 objects, a name is a single alphabetical character, and all quotas, capacities, weights, and values are integers.

Output format

The program should give its output in standard output. It should have the following form:

  • "Solution:" Set of objects in solution.
  • "Total weight:" Total weight
  • "Total value:" Total value
  • "Number of nodes in last pass:" Number of nodes generated in final iteration of the outside loop of the iterative deepening or informed search
  • "CPU time:" Total CPU time required.

If no solution exists, then the output for the program should be just "No solution".

For example, the output for the first problem above might be

Solution: (B C)
Total weight: 100
Total value: 100
Number of nodes in last pass: 3
CPU time: 1 msec.

Test file:

A test file with 20 sample problems is HERE

Program:

You may write your program in Lisp, Scheme, C, or Java. If you want to use any other language, you must get the approval of the grader. The program should be well-structured and commented. The grader can take off up to 5 discretionary points for code that works but is not clearly structured.

Deliverables:

You should email to the grader:

  • The source code for the program. Be sure to include your name as a comment at the top of the code.
  • The output of the program on the test file.
  • The grader should be able to run your program on other input files.