From b4c72af5a32123942dadcad94ecdf4deda37d56b Mon Sep 17 00:00:00 2001 From: Hironsan Date: Wed, 26 Jul 2023 16:41:37 +0900 Subject: [PATCH] Add weighted random strategy --- backend/examples/assignment/strategies.py | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 backend/examples/assignment/strategies.py diff --git a/backend/examples/assignment/strategies.py b/backend/examples/assignment/strategies.py new file mode 100644 index 00000000..36b52df8 --- /dev/null +++ b/backend/examples/assignment/strategies.py @@ -0,0 +1,29 @@ +import abc +import dataclasses +from typing import List + +import numpy as np + + +@dataclasses.dataclass +class Assignment: + user: int + example: int + + +class BaseStrategy(abc.ABC): + @abc.abstractmethod + def assign(self) -> List[Assignment]: + ... + + +class WeightedRandomStrategy: + def __init__(self, dataset_size: int, weights: List[int]): + assert sum(weights) == 100 + self.dataset_size = dataset_size + self.weights = weights + + def assign(self) -> List[Assignment]: + proba = np.array(self.weights) / 100 + assignees = np.random.choice(range(len(self.weights)), size=self.dataset_size, p=proba) + return [Assignment(user=user, example=example) for example, user in enumerate(assignees)]