pykp.knapsack.Knapsack.

add#

Knapsack.add(item: Item) list#

Includes a specific item in the knapsack.

Parameters:
itemItem

The item to add. Must already exist among self.items.

Returns:
list

The updated binary state array after adding the item.

Raises:
ValueError

If item is not of type Item or if it is not present in self.items.

Examples

>>> items = [Item(10, 5), Item(15, 5)]
>>> knapsack = Knapsack(
>>>     items=items,
...     capacity=6
... )
>>> knapsack.add(items[1])
array([0, 1])
>>> knapsack.value
15
>>> knapsack.weight
5