forked from amanirmk/AMISTAD-intention-exp3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
274 lines (254 loc) · 9.77 KB
/
algorithms.py
File metadata and controls
274 lines (254 loc) · 9.77 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from typeRotation import *
from typeDirection import *
from typeAngle import *
from typeCell import *
from typeThick import *
import classCell as c
import numpy as np
import copy
import csv
import math as m
def findDir(rotationType, angleType):
"""returns the direction of a trap piece from our bottom-up perspective (primarily for the arrow)"""
if(rotationType == RotationType.left):
if(angleType == AngleType.lacute):
return DirectionType.lowerright
elif(angleType == AngleType.racute):
return DirectionType.upperright
elif(angleType == AngleType.lright):
return DirectionType.down
elif(angleType == AngleType.rright):
return DirectionType.up
elif(angleType == AngleType.lobtuse):
return DirectionType.lowerleft
elif(angleType == AngleType.robtuse):
return DirectionType.upperleft
elif(angleType == AngleType.straight):
return DirectionType.left
else:
raise Exception("Error: angleType doesn't exist")
elif(rotationType == RotationType.right):
if(angleType == AngleType.lacute):
return DirectionType.upperleft
elif(angleType == AngleType.racute):
return DirectionType.lowerleft
elif(angleType == AngleType.lright):
return DirectionType.up
elif(angleType == AngleType.rright):
return DirectionType.down
elif(angleType == AngleType.lobtuse):
return DirectionType.upperright
elif(angleType == AngleType.robtuse):
return DirectionType.lowerright
elif(angleType == AngleType.straight):
return DirectionType.right
else:
raise Exception("Error: angleType doesn't exist")
elif(rotationType == RotationType.up):
if(angleType == AngleType.lacute):
return DirectionType.lowerleft
elif(angleType == AngleType.racute):
return DirectionType.lowerright
elif(angleType == AngleType.lright):
return DirectionType.left
elif(angleType == AngleType.rright):
return DirectionType.right
elif(angleType == AngleType.lobtuse):
return DirectionType.upperleft
elif(angleType == AngleType.robtuse):
return DirectionType.upperright
elif(angleType == AngleType.straight):
return DirectionType.up
else:
raise Exception("Error: angleType doesn't exist")
elif(rotationType == RotationType.down):
if(angleType == AngleType.lacute):
return DirectionType.upperright
elif(angleType == AngleType.racute):
return DirectionType.upperleft
elif(angleType == AngleType.lright):
return DirectionType.right
elif(angleType == AngleType.rright):
return DirectionType.left
elif(angleType == AngleType.lobtuse):
return DirectionType.lowerright
elif(angleType == AngleType.robtuse):
return DirectionType.lowerleft
elif(angleType == AngleType.straight):
return DirectionType.down
else:
raise Exception("Error: angleType doesn't exist")
def formatMatrix(matrix):
"""returns a string representation of the matrix that can then be printed"""
string = ""
colLength = len(matrix)
rowLength = len(matrix[0])
for y in range(colLength):
for x in range(rowLength):
string += str(matrix[y][x]) + "\t"
string += "\n\n\n"
return string
def flatten(l):
"""turns a matrix (list of lists) into a single list"""
return [item for sublist in l for item in sublist]
#Defining pieces necessary for fsc
TOTAL = 427929800129788411
r = TOTAL * (1 + m.log(TOTAL))
p = 1 / TOTAL
#These are the frequencies of varying levels of coherence
f_g = {
(0.0, 1.0) : 427929800129788411 / TOTAL,
(1.0, 9.0) : 354394707075243198 / TOTAL,
(1.0, 8.0) : 123453353582343198 / TOTAL,
(1.0, 7.0) : 102193295525793198 / TOTAL,
(1.0, 6.0) : 101346901331553198 / TOTAL,
(1.0, 5.0) : 101327843325852198 / TOTAL,
(2.0, 9.0) : 101327577622082748 / TOTAL,
(1.0, 4.0) : 18317428758242748 / TOTAL,
(2.0, 7.0) : 12289201862932377 / TOTAL,
(1.0, 3.0) : 12103878714006177 / TOTAL,
(3.0, 8.0) : 1272268411781292 / TOTAL,
(2.0, 5.0) : 689654429107497 / TOTAL,
(3.0, 7.0) : 689623309037907 / TOTAL,
(4.0, 9.0) : 677046035997297 / TOTAL,
(1.0, 2.0) : 41696845623225 / TOTAL,
(5.0, 9.0) : 18559182512862 / TOTAL,
(4.0, 7.0) : 919349539299 / TOTAL,
(3.0, 5.0) : 616034679885 / TOTAL,
(5.0, 8.0) : 615255422625 / TOTAL,
(2.0, 3.0) : 239164711182 / TOTAL,
(5.0, 7.0) : 6417454230 / TOTAL,
(3.0, 4.0) : 3925431153 / TOTAL,
(7.0, 9.0) : 1459677645 / TOTAL,
(4.0, 5.0) : 26456355 / TOTAL,
(5.0, 6.0) : 22595625 / TOTAL,
(6.0, 7.0) : 17067672 / TOTAL,
(7.0, 8.0) : 10561401 / TOTAL,
(8.0, 9.0) : 4297158 / TOTAL,
(1.0, 1.0) : 26730 / TOTAL,
(10.0, 9.0) : 3 / TOTAL,
}
def functional_specified_complexity(connectionTuple):
"""returns the fsc (surprisal) of a given traps' connection tuple
connection tuple: (numerator, denominator) of the simplified fraction for valid connections / wire and arrow pieces"""
global r
global p
v = 1 / f_g[connectionTuple]
k = r * p / v
fsc = -m.log(k, 2)
return fsc
def isTrap(trap, sigVal=13.29):
"""given a trap and a significant value, determines whether the trap is coherent enough to be considered designed"""
connectionTuple = connectionsPerPiece(trap)
if functional_specified_complexity(connectionTuple) >= sigVal:
return True
else:
return False
#the historical frequencies of intention gophers entering traps
#to be used for the cautious gopher
intentionEnter = {
"0.0" : 0.0,
"0.05" : 0.04988481269964185,
"0.1" : 0.09948793864971121,
"0.15" : 0.14881005760747487,
"0.2" : 0.199672975964652,
"0.25" : 0.24902111248844028,
"0.3" : 0.298916566348736,
"0.35" : 0.3495346742719904,
"0.4" : 0.39898116983534976,
"0.45" : 0.44854377391645306,
"0.5" : 0.5017578233012316,
"0.55" : 0.5510910365512773,
"0.6" : 0.6005349888710337,
"0.65" : 0.6493674432638634,
"0.7" : 0.7013113161728994,
"0.75" : 0.7513506373423156,
"0.8" : 0.799739176543086,
"0.85" : 0.850076699120308,
"0.9" : 0.8984355307600531,
"0.95" : 0.9488828397610392,
"1.0" : 1.0
}
def cautious(trap, probReal):
"""randomly determines whether the trap is real given a probability
for the cautious gopher's entering algorithm"""
realTrap = np.random.binomial(n=1, p=intentionEnter[str(probReal)])
return realTrap
def connectionsPerPiece(trap):
"""given a trap, returns its connection tuple (the simplified fraction for valid connections / wire and arrow pieces)"""
connections = totalConnections(trap)
if connections == 0:
return (0, 1.0)
numPieces = 0
for cell in flatten(trap.board):
if cell.cellType == CellType.wire or cell.cellType == CellType.arrow:
numPieces += 1
return simplifyRatioTuple(connections, numPieces)
def simplifyRatioTuple(num, denom):
"""Simplifies the tuple, which represents the numerator and denominator of a fraction.
Inputs:
num: the numerator
denom: the denominator"""
if num == 0:# group anything with num 0 into the (0, 1) category
return (0.0, 1.0)
elif num != 0 and denom == 0:
raise Exception("ERROR: All cells are floor cells, but connections were found.")
gcd = int(np.gcd(num, denom))
num = num/gcd
denom = denom/gcd
return (num, denom)
usedCells = [] #initializing usedCells as a global variable for use in several following methods
def totalConnections(trap):
"""
returns how many connections a trap has
"""
global usedCells
usedCells = []
endpoints = 0
allCells = flatten(trap.board)
for cell in allCells:
if cell.cellType == CellType.arrow or cell.cellType == CellType.wire or cell.cellType == CellType.door:
for endpoint in cell.endpoints:
neighbor = cell.getNeighboringCell(endpoint)
if neighbor != None:
if (neighbor.cellType == CellType.wire) or ((cell.cellType == CellType.wire or cell.cellType == CellType.door) and neighbor.cellType == CellType.arrow):
if checkConnection(cell, endpoint):
endpoints += 1
usedCells.append(cell)
return endpoints
def checkConnection(cell, endpoint):
"""
Helper func:
returns true if that cell is connected and thicktypes match
"""
global usedCells
cellAtEndpoint = cell.getNeighboringCell(endpoint)
matchingEndpoint = c.getOppositeEndpoint(endpoint)
if cell.cellType == CellType.door:
if (cellAtEndpoint not in usedCells) and (matchingEndpoint in cellAtEndpoint.endpoints):
return True
## arrow or wire cell
elif (cellAtEndpoint not in usedCells) and (matchingEndpoint in cellAtEndpoint.endpoints):
if (cell.thickType == cellAtEndpoint.thickType):
return True
return False
def gopherEatTimer(probEnter):
"""
decides how long the gopher should eat based on the gopher's detection of threat.
"""
idealTimer = probEnter * 5
initialProbs = [0.05, 0.05, 0.05, 0.05, 0.05]
for i in range(5):
if idealTimer <= i + 1:
initialProbs[i] = 0.6
if i == 0:
initialProbs[1] = 0.2
initialProbs[2] = 0.1
elif i == 4:
initialProbs[3] = 0.2
initialProbs[2] = 0.1
else:
initialProbs[i+1] = 0.15
initialProbs[i-1] = 0.15
break
return np.random.choice([1,2,3,4,5], p=initialProbs, size=1)[0]