-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayesHMMTestClasses.py
1134 lines (963 loc) · 51.3 KB
/
bayesHMMTestClasses.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# bayesHMMTestClasses.py
# ------------------------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).
import testClasses
import bayesNet
import random
import layout
import hunters
from copy import deepcopy
from tempfile import mkstemp
import time
from shutil import move
from os import remove, close
import util
from util import manhattanDistance
import busters
import bustersAgents
from game import Agent
from game import Actions
from game import Directions
import re
class GraphEqualityTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(GraphEqualityTest, self).__init__(question, testDict)
layoutText = testDict['layout']
self.layoutName = testDict['layoutName']
lay = layout.Layout([row.strip() for row in layoutText.split('\n')])
self.startState = hunters.GameState()
self.startState.initialize(lay, 0)
def getEmptyStudentBayesNet(self, moduleDict):
inferenceModule = moduleDict['inference']
studentComputation = inferenceModule.constructBayesNet
net = studentComputation(self.startState)
return net
def execute(self, grades, moduleDict, solutionDict):
# load student code and staff code solutions
studentNet = self.getEmptyStudentBayesNet(moduleDict)
goldNet = bayesNet.constructEmptyBayesNetFromString(solutionDict['solutionString'])
correct = studentNet.sameGraph(goldNet)
sameValues = studentNet.sameValuesDict(goldNet)
if correct and sameValues:
return self.testPass(grades)
self.addMessage('Bayes net graphs are not equal.')
missingVars = goldNet.variablesSet() - studentNet.variablesSet()
extraVars = studentNet.variablesSet() - goldNet.variablesSet()
if missingVars:
self.addMessage('Student solution is missing variables: ' + str(missingVars) + '\n')
if extraVars:
self.addMessage('Student solution has extra variables: ' + str(extraVars) + '\n')
studentEdges = set([str(fromVar) + " -> " + str(toVar) for toVar in studentNet.variablesSet() for fromVar in studentNet.inEdges()[toVar]])
goldEdges = set([str(fromVar) + " -> " + str(toVar) for toVar in goldNet.variablesSet() for fromVar in goldNet.inEdges()[toVar]])
missingEdges = goldEdges - studentEdges
extraEdges = studentEdges - goldEdges
if missingEdges:
self.addMessage('Student solution is missing edges:')
for edge in sorted(missingEdges):
self.addMessage(' ' + str(edge))
self.addMessage('\n')
if extraEdges:
self.addMessage('Student solution has extra edges:')
for edge in sorted(extraEdges):
self.addMessage(' ' + str(edge))
self.addMessage('\n')
if not sameValues:
self.addMessage('Student solution has incorrect values dictionary.')
studentDict = studentNet.variableDomainsDict()
goldDict = goldNet.variableDomainsDict()
missingDictVars = set(goldDict) - set(studentDict)
extraDictVars = set(studentDict) - set(goldDict)
if missingDictVars:
self.addMessage('Student dictionary is missing variables: ' + str(missingDictVars))
if extraDictVars:
self.addMessage('Student dictionary has extra variables: ' + str(extraDictVars))
for variable, assignments in goldDict.items():
if variable not in studentDict:
continue
studentAssignments = studentDict[variable]
missing = set(assignments) - set(studentAssignments)
extra = set(studentAssignments) - set(assignments)
if missing:
self.addMessage('Student dictionary for ' + variable + ' is missing assignments: ' + str(missing))
if extra:
self.addMessage('Student dictionary for ' + variable + ' has extra assignments: ' + str(extra))
return self.testFail(grades)
def writeSolution(self, moduleDict, filePath):
inferenceModule = moduleDict['inference']
with open(filePath, 'w') as handle:
handle.write('# This is the solution file for %s.\n\nsolutionString: """\n' % self.path)
net = inferenceModule.constructBayesNet(self.startState)
handle.write(str(net))
handle.write('\n"""\n')
return True
def createPublicVersion(self):
pass
class BayesNetEqualityTest(GraphEqualityTest):
def execute(self, grades, moduleDict, solutionDict):
# load student code and staff code solutions
studentNet = self.getEmptyStudentBayesNet(moduleDict)
goldNet = parseSolutionBayesNet(solutionDict)
if not studentNet.sameGraph(goldNet):
self.addMessage('Bayes net graphs are not equivalent. Please check that your Q1 implementation is correct.')
return self.testFail(grades)
moduleDict['bayesAgents'].fillCPTs(studentNet, self.startState)
for variable in goldNet.variablesSet():
try:
studentFactor = studentNet.getCPT(variable)
except KeyError:
self.addMessage('Student Bayes net missing CPT for variable ' + str(variable))
return self.testFail(grades)
goldFactor = goldNet.getCPT(variable)
if not studentFactor == goldFactor:
self.addMessage('First factor in which student answer differs from solution: P({} | {})'.format(studentFactor.unconditionedVariables(), studentFactor.conditionedVariables()))
self.addMessage('Student Factor:\n' + str(studentFactor))
self.addMessage('Correct Factor:\n' + str(goldFactor))
return self.testFail(grades)
return self.testPass(grades)
def writeSolution(self, moduleDict, filePath):
bayesAgentsModule = moduleDict['bayesAgents']
with open(filePath, 'w') as handle:
handle.write('# This is the solution file for %s.\n\n' % self.path)
net, _ = bayesAgentsModule.constructBayesNet(self.startState)
bayesAgentsModule.fillCPTs(net, self.startState)
handle.write(net.easierToParseString(printVariableDomainsDict=True))
return True
class FactorEqualityTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(FactorEqualityTest, self).__init__(question, testDict)
self.seed = self.testDict['seed']
random.seed(self.seed)
self.alg = self.testDict['alg']
self.max_points = int(self.testDict['max_points'])
self.testPath = testDict['path']
self.constructRandomly = testDict['constructRandomly']
def execute(self, grades, moduleDict, solutionDict):
# load student code and staff code solutions
studentFactor = self.solveProblem(moduleDict)
goldenFactor = parseFactorFromFileDict(solutionDict)
# compare computed factor to stored factor
self.addMessage('Executed FactorEqualityTest')
if studentFactor == goldenFactor:
# extra condition for test passing for this test type:
if self.alg == 'inferenceByVariableElimination':
goldenCallTrackingList = eval(solutionDict['callTrackingList'])
if self.callTrackingList != goldenCallTrackingList:
self.addMessage('Order of joining by variables and elimination by variables is incorrect for variable elimination')
self.addMessage('Student performed the following operations in order: ' + str(self.callTrackingList) + '\n')
self.addMessage('Correct order of operations: ' + str(goldenCallTrackingList) + '\n')
return self.testFail(grades)
return self.testPass(grades)
else:
self.addMessage('Factors are not equal.\n')
self.addMessage('Student generated factor:\n\n' + str(studentFactor) + '\n\n')
self.addMessage('Correct factor:\n\n' + str(goldenFactor) + '\n')
studentProbabilityTotal = sum([studentFactor.getProbability(assignmentDict) for assignmentDict in studentFactor.getAllPossibleAssignmentDicts()])
correctProbabilityTotal = sum([goldenFactor.getProbability(assignmentDict) for assignmentDict in goldenFactor.getAllPossibleAssignmentDicts()])
if abs(studentProbabilityTotal - correctProbabilityTotal) > 10e-12:
self.addMessage('Sum of probability in student generated factor is not the same as in correct factor')
self.addMessage('Student sum of probability: ' + str(studentProbabilityTotal))
self.addMessage('Correct sum of probability: ' + str(correctProbabilityTotal))
return self.testFail(grades)
def writeSolution(self, moduleDict, filePath):
if self.constructRandomly:
if self.alg == 'joinFactors' or self.alg == 'eliminate' or \
self.alg == 'normalize':
replaceTestFile(self.testPath, "Factors", self.factorsDict)
elif self.alg == 'inferenceByVariableElimination' or \
self.alg == 'inferenceByLikelihoodWeightingSampling':
replaceTestFile(self.testPath, "BayesNet", self.problemBayesNet)
factor = self.solveProblem(moduleDict)
with open(filePath, 'w') as handle:
handle.write('# This is the solution file for %s.\n' % self.path)
printString = factor.easierToParseString()
handle.write('%s\n' % (printString))
if self.alg == 'inferenceByVariableElimination':
handle.write('callTrackingList: "' + repr(self.callTrackingList) + '"\n')
return True
class FactorInputFactorEqualityTest(FactorEqualityTest):
def __init__(self, question, testDict):
super(FactorInputFactorEqualityTest, self).__init__(question, testDict)
self.factorArgs = self.testDict['factorArgs']
eliminateToPerform = (self.alg == 'eliminate')
evidenceAssignmentToPerform = (self.alg == 'normalize')
parseDict = parseFactorInputProblem(testDict, goingToEliminate=eliminateToPerform,
goingToEvidenceAssign=evidenceAssignmentToPerform)
self.variableDomainsDict = parseDict['variableDomainsDict']
self.factorsDict = parseDict['factorsDict']
if eliminateToPerform:
self.eliminateVariable = parseDict['eliminateVariable']
if evidenceAssignmentToPerform:
self.evidenceDict = parseDict['evidenceDict']
self.max_points = int(self.testDict['max_points'])
def solveProblem(self, moduleDict):
factorOperationsModule = moduleDict['factorOperations']
studentComputation = getattr(factorOperationsModule, self.alg)
if self.alg == 'joinFactors':
solvedFactor = studentComputation(self.factorsDict.values())
elif self.alg == 'eliminate':
solvedFactor = studentComputation(list(self.factorsDict.values())[0],
self.eliminateVariable)
elif self.alg == 'normalize':
newVariableDomainsDict = deepcopy(self.variableDomainsDict)
for variable, value in self.evidenceDict.items():
newVariableDomainsDict[variable] = [value]
origFactor = list(self.factorsDict.values())[0]
specializedFactor = origFactor.specializeVariableDomains(newVariableDomainsDict)
solvedFactor = studentComputation(specializedFactor)
return solvedFactor
class BayesNetInputFactorEqualityTest(FactorEqualityTest):
def __init__(self, question, testDict):
super(BayesNetInputFactorEqualityTest, self).__init__(question, testDict)
parseDict = parseBayesNetProblem(testDict)
self.queryVariables = parseDict['queryVariables']
self.evidenceDict = parseDict['evidenceDict']
if self.alg == 'inferenceByVariableElimination':
self.callTrackingList = []
self.variableEliminationOrder = parseDict['variableEliminationOrder']
elif self.alg == 'inferenceByLikelihoodWeightingSampling':
self.numSamples = parseDict['numSamples']
self.problemBayesNet = parseDict['problemBayesNet']
self.max_points = int(self.testDict['max_points'])
def solveProblem(self, moduleDict):
inferenceModule = moduleDict['inference']
if self.alg == 'inferenceByVariableElimination':
studentComputationWithCallTracking = getattr(inferenceModule, self.alg + 'WithCallTracking')
studentComputation = studentComputationWithCallTracking(self.callTrackingList)
solvedFactor = studentComputation(self.problemBayesNet, self.queryVariables, self.evidenceDict, self.variableEliminationOrder)
elif self.alg == 'inferenceByLikelihoodWeightingSampling':
randomSource = util.FixedRandom().random
studentComputationRandomSource = getattr(inferenceModule, self.alg + 'RandomSource')
studentComputation = studentComputationRandomSource(randomSource)
#random.seed(self.seed) # reset seed so that if we had to compute the bayes net we still have the initial seed
solvedFactor = studentComputation(self.problemBayesNet, self.queryVariables, self.evidenceDict, self.numSamples)
return solvedFactor
class MostLikelyFoodHousePositionTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(MostLikelyFoodHousePositionTest, self).__init__(question, testDict)
layoutText = testDict['layout']
self.layoutName = testDict['layoutName']
lay = layout.Layout([row.strip() for row in layoutText.split('\n')])
self.startState = hunters.GameState()
self.startState.initialize(lay, 0)
self.evidence = eval(testDict['evidence'])
self.eliminationOrder = eval(testDict['eliminationOrder'])
def execute(self, grades, moduleDict, solutionDict):
# load student code and staff code solutions
bayesAgentsModule = moduleDict['bayesAgents']
FOOD_HOUSE_VAR = bayesAgentsModule.FOOD_HOUSE_VAR
studentBayesNet, _ = bayesAgentsModule.constructBayesNet(self.startState)
bayesAgentsModule.fillCPTs(studentBayesNet, self.startState)
studentFunction = bayesAgentsModule.getMostLikelyFoodHousePosition
studentPosition = studentFunction(self.evidence, studentBayesNet, self.eliminationOrder)[FOOD_HOUSE_VAR]
goldPosition = solutionDict['answer']
correct = studentPosition == goldPosition
if not correct:
self.addMessage('Student answer: ' + str(studentPosition))
self.addMessage('Correct answer: ' + str(goldPosition))
return self.testPass(grades) if correct else self.testFail(grades)
def writeSolution(self, moduleDict, filePath):
bayesAgentsModule = moduleDict['bayesAgents']
staffBayesNet, _ = bayesAgentsModule.constructBayesNet(self.startState)
FOOD_HOUSE_VAR = bayesAgentsModule.FOOD_HOUSE_VAR
bayesAgentsModule.fillCPTs(staffBayesNet, self.startState)
staffFunction = bayesAgentsModule.getMostLikelyFoodHousePosition
answer = staffFunction(self.evidence, staffBayesNet, self.eliminationOrder)[FOOD_HOUSE_VAR]
with open(filePath, 'w') as handle:
handle.write('# This is the solution file for %s.\n\nanswer: """\n' % self.path)
handle.write(str(answer))
handle.write('\n"""\n')
return True
def createPublicVersion(self):
pass
class VPITest(testClasses.TestCase):
def __init__(self, question, testDict):
super(VPITest, self).__init__(question, testDict)
self.targetFunction = testDict['function']
layoutText = testDict['layout']
self.layoutName = testDict['layoutName']
lay = layout.Layout([row.strip() for row in layoutText.split('\n')])
self.startState = hunters.GameState()
self.startState.initialize(lay, 0)
self.evidence = eval(testDict['evidence'])
self.eliminationOrder = eval(testDict['eliminationOrder'])
def execute(self, grades, moduleDict, solutionDict):
# load student code and staff code solutions
bayesAgentsModule = moduleDict['bayesAgents']
studentAgent = bayesAgentsModule.VPIAgent()
studentAgent.registerInitialState(self.startState)
studentAnswer = eval('studentAgent.{}(self.evidence, self.eliminationOrder)'.format(self.targetFunction))
goldAnswer = eval(solutionDict['answer'])
if type(studentAnswer) == float:
correct = closeNums(studentAnswer, goldAnswer)
else:
correct = closeNums(studentAnswer[0], goldAnswer[0]) & closeNums(studentAnswer[1], goldAnswer[1])
if not correct:
self.addMessage('Student answer differed from solution by at least .0001')
self.addMessage('Student answer: ' + str(studentAnswer))
self.addMessage('Correct answer: ' + str(goldAnswer))
return self.testPass(grades) if correct else self.testFail(grades)
def writeSolution(self, moduleDict, filePath):
bayesAgentsModule = moduleDict['bayesAgents']
agent = bayesAgentsModule.VPIAgent()
agent.registerInitialState(self.startState)
answer = eval('agent.{}(self.evidence, self.eliminationOrder)'.format(self.targetFunction))
with open(filePath, 'w') as handle:
handle.write('# This is the solution file for %s.\n\nanswer: """\n' % self.path)
handle.write(str(answer))
handle.write('\n"""\n')
return True
def createPublicVersion(self):
pass
def closeNums(x, y):
return abs(x - y) < 1e-4
def parseFactorInputProblem(testDict, goingToEliminate=False, goingToEvidenceAssign=False):
parseDict = {}
variableDomainsDict = {}
for line in testDict['variableDomainsDict'].split('\n'):
variable, domain = line.split(' : ')
variableDomainsDict[variable] = domain.split(' ')
parseDict['variableDomainsDict'] = variableDomainsDict
factorsDict = {} # assume args is a list of factor names and maybe a variable name at the end
if goingToEliminate:
eliminateVariable = testDict["eliminateVariable"]
parseDict['eliminateVariable'] = eliminateVariable
# for normalize need evidence so that normalize is nontrivial
if goingToEvidenceAssign:
evidenceAssignmentString = testDict["evidenceDict"]
evidenceDict = {}
for line in evidenceAssignmentString.split('\n'):
if(line.count(' : ')): #so we can pass empty dicts for unnormalized variables
evidenceVariable, evidenceAssignment = line.split(' : ')
evidenceDict[evidenceVariable] = evidenceAssignment
parseDict['evidenceDict'] = evidenceDict
for factorName in testDict["factorArgs"].split(' '):
# construct a dict from names to factors and
# load a factor from the test file for each
currentFactor = parseFactorFromFileDict(testDict, variableDomainsDict=variableDomainsDict,
prefix=factorName)
factorsDict[factorName] = currentFactor
parseDict['factorsDict'] = factorsDict
return parseDict
def replaceTestFile(file_path, typeOfTest, inputToTest):
#Create temp file
fh, abs_path = mkstemp()
with open(abs_path,'w') as new_file:
with open(file_path) as old_file:
# Assumes that variableDomainsDict is the last
# entry in the test file before the factors start to
# get enumerated
for line in old_file:
new_file.write(line)
if 'endOfNonFactors' in line:
break
if typeOfTest == 'BayesNet':
new_file.write("\n" + inputToTest.easierToParseString())
elif typeOfTest == 'Factors':
new_file.write("\n" + "\n".join([factor.easierToParseString(prefix=name,
printVariableDomainsDict=False) for
name, factor in inputToTest.items()]))
close(fh)
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
def parseFactorFromFileDict(fileDict, variableDomainsDict=None, prefix=None):
if prefix is None:
prefix = ''
if variableDomainsDict is None:
variableDomainsDict = {}
for line in fileDict['variableDomainsDict'].split('\n'):
variable, domain = line.split(' : ')
variableDomainsDict[variable] = domain.split(' ')
# construct a dict from names to factors and
# load a factor from the test file for each
unconditionedVariables = []
for variable in fileDict[prefix + "unconditionedVariables"].split(' '):
unconditionedVariable = variable.strip()
unconditionedVariables.append(unconditionedVariable)
conditionedVariables = []
for variable in fileDict[prefix + "conditionedVariables"].split(' '):
conditionedVariable = variable.strip()
if variable != '':
conditionedVariables.append(conditionedVariable)
if 'constructRandomly' not in fileDict or fileDict['constructRandomly'] == 'False':
currentFactor = bayesNet.Factor(unconditionedVariables, conditionedVariables,
variableDomainsDict)
for line in fileDict[prefix + 'FactorTable'].split('\n'):
assignments, probability = line.split(" = ")
assignmentList = [assignment for assignment in assignments.split(', ')]
assignmentsDict = {}
for assignment in assignmentList:
var, value = assignment.split(' : ')
assignmentsDict[var] = value
currentFactor.setProbability(assignmentsDict, float(probability))
elif fileDict['constructRandomly'] == 'True':
currentFactor = bayesNet.constructAndFillFactorRandomly(unconditionedVariables, conditionedVariables, variableDomainsDict)
return currentFactor
def parseSolutionBayesNet(solutionDict):
# needs to be able to parse in a bayes net
variableDomainsDict = {}
for line in solutionDict['variableDomainsDict'].split('\n'):
variable, domain = line.split(' : ')
variableDomainsDict[variable] = domain.split(' ')
variables = list(variableDomainsDict.keys())
edgeList = []
for variable in variables:
parents = solutionDict[variable + 'conditionedVariables'].split(' ')
for parent in parents:
if parent != '':
edgeList.append((parent, variable))
net = bayesNet.constructEmptyBayesNet(variables, edgeList, variableDomainsDict)
factors = {}
for variable in variables:
net.setCPT(variable, parseFactorFromFileDict(solutionDict, variableDomainsDict, variable))
return net
def parseBayesNetProblem(testDict):
# needs to be able to parse in a bayes net,
# and figure out what type of operation to perform and on what
parseDict = {}
variableDomainsDict = {}
for line in testDict['variableDomainsDict'].split('\n'):
variable, domain = line.split(' : ')
variableDomainsDict[variable] = domain.split(' ')
parseDict['variableDomainsDict'] = variableDomainsDict
variables = []
for line in testDict["variables"].split('\n'):
variable = line.strip()
variables.append(variable)
edges = []
for line in testDict["edges"].split('\n'):
tokens = line.strip().split()
if len(tokens) == 2:
edges.append((tokens[0], tokens[1]))
else:
raise Exception("[parseBayesNetProblem] Bad evaluation line: |%s|" % (line,))
# inference query args
queryVariables = testDict['queryVariables'].split(' ')
parseDict['queryVariables'] = queryVariables
evidenceDict = {}
for line in testDict['evidenceDict'].split('\n'):
if(line.count(' : ')): #so we can pass empty dicts for unnormalized variables
(evidenceVariable, evidenceValue) = line.split(' : ')
evidenceDict[evidenceVariable] = evidenceValue
parseDict['evidenceDict'] = evidenceDict
if testDict['constructRandomly'] == 'False':
# load from test file
problemBayesNet = bayesNet.constructEmptyBayesNet(variables, edges, variableDomainsDict)
for variable in variables:
currentFactor = bayesNet.Factor([variable], problemBayesNet.inEdges()[variable], variableDomainsDict)
for line in testDict[variable + 'FactorTable'].split('\n'):
assignments, probability = line.split(" = ")
assignmentList = [assignment for assignment in assignments.split(', ')]
assignmentsDict = {}
for assignment in assignmentList:
var, value = assignment.split(' : ')
assignmentsDict[var] = value
currentFactor.setProbability(assignmentsDict, float(probability))
problemBayesNet.setCPT(variable, currentFactor)
elif testDict['constructRandomly'] == 'True':
problemBayesNet = bayesNet.constructRandomlyFilledBayesNet(variables, edges, variableDomainsDict)
parseDict['problemBayesNet'] = problemBayesNet
if testDict['alg'] == 'inferenceByVariableElimination':
variableEliminationOrder = testDict['variableEliminationOrder'].split(' ')
parseDict['variableEliminationOrder'] = variableEliminationOrder
elif testDict['alg'] == 'inferenceByLikelihoodWeightingSampling':
numSamples = int(testDict['numSamples'])
parseDict['numSamples'] = numSamples
return parseDict
###################################
####### From fa21 Tracking Project
fixed_order = ['West', 'East', 'Stop', 'South', 'North']
class GameScoreTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(GameScoreTest, self).__init__(question, testDict)
self.maxMoves = int(self.testDict['maxMoves'])
self.inference = self.testDict['inference']
self.layout_str = self.testDict['layout_str'].split('\n')
self.numRuns = int(self.testDict['numRuns'])
self.numWinsForCredit = int(self.testDict['numWinsForCredit'])
self.numGhosts = int(self.testDict['numGhosts'])
self.layout_name = self.testDict['layout_name']
self.min_score = int(self.testDict['min_score'])
self.observe_enable = self.testDict['observe'] == 'True'
self.elapse_enable = self.testDict['elapse'] == 'True'
def execute(self, grades, moduleDict, solutionDict):
ghosts = [SeededRandomGhostAgent(i) for i in range(1,self.numGhosts+1)]
print(self.inference)
pac = bustersAgents.GreedyBustersAgent(0, inference = self.inference, ghostAgents = ghosts, observeEnable = self.observe_enable, elapseTimeEnable = self.elapse_enable)
#if self.inference == "ExactInference":
# pac.inferenceModules = [moduleDict['inference'].ExactInference(a) for a in ghosts]
#else:
# print "Error inference type %s -- not implemented" % self.inference
# return
stats = run(self.layout_str, pac, ghosts, self.question.getDisplay(), nGames=self.numRuns, maxMoves=self.maxMoves, quiet = False)
aboveCount = [s >= self.min_score for s in stats['scores']].count(True)
msg = "%s) Games won on %s with score above %d: %d/%d" % (self.layout_name, grades.currentQuestion, self.min_score, aboveCount, self.numRuns)
grades.addMessage(msg)
if aboveCount >= self.numWinsForCredit:
grades.assignFullCredit()
return self.testPass(grades)
else:
return self.testFail(grades)
def writeSolution(self, moduleDict, filePath):
handle = open(filePath, 'w')
handle.write('# You must win at least %d/10 games with at least %d points' % (self.numWinsForCredit, self.min_score))
handle.close()
def createPublicVersion(self):
pass
class ZeroWeightTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(ZeroWeightTest, self).__init__(question, testDict)
self.maxMoves = int(self.testDict['maxMoves'])
self.inference = self.testDict['inference']
self.layout_str = self.testDict['layout'].split('\n')
self.numGhosts = int(self.testDict['numGhosts'])
self.observe_enable = self.testDict['observe'] == 'True'
self.elapse_enable = self.testDict['elapse'] == 'True'
self.ghost = self.testDict['ghost']
self.seed = int(self.testDict['seed'])
def execute(self, grades, moduleDict, solutionDict):
random.seed(self.seed)
inferenceFunction = getattr(moduleDict['inference'], self.inference)
ghosts = [globals()[self.ghost](i) for i in range(1, self.numGhosts+1)]
if self.inference == 'MarginalInference':
moduleDict['inference'].jointInference = moduleDict['inference'].JointParticleFilter()
disp = self.question.getDisplay()
pac = ZeroWeightAgent(inferenceFunction, ghosts, grades, self.seed, disp, elapse=self.elapse_enable, observe=self.observe_enable)
if self.inference == "ParticleFilter":
for pfilter in pac.inferenceModules: pfilter.setNumParticles(5000)
elif self.inference == "MarginalInference":
moduleDict['inference'].jointInference.setNumParticles(5000)
run(self.layout_str, pac, ghosts, disp, maxMoves = self.maxMoves)
if pac.getReset():
grades.addMessage('%s) successfully handled all weights = 0' % grades.currentQuestion)
return self.testPass(grades)
else:
grades.addMessage('%s) error handling all weights = 0' % grades.currentQuestion)
return self.testFail(grades)
def writeSolution(self, moduleDict, filePath):
handle = open(filePath, 'w')
handle.write('# This test checks that you successfully handle the case when all particle weights are set to 0\n')
handle.close()
def createPublicVersion(self):
self.testDict['seed'] = '188'
self.seed = 188
class DoubleInferenceAgentTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(DoubleInferenceAgentTest, self).__init__(question, testDict)
self.seed = int(self.testDict['seed'])
self.layout_str = self.testDict['layout'].split('\n')
self.observe = (self.testDict['observe'] == "True")
self.elapse = (self.testDict['elapse'] == "True")
self.checkUniform = (self.testDict['checkUniform'] == 'True')
self.maxMoves = int(self.testDict['maxMoves'])
self.numGhosts = int(self.testDict['numGhosts'])
self.inference = self.testDict['inference']
self.errorMsg = self.testDict['errorMsg']
self.L2Tolerance = float(self.testDict['L2Tolerance'])
self.ghost = self.testDict['ghost']
def execute(self, grades, moduleDict, solutionDict):
random.seed(self.seed)
lines = solutionDict['correctActions'].split('\n')
moves = []
# Collect solutions
for l in lines:
m = re.match('(\d+) (\w+) (.*)', l)
moves.append((m.group(1), m.group(2), eval(m.group(3))))
inferenceFunction = getattr(moduleDict['inference'], self.inference)
ghosts = [globals()[self.ghost](i) for i in range(1, self.numGhosts+1)]
if self.inference == 'MarginalInference':
moduleDict['inference'].jointInference = moduleDict['inference'].JointParticleFilter()
disp = self.question.getDisplay()
pac = DoubleInferenceAgent(inferenceFunction, moves, ghosts, grades, self.seed, disp, self.inference, elapse=self.elapse,
observe=self.observe, L2Tolerance=self.L2Tolerance, checkUniform = self.checkUniform)
if self.inference == "ParticleFilter":
for pfilter in pac.inferenceModules: pfilter.setNumParticles(5000)
elif self.inference == "MarginalInference":
moduleDict['inference'].jointInference.setNumParticles(5000)
run(self.layout_str, pac, ghosts, disp, maxMoves=self.maxMoves)
msg = self.errorMsg % pac.errors
grades.addMessage(("%s) " % (grades.currentQuestion))+msg)
if pac.errors == 0:
grades.addPoints(2)
return self.testPass(grades)
else:
return self.testFail(grades)
def writeSolution(self, moduleDict, filePath):
random.seed(self.seed)
if self.inference == 'ParticleFilter':
self.inference = 'ExactInference' # use exact inference to generate solution
inferenceFunction = getattr(moduleDict['inference'], self.inference)
ghosts = [globals()[self.ghost](i) for i in range(1, self.numGhosts+1)]
if self.inference == 'MarginalInference':
moduleDict['inference'].jointInference = moduleDict['inference'].JointParticleFilter()
moduleDict['inference'].jointInference.setNumParticles(5000)
pac = InferenceAgent(inferenceFunction, ghosts, self.seed, elapse=self.elapse, observe=self.observe)
run(self.layout_str, pac, ghosts, self.question.getDisplay(), maxMoves=self.maxMoves)
# run our gold code here and then write it to a solution file
answerList = pac.answerList
handle = open(filePath, 'w')
handle.write('# move_number action likelihood_dictionary\n')
handle.write('correctActions: """\n')
for (moveNum, move, dists) in answerList:
handle.write('%s %s [' % (moveNum, move))
for dist in dists:
handle.write('{')
for key in dist:
handle.write('%s: %s, ' % (key, dist[key]))
handle.write('}, ')
handle.write(']\n')
handle.write('"""\n')
handle.close()
def createPublicVersion(self):
self.testDict['seed'] = '188'
self.seed = 188
class OutputTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(OutputTest, self).__init__(question, testDict)
self.preamble = compile(testDict.get('preamble', ""), "%s.preamble" % self.getPath(), 'exec')
self.test = compile(testDict['test'], "%s.test" % self.getPath(), 'eval')
self.success = testDict['success']
self.failure = testDict['failure']
def evalCode(self, moduleDict):
bindings = dict(moduleDict)
exec(self.preamble, bindings)
return eval(self.test, bindings)
def execute(self, grades, moduleDict, solutionDict):
result = self.evalCode(moduleDict)
result = list(map(lambda x: str(x), result))
result = ' '.join(result)
if result == solutionDict['result']:
grades.addMessage('PASS: %s' % self.path)
grades.addMessage('\t%s' % self.success)
return True
else:
grades.addMessage('FAIL: %s' % self.path)
grades.addMessage('\t%s' % self.failure)
grades.addMessage('\tstudent result: "%s"' % result)
grades.addMessage('\tcorrect result: "%s"' % solutionDict['result'])
return False
def writeSolution(self, moduleDict, filePath):
handle = open(filePath, 'w')
handle.write('# This is the solution file for %s.\n' % self.path)
handle.write('# The result of evaluating the test must equal the below when cast to a string.\n')
solution = self.evalCode(moduleDict)
solution = list(map(lambda x: str(x), solution))
handle.write('result: "%s"\n' % ' '.join(solution))
handle.close()
return True
def createPublicVersion(self):
pass
def run(layout_str, pac, ghosts, disp, nGames = 1, name = 'games', maxMoves=-1, quiet = True):
"Runs a few games and outputs their statistics."
starttime = time.time()
lay = layout.Layout(layout_str)
#print '*** Running %s on' % name, layname,'%d time(s).' % nGames
games = busters.runGames(lay, pac, ghosts, disp, nGames, maxMoves)
#print '*** Finished running %s on' % name, layname, 'after %d seconds.' % (time.time() - starttime)
stats = {'time': time.time() - starttime, \
'wins': [g.state.isWin() for g in games].count(True), \
'games': games, 'scores': [g.state.getScore() for g in games]}
statTuple = (stats['wins'], len(games), sum(stats['scores']) * 1.0 / len(games))
if not quiet:
print('*** Won %d out of %d games. Average score: %f ***' % statTuple)
return stats
class InferenceAgent(bustersAgents.BustersAgent):
"Tracks ghosts and compares to reference inference modules, while moving randomly"
def __init__( self, inference, ghostAgents, seed, elapse=True, observe=True, burnIn=0):
self.inferenceModules = [inference(a) for a in ghostAgents]
self.elapse = elapse
self.observe = observe
self.burnIn = burnIn
self.numMoves = 0
#self.rand = rand
# list of tuples (move_num, move, [dist_1, dist_2, ...])
self.answerList = []
self.seed = seed
def final(self, gameState):
distributionList = []
self.numMoves += 1
for index,inf in enumerate(self.inferenceModules):
if self.observe:
inf.observe(gameState)
self.ghostBeliefs[index] = inf.getBeliefDistribution()
beliefCopy = deepcopy(self.ghostBeliefs[index])
distributionList.append(beliefCopy)
self.answerList.append((self.numMoves, None, distributionList))
random.seed(self.seed + self.numMoves)
def registerInitialState(self, gameState):
"Initializes beliefs and inference modules"
for inference in self.inferenceModules: inference.initialize(gameState)
self.ghostBeliefs = [inf.getBeliefDistribution() for inf in self.inferenceModules]
self.firstMove = True
self.answerList.append((self.numMoves,None,deepcopy(self.ghostBeliefs)))
def getAction(self, gameState):
"Updates beliefs, then chooses an action based on updated beliefs."
distributionList = []
self.numMoves += 1
for index,inf in enumerate(self.inferenceModules):
if self.elapse:
if not self.firstMove: inf.elapseTime(gameState)
self.firstMove = False
if self.observe:
inf.observe(gameState)
self.ghostBeliefs[index] = inf.getBeliefDistribution()
beliefCopy = deepcopy(self.ghostBeliefs[index])
distributionList.append(beliefCopy)
action = random.choice([a for a in gameState.getLegalPacmanActions() if a != 'STOP'])
self.answerList.append((self.numMoves, action, distributionList))
random.seed(self.seed + self.numMoves)
return action
class ZeroWeightAgent(bustersAgents.BustersAgent):
"Tracks ghosts and compares to reference inference modules, while moving randomly"
def __init__( self, inference, ghostAgents, grades, seed, disp, elapse=True, observe=True ):
self.inferenceModules = [inference(a) for a in ghostAgents]
self.elapse = elapse
self.observe = observe
self.grades = grades
self.numMoves = 0
self.seed = seed
self.display = disp
self.reset = False
def final(self, gameState):
pass
def registerInitialState(self, gameState):
"Initializes beliefs and inference modules"
for inference in self.inferenceModules: inference.initialize(gameState)
self.ghostBeliefs = [inf.getBeliefDistribution() for inf in self.inferenceModules]
self.firstMove = True
def getAction(self, gameState):
"Updates beliefs, then chooses an action based on updated beliefs."
newBeliefs = [None] * len(self.inferenceModules)
self.numMoves += 1
for index,inf in enumerate(self.inferenceModules):
if self.elapse:
if not self.firstMove: inf.elapseTime(gameState)
self.firstMove = False
if self.observe:
inf.observe(gameState)
newBeliefs[index] = inf.getBeliefDistribution()
self.checkReset(newBeliefs, self.ghostBeliefs)
self.ghostBeliefs = newBeliefs
self.display.updateDistributions(self.ghostBeliefs)
random.seed(self.seed + self.numMoves)
action = random.choice([a for a in gameState.getLegalPacmanActions() if a != 'STOP'])
return action
def checkReset(self, newBeliefs, oldBeliefs):
for i in range(len(newBeliefs)):
newKeys = list(filter(lambda x: newBeliefs[i][x] != 0, newBeliefs[i].keys()))
oldKeys = list(filter(lambda x: oldBeliefs[i][x] != 0, oldBeliefs[i].keys()))
if len(newKeys) > len(oldKeys):
self.reset = True
def getReset(self):
return self.reset
class DoubleInferenceAgent(bustersAgents.BustersAgent):
"Tracks ghosts and compares to reference inference modules, while moving randomly"
def __init__( self, inference, refSolution, ghostAgents, grades, seed, disp, func, elapse=True, observe=True, L2Tolerance=0.2, burnIn=0, checkUniform = False):
self.inferenceModules = [inference(a) for a in ghostAgents]
self.refSolution = refSolution
self.func = func
self.elapse = elapse
self.observe = observe
self.grades = grades
self.L2Tolerance = L2Tolerance
self.errors = 0
self.burnIn = burnIn
self.numMoves = 0
self.seed = seed
self.display = disp
self.checkUniform = checkUniform
def final(self, gameState):
self.numMoves += 1
moveNum,action,dists = self.refSolution[self.numMoves]
for index,inf in enumerate(self.inferenceModules):
if self.observe:
inf.observe(gameState)
self.ghostBeliefs[index] = inf.getBeliefDistribution()
if self.numMoves >= self.burnIn:
self.distCompare(self.ghostBeliefs[index], dists[index])
self.display.updateDistributions(self.ghostBeliefs)
random.seed(self.seed + self.numMoves)
if not self.display.checkNullDisplay():
time.sleep(3)
def registerInitialState(self, gameState):
"Initializes beliefs and inference modules"
for inference in self.inferenceModules: inference.initialize(gameState)
moveNum,action,dists = self.refSolution[self.numMoves]
for index,inf in enumerate(self.inferenceModules):
self.distCompare(inf.getBeliefDistribution(), dists[index])
self.ghostBeliefs = [inf.getBeliefDistribution() for inf in self.inferenceModules]
self.firstMove = True
def getAction(self, gameState):
"Updates beliefs, then chooses an action based on updated beliefs."
self.numMoves += 1
moveNum,action,dists = self.refSolution[self.numMoves]
for index,inf in enumerate(self.inferenceModules):
if self.elapse:
if not self.firstMove: inf.elapseTime(gameState)
self.firstMove = False
if self.observe:
inf.observe(gameState)
self.ghostBeliefs[index] = inf.getBeliefDistribution()
if self.numMoves >= self.burnIn: self.distCompare(self.ghostBeliefs[index], dists[index])
self.display.updateDistributions(self.ghostBeliefs)
random.seed(self.seed + self.numMoves)
return action
def distCompare(self, dist, refDist):
"Compares two distributions"
# copy and prepare distributions
dist = dist.copy()
refDist = refDist.copy()
for key in set(list(refDist.keys()) + list(dist.keys())):
if not key in dist.keys():
dist[key] = 0.0
if not key in refDist.keys():
refDist[key] = 0.0
# calculate l2 difference
if sum(refDist.values()) == 0 and self.func != 'ExactInference':
for key in refDist:
if key[1] != 1:
refDist[key] = 1.0 / float(len(refDist))
l2 = 0
for k in refDist.keys():
l2 += (dist[k] - refDist[k]) ** 2
if l2 > self.L2Tolerance:
if self.errors == 0:
t = (self.grades.currentQuestion, self.numMoves, l2)
summary = "%s) Distribution deviated at move %d by %0.4f (squared norm) from the correct answer.\n" % t
header = '%10s%5s%-25s%-25s\n' % ('key:', '', 'student', 'reference')
detail = '\n'.join(list(map(lambda x: '%9s:%5s%-25s%-25s' % (x, '', dist[x], refDist[x]), set(list(dist.keys()) + list(refDist.keys())))))
print(dist.items())
print(refDist.items())
self.grades.fail('%s%s%s' % (summary, header, detail))
self.errors += 1
# check for uniform distribution if necessary
if self.checkUniform:
if abs(max(dist.values()) - max(refDist.values())) > .008: