File size: 1,695 Bytes
71ea432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
import pandas as pd
import os


class Model:
    def __init__(self, name, elo):
        self.name = name
        self.elo = elo
        self.games_played = 0


class Matchmaking:
    def __init__(self):
        self.models = []
        self.queue = []
        self.start_elo = 1200
        self.k = 20
        self.max_diff = 500
        self.matches = pd.DataFrame()

    def read_history(self):
        path = "match_history"
        files = os.listdir(path)
        for file in files:
            self.matches = pd.concat([self.matches, pd.read_csv(os.path.join(path, file))], ignore_index=True)
        self.matches["datetime"] = pd.to_datetime(self.matches["datetime"], format="%Y-%m-%d %H:%M:%S.%f", errors="coerce")
        self.matches = self.matches.dropna()
        self.matches = self.matches.sort_values("datetime")
        self.matches.reset_index(drop=True, inplace=True)
        model_names = self.matches["model1"].unique()
        self.models = [Model(name, self.start_elo) for name in model_names]

    def compute_elo(self):
        for i, row in self.matches.iterrows():
            model1 = self.get_model(row["model1"])
            model2 = self.get_model(row["model2"])
            result = row["result"]
            delta = model1.elo - model2.elo
            win_probability = 1 / (1 + 10 ** (-delta / 500))
            model1.elo += self.k * (result - win_probability)
            model2.elo -= self.k * (result - win_probability)
            model1.games_played += 1
            model2.games_played += 1

    def get_model(self, name):
        for model in self.models:
            if model.name == name:
                return model
        return None