-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
127 lines (87 loc) · 3.83 KB
/
player.py
File metadata and controls
127 lines (87 loc) · 3.83 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from __future__ import annotations
from abc import ABC, abstractmethod
from random import uniform
SCALE_FACTOR = 400
INITIAL_PLAYER_K = 32
FINAL_PLAYER_K = 16
INITIAL_QUESTION_K = 32
FINAL_QUESTION_K = 8
class RatedEntity:
"""Represents an entity that has an ELO-like numerical rating."""
def __init__(self, rating: float, K: int):
self.rating = rating
self.K = K
def expected_score(self, other: RatedEntity) -> float:
"""Compute the expected score when facing the other rated entity."""
return 1 / (1 + pow(10, (other.rating - self.rating) / SCALE_FACTOR))
def update_score(self, expected: float, actual: float):
"""Update the rating according to the given scores."""
self.rating += self.score_delta(expected, actual)
def score_delta(self, expected: float, actual: float) -> float:
"""Compute how much the rating would change according to the given scores."""
return self.K * (actual - expected)
class Question(RatedEntity):
"""Represents a question with an ELO rating.
A question is a rated entity that defines a strategy to handle the evolution
of the K factor.
"""
def __init__(self, rating: float):
super().__init__(rating, INITIAL_QUESTION_K)
def update_K(self):
"""Define the strategy to evolve K over time."""
self.K = max(FINAL_QUESTION_K, self.K - 1)
def update_score(self, expected: float, actual: float):
"""Update the score of the rated entity and then update K."""
super().update_score(expected, actual)
self.update_K()
class Player(RatedEntity, ABC):
"""Represents a player with an ELO rating.
A player is a rated entity that provides a (possibly random) function
that determines the score the player would get when answering a question.
This class also defines a strategy to handle the evolution of the K factor.
"""
def __init__(self, rating: float, question_queue: list[Question]):
super().__init__(rating, INITIAL_PLAYER_K)
self.question_queue = question_queue
@abstractmethod
def generate_score(self) -> float:
"""Generates the answering score of the player following its archetype."""
pass
def update_K(self):
"""Define the strategy to evolve K over time."""
self.K = max(FINAL_PLAYER_K, self.K - 1)
def answer_and_update(self, question: RatedEntity):
"""Answer a question and update own and question's ratings."""
expected_score = self.expected_score(question)
actual_score = self.generate_score()
self.update_score(expected_score, actual_score)
question.update_score(1 - expected_score, 1 - actual_score)
self.update_K()
def answer_next(self):
"""Takes the next question in the player queue and answers it."""
self.answer_and_update(self.question_queue.pop())
class AlwaysRight(Player):
"""A player that gets all questions correctly."""
def generate_score(self) -> float:
return 1
class AlwaysMid(Player):
"""A player that always gets all questions half-right."""
def generate_score(self) -> float:
return 0.5
class AlwaysWrong(Player):
"""A player that gets all questions incorrectly."""
def generate_score(self) -> float:
return 0
class UsuallyRight(Player):
"""A player with a uniformly distributed score in [0.3, 1]."""
def generate_score(self) -> float:
return uniform(0.3, 1)
class UsuallyWrong(Player):
"""A player with a uniformly distributed score in [0, 0.7]."""
def generate_score(self) -> float:
return uniform(0, 0.7)
if __name__ == "__main__":
# Wikipedia examples.
A = RatedEntity(1613, 32)
print(A.expected_score(RatedEntity(1609, 32))) # Should give approximately .51
print(A.score_delta(2.88, 2.5)) # Should give approximately -12.