diff --git a/README.md b/README.md index 0b6e4a7..ec9af86 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # knapsack -A solver for the knapsack problem in Prolog (Eclipse CSP) + +A solver for the knapsack problem in Eclipse CSP-flavoured Prolog. +Nothing too fancy. Have fun! diff --git a/knapsack.ecl b/knapsack.ecl new file mode 100644 index 0000000..e4a3725 --- /dev/null +++ b/knapsack.ecl @@ -0,0 +1,20 @@ +:-lib(fd). +:-lib(fd_global). +:-lib(branch_and_bound). + +knapsack(P, W, C, T, V) :- + length(P, N), + length(V, N), + minlist(W, Min), + D is C // Min, + V::0..D, + C #>= W*V, + T #= P*V, + sumlist(V, S), + O #= (-1)*T, + Negs #= (-1)*T, + bb_min(labeling(V), O, V, _, _, _), + bb_min(labeling(V), Negs, _). + +test(P, V):- + knapsack([60, 100, 120], [15, 20, 30], 325, P, V).