Note
TL;DR: Your function needs to be callable as: func(profits, weights, capacities, *args)
and needs to return assignments
in the binary form.
If you want to implement and test a novel solution algorithm for the QMKP, you simply need to write a Python function that takes profits
as first argument, weights
as second, and capacities
as third argument. Beyond that, it can have an arbitrary number of additional arguments. However, it needs to be possible to pass them positionally.
The return of the function needs to be the assignment matrix in binary form.
The following example is also illustrated in a Jupyter notebook that you can either run locally or using an online service like Binder.
As an example, we want to implement the following algorithm
Assign the item i with the smallest weight wi to the first knapsack k where it fits, i.e., where ck ≥ wi.
Obviously, this algorithm ignores the profits and will not yield very good results. However, it only serves demonstration purposes.
The above algorithm could be implemented as follows
def example_algorithm(profits, weights, capacities):
assignments = np.zeros((len(weights), len(capacities)))
remaining_capacities = np.copy(capacities)
items_by_weight = np.argsort(weights)
for _item in items_by_weight:
_weight = weights[_item]
_first_ks = np.argmax(remaining_capacities >= _weight)
assignments[_item, _first_ks] = 1
remaining_capacities[_first_ks] -= _weight
return assignments
It should be emphasized that you should not modify any of the input arrays, e.g., capacities
inplace, since this could lead to unintended consequences.
The newly implemented algorithm can then easily be used as follows.
import numpy as np
from qmkpy import total_profit_qmkp, QMKProblem
from qmkpy import algorithms
weights = [5, 2, 3, 4] # four items
capacities = [1, 5, 5, 6, 2] # five knapsacks
profits = np.array([[3, 1, 0, 2],
[1, 1, 1, 4],
[0, 1, 2, 2],
[2, 4, 2, 3]]) # symmetric profit matrix
qmkp = QMKProblem(profits, weights, capacities)
qmkp.algorithm = example_algorithm
assignments, total_profit = qmkp.solve()
print(assignments)
print(total_profit)
When you feel that your algorithm should be added to the QMKPy package, please follow the following steps:
- Place your code in the
qmkpy.algorithms
module, i.e., in theqmkpy/algorithms.py
file. - Make sure that you added documentation in form of a docstring. This should also include possible references to literature, if the algorithm is taken from any published work.
- Make sure that all unit tests pass. In order to do this, add your algorithm to the
SOLVERS
list in the test filetests/test_algorithms.py
. Additionally, you should create a new test filetests/test_algorithm_<your_algo>.py
which includes tests that are specific to your algorithm, e.g., testing different parameter constellations. You can run all tests using thepytest
command.