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

  1. The profit matrix is quadratic of size \(N\),

  2. 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 (list of float, 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 True when the array x is binary and False otherwise.

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 as K=len(capacities).

  • raise_error (bool, optional) – If raise_error is False, the function returns a bool, that states whether the solution is feasible. If raise_error is True, the function raises a ValueError instead.

Returns

Indication if the solution is feasible (True) or not (False)

Return type

bool

Raises

ValueError – This is only raised when raise_error is True.

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 True if the matrix is symmetric and False otherwise. When raise_error is set to True, a ValueError is 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) – If raise_error is False, the function returns a bool, that states whether the solution is feasible. If raise_error is True, the function raises a ValueError instead.

Returns

Indication if the solution is feasible (True) or not (False)

Return type

bool

Raises

ValueError – This is raised when raise_error is True and the matrix is not symmetric. It can also be raised when the provided profits is not a square matrix.