-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactorOperations.py
273 lines (216 loc) · 11 KB
/
factorOperations.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
# factorOperations.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
# ([email protected]) and Dan Klein ([email protected]).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel ([email protected]).
from bayesNet import Factor
import operator as op
import util
def joinFactorsByVariableWithCallTracking(callTrackingList=None):
def joinFactorsByVariable(factors, joinVariable):
"""
Input factors is a list of factors.
Input joinVariable is the variable to join on.
This function performs a check that the variable that is being joined on
appears as an unconditioned variable in only one of the input factors.
Then, it calls your joinFactors on all of the factors in factors that
contain that variable.
Returns a tuple of
(factors not joined, resulting factor from joinFactors)
"""
if not (callTrackingList is None):
callTrackingList.append(('join', joinVariable))
currentFactorsToJoin = [factor for factor in factors if joinVariable in factor.variablesSet()]
currentFactorsNotToJoin = [factor for factor in factors if joinVariable not in factor.variablesSet()]
# typecheck portion
numVariableOnLeft = len([factor for factor in currentFactorsToJoin if joinVariable in factor.unconditionedVariables()])
if numVariableOnLeft > 1:
print "Factor failed joinFactorsByVariable typecheck: ", factor
raise ValueError, ("The joinBy variable can only appear in one factor as an \nunconditioned variable. \n" +
"joinVariable: " + str(joinVariable) + "\n" +
", ".join(map(str, [factor.unconditionedVariables() for factor in currentFactorsToJoin])))
joinedFactor = joinFactors(currentFactorsToJoin)
return currentFactorsNotToJoin, joinedFactor
return joinFactorsByVariable
joinFactorsByVariable = joinFactorsByVariableWithCallTracking()
def joinFactors(factors):
"""
Question 1: Your join implementation
Input factors is a list of factors.
You should calculate the set of unconditioned variables and conditioned
variables for the join of those factors.
Return a new factor that has those variables and whose probability entries
are product of the corresponding rows of the input factors.
You may assume that the variableDomainsDict for all the input
factors are the same, since they come from the same BayesNet.
joinFactors will only allow unconditionedVariables to appear in
one input factor (so their join is well defined).
Hint: Factor methods that take an assignmentDict as input
(such as getProbability and setProbability) can handle
assignmentDicts that assign more variables than are in that factor.
Useful functions:
Factor.getAllPossibleAssignmentDicts
Factor.getProbability
Factor.setProbability
Factor.unconditionedVariables
Factor.conditionedVariables
Factor.variableDomainsDict
"""
# typecheck portion
setsOfUnconditioned = [set(factor.unconditionedVariables()) for factor in factors]
if len(factors) > 1:
intersect = reduce(lambda x, y: x & y, setsOfUnconditioned)
if len(intersect) > 0:
print "Factor failed joinFactors typecheck: ", factor
raise ValueError, ("unconditionedVariables can only appear in one factor. \n"
+ "unconditionedVariables: " + str(intersect) +
"\nappear in more than one input factor.\n" +
"Input factors: \n" +
"\n".join(map(str, factors)))
factor1 = factors[0]
totalUnconditionedVars = set()
totalConditionedVars = set()
varsToDomain = {}
for factor in factors:
for uncon in factor.unconditionedVariables():
if uncon not in totalUnconditionedVars:
totalUnconditionedVars.add(uncon)
varsToDomain[uncon] = factor.variableDomainsDict()[uncon]
for factor in factors:
for con in factor.conditionedVariables():
if con not in totalUnconditionedVars and con not in totalConditionedVars:
totalConditionedVars.add(con)
varsToDomain[con] = factor.variableDomainsDict()[con]
newFactor = Factor(list(totalUnconditionedVars), list(totalConditionedVars), varsToDomain)
for ass in newFactor.getAllPossibleAssignmentDicts():
newFactor.setProbability(ass, float(1.0))
for factor in factors:
asses = newFactor.getAllPossibleAssignmentDicts()
for ass in asses:
for facAss in factor.getAllPossibleAssignmentDicts():
pro = factor.getProbability(facAss)
works = True
for var in facAss.keys():
if facAss[var] != ass[var]:
works = False
break
if works:
newFactor.setProbability(ass, newFactor.getProbability(ass) * pro)
break
return newFactor
def eliminateWithCallTracking(callTrackingList=None):
def eliminate(factor, eliminationVariable):
"""
Question 2: Your eliminate implementation
Input factor is a single factor.
Input eliminationVariable is the variable to eliminate from factor.
eliminationVariable must be an unconditioned variable in factor.
You should calculate the set of unconditioned variables and conditioned
variables for the factor obtained by eliminating the variable
eliminationVariable.
Return a new factor where all of the rows mentioning
eliminationVariable are summed with rows that match
assignments on the other variables.
Useful functions:
Factor.getAllPossibleAssignmentDicts
Factor.getProbability
Factor.setProbability
Factor.unconditionedVariables
Factor.conditionedVariables
Factor.variableDomainsDict
"""
# autograder tracking -- don't remove
if not (callTrackingList is None):
callTrackingList.append(('eliminate', eliminationVariable))
# typecheck portion
if eliminationVariable not in factor.unconditionedVariables():
print "Factor failed eliminate typecheck: ", factor
raise ValueError, ("Elimination variable is not an unconditioned variable " \
+ "in this factor\n" +
"eliminationVariable: " + str(eliminationVariable) + \
"\nunconditionedVariables:" + str(factor.unconditionedVariables()))
if len(factor.unconditionedVariables()) == 1:
print "Factor failed eliminate typecheck: ", factor
raise ValueError, ("Factor has only one unconditioned variable, so you " \
+ "can't eliminate \nthat variable.\n" + \
"eliminationVariable:" + str(eliminationVariable) + "\n" +\
"unconditionedVariables: " + str(factor.unconditionedVariables()))
new_unconditioned = factor.unconditionedVariables()
new_unconditioned.remove(eliminationVariable)
new_Factor = Factor(new_unconditioned,factor.conditionedVariables(), factor.variableDomainsDict())
for j in new_Factor.getAllPossibleAssignmentDicts():
probabilitySum = 0
for i in new_Factor.variableDomainsDict().get(eliminationVariable):
j[eliminationVariable] = i
currentProb = factor.getProbability(j)
probabilitySum += currentProb
del j[eliminationVariable]
new_Factor.setProbability(j, probabilitySum)
return new_Factor
return eliminate
eliminate = eliminateWithCallTracking()
def normalize(factor):
"""
Question 3: Your normalize implementation
Input factor is a single factor.
The set of conditioned variables for the normalized factor consists
of the input factor's conditioned variables as well as any of the
input factor's unconditioned variables with exactly one entry in their
domain. Since there is only one entry in that variable's domain, we
can either assume it was assigned as evidence to have only one variable
in its domain, or it only had one entry in its domain to begin with.
This blurs the distinction between evidence assignments and variables
with single value domains, but that is alright since we have to assign
variables that only have one value in their domain to that single value.
Return a new factor where the sum of the all the probabilities in the table is 1.
This should be a new factor, not a modification of this factor in place.
If the sum of probabilities in the input factor is 0,
you should return None.
This is intended to be used at the end of a probabilistic inference query.
Because of this, all variables that have more than one element in their
domain are assumed to be unconditioned.
There are more general implementations of normalize, but we will only
implement this version.
Useful functions:
Factor.getAllPossibleAssignmentDicts
Factor.getProbability
Factor.setProbability
Factor.unconditionedVariables
Factor.conditionedVariables
Factor.variableDomainsDict
"""
# typecheck portion
variableDomainsDict = factor.variableDomainsDict()
for conditionedVariable in factor.conditionedVariables():
if len(variableDomainsDict[conditionedVariable]) > 1:
print "Factor failed normalize typecheck: ", factor
raise ValueError, ("The factor to be normalized must have only one " + \
"assignment of the \n" + "conditional variables, " + \
"so that total probability will sum to 1\n" +
str(factor))
originalDomains = factor.variableDomainsDict()
uncond = factor.unconditionedVariables()
cond = factor.conditionedVariables()
for variable, domain in originalDomains.items():
if len(domain) == 1:
if variable not in cond and variable in uncond:
cond.append(variable)
if variable in uncond:
uncond.remove(variable)
new_factor = Factor(uncond, cond, factor.variableDomainsDict())
possibles = new_factor.getAllPossibleAssignmentDicts()
total_probability = 0
for i in possibles:
probability = factor.getProbability(i)
total_probability += probability
for i in possibles:
probability = factor.getProbability(i)
new_factor.setProbability(i, probability/total_probability)
return new_factor