-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
291 lines (235 loc) · 8.26 KB
/
test.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import random
from q1b import ExpectationMaximisation as EM
from q2d import TreeNode
import sys
TEST_Q1B = True
TEST_Q2D = False
def getStandardDeviation(structure1, structure2):
if (type(structure1) == list):
total = 0
denom = 0
for i in range(len(structure1)):
(SD, num) = getStandardDeviation(structure1[i], structure2[i])
total += SD * num
denom += num
return (total/denom, denom)
else:
return (abs(structure1 - structure2), 1)
def sample(distribution):
# normalise
distSum = sum(distribution)
distribution = [i / distSum for i in distribution]
val = random.random()
for i, x in enumerate(distribution):
val -= x
if (val < 0):
return i
return 'bruh'
def HMM(initial, transitions, emissions, sequenceLength):
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-=_;:,.<>[]`¬!"£$%^&*()\\|~#'
num_states = len(initial)
state = sample(initial)
sequence = letters[sample(emissions[state])]
for i in range(sequenceLength-1):
state = sample(transitions[state])
sequence += letters[sample(emissions[state])]
return sequence
def gen_random_sequence(length, num_symbols = 4):
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-=_;:,.<>[]`¬!"£$%^&*()\\|~#'
letters = letters[:num_symbols]
output = ''
for i in range(length):
output += letters[random.randrange(num_symbols)]
return output
def generateProbabilityMatrix(height: int, width: int) -> list:
"""Generates and returns a randomised matrix where each row contains a discrete probability distribution
Parameters
- height = number of distributions
- width = number of items in each distribution
"""
import random
matrix = []
for i in range(height):
# randpd2 from https://thehousecarpenter.wordpress.com/2017/02/22/generating-random-probability-distributions/
variates = [random.random() for i in range(width)]
s = sum(variates)
matrix.append([i/s for i in variates])
return matrix
if(TEST_Q1B):
NUM_ITERATIONS = 10
if (NUM_ITERATIONS == 0):
NUM_STATES = 8
observations = 'ABCDCBASAKDHASJILDJASOPKAHSDASJKHDASJKDHASJDKHASDJKASHDIAUSDAJSDIASHNDSALIDHNASLIDUHASDUKJASGBDLYIUSAKHDBUADHB JASCHBASJKDCGAIUKJWDHIUADKHSDBUIASCHJBYSAUIDHASLBYDUUHAKLEGDWIUKDJBSAUYKHNSABUCYGISAHDBLSAUYJDHKBWALUDBAUIWDHNSAOIDUHBSAIO;DLHSAGUYCJBKAIUSDHBNASIODUHBASIUDHBSALKDUHASLKDJHAKUWKDBKUAYJHDBYUBASDILAYWGDBILUWAGYBDSAGCIASUBDUAWIHBWIDIOKASNBHCHUOALSUIDALSIEOIAJDSAKJDSADASKJDKLSAJDSIOAPUDIOWJKNADBJSIUACJSADBWIUODJASDBASUDOIJASDASFIUSAJFNSABDIASULDHSADJAKSDHAHSFSATYGUFGYDSTG'
print(EM(observations, NUM_STATES))
else:
USE_HMMS = True
if (not USE_HMMS):
for i in range(NUM_ITERATIONS):
NUM_STATES = random.randrange(1, 16)
observations = gen_random_sequence(random.randrange(1, 400), random.randrange(1, 30))
sys.stdout = sys.__stdout__
print(f'Beginning run:\nNUM_STATES: {NUM_STATES}\nobservations: {observations}\n')
f = open(f'randrun{i}.txt', 'w')
sys.stdout = f
print(EM(observations, NUM_STATES))
f.close()
else:
for i in range(NUM_ITERATIONS):
NUM_STATES = random.randrange(2, 16)
NUM_SYMBOLS = random.randrange(2, 32)
initial = generateProbabilityMatrix(1, NUM_STATES)[0]
transitions = generateProbabilityMatrix(NUM_STATES, NUM_STATES)
emissions = generateProbabilityMatrix(NUM_STATES, NUM_SYMBOLS)
sequenceLength = random.randrange(10, 400)
observations = HMM(initial, transitions, emissions, sequenceLength)
print(f'Beginning run:\nNUM_STATES: {NUM_STATES}\nobservations: {observations}\n...')
f = open(f'hmmrun{i}.txt', 'w')
sys.stdout = f
results = EM(observations, NUM_STATES)
print(results)
"""SDs = [getStandardDeviation(initial, results[0]), getStandardDeviation(transitions, results[1]), getStandardDeviation(emissions, results[2])]
print(SDs)"""
f.close()
sys.stdout = sys.__stdout__
print(f'{results}\n')
if (TEST_Q2D):
e = TreeNode([
TreeNode([
TreeNode([], 'a'),
TreeNode([], 'b')
]),
TreeNode([], 'c')
])
f = TreeNode([
TreeNode([], 'd'),
TreeNode([], 'e'),
TreeNode([], 'f'),
TreeNode([
TreeNode([], 'g'),
TreeNode([], 'h')
]),
TreeNode([
TreeNode([
TreeNode([], 'i'),
TreeNode([], 'j')
]),
TreeNode([
TreeNode([], 'k'),
TreeNode([], 'l')
])
])
])
stefan = TreeNode([
TreeNode([
TreeNode([
TreeNode([
TreeNode([], 'c'),
TreeNode([], 'h')
]),
TreeNode([], 'a')
]),
TreeNode([
TreeNode([
TreeNode([], 'j'),
TreeNode([], 'n')
]),
TreeNode([], 'l')
]),
TreeNode([
TreeNode([], 'e'),
TreeNode([], 'f')
])
]),
TreeNode([
TreeNode([
TreeNode([], 'd'),
TreeNode([], 'i')
]),
TreeNode([
TreeNode([], 'g'),
TreeNode([], 'b')
])
]),
TreeNode([
TreeNode([], 'k'),
TreeNode([], 'm')
])
])
chungus = TreeNode([
TreeNode([
TreeNode([
TreeNode([], 'a'),
TreeNode([], 'b'),
TreeNode([], 'c'),
TreeNode([], 'd')
]),
TreeNode([], 'e'),
TreeNode([], 'f'),
TreeNode([], 'g'),
TreeNode([
TreeNode([], 'h'),
TreeNode([], 'i')
])
]),
TreeNode([
TreeNode([
TreeNode([
TreeNode([], 'j'),
TreeNode([
TreeNode([], 'k'),
TreeNode([], 'm')
])
]),
TreeNode([
TreeNode([
TreeNode([], 'n'),
TreeNode([], 'o')
]),
TreeNode([
TreeNode([], 'p'),
TreeNode([], 'q')
])
]),
TreeNode([
TreeNode([
TreeNode([], 'r'),
TreeNode([], 's')
]),
TreeNode([
TreeNode([], 't'),
TreeNode([], 'u')
])
]),
TreeNode([], 'v')
]),
TreeNode([
TreeNode([
TreeNode([], 'w'),
TreeNode([], 'x')
]),
TreeNode([], 'y')
])
]),
TreeNode([], 'z')
])
saving = TreeNode([
TreeNode([
TreeNode([], 'a'),
TreeNode([], 'b')
]),
TreeNode([
TreeNode([], 'i'),
TreeNode([], 'j')
]),
TreeNode([], 's'),
TreeNode([], 't')
])
print([i.text for i in e.getConstraints()])
print([i.text for i in f.getConstraints()])
CHAOTIC_STEFAN = stefan.getConstraints()
print([i.text for i in CHAOTIC_STEFAN], len(CHAOTIC_STEFAN))
BIG_CHUNGUS = chungus.getConstraints()
print([i.text for i in BIG_CHUNGUS], len(BIG_CHUNGUS))
LE_SAVING = saving.traverse()
print([i.text for i in LE_SAVING[0]], len(LE_SAVING))
print([i for i in LE_SAVING], len(LE_SAVING))