Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,25 @@
# Input should be the array of objects giving name, weight and value.
# Because of the way addition is defined on null and because of the
# way setpath works, there is no need to initialize the matrix m in
# detail.
def dynamic_knapsack(W):
. as $objects
| length as $n
| reduce range(1; $n+1) as $i # i is the number of items
# state: m[i][j] is an array of [value, array_of_object_names]
(null; # see above remark about initialization of m
$objects[$i-1] as $o
| reduce range(0; W+1) as $j
( .;
if $o.weight <= $j then
.[$i-1][$j][0] as $v1 # option 1: do not add this object
| (.[$i-1][$j - $o.weight][0] + $o.value) as $v2 # option 2: add it
| (if $v1 > $v2 then
[$v1, .[$i-1][$j][1]] # do not add this object
else [$v2, .[$i-1][$j - $o.weight][1]+[$o.name]] # add it
end) as $mx
| .[$i][$j] = $mx
else
.[$i][$j] = .[$i-1][$j]
end))
| .[$n][W];

View file

@ -0,0 +1,26 @@
def objects: [
{name: "map", "weight": 9, "value": 150},
{name: "compass", "weight": 13, "value": 35},
{name: "water", "weight": 153, "value": 200},
{name: "sandwich", "weight": 50, "value": 160},
{name: "glucose", "weight": 15, "value": 60},
{name: "tin", "weight": 68, "value": 45},
{name: "banana", "weight": 27, "value": 60},
{name: "apple", "weight": 39, "value": 40},
{name: "cheese", "weight": 23, "value": 30},
{name: "beer", "weight": 52, "value": 10},
{name: "suntancream", "weight": 11, "value": 70},
{name: "camera", "weight": 32, "value": 30},
{name: "T-shirt", "weight": 24, "value": 15},
{name: "trousers", "weight": 48, "value": 10},
{name: "umbrella", "weight": 73, "value": 40},
{name: "waterproof trousers", "weight": 42, "value": 70},
{name: "waterproof overclothes", "weight": 43, "value": 75},
{name: "note-case", "weight": 22, "value": 80},
{name: "sunglasses", "weight": 7, "value": 20},
{name: "towel", "weight": 18, "value": 12},
{name: "socks", "weight": 4, "value": 50},
{name: "book", "weight": 30, "value": 10}
];
objects | dynamic_knapsack(400)[]

View file

@ -0,0 +1,3 @@
$jq -M -c -n -f knapsack.jq
1030
["map","compass","water","sandwich","glucose","banana","suntancream","waterproof trousers","waterproof overclothes","note-case","sunglasses","socks"]