Created
October 11, 2019 04:48
-
-
Save BlueRayi/c67e16faf1d8b975af1827c55f02b79b to your computer and use it in GitHub Desktop.
ゲームのシミュレーション
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[1]: | |
import random | |
from pathlib import Path | |
# In[2]: | |
import pandas as pd | |
# In[3]: | |
class Player: | |
def __init__(self, rating=1500, rate_num=0): | |
self.rating = rating | |
self.rate_num = rate_num | |
def win_probability(self, rival): | |
return 1. / (10. ** ((rival.rating - self.rating) / 400.) + 1.) | |
def win(self, rival): | |
return random.random() < self.win_probability(rival) | |
@property | |
def K(self): | |
return 24 if self.rate_num >= 20 else 32 | |
# In[4]: | |
random.seed(913) # ゲーム結果を固定する | |
# In[5]: | |
players = { | |
'Emu': Player(2700.), | |
'Parado': Player(2700.), | |
'Niko': Player(2500.), | |
'Taiga': Player(2000.), | |
'Hiiro': Player(2000.), | |
'Kiriya': Player(1700.), | |
'Kuroto': Player(1700.), | |
'Poppy': Player(1500.), | |
} | |
# In[6]: | |
column_names = [] | |
for i in range(len(players) // 2): | |
column_names.append(f'Match {i + 1} Winner') | |
column_names.append(f'Match {i + 1} Loser') | |
# In[7]: | |
players_names = ['Emu', 'Parado', 'Niko', 'Taiga', 'Hiiro', 'Kiriya', 'Kuroto', 'Poppy'] | |
rows = [] | |
for _ in range(500): | |
matching_order = [] | |
for players_name in players_names: | |
matching_order.append((players_name,players[players_name],)) | |
matching_order = random.sample(matching_order, len(matching_order)) | |
result_per_game = matching_order.copy() | |
for i in range(0, len(result_per_game), 2): | |
if result_per_game[i + 1][1].win(result_per_game[i][1]): | |
result_per_game[i],result_per_game[i + 1], = result_per_game[i + 1],result_per_game[i], | |
result_per_game_name = list(map(lambda x: x[0], result_per_game)) | |
rows.append(pd.Series(result_per_game_name, index=column_names)) | |
# In[8]: | |
result_df = pd.DataFrame(rows) | |
result_df.index = result_df.index + 1 | |
result_df = result_df.reset_index() | |
result_df | |
# In[9]: | |
simulation_result_csv = Path('./simulation_result.csv') | |
result_df.to_csv(simulation_result_csv, index=False) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment