traking q4
This commit is contained in:
@ -188,7 +188,32 @@ def inferenceByVariableEliminationWithCallTracking(callTrackingList=None):
|
|||||||
eliminationOrder = sorted(list(eliminationVariables))
|
eliminationOrder = sorted(list(eliminationVariables))
|
||||||
|
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
raiseNotDefined()
|
# print("bn=",bayesNet)
|
||||||
|
# print("queryVariables=",queryVariables)
|
||||||
|
# print("evidenceDict=",evidenceDict)
|
||||||
|
# print("eliminationOrder=",eliminationOrder)
|
||||||
|
# Initialize current factors list with evidence
|
||||||
|
currentFactorsList = bayesNet.getAllCPTsWithEvidence(evidenceDict)
|
||||||
|
# print("currentFactorsList=",currentFactorsList)
|
||||||
|
|
||||||
|
# Iteratively join and eliminate variables in the elimination order
|
||||||
|
for variable in eliminationOrder:
|
||||||
|
# Join all factors containing the variable, thus making the variable "disappear" in the right part of the factor
|
||||||
|
currentFactorsList, joinedFactor = joinFactorsByVariable(currentFactorsList, variable)
|
||||||
|
|
||||||
|
# Only eliminate if there are more than one unconditioned variable, thus making the variable "disappear" in the left part of the factor
|
||||||
|
if len(joinedFactor.unconditionedVariables()) > 1:
|
||||||
|
eliminatedFactor = eliminate(joinedFactor, variable)
|
||||||
|
currentFactorsList.append(eliminatedFactor)
|
||||||
|
|
||||||
|
# Join all remaining factors
|
||||||
|
finalFactor = joinFactors(currentFactorsList)
|
||||||
|
|
||||||
|
# Normalize to get the true conditional probability
|
||||||
|
normalizedFactor = normalize(finalFactor)
|
||||||
|
# print("normalizedFactor=",normalizedFactor)
|
||||||
|
|
||||||
|
return normalizedFactor
|
||||||
"*** END YOUR CODE HERE ***"
|
"*** END YOUR CODE HERE ***"
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user