def fillHouseCPT(bayesNet, gameState):
    foodHouseFactor = bn.Factor([FOOD_HOUSE_VAR], [X_POS_VAR, Y_POS_VAR], bayesNet.variableDomainsDict())
    for assignment in foodHouseFactor.getAllPossibleAssignmentDicts():
        left = assignment[X_POS_VAR] == FOOD_LEFT_VAL
        top = assignment[Y_POS_VAR] == BOTH_TOP_VAL or \
                (left and assignment[Y_POS_VAR] == LEFT_TOP_VAL)

        if top and left and assignment[FOOD_HOUSE_VAR] == TOP_LEFT_VAL or \
                top and not left and assignment[FOOD_HOUSE_VAR] == TOP_RIGHT_VAL or \
                not top and left and assignment[FOOD_HOUSE_VAR] == BOTTOM_LEFT_VAL or \
                not top and not left and assignment[FOOD_HOUSE_VAR] == BOTTOM_RIGHT_VAL:
            prob = 1
        else:
            prob = 0

        foodHouseFactor.setProbability(assignment, prob)
    bayesNet.setCPT(FOOD_HOUSE_VAR, foodHouseFactor)

    ghostHouseFactor = bn.Factor([GHOST_HOUSE_VAR], [X_POS_VAR, Y_POS_VAR], bayesNet.variableDomainsDict())
    for assignment in ghostHouseFactor.getAllPossibleAssignmentDicts():
        left = assignment[X_POS_VAR] == GHOST_LEFT_VAL
        top = assignment[Y_POS_VAR] == BOTH_TOP_VAL or \
                (left and assignment[Y_POS_VAR] == LEFT_TOP_VAL)

        if top and left and assignment[GHOST_HOUSE_VAR] == TOP_LEFT_VAL or \
                top and not left and assignment[GHOST_HOUSE_VAR] == TOP_RIGHT_VAL or \
                not top and left and assignment[GHOST_HOUSE_VAR] == BOTTOM_LEFT_VAL or \
                not top and not left and assignment[GHOST_HOUSE_VAR] == BOTTOM_RIGHT_VAL:
            prob = 1
        else:
            prob = 0

        ghostHouseFactor.setProbability(assignment, prob)
    bayesNet.setCPT(GHOST_HOUSE_VAR, ghostHouseFactor)
Esempio n. 2
0
def fillXCPT(bayesNet, gameState):
    from layout import PROB_FOOD_LEFT
    xFactor = bn.Factor([X_POS_VAR], [], bayesNet.variableDomainsDict())
    xFactor.setProbability({X_POS_VAR: FOOD_LEFT_VAL}, PROB_FOOD_LEFT)
    xFactor.setProbability({X_POS_VAR: GHOST_LEFT_VAL}, 1 - PROB_FOOD_LEFT)

    bayesNet.setCPT(X_POS_VAR, xFactor)
    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
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """

    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    "*** YOUR CODE HERE ***"
    """ FUNCTIONALITY
    here all 4 possibilies BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, and LEFT_BOTTOM_VAL
    are linked to their position. this is related to the 4 possibilieties set manually.
    """
    #print PROB_BOTH_TOP
    #print bayesNet
    #print yFactor  
    "setting all probabilities of the 4 options"
    yFactor.setProbability({Y_POS_VAR: BOTH_TOP_VAL}, PROB_BOTH_TOP)
    yFactor.setProbability({Y_POS_VAR: BOTH_BOTTOM_VAL}, PROB_BOTH_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: LEFT_TOP_VAL}, PROB_ONLY_LEFT_TOP)
    yFactor.setProbability({Y_POS_VAR: LEFT_BOTTOM_VAL}, PROB_ONLY_LEFT_BOTTOM)
    #print yFactor  
    
    #util.raiseNotDefined()
    #print bayesNet
    "here are probabilities are updated to BayesNet"
    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 5
0
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """

    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    "*** YOUR CODE HERE ***"
    ### BEGIN SOLUTION
    from layout import PROB_BOTH_TOP
    from layout import PROB_BOTH_BOTTOM
    from layout import PROB_ONLY_LEFT_TOP
    from layout import PROB_ONLY_LEFT_BOTTOM

    yFactor.setProbability({Y_POS_VAR: BOTH_TOP_VAL}, PROB_BOTH_TOP)
    yFactor.setProbability({Y_POS_VAR: BOTH_BOTTOM_VAL}, PROB_BOTH_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: LEFT_TOP_VAL}, PROB_ONLY_LEFT_TOP)
    yFactor.setProbability({Y_POS_VAR: LEFT_BOTTOM_VAL}, PROB_ONLY_LEFT_BOTTOM)

    ### END SOLUTION
    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 6
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses(
    )

    "*** YOUR CODE HERE ***"
    for house in gameState.getPossibleHouses():
        for wall in gameState.getHouseWalls(house):
            if house == bottomLeftPos:
                house = BOTTOM_LEFT_VAL
            if house == topLeftPos:
                house = TOP_LEFT_VAL
            if house == bottomRightPos:
                house = BOTTOM_RIGHT_VAL
            if house == topRightPos:
                house = TOP_RIGHT_VAL
            obsVar = OBS_VAR_TEMPLATE % wall
            obsFactor = bn.Factor([obsVar], HOUSE_VARS,
                                  bayesNet.variableDomainsDict())
            for assignment in obsFactor.getAllPossibleAssignmentDicts():
                probability = {}
                if assignment[FOOD_HOUSE_VAR] == house:
                    probability[RED_OBS_VAL] = PROB_FOOD_RED
                    probability[BLUE_OBS_VAL] = 1 - PROB_FOOD_RED
                    probability[NO_OBS_VAL] = 0
                elif assignment[GHOST_HOUSE_VAR] == house:
                    probability[RED_OBS_VAL] = PROB_GHOST_RED
                    probability[BLUE_OBS_VAL] = 1 - PROB_GHOST_RED
                    probability[NO_OBS_VAL] = 0
                elif assignment[FOOD_HOUSE_VAR] != house and assignment[
                        GHOST_HOUSE_VAR] != house:
                    probability[RED_OBS_VAL] = 0
                    probability[BLUE_OBS_VAL] = 0
                    probability[NO_OBS_VAL] = 1
                obsFactor.setProbability(assignment,
                                         probability[assignment[obsVar]])
            bayesNet.setCPT(obsVar, obsFactor)
