-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlimited_uniform_latex_blue.py
215 lines (171 loc) · 8.59 KB
/
unlimited_uniform_latex_blue.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 27 13:02:18 2020
@author: Johan
"""
import numpy as np
from scipy.stats import hypergeom
from itertools import chain
def beta(u_i,i,n):
#beta(i,u_i)=P(U_i=u_i)
concat_range=chain(range(int(n/2)),range(int(n/2)+1,n+1))
prob=0
for j in concat_range:
prob+=hypergeom.pmf(u_i,n,j,i)
return prob/n
def rho(i,u_i,n):
#=number of boxes
#Rho(i,x) = P(u_12 >= 7 | U_i=u_i) = P(red majority given U_i=u_i)
#numerator=P(u_12 >= 7 and u_i=x) = P(majority red and u_i=x)
nume=0
for j in range(int((n/2))+1,n+1):
#P(u_i=x|u_n=j)=hypergeom.pmf(x,n,j,i)
nume+=hypergeom.pmf(u_i,n,j,i)
nume=nume/n
#denumerator=P(U_i=u_i)
denume=beta(u_i,i,n)
return nume/denume
#making functions to find loss2:
def gamma(k,u_i,i,n):
#gamma(k,u_i,n)=P(X_i+1 | U_i=u_i)
#correction: gamma(k,u_i,i,n)=P(X_{i+1}=1|U_n=k,U_i=u_i)
if u_i>k or k==int(n/2) or (k-u_i)>(n-i):
return 0
else :
return (k-u_i)/(n-i)
def epsilon(u_i,i,n):
#epsilon(u_i,i,n)=P(X_i+1 = 1 | U_i=u_i)
prob=0
b = beta(u_i,i,n)
concat_range=chain(range(int(n/2)),range(int(n/2)+1,n+1))
for k in concat_range:
prob+=gamma(k,u_i,i,n)*hypergeom.pmf(u_i,n,k,i)
prob=prob/(n*b)
return prob
def find_prob_loss_0_1(n,alpha):
if n%2 != 0 or n==0:
raise ValueError('Number of boxes must be an even number above zero.')
m = np.zeros((n,n),dtype=dict)
for i in range(0,n):
for u_i in range(0,i+1):
r=rho(i,u_i,n)
L0=r #Expected loss when choosing blue
L1=1-r #Expected loss when chosing red
if i==(n-1):
#making the last "Loss2"=alpha
m[i,u_i]={"prob":r,"Loss0":L0,"Loss1":L1,"Loss2":alpha}
else:
#don't know the rest of the "loss2" yet, just putting it tp a high value.
m[i,u_i]={"prob":r,"Loss0":L0,"Loss1":L1,"Loss2":1000}
return m
def find_loss2(matrix,n,alpha):
for i in range(n-2,-1,-1):
#looping from the second to last row of the matrix to the first one
for u_i in range(0,i+1):
#expected loss if the next one is blue:
EL_0 = min(matrix[i+1,u_i]["Loss0"],matrix[i+1,u_i]["Loss1"],matrix[i+1,u_i]["Loss2"])
#expected loss if the next one is red.
EL_1 = min(matrix[i+1,u_i+1]["Loss0"],matrix[i+1,u_i+1]["Loss1"],matrix[i+1,u_i+1]["Loss2"])
eps=epsilon(u_i,i,n)
matrix[i,u_i]["Loss2"]=alpha + (1-eps)*EL_0 + eps*EL_1
return matrix
def make_matrix(n,alpha):
matrix=find_prob_loss_0_1(n,alpha)
matrix=find_loss2(matrix,n,alpha)
return matrix
#mat_losses=make_matrix(12,0.02)
#print(mat_losses)
#Now the matrix is done, the next step is then to find the optimal solution:
def make_node_matrix(matrix):
node_mat=np.zeros_like(matrix)
n = len(matrix)
for i in range(n):
for j in range(i+1):
l0 = matrix[i][j]["Loss0"]
l1 = matrix[i][j]["Loss1"]
l2 = matrix[i][j]["Loss2"]
e0 = round(l1+l2,14) #=sum of losses - l0
e1 = round(l0+l2,14)
e2 = round(l0+l1,14)
col1 = "green!70!black"
col2 = "green!70!black"
if l0<l2 and round(l0,3)<round(l1,3): #if blue has the smallest loss
col1 = "blue"
col2 = "blue"
elif l1<l2 and round(l1,3)<round(l0,3): #red has the smallest loss
col1 = "red"
col2 = "red"
elif l0<l2 and round(l0,3) == round(l1,3): #red and blue has the smallest loss, but they are equal
col1 = "blue"
col2 = "red"
name = "N" + str(i) + "-" + str(j)
node_mat[i][j] = {"name":name, "col1":col1, "col2":col2, "e0":e0, "e1":e1, "e2":e2}
return node_mat
#nodes=make_node_matrix(mat_losses)
#print(nodes)
def visualize_optimal(mat,filename, radius):
file_location_and_name=r"C:\\Users\\Johan\\OneDrive\\Documents\\NTNU-Host-2020\\Prosjektoppgave\\Prosjektoppgave-python\\Tikz-trees2\\" + filename
file = open(file_location_and_name,"a")
start_of_doc=r"""
\begin{tikzpicture}[
treenodeT/.style={
circle, align=center},
node distance=1cm,
]
"""
file.write(start_of_doc)
#the first node:
string = "\DoNode{N0-0}{" + str(mat[0][0]["e0"]) + "}{" + str(mat[0][0]["e1"]) + "}{1}{" + str(mat[0][0]["col1"]) + "}{" + str(mat[0][0]["col2"]) + "}{" + str(radius) + "};\n "
file.write(string)
n = len(mat)
for i in range(1,n):
#new part:::
#to check if we have to break the loop (we have reached a decision in all of the nodes above)
g=0
if i>1:
for j in range(i):
if str(mat[i-1][j]["col1"]) == "green!70!black": #checing if any of the nodes in the row above are green
g=1
if g == 0: #if none of the nodes on the row above are green, break out of the for loop
#break out of the for loop
print("breaking loop at row", i)
break
#the new part stops here
for j in range(i+1):
if j==0: #we are at the left side of the tree. the only possible parent i at (i-1,j)
if mat[i-1][j]["col1"] == "green!70!black": #if we continue to open boxes in the last node
string = "\DoNode[below of=" + mat[i-1][j]["name"] + ", left of= " + mat[i-1][j]["name"] + "]{"+ mat[i][j]["name"] +"}{" + str(mat[i][j]["e0"]) + "}{" + str(mat[i][j]["e1"]) + "}{1}{" + str(mat[i][j]["col1"]) + "}{" + str(mat[i][j]["col2"]) + "}{" + str(radius) + "};\n "
file.write(string)
string2 = "\draw[->] (" + str(mat[i-1][j]["name"]) + ") -- (" + mat[i][j]["name"] + ");\n "
file.write(string2)
elif j==i: #we are at the right side of the tree. the only possible parent is at (i-1,j-1)
if mat[i-1][j-1]["col1"] == "green!70!black": #if we continue to open boxes in the last node.
string = "\DoNode[below of=" + mat[i-1][j-1]["name"] + ", right of= " + mat[i-1][j-1]["name"] + "]{"+ mat[i][j]["name"] +"}{" + str(mat[i][j]["e0"]) + "}{" + str(mat[i][j]["e1"]) + "}{1}{" + str(mat[i][j]["col1"]) + "}{" + str(mat[i][j]["col2"]) + "}{" + str(radius) + "};\n "
file.write(string)
string2 = "\draw[->] (" + str(mat[i-1][j-1]["name"]) + ") -- (" + mat[i][j]["name"] + ");\n "
file.write(string2)
else: #we are not on either side of the tree
if mat[i-1][j-1]["col1"]=="green!70!black": #if the left top node is a parent
string = "\DoNode[below of=" + mat[i-1][j-1]["name"] + ", right of= " + mat[i-1][j-1]["name"] + "]{"+ mat[i][j]["name"] +"}{" + str(mat[i][j]["e0"]) + "}{" + str(mat[i][j]["e1"]) + "}{1}{" + str(mat[i][j]["col1"]) + "}{" + str(mat[i][j]["col2"]) + "}{" + str(radius) + "};\n "
file.write(string)
string2 = "\draw[->] (" + str(mat[i-1][j-1]["name"]) + ") -- (" + mat[i][j]["name"] + ");\n "
file.write(string2)
if mat[i-1][j]["col1"] == "green!70!black": #if the top right node also is a parent
string3 = "\draw[->] (" + str(mat[i-1][j]["name"]) + ") -- (" + mat[i][j]["name"] + ");\n "
file.write(string3)
elif mat[i-1][j-1]["col1"] != "green!70!black" and mat[i-1][j]["col1"]=="green!70!black": #left is not a parent, but the right is
string = "\DoNode[below of=" + mat[i-1][j]["name"] + ", left of= " + mat[i-1][j]["name"] + "]{"+ mat[i][j]["name"] +"}{" + str(mat[i][j]["e0"]) + "}{" + str(mat[i][j]["e1"]) + "}{1}{" + str(mat[i][j]["col1"]) + "}{" + str(mat[i][j]["col2"]) + "}{" + str(radius) + "};\n "
file.write(string)
string2 = "\draw[->] (" + str(mat[i-1][j]["name"]) + ") -- (" + mat[i][j]["name"] + ");\n "
file.write(string2)
end_of_file= r"""
\end{tikzpicture}
"""
file.write(end_of_file)
file.close()
#visualize_optimal(nodes,"latex_test8.tex",0.6)
def main(n, alpha, filename, node_radius):
mat_losses=make_matrix(n,alpha)
nodes=make_node_matrix(mat_losses)
visualize_optimal(nodes,filename,node_radius)
main(12,0.05,"unlimited_uniform_a0.05.tex",0.4)