-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandom_algo.py
41 lines (27 loc) · 1.17 KB
/
random_algo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from random import sample
import numpy as np
import ProblemModel
def find_node_containing_context(context, leaves):
for leaf in leaves:
if leaf.contains_context(context):
return leaf
"""
This class represents a random choosing algorithm.
"""
class Random:
problem_model: ProblemModel
def __init__(self, problem_model: ProblemModel, budget):
self.num_rounds = problem_model.num_rounds
self.budget = budget
self.problem_model = problem_model
def run_algorithm(self):
total_reward_arr = np.zeros(self.num_rounds)
regret_arr = np.zeros(self.num_rounds)
for t in range(1, self.num_rounds + 1):
available_arms = self.problem_model.get_available_arms(t)
slate = sample(available_arms, self.budget)
rewards = self.problem_model.play_arms(t, slate) # Returns a list of Reward objects
# Store reward obtained
total_reward_arr[t - 1] = self.problem_model.get_total_reward(rewards)
regret_arr[t - 1] = self.problem_model.get_regret(t, self.budget, slate)
return total_reward_arr, regret_arr