Esempio n. 7
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """
    "*** YOUR CODE HERE ***"
    e = 0
    var = gameState.getPossibleHouses()
    for i in var:
        for j in gameState.getHouseWalls(i):
            rt = OBS_VAR_TEMPLATE % j
            set = bn.Factor([rt], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], bayesNet.variableDomainsDict())
            obsCPT = set.getAllPossibleAssignmentDicts()
            for x in obsCPT:
                x1 = x[GHOST_HOUSE_VAR]
                x2 = x[FOOD_HOUSE_VAR]
                temp = x[rt]
                s = getAdjQuadrant(gameState, j)
                if (s != x1 and s != x2):
                    if (temp == RED_OBS_VAL):
                        e = 0
                    elif (temp == BLUE_OBS_VAL):
                        e = 0
                    elif (temp == NO_OBS_VAL):
                        e = 1
                elif (x1 == x2 or x2 == s):
                    if (temp == RED_OBS_VAL):
                        e = PROB_FOOD_RED
                    elif (temp == BLUE_OBS_VAL):
                        e = 1 - PROB_FOOD_RED
                    elif (temp == NO_OBS_VAL):
                        e = 0
                elif (s == x1):
                    if (temp == RED_OBS_VAL):
                        e = PROB_GHOST_RED
                    elif (temp == BLUE_OBS_VAL):
                        e = 1 - PROB_GHOST_RED
                    elif (temp == NO_OBS_VAL):
                        e = 0
                set.setProbability(x, e)
            bayesNet.setCPT(rt, set)    
Esempio n. 8
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses()

    "*** YOUR CODE HERE ***"
    boardWidth = gameState.data.layout.width
    boardHeight = gameState.data.layout.height

    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVar = OBS_VAR_TEMPLATE % obsPos
            obsFactor = bn.Factor([obsVar], HOUSE_VARS, bayesNet.variableDomainsDict())

            if obsPos[0] < boardWidth/2.0 and obsPos[1] > boardHeight/2.0:
                obsVarLocation = TOP_LEFT_VAL
            if obsPos[0] > boardWidth/2.0 and obsPos[1] > boardHeight/2.0:
                obsVarLocation = TOP_RIGHT_VAL
            if obsPos[0] < boardWidth/2.0 and obsPos[1] < boardHeight/2.0:
                obsVarLocation = BOTTOM_LEFT_VAL
            if obsPos[0] > boardWidth/2.0 and obsPos[1] < boardHeight/2.0:
                obsVarLocation = BOTTOM_RIGHT_VAL

            for assignment in obsFactor.getAllPossibleAssignmentDicts():
                if assignment[FOOD_HOUSE_VAR] == obsVarLocation:
                    red, blue, none = PROB_FOOD_RED, 1 - PROB_FOOD_RED, 0
                elif assignment[GHOST_HOUSE_VAR] == obsVarLocation:
                    red, blue, none = PROB_GHOST_RED, 1 - PROB_GHOST_RED, 0
                else:
                    red, blue, none = 0, 0, 1
                if assignment[obsVar] == NO_OBS_VAL:
                    obsFactor.setProbability(assignment, none)
                elif assignment[obsVar] == BLUE_OBS_VAL:
                    obsFactor.setProbability(assignment, blue)
                elif assignment[obsVar] == RED_OBS_VAL:
                    obsFactor.setProbability(assignment, red)

            bayesNet.setCPT(obsVar, obsFactor)
Esempio n. 9
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses()

    "*** YOUR CODE HERE ***"
    CenterPos={TOP_RIGHT_VAL:topRightPos,BOTTOM_LEFT_VAL:bottomLeftPos,TOP_LEFT_VAL:topLeftPos,BOTTOM_RIGHT_VAL:bottomRightPos}
    ObsPos_list=[]
    #print(CenterPos.values())
    for i in CenterPos.values():
        for j in range(-1,2):
            for k in range(-1,2):
                if not (j==0 and k==-1):ObsPos_list.append((i[0]+j,i[1]+k))
    ObsPos_list=list(set(ObsPos_list)-set(CenterPos.values()))
    #print(ObsPos_list)
    for i in ObsPos_list:
        Obs_Factor=bn.Factor([OBS_VAR_TEMPLATE%i],[GHOST_HOUSE_VAR,FOOD_HOUSE_VAR],bayesNet.variableDomainsDict())
        for dic in Obs_Factor.getAllPossibleAssignmentDicts():
            food_pos=dic[FOOD_HOUSE_VAR]
            ghost_pos=dic[GHOST_HOUSE_VAR]
            if(game.manhattanDistance(CenterPos[food_pos],i)>=3 and game.manhattanDistance(CenterPos[ghost_pos],i)>=3):
                if(dic[OBS_VAR_TEMPLATE%i]==NO_OBS_VAL):
                    Obs_Factor.setProbability(dic,1)
                else:Obs_Factor.setProbability(dic,0)
            elif(game.manhattanDistance(CenterPos[ghost_pos],i)<=2 and game.manhattanDistance(CenterPos[food_pos],i)>=3):
                if(dic[OBS_VAR_TEMPLATE%i]==NO_OBS_VAL):
                    Obs_Factor.setProbability(dic,0)
                elif(dic[OBS_VAR_TEMPLATE%i]==RED_OBS_VAL):
                    Obs_Factor.setProbability(dic,PROB_GHOST_RED)
                else:Obs_Factor.setProbability(dic,1-PROB_GHOST_RED)
            else:
                if (dic[OBS_VAR_TEMPLATE%i] == NO_OBS_VAL):
                    Obs_Factor.setProbability(dic, 0)
                elif (dic[OBS_VAR_TEMPLATE%i] == RED_OBS_VAL):
                    Obs_Factor.setProbability(dic, PROB_FOOD_RED)
                else:
                    Obs_Factor.setProbability(dic, 1 - PROB_FOOD_RED)
        bayesNet.setCPT(OBS_VAR_TEMPLATE%i,Obs_Factor)
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """
    wid, hei = gameState.data.layout.width, gameState.data.layout.height
    def check_position(x,y):
        if x < wid / 2 and y < hei/2:
            return BOTTOM_LEFT_VAL
        elif x >= wid/2 and y < hei /2:
            return BOTTOM_RIGHT_VAL
        elif x < wid /2 and y >= hei/2:
            return TOP_LEFT_VAL
        elif x >= wid /2 and y >= hei/2:
            return TOP_RIGHT_VAL
    "*** YOUR CODE HERE ***"

    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVar = OBS_VAR_TEMPLATE % obsPos
            obsFactor = bn.Factor([obsVar], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], bayesNet.variableDomainsDict())
            for assignment in obsFactor.getAllPossibleAssignmentDicts():
                Red = 0
                Blue = 0
                No_Col = 0
                if assignment[FOOD_HOUSE_VAR] == check_position(obsPos[0], obsPos[1]):
                    Red = PROB_FOOD_RED
                    Blue = 1 - PROB_FOOD_RED
                elif assignment[GHOST_HOUSE_VAR] == check_position(obsPos[0], obsPos[1]):
                    Red = PROB_GHOST_RED
                    Blue = 1 - PROB_GHOST_RED
                else: 
                    No_Col = 1
                if assignment[obsVar] == BLUE_OBS_VAL:
                    obsFactor.setProbability(assignment, Blue)
                elif assignment[obsVar] == RED_OBS_VAL:
                    obsFactor.setProbability(assignment, Red)
                else:
                    obsFactor.setProbability(assignment, No_Col)

            bayesNet.setCPT(obsVar, obsFactor)
