-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneration.py
41 lines (31 loc) · 1.19 KB
/
generation.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
#!/bin/false
# Not for execution
DEFAULT_AGE = 3
class GenerationTracker:
def __init__(self):
self.last_chosen = dict()
self.generation = 1
def get_weights(self, additive_offset=None):
if additive_offset is None:
additive_offset = 0
return {o: (self.generation - last_time + additive_offset) ** 2 for o, last_time in self.last_chosen.items()}
def notify_join(self, option):
assert option not in self.last_chosen
self.last_chosen[option] = self.generation - DEFAULT_AGE
def notify_leave(self, option):
assert option in self.last_chosen
del self.last_chosen[option]
def notify_chosen(self, chosen_option):
self.generation += 1
self.last_chosen[chosen_option] = self.generation
def to_dict(self):
return dict(g=self.generation, lc=self.last_chosen)
@staticmethod
def from_dict(d):
gt = GenerationTracker()
gt.generation = d['g']
gt.last_chosen = d['lc']
return gt
@staticmethod
def combine_weights(coeff1, weights1, coeff2, weights2):
return {p: coeff1 * w + coeff2 * weights2[p] for p, w in weights1.items() if p in weights2}