def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" # populate `obsVars` for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos obsVars.append(obsVar) # populate `edges` for houseVar in HOUSE_VARS: edges.append((X_POS_VAR, houseVar)) edges.append((Y_POS_VAR, houseVar)) for obsVar in obsVars: edges.append((houseVar, obsVar)) # set values variableDomainsDict[X_POS_VAR] = X_POS_VALS variableDomainsDict[Y_POS_VAR] = Y_POS_VALS for houseVar in HOUSE_VARS: variableDomainsDict[houseVar] = HOUSE_VALS for obsVar in obsVars: variableDomainsDict[obsVar] = OBS_VALS variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
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 constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos obsVars.append(obsVar) global OBS_VARs OBS_VARs = obsVars for houseVar in HOUSE_VARS: edges.append((X_POS_VAR,houseVar)) edges.append((Y_POS_VAR,houseVar)) for obsVar in OBS_VARs: variableDomainsDict[obsVar] = OBS_VALS edges.append((houseVar,obsVar)) variableDomainsDict[X_POS_VAR] = X_POS_VALS variableDomainsDict[Y_POS_VAR] = Y_POS_VALS variableDomainsDict[FOOD_HOUSE_VAR] = HOUSE_VALS variableDomainsDict[GHOST_HOUSE_VAR] = HOUSE_VALS variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + OBS_VARs net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, OBS_VARs
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. NOTES: VARS represent variables (nodes), VALS represent values taken on by those variables. Keys in the dictionary are X_POS_VAR, FOOD_HOUSE_VAR, etc. and a key for each obs(%d, %d) where (%d, %d) is the x, y position for each of the 28 positions where there could be a house wall. Values is the list of all possible values that each key could be. """ obsVars = [] edges = [] variableDomainsDict = {X_POS_VAR: X_POS_VALS, Y_POS_VAR: Y_POS_VALS} for HOUSE in HOUSE_VARS: edges.extend(((X_POS_VAR, HOUSE), (Y_POS_VAR, HOUSE))) # edges.append((X_POS_VAR, HOUSE)) # edges.append((Y_POS_VAR, HOUSE)) variableDomainsDict[HOUSE] = HOUSE_VALS for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos variableDomainsDict[obsVar] = OBS_VALS obsVars.append(obsVar) for HOUSE in HOUSE_VARS: edges.append((HOUSE, obsVar)) variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} dictToAdd = {X_POS_VAR: [FOOD_LEFT_VAL, GHOST_LEFT_VAL], Y_POS_VAR: [BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, LEFT_BOTTOM_VAL], FOOD_HOUSE_VAR: [TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL], GHOST_HOUSE_VAR: [TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL]} variableDomainsDict.update(dictToAdd) edges = edges + [(X_POS_VAR, FOOD_HOUSE_VAR), (Y_POS_VAR, FOOD_HOUSE_VAR), (X_POS_VAR, GHOST_HOUSE_VAR), (Y_POS_VAR, GHOST_HOUSE_VAR)] for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos obsVars = obsVars + [obsVar] edges = edges + [(FOOD_HOUSE_VAR, obsVar)] edges = edges + [(GHOST_HOUSE_VAR, obsVar)] variableDomainsDict[obsVar] = [NO_OBS_VAL, BLUE_OBS_VAL, RED_OBS_VAL] variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos variableDomainsDict[obsVar] = [BLUE_OBS_VAL, RED_OBS_VAL, NO_OBS_VAL] obsVars += [obsVar] edges += [(FOOD_HOUSE_VAR,obsVar)] edges += [(GHOST_HOUSE_VAR,obsVar)] edges += [(X_POS_VAR, FOOD_HOUSE_VAR)] edges += [(Y_POS_VAR, FOOD_HOUSE_VAR)] edges += [(X_POS_VAR, GHOST_HOUSE_VAR)] edges += [(Y_POS_VAR, GHOST_HOUSE_VAR)] "*** YOUR CODE HERE ***" variableDomainsDict[X_POS_VAR] = [FOOD_LEFT_VAL,GHOST_LEFT_VAL] variableDomainsDict[Y_POS_VAR] = [BOTH_TOP_VAL,BOTH_BOTTOM_VAL,LEFT_TOP_VAL,LEFT_BOTTOM_VAL] variableDomainsDict[FOOD_HOUSE_VAR] = [TOP_LEFT_VAL,TOP_RIGHT_VAL,BOTTOM_LEFT_VAL,BOTTOM_RIGHT_VAL] variableDomainsDict[GHOST_HOUSE_VAR] = [TOP_LEFT_VAL,TOP_RIGHT_VAL,BOTTOM_LEFT_VAL,BOTTOM_RIGHT_VAL] variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ "*** YOUR CODE HERE ***" arr = [] d = {} r = [] d[X_POS_VAR] = X_POS_VALS d[Y_POS_VAR] = Y_POS_VALS d[FOOD_HOUSE_VAR] = HOUSE_VALS d[GHOST_HOUSE_VAR] = HOUSE_VALS r.append((X_POS_VAR, GHOST_HOUSE_VAR)) r.append((X_POS_VAR, FOOD_HOUSE_VAR)) r.append((Y_POS_VAR, FOOD_HOUSE_VAR)) r.append((Y_POS_VAR, GHOST_HOUSE_VAR)) for i in gameState.getPossibleHouses(): for j in gameState.getHouseWalls(i): temp = OBS_VAR_TEMPLATE % j arr.append(temp) d[temp] = OBS_VALS for temp in arr: r.append((FOOD_HOUSE_VAR, temp)) r.append((GHOST_HOUSE_VAR, temp)) variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + arr c = bn.constructEmptyBayesNet(variables, r, d) return c, arr
def constructBayesNet(gameState): """ Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ # Populate Edge list with original 4 variables edges = [(X_POS_VAR, FOOD_HOUSE_VAR), (X_POS_VAR, GHOST_HOUSE_VAR), (Y_POS_VAR, FOOD_HOUSE_VAR), (Y_POS_VAR, GHOST_HOUSE_VAR)] # Get list of Obs Variables and populate Edges as we go along obsVars = [] variableDomainsDict = {} for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos variableDomainsDict[obsVar] = OBS_VALS obsVars.append(obsVar) edges.append((FOOD_HOUSE_VAR, obsVar)) edges.append((GHOST_HOUSE_VAR, obsVar)) # Set each `variableDomainsDict[var] = values`, where `values` is the set # of possible assignments to `var` #variableDomainsDict = {} variableDomainsDict[X_POS_VAR] = X_POS_VALS variableDomainsDict[Y_POS_VAR] = Y_POS_VALS variableDomainsDict[FOOD_HOUSE_VAR] = HOUSE_VALS variableDomainsDict[GHOST_HOUSE_VAR] = HOUSE_VALS variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" # - a single "x position" variable (controlling the x pos of the houses) variableDomainsDict[X_POS_VAR] = X_POS_VALS # set x pos domain # - a single "y position" variable (controlling the y pos of the houses) variableDomainsDict[Y_POS_VAR] = Y_POS_VALS # set y pos domain # - a single "food house" variable (containing the house centers) variableDomainsDict[FOOD_HOUSE_VAR] = HOUSE_VALS # set food pos domain # - a single "ghost house" variable (containing the house centers) variableDomainsDict[GHOST_HOUSE_VAR] = HOUSE_VALS # set ghost pos domain # - a large number of "observation" variables for each cell Pacman can measure for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): # get full set of observation variables obsVar = OBS_VAR_TEMPLATE % obsPos obsVars.append(obsVar) # - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) edges.append((X_POS_VAR, GHOST_HOUSE_VAR)) edges.append((X_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, GHOST_HOUSE_VAR)) for item in obsVars: variableDomainsDict[item] = OBS_VALS # set domain of obsVar edges.append((FOOD_HOUSE_VAR, item)) edges.append((GHOST_HOUSE_VAR, item)) # return variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
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 constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" #At first, we use the above described methods to get the full set of observetion variables #and add the observation variables to obsvars[], add the (house,obsvar) edge to edges[],and set the domain for each observetion variable for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar=OBS_VAR_TEMPLATE % obsPos obsVars.append(obsVar) edges.append((FOOD_HOUSE_VAR,obsVar)) #each edge is a (from,to) tuple edges.append((GHOST_HOUSE_VAR,obsVar)) variableDomainsDict[obsVar]=OBS_VALS #use the dictionary struct to store the domain #After add all the obervation variable, we need to add edge about X,Y,FoodHose,GoastHouse variable and set their domain #add edge edges.append((X_POS_VAR,FOOD_HOUSE_VAR)) edges.append((X_POS_VAR,GHOST_HOUSE_VAR)) edges.append((Y_POS_VAR,FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR,GHOST_HOUSE_VAR)) #set domain variableDomainsDict[X_POS_VAR]=X_POS_VALS variableDomainsDict[Y_POS_VAR]=Y_POS_VALS variableDomainsDict[FOOD_HOUSE_VAR]=HOUSE_VALS variableDomainsDict[GHOST_HOUSE_VAR]=HOUSE_VALS variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" # add all the things to obsVars and to edges lists for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos obsVars.append(obsVar) edges.append((FOOD_HOUSE_VAR, obsVar)) edges.append((GHOST_HOUSE_VAR, obsVar)) edges.append((X_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, FOOD_HOUSE_VAR)) edges.append((X_POS_VAR, GHOST_HOUSE_VAR)) edges.append((Y_POS_VAR, GHOST_HOUSE_VAR)) # variableDomainsDict['Raining'] = ['yes', 'no'] # add all the things to variableDomainsDict variableDomainsDict[X_POS_VAR] = [FOOD_LEFT_VAL, GHOST_LEFT_VAL] variableDomainsDict[Y_POS_VAR] = [BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, LEFT_BOTTOM_VAL] variableDomainsDict[FOOD_HOUSE_VAR] = [TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL] variableDomainsDict[GHOST_HOUSE_VAR] = [TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL] for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos variableDomainsDict[obsVar] = [BLUE_OBS_VAL, RED_OBS_VAL, NO_OBS_VAL] # print "@@@@EDGES", edges variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
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
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" """ by looking at the requirement, we know that two things needs to be done: 1. get the obsVars[] all assigned (with method above) 2. get the edges[] all assigned (with method in bayesNet.py) """ #print OBS_VAR_TEMPLATE #here's sample of how to connect edges, the remainning problem is to connect house to the obsVars edges = [(X_POS_VAR, FOOD_HOUSE_VAR), (Y_POS_VAR, FOOD_HOUSE_VAR), (X_POS_VAR, GHOST_HOUSE_VAR), (Y_POS_VAR, GHOST_HOUSE_VAR)] for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos obsVars.append(obsVar) variableDomainsDict[obsVar] = [ OBS_VALS[0], OBS_VALS[1], OBS_VALS[2] ] edges.append((GHOST_HOUSE_VAR, obsVar)) edges.append((FOOD_HOUSE_VAR, obsVar)) variableDomainsDict[X_POS_VAR] = [X_POS_VALS[0], X_POS_VALS[1]] variableDomainsDict[Y_POS_VAR] = [ Y_POS_VALS[0], Y_POS_VALS[1], Y_POS_VALS[2], Y_POS_VALS[3] ] variableDomainsDict[HOUSE_VARS[0]] = [ HOUSE_VALS[0], HOUSE_VALS[1], HOUSE_VALS[2], HOUSE_VALS[3] ] variableDomainsDict[HOUSE_VARS[1]] = [ HOUSE_VALS[0], HOUSE_VALS[1], HOUSE_VALS[2], HOUSE_VALS[3] ] # print # `print obsVars # util.raiseNotDefined() variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) #here list the three thing we should compute: #variables. has an expression, need to fill in X_POS_VAR, Y_POS_VAR, HOUSE_VARS, obsVars #edges. has to connect those, i think there's function about in/out in bayesNet.py #obsVars. has to be computed with the given method #variableDomainDict(). return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" #first we should get all the observation variables #use the procedure above for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVar = OBS_VAR_TEMPLATE % obsPos #append the current observation variable into the observation var list obsVars.append(obsVar) #second,populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) #define all the edges in Q1 and qppend them into the edges list #Constructing Bayes' nets, edge list: (x, y) means edge from x to y edges.append((X_POS_VAR, FOOD_HOUSE_VAR)) edges.append((X_POS_VAR, GHOST_HOUSE_VAR)) edges.append((Y_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, GHOST_HOUSE_VAR)) i = 0 while i < len(obsVars): edges.append((FOOD_HOUSE_VAR, obsVars[i])) edges.append((GHOST_HOUSE_VAR, obsVars[i])) i = i + 1 #third,set each `variableDomainsDict[var] = values` #X_POS_VALS = [FOOD_LEFT_VAL, GHOST_LEFT_VAL] #Y_POS_VALS = [BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, LEFT_BOTTOM_VAL] #HOUSE_VARS = [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR] #HOUSE_VALS = [TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL] #OBS_VALS = [BLUE_OBS_VAL, RED_OBS_VAL, NO_OBS_VAL] variableDomainsDict[X_POS_VAR] = X_POS_VALS variableDomainsDict[Y_POS_VAR] = Y_POS_VALS variableDomainsDict[FOOD_HOUSE_VAR] = HOUSE_VALS variableDomainsDict[GHOST_HOUSE_VAR] = HOUSE_VALS m = 0 while m < len(obsVars): variableDomainsDict[obsVars[m]] = OBS_VALS m = m + 1 #util.raiseNotDefined() #create an empty bayesian network use the list we define above variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" ### BEGIN SOLUTION for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): # Append the obsVar to the list obsVar = OBS_VAR_TEMPLATE % obsPos obsVars.append(obsVar) # Bottom layer of BN # Food house -> All observations # Ghost house -> All observations edges.append((FOOD_HOUSE_VAR, obsVar)) edges.append((GHOST_HOUSE_VAR, obsVar)) # Add observation dictionary variableDomainsDict[obsVar] = OBS_VALS # Top layer of BNN # Positions (X and Y) -> House (Food house and Ghost house) edges.append((X_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, FOOD_HOUSE_VAR)) edges.append((X_POS_VAR, GHOST_HOUSE_VAR)) edges.append((Y_POS_VAR, GHOST_HOUSE_VAR)) # Dictionaries for position nodes and house nodes variableDomainsDict[FOOD_HOUSE_VAR] = HOUSE_VALS variableDomainsDict[GHOST_HOUSE_VAR] = HOUSE_VALS variableDomainsDict[X_POS_VAR] = X_POS_VALS variableDomainsDict[Y_POS_VAR] = Y_POS_VALS ### END SOLUTION variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" """FUNCTIONALITY in this section structure of the Bayesnet is made. for the first section, all observations are made through the following alorithm mentioned in the description above: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos Then all edges are added. for each X_POS_VAR and Y_POS_VAR, there are two options of FOOD_HOUSE_VAR which contains food and GHOST_HOUSE_VAR whcih contains ghost. these 4 values can be added to the edge manually and then all other possible edges based on the observation is added consequently. This implementation is based on the two X_POSITION and Y_POSITION are connected to FoodHouse and GhostHouse and thus 4 possibilities are availabe. The same process is made for the variableDomainsDict too. first all possible values are added manually and then based upon observations all other observation values are added. these values can be [BLUE_OBS_VAL, RED_OBS_VAL, NO_OBS_VAL] for any observation point. """ "generating all obsevation point in the obsVars" for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos): obsVars.append(OBS_VAR_TEMPLATE % obsPos) "generating all edges with all 4 possibilies of (X,food), (Y,food), (X,ghost), (Y,ghost)" edges.append((X_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, FOOD_HOUSE_VAR)) edges.append((X_POS_VAR, GHOST_HOUSE_VAR)) edges.append((Y_POS_VAR, GHOST_HOUSE_VAR)) #print edges "adding all other edges based upon all observations. it consists of (food/ghost, position)" for obs in obsVars: edges.append((FOOD_HOUSE_VAR, obs)) edges.append((GHOST_HOUSE_VAR, obs)) #print edges "generating all variableDomainsDict with all 4 possibilies of manually" variableDomainsDict[X_POS_VAR] = X_POS_VALS #[FOOD_LEFT_VAL, GHOST_LEFT_VAL] variableDomainsDict[Y_POS_VAR] = Y_POS_VALS #[BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, LEFT_BOTTOM_VAL] variableDomainsDict[FOOD_HOUSE_VAR] = HOUSE_VALS #[TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL] variableDomainsDict[GHOST_HOUSE_VAR] = HOUSE_VALS #[TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL] "adding all other values based upon all observations. it consists of (blue, red, none) optiosn for each cell" for obs in obsVars: variableDomainsDict[obs] = OBS_VALS # [BLUE_OBS_VAL, RED_OBS_VAL, NO_OBS_VAL] #util.raiseNotDefined() variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def constructBayesNet(gameState): """ Question 1: Bayes net structure Construct an empty Bayes net according to the structure given in the project description. There are 5 kinds of variables in this Bayes net: - a single "x position" variable (controlling the x pos of the houses) - a single "y position" variable (controlling the y pos of the houses) - a single "food house" variable (containing the house centers) - a single "ghost house" variable (containing the house centers) - a large number of "observation" variables for each cell Pacman can measure You *must* name all position and house variables using the constants (X_POS_VAR, FOOD_HOUSE_VAR, etc.) at the top of this file. The full set of observation variables can be obtained as follows: for housePos in gameState.getPossibleHouses(): for obsPos in gameState.getHouseWalls(housePos) obsVar = OBS_VAR_TEMPLATE % obsPos In this method, you should: - populate `obsVars` using the procedure above - populate `edges` with every edge in the Bayes Net (a tuple `(from, to)`) - set each `variableDomainsDict[var] = values`, where `values` is the set of possible assignments to `var`. These should again be set using the constants defined at the top of this file. """ obsVars = [] edges = [] variableDomainsDict = {} "*** YOUR CODE HERE ***" #util.raiseNotDefined() variableDomainsDict[X_POS_VAR] = X_POS_VALS variableDomainsDict[Y_POS_VAR] = Y_POS_VALS variableDomainsDict[FOOD_HOUSE_VAR] = HOUSE_VALS variableDomainsDict[GHOST_HOUSE_VAR] = HOUSE_VALS edges.append((X_POS_VAR, GHOST_HOUSE_VAR)) edges.append((X_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, FOOD_HOUSE_VAR)) edges.append((Y_POS_VAR, GHOST_HOUSE_VAR)) #obtaining the full set of observed variables as #guided before the code #parsing through all the legal house positions for legalhousePos in gameState.getPossibleHouses(): #parsing through all walls in the house for legalobsPos in gameState.getHouseWalls(legalhousePos): currentobsVar = OBS_VAR_TEMPLATE % legalobsPos #append the observed variable to the variables array obsVars.append(currentobsVar) #set the dict val of current observed value to constant -> OBS_VALS=(none) variableDomainsDict[currentobsVar] = OBS_VALS #parsing through all the observed variables for currentobsVar in obsVars: #populating every food and ghost house edge edges.append((FOOD_HOUSE_VAR, currentobsVar)) edges.append((GHOST_HOUSE_VAR, currentobsVar)) variables = [X_POS_VAR, Y_POS_VAR] + HOUSE_VARS + obsVars net = bn.constructEmptyBayesNet(variables, edges, variableDomainsDict) return net, obsVars
def powersBayesNet(): powerVars = ['laser', 'capacity', 'invisibility', 'respawn', 'speed'] sumVars = ['sum0', 'sum1', 'sum2', 'sum3'] variablesList = powerVars + sumVars variableDomainsDict = {} variableDomainsDict['laser'] = range(3) variableDomainsDict['speed'] = range(3) variableDomainsDict['capacity'] = range(3) variableDomainsDict['invisibility'] = range(3) variableDomainsDict['respawn'] = range(3) variableDomainsDict['sum0'] = range(6) variableDomainsDict['sum1'] = range(9) variableDomainsDict['sum2'] = range(9) variableDomainsDict['sum3'] = range(6) """ edgeTuplesList = [('laser', 'sum0'), ('laser', 'sum2'), ('capacity', 'sum0'), ('capacity', 'sum1'), ('invisibility', 'sum0'), ('invisibility', 'sum1'), ('respawn', 'sum1'), ('respawn', 'sum2'), ('speed', 'sum1'), ('speed', 'sum2'), #('speed', 'sum1') ('speed', 'sum3') ] """ edgeTuplesList = [('laser', 'sum1'), ('laser', 'sum2'), ('capacity', 'sum0'), ('capacity', 'sum2'), ('invisibility', 'sum1'), ('invisibility', 'sum3'), ('respawn', 'sum0'), ('respawn', 'sum1'), ('speed', 'sum2'), ('speed', 'sum3'), #('speed', 'sum1') # ('speed', 'sum3') ] net = bayesNet.constructEmptyBayesNet(variablesList, edgeTuplesList, variableDomainsDict) def sumFactor(sumVar, inputVars): newFactor = bayesNet.Factor([sumVar], inputVars, variableDomainsDict) # only need to set the ones for assignmentDict in newFactor.getAllPossibleAssignmentDicts(): inputSum = sum([int(assignmentDict[inputVar]) for inputVar in inputVars]) if inputSum == int(assignmentDict[sumVar]): newFactor.setProbability(assignmentDict, 1.0) return newFactor def powerFactor(powerVar): newFactor = bayesNet.Factor([powerVar], [], variableDomainsDict) numChoices = len(variableDomainsDict[powerVar]) for assignmentDict in newFactor.getAllPossibleAssignmentDicts(): newFactor.setProbability(assignmentDict, 1.0 / numChoices) return newFactor inEdges = net.inEdges() for sumVar in sumVars: inputVars = inEdges[sumVar] net.setCPT(sumVar, sumFactor(sumVar, inputVars)) for powerVar in powerVars: net.setCPT(powerVar, powerFactor(powerVar)) return net