Shuffles the values and sorts them afterwards. This can be used to break the tie when the highest utility score is not unique. The shuffle randomizes order, which is preserved by the mergesort algorithm. Args: values: Contains the values to be selected from. n_insta
(values: np.ndarray, n_instances: int = 1)
| 6 | |
| 7 | |
| 8 | def shuffled_argmax(values: np.ndarray, n_instances: int = 1) -> np.ndarray: |
| 9 | """ |
| 10 | Shuffles the values and sorts them afterwards. This can be used to break |
| 11 | the tie when the highest utility score is not unique. The shuffle randomizes |
| 12 | order, which is preserved by the mergesort algorithm. |
| 13 | |
| 14 | Args: |
| 15 | values: Contains the values to be selected from. |
| 16 | n_instances: Specifies how many indices and values to return. |
| 17 | Returns: |
| 18 | The indices and values of the n_instances largest values. |
| 19 | """ |
| 20 | assert n_instances <= values.shape[0], 'n_instances must be less or equal than the size of utility' |
| 21 | |
| 22 | # shuffling indices and corresponding values |
| 23 | shuffled_idx = np.random.permutation(len(values)) |
| 24 | shuffled_values = values[shuffled_idx] |
| 25 | |
| 26 | # getting the n_instances best instance |
| 27 | # since mergesort is used, the shuffled order is preserved |
| 28 | sorted_query_idx = np.argsort(shuffled_values, kind='mergesort')[ |
| 29 | len(shuffled_values)-n_instances:] |
| 30 | |
| 31 | # inverting the shuffle |
| 32 | query_idx = shuffled_idx[sorted_query_idx] |
| 33 | |
| 34 | return query_idx, values[query_idx] |
| 35 | |
| 36 | |
| 37 | def shuffled_argmin(values: np.ndarray, n_instances: int = 1) -> np.ndarray: |
no outgoing calls
no test coverage detected
searching dependent graphs…