Getting Started

In the following, a few simple examples are shown.

Basic Usage

The following script contains an example in which a QMKP is defined and solved by the implemented constructive procedure.

Defining and Solving a QMKP
 1import numpy as np
 2from qmkpy import total_profit_qmkp, QMKProblem
 3from qmkpy import algorithms
 4
 5weights = [5, 2, 3, 4]  # four items
 6capacities = [10, 5, 12, 4, 2]  # five knapsacks
 7profits = np.array([[3, 1, 0, 2],
 8                    [1, 1, 1, 4],
 9                    [0, 1, 2, 2],
10                    [2, 4, 2, 3]])  # symmetric profit matrix
11
12qmkp = QMKProblem(profits, weights, capacities)
13qmkp.algorithm = algorithms.constructive_procedure
14assignments, total_profit = qmkp.solve()
15
16print(assignments)
17print(total_profit)

Saving a Problem Instance

It is possible to save a problem instance of a QMKP. This can be useful to share examples as a benchmark dataset to compare different algorithms.

Saving a QMKProblem Instance
 1import numpy as np
 2from qmkpy import QMKProblem
 3
 4weights = [5, 2, 3, 4]  # four items
 5capacities = [10, 5, 12, 4, 2]  # five knapsacks
 6profits = np.array([[3, 1, 0, 2],
 7                    [1, 1, 1, 4],
 8                    [0, 1, 2, 2],
 9                    [2, 4, 2, 3]])  # symmetric profit matrix
10
11qmkp = QMKProblem(profits, weights, capacities)
12
13# Save the problem instance using the Numpy npz format
14qmkp.save("my_problem.npz", strategy="numpy")

Loading a Saved QMKProblem Instance

You can also load a previously saved QMKProblem instance to get a qmkpy.QMKProblem object with the same profits, weights and weight capacities.

Loading a Saved QMKProblem
1from qmkpy import QMKProblem
2
3saved_problem = "my_problem.npz"  # saved model file
4qmkp = QMKProblem.load(saved_problem, strategy="numpy")