-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
68 lines (57 loc) · 2.4 KB
/
simulation.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
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
""" This module runs successively game turns
"""
import pickle
import pandas as pd
from collections import defaultdict
import game_on
def statistics(n):
""" This function calls individual games up to N
and organizes the dictionaries in DataFrame lines
:param n: number of times to run individual games
:returns DataFrame with processed results
"""
goals = defaultdict(int)
output = pd.DataFrame(columns=['tie', 'strategy', 'goal', 'n_countries', 'o_avg_dice',
'w_avg_dice', 'w_num_rolls', 'o_avg_num_rolls',
'2nd_avg_dice', '2nd_num_rolls', 'n_players_end', 'n_changed_goals'])
for i in range(n):
print(f'Game {i}')
# Results of game_on come as a dictionary and the actual world object
results, world = game_on.main(6, False)
output.loc[i, ] = results
for p in world.players:
goals[p.goal.type] += 1
goals2 = pd.DataFrame(goals, index=[0])
goals2.to_csv(f'results/goals_{n}.csv', sep=';', index=False)
return output
def summary(data):
n = len(data)
data = data.groupby(by=['strategy', 'goal', 'tie']).agg(['mean', 'count'])
data = data.reset_index()
data.columns = data.columns.droplevel(1)
d1 = data.iloc[:, :5]
d2 = data.iloc[:, [5, 7, 9, 11, 13, 15, 17, 19]]
data = pd.concat([d1, d2], axis=1)
data.columns = ['strategy', 'goal', 'tie', 'n_countries', 'num_wins', 'o_avg_dice', 'w_avg_dice', 'w_num_rolls',
'o_avg_num_rolls', '2nd_avg_dice', '2nd_num_rolls', 'n_players_end', 'avg_n_changed_goals']
data.to_csv(f'results/summary_{n}.csv', sep=';', index=False)
return data
def main(n=10000, generate=True):
if generate:
c = statistics(n)
with open(f'results/objects_{n}', 'wb') as f:
pickle.dump(c, f)
else:
with open(f'results/objects_{n}', 'rb') as f:
c = pickle.load(f)
c[['n_countries', 'o_avg_dice', 'w_avg_dice', 'w_num_rolls', 'o_avg_num_rolls', '2nd_avg_dice', '2nd_num_rolls',
'n_players_end', 'n_changed_goals']] = \
c[['n_countries', 'o_avg_dice', 'w_avg_dice', 'w_num_rolls', 'o_avg_num_rolls', '2nd_avg_dice',
'2nd_num_rolls', 'n_players_end', 'n_changed_goals']].astype('float')
s = summary(c)
# print(s)
return c
if __name__ == '__main__':
m = 100000
gen = False
o = main(m, gen)