Esempio n. 11
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    "*** YOUR CODE HERE ***"
    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVar = OBS_VAR_TEMPLATE % obsPos
            obsF = bn.Factor([obsVar], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR],
                             bayesNet.variableDomainsDict())
            for i in obsF.getAllPossibleAssignmentDicts():
                foodHouseVal = i[FOOD_HOUSE_VAR]
                ghostHouseVal = i[GHOST_HOUSE_VAR]
                color = i[obsVar]
                four = get(gameState, obsPos)
                prob = 0

                if four != ghostHouseVal and four != foodHouseVal:
                    if color == BLUE_OBS_VAL or color == RED_OBS_VAL:
                        prob = 0
                    else:
                        prob = 1
                elif ghostHouseVal == foodHouseVal or foodHouseVal == four:
                    if color == RED_OBS_VAL:
                        prob = PROB_FOOD_RED
                    elif color == BLUE_OBS_VAL:
                        prob = 1 - PROB_FOOD_RED
                    else:
                        prob = 0
                elif four == ghostHouseVal:
                    if color == RED_OBS_VAL:
                        prob = PROB_GHOST_RED
                    elif color == BLUE_OBS_VAL:
                        prob = 1 - PROB_GHOST_RED
                    else:
                        prob = 0
                obsF.setProbability(i, prob)
            bayesNet.setCPT(obsVar, obsF)
