-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainDice.py
165 lines (110 loc) · 2.4 KB
/
mainDice.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#mainDice.py
import random as rd
import matplotlib as mtp
import pickle as pl
import numpy as np
from PIL import Image
import math
from diceFuncs import *
def initgen():
n = 300
maindie = generate(1000, n)
of = open("example.p", "wb")
pl.dump(maindie, of)
of.close()
return 0
def testgen():
infile = open("s300x1000.p", "rb")
maindie = pl.load(infile)
infile.close()
print maindie
def calc_result():
n = 300
x = 1000
infile = open("s300x1000.p", "rb")
dice = pl.load(infile)
infile.close()
results = [[0 for i in range(x)] for j in range(x)]
for i in range(x):
for j in range(x):
results[i][j] = dom(dice[i], dice[j], n)
ofile = open("results_s300x1s000_1.p", "wb")
pl.dump(results, ofile)
ofile.close()
return 0
def test_res():
n = 100
x = 1000
infile = open("s100x1000.p", "rb")
result = pl.load(infile)
infile.close()
#for i in range(10):
# print(result[i])
#return
results = []
#Create the histogram for the standard die
stan = {}
for i in range(n):
stan[str(i + 1)] = 1
for i in range(x):
g = dict()
totvar = 0
for j in range(n):
item = str(result[i][j])
if item in g:
g[item] += 1
else:
g[item] = 1
for key in stan:
#print(g[key])
#print(stan[key])
#print()
#time.sleep(1)
if key not in g:
ref = 0
else:
ref = g[key]
#Total Variation Distance
totvar += (.5 * abs(ref - stan[key]))
#print(totvar)
results.append(totvar)
show = np.array(list(results))
print("Mean: " + str(show.mean()))
print("Var: " + str(show.var()))
#print(result)
return 0
def plot_result():
n = 300
x = 1000
infile = open("results_s300x1000_1.p", "rb")
result = pl.load(infile)
infile.close()
plt = np.zeros((x, x, 3))
red = np.array([255, 0, 0])
green = np.array([0, 0, 255])
win_per = []
for i in range(x):
for j in range(x):
if result[i][j] == "Loses":
plt[i][j] = red
elif result[i][j] == "Beats":
plt[i][j] = green
plt_im = Image.fromarray(np.uint8(plt), "RGB")
#plt_im.show()
plt_im.save("results_image_s300x1000_1.png", "PNG")
for i in range(x):
runwin = 0
for j in range(x):
if result[i][j] == "Beats":
runwin += 1
win_per.append(float(runwin) / x)
print win_per
win_per = np.array(win_per)
print "Mean: " + str(win_per.mean())
print "Var: " + str(win_per.var())
return 0
#testgen()
#initgen()
#main()
test_res()
#plot_result()