forked from gamescomputersplay/monopoly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonopoly-simulator.py
131 lines (101 loc) · 3.5 KB
/
monopoly-simulator.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
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
128
129
130
131
import random
import math
import time
import matplotlib.pyplot as plt
import numpy as np
from game import *
from utils import *
from game.runner import oneGame
# simulation settings
nPlayers = 4
nMoves = 1000
nSimulations = 1000
seed = "" # "" for none
shufflePlayers = True
# reporting settings
progressAsterix = 1 # output * every N simulations
showMap = False # only for 1 game: show final board map
showResult = True # only for 1 game: show final money score
showRemPlayers = True
writeLog= False # write log with game events (log.txt file)
## Various raw data to output (to data.txt file)
#writeData = "none"
#writeData = "popularCells" # Cells to land
#writeData = "lastTurn" # Length of the game
writeData = "losersNames" # Who lost
#writeData = "netWorth" # history of a game
#writeData = "remainingPlayers"
# run multiple game somulations
def runSimulation():
results = []
for i in range(nSimulations):
log.write("="*10+" GAME "+str(i+1)+" "+"="*10+"\n")
# remaining players - add to the results list
results.append(oneGame())
# write remaining players in a data log
if writeData=="remainingPlayers":
remPlayers = sum([1 for r in results[-1] if r>0])
log.write(str(remPlayers), data=True)
if (i+1)%progressAsterix == 0:
pass
print ("*", end = "")
print ()
return results
# Analize results
def analyzeResults(results):
remainingPlayers = [0,]*nPlayers
for result in results:
alive = 0
for score in result:
if score>=0:
alive +=1
remainingPlayers[alive-1] += 1
if showRemPlayers:
print ("Remaining:", remainingPlayers)
def analyzeData():
if writeData == "losersNames" or writeData == "experiment" or writeData=="remainingPlayers":
groups = {}
with open("data.txt", "r") as fs:
for line in fs:
item = line.strip()
if item in groups:
groups[item] += 1
else:
groups[item] = 1
experiment = 0
control = 0
for item in sorted(groups.keys()):
count = groups[item]/nSimulations
if writeData == "losersNames":
count = 1-count
if item=="exp":
experiment = count
else:
control += count
margin = 1.96 * math.sqrt(count*(1-count)/nSimulations)
print ("{}: {:.1%} +- {:.1%}".format(item, count, margin) )
if experiment!=0:
print ("Exp result: {:.1%}".format(experiment-control/(nPlayers-1)) )
if writeData == "netWorth":
print ("graph here")
npdata = np.transpose( np.loadtxt("data.txt", dtype=int, delimiter="\t") )
x = np.arange(0, max( [len(d) for d in npdata] ) )
plt.ioff()
fig, ax = plt.subplots()
for i in range(nPlayers):
ax.plot(x, npdata[i], label='1')
plt.savefig("fig"+str(time.time())+".png")
if __name__ == "__main__":
print ("="*40)
t = time.time()
log = Log()
if seed != "":
random.seed(seed)
else:
random.seed()
print ("Players:", nPlayers, " Turns:", nMoves, " Games:", nSimulations, " Seed:", seed)
results = runSimulation()
analyzeResults(results)
log.close()
analyzeData()
print ("Done in {:.2f}s".format(time.time()-t))