pykp.metrics.sahni_k#

Provides an implementation of the Sahni-K metric for evaluating

arrangements of items in the knapsack problem.

Example

To calculate the Sahni-k of the optimal solution to a knapsack problem
instance, first solve the instance and then call the metric on the

optimal arrangement:

from pyinstance import Knapsack from pyinstance import Item import pyinstance.metrics as metrics

items = [

Item(value=10, weight=5), Item(value=15, weight=10), Item(value=7, weight=3),

] capacity = 15 instance = Knapsack(items=items, capacity=capacity) instance.solve()

sahni_k = metrics.sahni_k(instance.optimal_nodes[0], capacity) print(sahni_k)