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) #print 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
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) #print 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