pykp.knapsack.Knapsack.
solve#
- Knapsack.solve(method: Literal['branch_and_bound', 'minizinc'] = 'branch_and_bound', minizinc_solver: str = 'coinbc')#
Solves the knapsack problem to find optimal arrangements.
- Parameters:
- method{“branch_and_bound”, “minizinc”}, optional
The algorithm to use for solving. Default is “branch_and_bound”.
- Returns:
- np.ndarray
An array of Arrangement objects representing the optimal solutions.
- Other Parameters:
- minizinc_solver: str, optional
If
method="minizinc", this argument specifies which minizinc solver to use. Default is “coinbc”.
- Raises:
- ValueError
If the specified method is invalid.
Examples
>>> from pykp import Knapsack >>> from pykp import Item >>> items = [ ... Item(value=10, weight=5), ... Item(value=15, weight=10), ... Item(value=7, weight=3), ... ] >>> capacity = 15 >>> knapsack = Knapsack(items=items, capacity=capacity) >>> knapsack.solve(method="branch_and_bound") >>> print(knapsack.optimal_nodes) [(v: 25, w: 15, s: 3)]