RosettaCodeData/Task/Knapsack-problem-Unbounded/Bracmat/knapsack-problem-unbounded....

71 lines
1.6 KiB
Plaintext

(knapsack=
( things
= (panacea.3000.3/10.25/1000)
(ichor.1800.2/10.15/1000)
(gold.2500.2.2/1000)
)
& 0:?maxvalue
& :?sack
& ( add
= cumwght
cumvol
cumvalue
cumsack
name
wght
val
vol
tings
n
ncumwght
ncumvalue
ncumvol
. !arg
: ( ?cumwght
. ?cumvol
. ?cumvalue
. ?cumsack
. (?name.?val.?wght.?vol) ?tings
)
& -1:?n
& whl
' ( 1+!n:?n
& !cumwght+!n*!wght:~>25:?ncumwght
& !cumvol+!n*!vol:~>250/1000:?ncumvol
& !cumvalue+!n*!val:?ncumvalue
& ( !tings:
& ( !ncumvalue:>!maxvalue:?maxvalue
& !cumsack
( !n:0&
| ( !cumsack:&Take
| Finally
)
" take "
!n
" items of "
!name
".\n"
)
: ?sack
|
)
| add
$ ( !ncumwght
. !ncumvol
. !ncumvalue
. !cumsack
( !n:0&
| "Take " !n " items of " !name ".\n"
)
. !tings
)
)
)
)
& add$(0.0.0..!things)
& out$(str$(!sack "The value in the knapsack is " !maxvalue "."))
&
);
!knapsack;