pykp.solvers.mzn_gecode#
Provides an implementation of the MiniZinc and Gecode solver for solving the knapsack problem. You should have MiniZinc 2.5.0 (or higher) installed on your system to use this solver. Note that this solver is not robust to multiple solutions, and will report only the first optimal solution found. If knowing all optimal solutions is important, consider using the branch-and-bound solver.
Example
To solve a knapsack problem instance using the MiniZinc Gecode solver, first create a list of items and then call the solver with the items and capacity:
from pykp import Item, Solvers
items = [
Item(value=10, weight=5),
Item(value=15, weight=10),
Item(value=7, weight=3),
]
capacity = 15
optimal_node = solvers.mzn_gecode(items, capacity)
print(optimal_node)
Alternatively, construct an instance of the Knapsack class and call the solve method with “mzn_gecode” as the method argument:
from pykp import Item, Knapsack
items = [
Item(value=10, weight=5),
Item(value=15, weight=10),
Item(value=7, weight=3),
]
capacity = 15
instance = Knapsack(items=items, capacity=capacity)
optimal_node = instance.solve(method="mzn_gecode")
print(optimal_node)