qmkpy.checks module
Various checks/verification functions.
This module contains various functions to perform check/verify provided parameters in the context of the QMKP. For example, this includes a check whether a provided assignment complies with the weight/capacity constraints.
- qmkpy.checks.check_dimensions(profits: array, weights: Optional[Iterable[float]] = None) NoReturn
Simple check whether the dimensions of the parameters match.
This function checks that
The profit matrix is quadratic of size \(N\),
The number of items is equal to \(N\), i.e.,
len(weights)==N.
- Parameters
profits (
np.array) – Symmetric matrix of size \(N\times N\) containing the profits \(p_{ij}\).weights (
listoffloat, optional) – List which contains the weights of the \(N\) items.
- Raises
ValueError – This function raises a
ValueError, if there is a mismatch.
- qmkpy.checks.is_binary(x: Iterable[float]) bool
Check whether a provided array is binary
This function checks that all elements of the input are either 0 or 1.
- Parameters
x (
Iterable) – Array of numbers- Returns
binary – Returns
Truewhen the arrayxis binary andFalseotherwise.- Return type
bool
- qmkpy.checks.is_feasible_solution(assignments: array, profits: array, weights: Iterable[float], capacities: Iterable[float], raise_error: bool = False) bool
Check whether a provided assignment is a feasible solution.
This function performs a formal check whether the provided assignments is a feasible solution of the specified QMKProblem. This means that the shapes of the arrays match and that no weight capacity constraint is violated.
- Parameters
assignments (
np.array) – Binary matrix of size \(N\times K\) which represents the final assignments of items to knapsacks. If \(a_{ij}=1\), element \(i\) is assigned to knapsack \(j\).profits (
np.array) – Symmetric matrix of size \(N\times N\) that contains the (joint) profit values \(p_{ij}\). The profit of the single items \(p_i\) corresponds to the main diagonal elements, i.e., \(p_i = p_{ii}\).weights (
list) – List of weights \(w_i\) of the \(N\) items that can be assigned.capacities (
list) – Capacities of the knapsacks. The number of knapsacks \(K\) is determined asK=len(capacities).raise_error (
bool, optional) – Ifraise_errorisFalse, the function returns abool, that states whether the solution is feasible. Ifraise_errorisTrue, the function raises aValueErrorinstead.
- Returns
Indication if the solution is feasible (
True) or not (False)- Return type
bool- Raises
ValueError – This is only raised when
raise_errorisTrue.
- qmkpy.checks.is_symmetric_profits(profits: array, raise_error: bool = False) bool
Check whether the profit matrix is symmetric.
This function performs a check whether the profit matrix \(P\) is symmetric. This is expected for the QMKP.
By default, the function returns
Trueif the matrix is symmetric andFalseotherwise. Whenraise_erroris set toTrue, aValueErroris raised instead.- Parameters
profits (
np.array) – Symmetric matrix of size \(N\times N\) that contains the (joint) profit values \(p_{ij}\). The profit of the single items \(p_i\) corresponds to the main diagonal elements, i.e., \(p_i = p_{ii}\).raise_error (
bool, optional) – Ifraise_errorisFalse, the function returns abool, that states whether the solution is feasible. Ifraise_errorisTrue, the function raises aValueErrorinstead.
- Returns
Indication if the solution is feasible (
True) or not (False)- Return type
bool- Raises
ValueError – This is raised when
raise_errorisTrueand the matrix is not symmetric. It can also be raised when the providedprofitsis not a square matrix.