Esempio n. 12
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses(
    )
    houseLocations = dict(zip([topLeftPos, topRightPos, bottomLeftPos, bottomRightPos], \
                            HOUSE_VALS))

    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVar = OBS_VAR_TEMPLATE % obsPos
            obsFactor = bn.Factor([obsVar], HOUSE_VARS,
                                  bayesNet.variableDomainsDict())
            for assignment in obsFactor.getAllPossibleAssignmentDicts():
                # For Food House
                if assignment[FOOD_HOUSE_VAR] == houseLocations[housePos]:
                    if assignment[obsVar] == RED_OBS_VAL:
                        prob = PROB_FOOD_RED
                    elif assignment[obsVar] == BLUE_OBS_VAL:
                        prob = 1 - PROB_FOOD_RED
                    else:
                        prob = 0
                # For Ghost House
                elif assignment[GHOST_HOUSE_VAR] == houseLocations[housePos]:
                    if assignment[obsVar] == RED_OBS_VAL:
                        prob = PROB_GHOST_RED
                    elif assignment[obsVar] == BLUE_OBS_VAL:
                        prob = 1 - PROB_GHOST_RED
                    else:
                        prob = 0
                # For No House
                else:
                    prob = 1 if assignment[obsVar] == NO_OBS_VAL else 0

                obsFactor.setProbability(assignment, prob)

            bayesNet.setCPT(obsVar, obsFactor)
 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
