-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathknapsack.lp
More file actions
29 lines (24 loc) · 795 Bytes
/
knapsack.lp
File metadata and controls
29 lines (24 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
%*
* File: knapsack.lp
* Author: Joshua T. Guerin
*
* Description: Given a set of (value, weight) pairs, and parameter n,
* select the maximize the value of included items while
* not exceeding a weight of n.
* Use: clingo knapsack.lp instance.lp -c n=<val>
* where instance.lp contains item predicates.
*%
% Every item may be in the knapsack.
{knapsack(V, W)} :- item(V, W).
% Compute total weight, value, and count of items.
weight(S) :- S = #sum{W, V : knapsack(V, W)}.
value(S) :- S = #sum{V, W : knapsack(V, W)}.
count(C) :- C = #count{V, W : knapsack(V, W)}.
% Total weight may not exceed n.
:- weight(W), W>n.
% Maximize individual values.
#maximize{V, W : value(V), weight(W)}.
#show knapsack/2.
#show weight/1.
#show value/1.
#show count/1.