|
| 1 | +import random |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +EPS = 1e-6 |
| 5 | + |
| 6 | + |
| 7 | +def blend( |
| 8 | + candidates_dict: dict[str, list[dict[str, Any]]], |
| 9 | + weights_dict: dict[str, float], |
| 10 | + fixed_pos: dict[int, str] = None, |
| 11 | + limit: int = 0, |
| 12 | + random_seed: int = 42 |
| 13 | + ) -> list[dict[str, Any]]: |
| 14 | + """ |
| 15 | + Blends candidates from multiple recommendation engines. Blending is implemented |
| 16 | + as sampling with weights. Besides of that, it is possible to set fixed engines |
| 17 | + to some positions. |
| 18 | +
|
| 19 | +
|
| 20 | + Args: |
| 21 | + - candidates_dict: Contains recommendation engine names with their outputs |
| 22 | + Items in candidate lists must have "id" field |
| 23 | + - weights_dict: Contains weights for each engine. Should have the same keys |
| 24 | + as candidates_dict. Weights may not sum to 1 |
| 25 | + - fixed_pos: Allows to set fixed engines to provided positions. Starts from 0 |
| 26 | + - limit |
| 27 | + - random_seed |
| 28 | + """ |
| 29 | + |
| 30 | + random.seed(random_seed) |
| 31 | + |
| 32 | + # input validation and processing |
| 33 | + if set(candidates_dict.keys()) != set(weights_dict.keys()): |
| 34 | + raise ValueError('Keys in candidates_dict and weights_dict do not match') |
| 35 | + |
| 36 | + if fixed_pos: |
| 37 | + for engine in fixed_pos.values(): |
| 38 | + if engine not in candidates_dict: |
| 39 | + raise ValueError(f'Engine {engine} does not present in candidates_dict') |
| 40 | + |
| 41 | + if limit == 0: |
| 42 | + for candidates in candidates_dict.values(): |
| 43 | + limit += len(candidates) |
| 44 | + |
| 45 | + # candidates_dict will be changed inplace further |
| 46 | + candidates_dict = candidates_dict.copy() |
| 47 | + for engine in candidates_dict.keys(): |
| 48 | + candidates_dict[engine] = candidates_dict[engine].copy() |
| 49 | + |
| 50 | + # engines list is ensured to have non-empty engines |
| 51 | + engines = [ |
| 52 | + engine for engine in candidates_dict.keys() |
| 53 | + if len(candidates_dict[engine]) > 0 |
| 54 | + ] |
| 55 | + |
| 56 | + weights = [ (weights_dict[engine] + EPS) for engine in engines] |
| 57 | + if len(engines) == 0: |
| 58 | + return [] |
| 59 | + |
| 60 | + res = [] |
| 61 | + |
| 62 | + for res_idx in range(limit): |
| 63 | + engine = None |
| 64 | + |
| 65 | + # process fixed positions |
| 66 | + if fixed_pos and res_idx in fixed_pos: |
| 67 | + engine = fixed_pos[res_idx] if fixed_pos[res_idx] in engines else None |
| 68 | + |
| 69 | + # sample engine |
| 70 | + if engine is None: |
| 71 | + engine = random.choices(population=engines, weights=weights)[0] |
| 72 | + |
| 73 | + next_item = candidates_dict[engine][0].copy() |
| 74 | + res.append(next_item) |
| 75 | + |
| 76 | + # process candidates intersection |
| 77 | + for engine in engines: |
| 78 | + # remove all matches with next_item |
| 79 | + stop = False |
| 80 | + while not stop: |
| 81 | + stop = True |
| 82 | + for idx in range(len(candidates_dict[engine])): |
| 83 | + if next_item['id'] == candidates_dict[engine][idx]['id']: |
| 84 | + candidates_dict[engine].pop(idx) |
| 85 | + stop = False |
| 86 | + break |
| 87 | + |
| 88 | + # maintain non-empty engines |
| 89 | + engines = [engine for engine in engines if len(candidates_dict[engine]) > 0] |
| 90 | + weights = [ (weights_dict[engine] + EPS) for engine in engines ] |
| 91 | + |
| 92 | + if len(engines) == 0: |
| 93 | + break |
| 94 | + |
| 95 | + return res |
0 commit comments