pykp.sampler#

Provides an interface for sampling knapsack instances.

Example

Randomly sample a large number of knapsack instances using the Sampler class:

from pykp.sampler import Sampler

samples = []
for _ in tqdm(range(100)):
    sampler = Sampler(
        num_items=10,
        normalised_capacity=0.5,
    )
    samples.append(Sampler.sample(sampler))

You can also specify the distribution to sample weights and values from:

from pykp.sampler import Sampler

samples = []
for _ in tqdm(range(100)):
    sampler = Sampler(
        num_items=10,
        normalised_capacity=0.5,
        weight_dist=(
            np.random.default_rng().normal,
            {"loc": 100, "scale": 10},
        ),
        value_dist=(
            np.random.default_rng().normal,
            {"loc": 100, "scale": 10},
        ),
    )

Classes

Sampler(num_items, normalised_capacity, ...)