Esempio n. 14
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses(
    )
    "*** YOUR CODE HERE ***"
    houseNameVals = {  #makes it easier to look up positions when dealing with their values
        BOTTOM_LEFT_VAL: bottomLeftPos,
        TOP_LEFT_VAL: topLeftPos,
        BOTTOM_RIGHT_VAL: bottomRightPos,
        TOP_RIGHT_VAL: topRightPos
    }
    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVal = OBS_VAR_TEMPLATE % obsPos
            newFactor = bn.Factor([obsVal], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR],
                                  bayesNet.variableDomainsDict())
            for dictVal in newFactor.getAllPossibleAssignmentDicts():
                valProbab = 0
                if houseNameVals[dictVal[
                        FOOD_HOUSE_VAR]] == housePos:  #prioritize food check first
                    if dictVal[
                            obsVal] != NO_OBS_VAL:  #did this check so I could be cool and use list comprehensions :P
                        valProbab = PROB_FOOD_RED if dictVal[
                            obsVal] == RED_OBS_VAL else 1 - PROB_FOOD_RED
                        #^PROB_FOOD_RED = RED, 1-PROB_FOOD_RED = BLUE
                elif houseNameVals[dictVal[GHOST_HOUSE_VAR]] == housePos:
                    if dictVal[obsVal] != NO_OBS_VAL:
                        valProbab = PROB_GHOST_RED if dictVal[
                            obsVal] == RED_OBS_VAL else 1 - PROB_GHOST_RED
                elif dictVal[obsVal] == NO_OBS_VAL:
                    valProbab = 1  #nothing to observe, definitely gonna happen
                newFactor.setProbability(dictVal, valProbab)
            bayesNet.setCPT(obsVal, newFactor)
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses()

    pos = {}
    pos[bottomLeftPos] = BOTTOM_LEFT_VAL
    pos[topLeftPos] = TOP_LEFT_VAL
    pos[bottomRightPos] = BOTTOM_RIGHT_VAL
    pos[topRightPos] = TOP_RIGHT_VAL

    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVar = OBS_VAR_TEMPLATE % obsPos
            obsFactor = bn.Factor([obsVar], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], bayesNet.variableDomainsDict())
            for ghost in HOUSE_VALS:
                for food in HOUSE_VALS:
                    if pos[housePos] != food and pos[housePos] != ghost:                    
                        obsFactor.setProbability({obsVar: NO_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 1)
                        obsFactor.setProbability({obsVar: RED_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 0)
                        obsFactor.setProbability({obsVar: BLUE_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 0)
                    elif pos[housePos] == food and pos[housePos] == ghost:
                        obsFactor.setProbability({obsVar: RED_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, PROB_FOOD_RED)
                        obsFactor.setProbability({obsVar: BLUE_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 1 - PROB_FOOD_RED)
                        obsFactor.setProbability({obsVar: NO_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 0)
                    elif pos[housePos] == food:
                        obsFactor.setProbability({obsVar: RED_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, PROB_FOOD_RED)
                        obsFactor.setProbability({obsVar: BLUE_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 1 - PROB_FOOD_RED)
                        obsFactor.setProbability({obsVar: NO_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 0)
                    else:
                        obsFactor.setProbability({obsVar: RED_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, PROB_GHOST_RED)
                        obsFactor.setProbability({obsVar: BLUE_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 1 - PROB_GHOST_RED)
                        obsFactor.setProbability({obsVar: NO_OBS_VAL, FOOD_HOUSE_VAR: food, GHOST_HOUSE_VAR: ghost}, 0)              
            bayesNet.setCPT(obsVar, obsFactor)
Esempio n. 16
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """
    HOUSE_VALUES = [
        BOTTOM_LEFT_VAL, TOP_LEFT_VAL, BOTTOM_RIGHT_VAL, TOP_RIGHT_VAL
    ]
    counter = 0

    for pos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(pos):
            obsPos = OBS_VAR_TEMPLATE % obsPos
            obsVarFactor = bn.Factor([obsPos], HOUSE_VARS,
                                     bayesNet.variableDomainsDict())
            for assignment in obsVarFactor.getAllPossibleAssignmentDicts():
                ghostHouseVal = assignment[GHOST_HOUSE_VAR]
                foodHouseVal = assignment[FOOD_HOUSE_VAR]
                color = assignment[obsPos]
                prob = 0
                if foodHouseVal == HOUSE_VALUES[counter]:
                    if color == RED_OBS_VAL:
                        prob = PROB_FOOD_RED
                    elif color == BLUE_OBS_VAL:
                        prob = 1 - PROB_FOOD_RED
                elif ghostHouseVal == HOUSE_VALUES[counter]:
                    if color == RED_OBS_VAL:
                        prob = PROB_GHOST_RED
                    elif color == BLUE_OBS_VAL:
                        prob = 1 - PROB_GHOST_RED
                else:
                    if color == NO_OBS_VAL:
                        prob = 1
                obsVarFactor.setProbability(assignment, prob)
            bayesNet.setCPT(obsPos, obsVarFactor)
        counter = (counter + 1) % 4
Esempio n. 17
0
def fillYCPT(bayesNet, gameState):
    """
    Fill the CPT that gives the prior probability over the y position variable.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """
    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())

    yFactor.setProbability({Y_POS_VAR: BOTH_TOP_VAL}, PROB_BOTH_TOP)
    yFactor.setProbability({Y_POS_VAR: BOTH_BOTTOM_VAL}, PROB_BOTH_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: LEFT_TOP_VAL}, PROB_ONLY_LEFT_TOP)
    yFactor.setProbability({Y_POS_VAR: LEFT_BOTTOM_VAL}, PROB_ONLY_LEFT_BOTTOM)

    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 18
0
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """

    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    "*** YOUR CODE HERE ***"
    util.raiseNotDefined()
    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 19
0
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """
    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    yProb = [
        PROB_BOTH_TOP, PROB_BOTH_BOTTOM, PROB_ONLY_LEFT_TOP,
        PROB_ONLY_LEFT_BOTTOM
    ]
    #Y_POS_VALS = [BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, LEFT_BOTTOM_VAL]
    "*** YOUR CODE HERE ***"
    for val, prob in zip(Y_POS_VALS, yProb):
        yFactor.setProbability({Y_POS_VAR: val}, prob)
    bayesNet.setCPT(Y_POS_VAR, yFactor)
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
Esempio n. 21
0
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities
    Changes to UC Berkeley CS188's base code written by Will Park.

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """

    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    "*** YOUR CODE HERE ***"
    from layout import PROB_BOTH_BOTTOM, PROB_BOTH_TOP, PROB_ONLY_LEFT_BOTTOM, PROB_ONLY_LEFT_TOP
    yFactor.setProbability({Y_POS_VAR: BOTH_BOTTOM_VAL}, PROB_BOTH_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: BOTH_TOP_VAL}, PROB_BOTH_TOP)
    yFactor.setProbability({Y_POS_VAR: LEFT_BOTTOM_VAL}, PROB_ONLY_LEFT_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: LEFT_TOP_VAL}, PROB_ONLY_LEFT_TOP)
    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 22
0
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """

    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    "*** YOUR CODE HERE ***"
    #Y position has four value = BOTH_TOP, BOTH_BOTTOM, LEFT_TOP and LEFT_BOTTOM.
    #we get each value's probability and setProbability()
    #from layout import PROB_BOTH_TOP, PROB_BOTH_BOTTOM, PROB_ONLY_LEFT_TOP,PROB_ONLY_LEFT_BOTTOM
    yFactor.setProbability({Y_POS_VAR: BOTH_TOP_VAL}, PROB_BOTH_TOP)
    yFactor.setProbability({Y_POS_VAR: BOTH_BOTTOM_VAL}, PROB_BOTH_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: LEFT_TOP_VAL}, PROB_ONLY_LEFT_TOP)
    yFactor.setProbability({Y_POS_VAR: LEFT_BOTTOM_VAL}, PROB_ONLY_LEFT_BOTTOM)
    #util.raiseNotDefined()
    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 23
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses()

    "*** YOUR CODE HERE ***"
    for house_location in gameState.getPossibleHouses():
        for position in gameState.getHouseWalls(house_location):
            var = OBS_VAR_TEMPLATE % position
            factor = bn.Factor([var], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], bayesNet.variableDomainsDict())
            cpt = factor.getAllPossibleAssignmentDicts()
            for c in cpt:
                ghost_house, food_house, color = c[GHOST_HOUSE_VAR], c[FOOD_HOUSE_VAR], c[var]
                pos = get_closest_pos(gameState, position)
                prob = get_prob(pos, ghost_house, food_house, color)
                factor.setProbability(c, prob)
            bayesNet.setCPT(var, factor)
Esempio n. 24
0
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """
    "*** YOUR CODE HERE ***"
    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    positions = []

    for pos in Y_POS_VALS:
        positions.append({Y_POS_VAR: pos})

    x = 0

    for prob in PROB_VALS:
        yFactor.setProbability(positions[x], prob)
        x = x + 1

    bayesNet.setCPT(Y_POS_VAR, yFactor)
def fillYCPT(bayesNet, gameState):
    """
    Question 2a: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """

    #we just learn from the fillXCPT function
    #the fact that x and y are opposite to each other give us convienent to this question
    #also in layout, there's no probability of ghost_left, which means we can only use food_left

    #we make the correct import for all the probability for possible y value
    from layout import PROB_BOTH_TOP, PROB_BOTH_BOTTOM, PROB_ONLY_LEFT_BOTTOM, PROB_ONLY_LEFT_TOP

    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    "*** YOUR CODE HERE ***"
    yFactor.setProbability({Y_POS_VAR: BOTH_TOP_VAL}, PROB_BOTH_TOP)
    yFactor.setProbability({Y_POS_VAR: BOTH_BOTTOM_VAL}, PROB_BOTH_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: LEFT_TOP_VAL}, PROB_ONLY_LEFT_TOP)
    yFactor.setProbability({Y_POS_VAR: LEFT_BOTTOM_VAL}, PROB_ONLY_LEFT_BOTTOM)
    """
        here is what value can y-pos-var can take
        
        Y_POS_VAR = "yPos"
        BOTH_TOP_VAL = "bothTop"
        BOTH_BOTTOM_VAL = "bothBottom"
        LEFT_TOP_VAL = "leftTop"
        LEFT_BOTTOM_VAL = "leftBottom"
        Y_POS_VALS = [BOTH_TOP_VAL, BOTH_BOTTOM_VAL, LEFT_TOP_VAL, LEFT_BOTTOM_VAL]
    """

    #util.raiseNotDefined()
    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 26
0
def fillYCPT(bayesNet, gameState):
    """
    Question 2: Bayes net probabilities

    Fill the CPT that gives the prior probability over the y position variable.
    See the definition of `fillXCPT` above for an example of how to do this.
    You can use the PROB_* constants imported from layout rather than writing
    probabilities down by hand.
    """
    from layout import PROB_FOOD_LEFT 



    yFactor = bn.Factor([Y_POS_VAR], [], bayesNet.variableDomainsDict())
    "*** YOUR CODE HERE ***"
    #autograder gives insight into what variables to include when setting probabiltiies
    yFactor.setProbability({Y_POS_VAR: BOTH_TOP_VAL}, PROB_BOTH_TOP)
    yFactor.setProbability({Y_POS_VAR: BOTH_BOTTOM_VAL}, PROB_BOTH_BOTTOM )
    yFactor.setProbability({Y_POS_VAR: LEFT_BOTTOM_VAL}, PROB_ONLY_LEFT_BOTTOM)
    yFactor.setProbability({Y_POS_VAR: LEFT_TOP_VAL}, PROB_ONLY_LEFT_TOP)


    "*** END YOUR CODE HERE ***"
    bayesNet.setCPT(Y_POS_VAR, yFactor)
Esempio n. 27
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses(
    )

    "*** YOUR CODE HERE ***"

    def getHouseValCorrespToPos(x, y):
        width = gameState.data.layout.width
        height = gameState.data.layout.height
        top = y > height / 2.0
        right = x > width / 2.0

        if top and right:
            return TOP_RIGHT_VAL
        if (not top) and right:
            return BOTTOM_RIGHT_VAL
        if top and (not right):
            return TOP_LEFT_VAL
        return BOTTOM_LEFT_VAL

    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsFactor = bn.Factor([OBS_VAR_TEMPLATE % obsPos],
                                  [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR],
                                  bayesNet.variableDomainsDict())
            for assignment in obsFactor.getAllPossibleAssignmentDicts():
                probRed = 0
                probBlue = 0
                probNone = 0

                food = assignment[FOOD_HOUSE_VAR] == getHouseValCorrespToPos(
                    obsPos[0], obsPos[1])
                ghost = assignment[GHOST_HOUSE_VAR] == getHouseValCorrespToPos(
                    obsPos[0], obsPos[1])
                blue = assignment[OBS_VAR_TEMPLATE % obsPos] == BLUE_OBS_VAL
                red = assignment[OBS_VAR_TEMPLATE % obsPos] == RED_OBS_VAL

                if blue:
                    if food:
                        obsFactor.setProbability(assignment, 1 - PROB_FOOD_RED)
                    elif ghost:
                        obsFactor.setProbability(assignment,
                                                 1 - PROB_GHOST_RED)
                elif red:
                    if food:
                        obsFactor.setProbability(assignment, PROB_FOOD_RED)
                    elif ghost:
                        obsFactor.setProbability(assignment, PROB_GHOST_RED)
                elif food or ghost:
                    obsFactor.setProbability(assignment, 0)
                else:
                    obsFactor.setProbability(assignment, 1)

            bayesNet.setCPT(OBS_VAR_TEMPLATE % obsPos, obsFactor)
Esempio n. 28
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses(
    )

    "*** YOUR CODE HERE ***"
    for housePos in gameState.getPossibleHouses():
        houseDict = {
            bottomLeftPos: BOTTOM_LEFT_VAL,
            topLeftPos: TOP_LEFT_VAL,
            bottomRightPos: BOTTOM_RIGHT_VAL,
            topRightPos: TOP_RIGHT_VAL
        }
        for obsPos in gameState.getHouseWalls(housePos):
            tmpFactor = bn.Factor([OBS_VAR_TEMPLATE % obsPos],
                                  [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR],
                                  bayesNet.variableDomainsDict())

            for foodPos in HOUSE_VALS:
                for ghostPos in HOUSE_VALS:
                    prob = {}
                    prob[FOOD_HOUSE_VAR] = foodPos
                    prob[GHOST_HOUSE_VAR] = ghostPos
                    if foodPos != houseDict[
                            housePos] and ghostPos != houseDict[housePos]:
                        prob[OBS_VAR_TEMPLATE % obsPos] = NO_OBS_VAL
                        tmpFactor.setProbability(prob, 1.)
                    elif houseDict[housePos] == foodPos:
                        prob[OBS_VAR_TEMPLATE % obsPos] = RED_OBS_VAL
                        tmpFactor.setProbability(prob, PROB_FOOD_RED)
                        prob[OBS_VAR_TEMPLATE % obsPos] = BLUE_OBS_VAL
                        tmpFactor.setProbability(prob, 1. - PROB_FOOD_RED)
                    else:
                        prob[OBS_VAR_TEMPLATE % obsPos] = RED_OBS_VAL
                        tmpFactor.setProbability(prob, PROB_GHOST_RED)
                        prob[OBS_VAR_TEMPLATE % obsPos] = BLUE_OBS_VAL
                        tmpFactor.setProbability(prob, 1. - PROB_GHOST_RED)

            bayesNet.setCPT(OBS_VAR_TEMPLATE % obsPos, tmpFactor)
Esempio n. 29
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """
    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses(
    )

    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVar = OBS_VAR_TEMPLATE % obsPos

            factor = bn.Factor([obsVar], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR],
                               bayesNet.variableDomainsDict())

            for row in factor.getAllPossibleAssignmentDicts():
                foodHouse = row[FOOD_HOUSE_VAR]
                ghostHouse = row[GHOST_HOUSE_VAR]
                wallColor = row[obsVar]

                if getHouseVal(obsPos[0], obsPos[1],
                               gameState) == row[FOOD_HOUSE_VAR]:
                    redWall = PROB_FOOD_RED
                    blueWall = 1 - redWall
                    noWall = 1 - redWall - blueWall

                elif getHouseVal(obsPos[0], obsPos[1],
                                 gameState) == row[GHOST_HOUSE_VAR]:
                    redWall = PROB_GHOST_RED
                    blueWall = 1 - redWall
                    noWall = 1 - redWall - blueWall

                else:
                    redWall = 0
                    blueWall = 0
                    noWall = 1

                if row[obsVar] == RED_OBS_VAL:
                    factor.setProbability(row, redWall)
                elif row[obsVar] == BLUE_OBS_VAL:
                    factor.setProbability(row, blueWall)
                elif row[obsVar] == NO_OBS_VAL:
                    factor.setProbability(row, noWall)

            bayesNet.setCPT(obsVar, factor)
Esempio n. 30
0
def fillObsCPT(bayesNet, gameState):
    """
    Question 2b: Bayes net probabilities

    Fill the CPT that gives the probability of an observation in each square,
    given the locations of the food and ghost houses. Refer to the project
    description for what this probability table looks like. You can use
    PROB_FOOD_RED and PROB_GHOST_RED from the top of the file.

    You will need to create a new factor for *each* of 4*7 = 28 observation
    variables. Don't forget to call bayesNet.setCPT for each factor you create.

    The XXXPos variables at the beginning of this method contain the (x, y)
    coordinates of each possible house location.

    IMPORTANT:
    Because of the particular choice of probabilities higher up in the Bayes
    net, it will never be the case that the ghost house and the food house are
    in the same place. However, the CPT for observations must still include a
    vaild probability distribution for this case. To conform with the
    autograder, use the *food house distribution* over colors when both the food
    house and ghost house are assigned to the same cell.
    """
    def getAdjHouse(bottomLeftPos, topLeftPos, bottomRightPos, topRightPos,
                    obsPos):
        dist = [
            util.manhattanDistance(obsPos, pos) for pos in
            [bottomLeftPos, topLeftPos, bottomRightPos, topRightPos]
        ]
        index = dist.index(min(dist))
        if index == 0:
            return BOTTOM_LEFT_VAL
        elif index == 1:
            return TOP_LEFT_VAL
        elif index == 2:
            return BOTTOM_RIGHT_VAL
        else:
            return TOP_RIGHT_VAL

    bottomLeftPos, topLeftPos, bottomRightPos, topRightPos = gameState.getPossibleHouses(
    )
    for housePos in gameState.getPossibleHouses():
        for obsPos in gameState.getHouseWalls(housePos):
            obsVar = OBS_VAR_TEMPLATE % obsPos
            obsFactor = bn.Factor([obsVar], [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR],
                                  bayesNet.variableDomainsDict())
            obsCPT = obsFactor.getAllPossibleAssignmentDicts()
            ajacentHouse = getAdjHouse(bottomLeftPos, topLeftPos,
                                       bottomRightPos, topRightPos, obsPos)
            for assignment in obsCPT:
                prob = 0
                if (assignment[GHOST_HOUSE_VAR] != ajacentHouse
                        and assignment[FOOD_HOUSE_VAR] != ajacentHouse):
                    if assignment[obsVar] == RED_OBS_VAL:
                        prob = 0
                    elif assignment[obsVar] == BLUE_OBS_VAL:
                        prob = 0
                    elif assignment[obsVar] == NO_OBS_VAL:
                        prob = 1
                elif assignment[GHOST_HOUSE_VAR] == assignment[
                        FOOD_HOUSE_VAR] or assignment[
                            FOOD_HOUSE_VAR] == ajacentHouse:
                    if assignment[obsVar] == RED_OBS_VAL:
                        prob = PROB_FOOD_RED
                    elif assignment[obsVar] == BLUE_OBS_VAL:
                        prob = 1 - PROB_FOOD_RED
                    elif assignment[obsVar] == NO_OBS_VAL:
                        prob = 0
                elif assignment[GHOST_HOUSE_VAR] == ajacentHouse:
                    if assignment[obsVar] == RED_OBS_VAL:
                        prob = PROB_GHOST_RED
                    elif assignment[obsVar] == BLUE_OBS_VAL:
                        prob = 1 - PROB_GHOST_RED
                    elif assignment[obsVar] == NO_OBS_VAL:
                        prob = 0
                obsFactor.setProbability(assignment, prob)
            bayesNet.setCPT(obsVar, obsFactor)