def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """
        "*** YOUR CODE HERE ***"
        from inference import inferenceByVariableElimination
        query_factor = inferenceByVariableElimination(self.bayesNet, HOUSE_VARS, evidence, eliminationOrder)
        p_food_left, p_food_right = 0, 0
        for assignment in query_factor.getAllPossibleAssignmentDicts():
            if assignment[FOOD_HOUSE_VAR] == TOP_LEFT_VAL and assignment[GHOST_HOUSE_VAR] == TOP_RIGHT_VAL:
                p_food_left = query_factor.getProbability(assignment)
            elif assignment[FOOD_HOUSE_VAR] == TOP_RIGHT_VAL and assignment[GHOST_HOUSE_VAR] == TOP_LEFT_VAL:
                p_food_right = query_factor.getProbability(assignment)

        leftExpectedValue = p_food_left * WON_GAME_REWARD + (1  - p_food_left) * GHOST_COLLISION_REWARD
        rightExpectedValue = p_food_right * WON_GAME_REWARD + (1 - p_food_right) * GHOST_COLLISION_REWARD

        return leftExpectedValue, rightExpectedValue
예제 #2
0
파일: bayesAgents.py 프로젝트: JunTan/DVC
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        factorCPT = inference.inferenceByVariableElimination(self.bayesNet, [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], evidence, eliminationOrder)
        foodTopLeft = {}
        foodTopLeft.update(evidence)
        foodTopLeft[FOOD_HOUSE_VAR] = TOP_LEFT_VAL
        foodTopLeft[GHOST_HOUSE_VAR] = TOP_RIGHT_VAL

        foodTopRight = {}
        foodTopRight.update(evidence)
        foodTopRight[FOOD_HOUSE_VAR] = TOP_RIGHT_VAL
        foodTopRight[GHOST_HOUSE_VAR] = TOP_LEFT_VAL
        
        leftExpectedValue = factorCPT.getProbability(foodTopLeft) * WON_GAME_REWARD \
                            + factorCPT.getProbability(foodTopRight) * GHOST_COLLISION_REWARD
        rightExpectedValue = factorCPT.getProbability(foodTopLeft) * GHOST_COLLISION_REWARD \
                             + factorCPT.getProbability(foodTopRight) * WON_GAME_REWARD
        return leftExpectedValue, rightExpectedValue
예제 #3
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    # print "@@@@@evidence ", evidence
    # print "@@@@@eliminationOrder ", eliminationOrder
    # print "@@@@@bayesNet ", bayesNet
    factor = inference.inferenceByVariableElimination(bayesNet, ['foodHouse', 'ghostHouse'], evidence, eliminationOrder)
    # print "@@@@factor ", factor
    highestProb = float('-inf')
    highestAssignDict = dict()
    for assignmentDict in factor.getAllPossibleAssignmentDicts():
        # print type(assignmentDict)
        probability = factor.getProbability(assignmentDict)
        if probability > highestProb:
            highestProb = probability
            highestAssignDict = assignmentDict
    return highestAssignDict
예제 #4
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        table = inference.inferenceByVariableElimination(self.bayesNet, 
            [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], evidence, eliminationOrder)

        left = evidence.copy()
        left.update({FOOD_HOUSE_VAR: TOP_LEFT_VAL, GHOST_HOUSE_VAR: TOP_RIGHT_VAL})

        right = evidence.copy()
        right.update({GHOST_HOUSE_VAR: TOP_LEFT_VAL, FOOD_HOUSE_VAR: TOP_RIGHT_VAL})

        prob_left = (table.getProbability(left) * WON_GAME_REWARD) + (table.getProbability(right) * GHOST_COLLISION_REWARD)
        prob_right = (table.getProbability(right) * WON_GAME_REWARD) + (table.getProbability(left) * GHOST_COLLISION_REWARD)

        return prob_left, prob_right
예제 #5
0
    def getExplorationProbsAndOutcomes(self, evidence):
        unknownVars = [o for o in self.obsVars if o not in evidence]
        assert len(unknownVars) == 7
        assert len(set(evidence.keys()) & set(unknownVars)) == 0
        firstUnk = unknownVars[0]
        restUnk = unknownVars[1:]

        unknownVars = [o for o in self.obsVars if o not in evidence]
        eliminationOrder = unknownVars + [X_POS_VAR, Y_POS_VAR]
        houseMarginals = inference.inferenceByVariableElimination(self.bayesNet,
                [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], evidence, eliminationOrder)

        probs = [0 for i in range(8)]
        outcomes = []
        for nRed in range(8):
            outcomeVals = [RED_OBS_VAL] * nRed + [BLUE_OBS_VAL] * (7 - nRed)
            outcomeEvidence = dict(zip(unknownVars, outcomeVals))
            outcomeEvidence.update(evidence)
            outcomes.append(outcomeEvidence)

        for foodHouseVal, ghostHouseVal in [(TOP_LEFT_VAL, TOP_RIGHT_VAL),
                (TOP_RIGHT_VAL, TOP_LEFT_VAL)]:

            condEvidence = dict(evidence)
            condEvidence.update({FOOD_HOUSE_VAR: foodHouseVal, 
                GHOST_HOUSE_VAR: ghostHouseVal})
            assignmentProb = houseMarginals.getProbability(condEvidence)

            oneObsMarginal = inference.inferenceByVariableElimination(self.bayesNet,
                    [firstUnk], condEvidence, restUnk + [X_POS_VAR, Y_POS_VAR])

            assignment = oneObsMarginal.getAllPossibleAssignmentDicts()[0]
            assignment[firstUnk] = RED_OBS_VAL
            redProb = oneObsMarginal.getProbability(assignment)

            for nRed in range(8):
                outcomeProb = combinations(7, nRed) * \
                        redProb ** nRed * (1 - redProb) ** (7 - nRed)
                outcomeProb *= assignmentProb
                probs[nRed] += outcomeProb

        return list(zip(probs, outcomes))
예제 #6
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    """
    "*** YOUR CODE HERE ***"
    food_position = (0, 0)
    # call variable eliminataion method
    probabilistic_query = inference.inferenceByVariableElimination(
        bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
    # loop through to find the most probably location of the food house
    for each in probabilistic_query.getAllPossibleAssignmentDicts():
        p = probabilistic_query.getProbability(each)
        if p > food_position[0]:
            food_position = (p, each)
    return food_position[1]
예제 #7
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    factor = inference.inferenceByVariableElimination(
        bayesNet, ['foodHouse', 'ghostHouse'], evidence, eliminationOrder)
    maxProb = float('-inf')
    bestPos = dict()
    for assignment in factor.getAllPossibleAssignmentDicts():
        prob = factor.getProbability(assignment)
        if prob > maxProb:
            maxProb = prob
            bestPos = assignment
    return bestPos
예제 #8
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman
    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.
    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    result = inference.inferenceByVariableElimination(bayesNet,
                                                      set('foodHouse'),
                                                      evidence,
                                                      eliminationOrder)
    prob = 0
    for assignmentDict in result.getAllPossibleAssignmentDicts():
        if prob < result.getProbability(assignmentDict):
            prob = result.getProbability(assignmentDict)
            place = assignmentDict

    return place
예제 #9
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    joinedfactor = inference.inferenceByVariableElimination(
        bayesNet, [FOOD_HOUSE_VAR], evidence, eliminationOrder)
    maxprob = float("-inf")
    maxlocation = {FOOD_HOUSE_VAR: FOOD_LEFT_VAL}
    for assignment in joinedfactor.getAllPossibleAssignmentDicts():
        if maxprob < joinedfactor.getProbability(assignment):
            maxprob = joinedfactor.getProbability(assignment)
            maxlocation = {FOOD_HOUSE_VAR: assignment[FOOD_HOUSE_VAR]}
    return maxlocation
예제 #10
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    res = inference.inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR,
                                                   evidence, eliminationOrder)
    m = 0
    for assignment in res.getAllPossibleAssignmentDicts():
        if res.getProbability(assignment) > m:
            m = res.getProbability(assignment)
            pos = assignment[FOOD_HOUSE_VAR]
    return {FOOD_HOUSE_VAR: pos}
    "*** END YOUR CODE HERE ***"
예제 #11
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"

        CPT = inference.inferenceByVariableElimination(
            self.bayesNet, [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR], evidence,
            eliminationOrder)

        def updater(d, x, y):
            d.update(evidence)
            d[FOOD_HOUSE_VAR] = x
            d[GHOST_HOUSE_VAR] = y
            return d

        topLeft = updater({}, TOP_LEFT_VAL, TOP_RIGHT_VAL)
        topRight = updater({}, TOP_RIGHT_VAL, TOP_LEFT_VAL)

        bb = lambda x, y: CPT.getProbability(topLeft) * x + CPT.getProbability(
            topRight) * y

        leftExpectedValue = bb(WON_GAME_REWARD, GHOST_COLLISION_REWARD)
        rightExpectedValue = bb(GHOST_COLLISION_REWARD, WON_GAME_REWARD)

        return leftExpectedValue, rightExpectedValue
예제 #12
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    f = inference.inferenceByVariableElimination(bayesNet, [FOOD_HOUSE_VAR], evidence, eliminationOrder)
    prob = 0
    a_max = ''
    for a in f.getAllPossibleAssignmentDicts():
        proba = f.getProbability(a)
        if proba > prob:
            prob = proba
            a_max = a
    return a_max
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    factor = inference.inferenceByVariableElimination(bayesNet, [FOOD_HOUSE_VAR], evidence, eliminationOrder)

    loc = ""
    m = 0
    for a in factor.getAllPossibleAssignmentDicts():
        v = factor.getProbability(a)
        if v > m:
            loc = a[FOOD_HOUSE_VAR]
            m = v
    return {FOOD_HOUSE_VAR: loc}
예제 #14
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    factor = inference.inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
    highestProb = float('-inf')
    highestAssign = dict()
    for assignDict in factor.getAllPossibleAssignmentDicts():
        probability = factor.getProbability(assignDict)
        if highestProb< probability:
            highestProb = probability
            highestAssign = assignDict
    return highestAssign
예제 #15
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    factor=inference.inferenceByVariableElimination(bayesNet,HOUSE_VARS,evidence,eliminationOrder)
    temp=0
    best=dict()
    for assignment in factor.getAllPossibleAssignmentDicts():
        if factor.getProbability(assignment)>temp:
            temp=factor.getProbability(assignment)
            best=assignment
    #print best
    return best
예제 #16
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        factor = inference.inferenceByVariableElimination(
            self.bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
        pFoodLeft = 0
        pFoodRight = 0

        for assignment in factor.getAllPossibleAssignmentDicts():
            if assignment[FOOD_HOUSE_VAR] == TOP_LEFT_VAL and assignment[
                    GHOST_HOUSE_VAR] == TOP_RIGHT_VAL:
                pFoodLeft = factor.getProbability(assignment)
            elif assignment[GHOST_HOUSE_VAR] == TOP_LEFT_VAL and assignment[
                    FOOD_HOUSE_VAR] == TOP_RIGHT_VAL:
                pFoodRight = factor.getProbability(assignment)

        pGhostRight = 1 - pFoodLeft
        pGhostLeft = 1 - pFoodRight

        # Utilities
        leftExpectedValue = pFoodLeft * WON_GAME_REWARD + pGhostRight * GHOST_COLLISION_REWARD
        rightExpectedValue = pFoodRight * WON_GAME_REWARD + pGhostLeft * GHOST_COLLISION_REWARD

        return leftExpectedValue, rightExpectedValue
예제 #17
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        from inference import inferenceByVariableElimination
        left = 0
        right = 0
        likely = inferenceByVariableElimination(self.bayesNet, HOUSE_VARS,
                                                evidence, eliminationOrder)

        for x in likely.getAllPossibleAssignmentDicts():
            if x[FOOD_HOUSE_VAR] == TOP_RIGHT_VAL and x[
                    GHOST_HOUSE_VAR] == TOP_LEFT_VAL:
                right = likely.getProbability(x)
            elif x[GHOST_HOUSE_VAR] == TOP_RIGHT_VAL and x[
                    FOOD_HOUSE_VAR] == TOP_LEFT_VAL:
                left = likely.getProbability(x)

        left_ = (left * WON_GAME_REWARD) + (
            (1 - left) * GHOST_COLLISION_REWARD)
        right_ = (right * WON_GAME_REWARD) + (
            (1 - right) * GHOST_COLLISION_REWARD)

        return left_, right_
예제 #18
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        factor = inference.inferenceByVariableElimination(
            self.bayesNet, [HOUSE_VARS], evidence, eliminationOrder)

        topRight = evidence.copy()
        topRight[FOOD_HOUSE_VAR] = TOP_RIGHT_VAL
        topRight[GHOST_HOUSE_VAR] = TOP_LEFT_VAL

        topLeft = evidence.copy()
        topLeft[FOOD_HOUSE_VAR] = TOP_LEFT_VAL
        topLeft[GHOST_HOUSE_VAR] = TOP_RIGHT_VAL

        leftReward1 = factor.getProbability(topLeft) * WON_GAME_REWARD
        rightReward1 = factor.getProbability(topRight) * GHOST_COLLISION_REWARD
        leftExpectedValue = leftReward1 + rightReward1

        leftReward2 = factor.getProbability(topLeft) * GHOST_COLLISION_REWARD
        rightReward2 = factor.getProbability(topRight) * WON_GAME_REWARD
        rightExpectedValue = leftReward2 + rightReward2

        return leftExpectedValue, rightExpectedValue
예제 #19
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        #util.raiseNotDefined()
        from inference import inferenceByVariableElimination
        queryFactor = inferenceByVariableElimination(self.bayesNet, HOUSE_VARS,
                                                     evidence,
                                                     eliminationOrder)
        leftPFood, rightPFood = 0, 0
        for assignment in queryFactor.getAllPossibleAssignmentDicts():
            if assignment[FOOD_HOUSE_VAR] == TOP_LEFT_VAL and assignment[
                    GHOST_HOUSE_VAR] == TOP_RIGHT_VAL:
                leftPFood = queryFactor.getProbability(assignment)
            elif assignment[FOOD_HOUSE_VAR] == TOP_RIGHT_VAL and assignment[
                    GHOST_HOUSE_VAR] == TOP_LEFT_VAL:
                rightPFood = queryFactor.getProbability(assignment)

        leftExpectedValue = leftPFood * WON_GAME_REWARD + (
            1 - leftPFood) * GHOST_COLLISION_REWARD
        rightExpectedValue = rightPFood * WON_GAME_REWARD + (
            1 - rightPFood) * GHOST_COLLISION_REWARD

        return leftExpectedValue, rightExpectedValue
예제 #20
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        queryVariables = [GHOST_HOUSE_VAR, FOOD_HOUSE_VAR]
        factor = inference.inferenceByVariableElimination(
            self.bayesNet, queryVariables, evidence, eliminationOrder)
        assignmentDict = {}
        assignmentDict.update(evidence)

        assignmentDict[FOOD_HOUSE_VAR] = TOP_RIGHT_VAL
        assignmentDict[GHOST_HOUSE_VAR] = TOP_LEFT_VAL

        rightWon = factor.getProbability(assignmentDict)
        leftGhost = rightWon

        assignmentDict[FOOD_HOUSE_VAR] = TOP_LEFT_VAL
        assignmentDict[GHOST_HOUSE_VAR] = TOP_RIGHT_VAL
        rightGhost = factor.getProbability(assignmentDict)
        leftWon = rightGhost

        rightExpectedValue = rightWon * WON_GAME_REWARD + rightGhost * GHOST_COLLISION_REWARD
        leftExpectedValue = leftWon * WON_GAME_REWARD + leftGhost * GHOST_COLLISION_REWARD
        return leftExpectedValue, rightExpectedValue
예제 #21
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        factor = inference.inferenceByVariableElimination(
            self.bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
        #print factor.getAllPossibleAssignmentDicts()

        l_r_assignment_dic = {
            FOOD_HOUSE_VAR: TOP_LEFT_VAL,
            GHOST_HOUSE_VAR: TOP_RIGHT_VAL
        }
        r_l_assignment_dic = {
            FOOD_HOUSE_VAR: TOP_RIGHT_VAL,
            GHOST_HOUSE_VAR: TOP_LEFT_VAL
        }
        l_r_assignment_dic.update(evidence)
        r_l_assignment_dic.update(evidence)

        left_right = factor.getProbability(l_r_assignment_dic)
        right_left = factor.getProbability(r_l_assignment_dic)
        leftExpectedValue = left_right * WON_GAME_REWARD + right_left * GHOST_COLLISION_REWARD
        rightExpectedValue = left_right * GHOST_COLLISION_REWARD + right_left * WON_GAME_REWARD
        return leftExpectedValue, rightExpectedValue
예제 #22
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """

    factor = inference.inferenceByVariableElimination(bayesNet, [FOOD_HOUSE_VAR], \
                                                evidence, eliminationOrder)
    bestProb = -float('inf')
    bestAssignment = None
    for assignment in factor.getAllPossibleAssignmentDicts():
        prob = factor.getProbability(assignment)
        if prob > bestProb:
            bestProb = prob
            bestAssignment = assignment
    return bestAssignment
예제 #23
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    eliminatedFactor = inference.inferenceByVariableElimination(
        bayesNet, 'FoodHouse', evidence, eliminationOrder)
    mostLikely = 0
    bestLocation = None
    for possible in eliminatedFactor.getAllPossibleAssignmentDicts():
        if eliminatedFactor.getProbability(possible) > mostLikely:
            mostLikely = eliminatedFactor.getProbability(possible)
            bestLocation = possible
    print(bestLocation)
    return bestLocation
예제 #24
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    # from inference import inferenceByVariableElimination
    pdf = inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
    most_probable = pdf.getAllPossibleAssignmentDicts()[0]
    max = float("-inf")
    for assignment in pdf.getAllPossibleAssignmentDicts():
        prob = pdf.getProbability(assignment)
        if prob > max:
            max = prob
            most_probable = assignment
    return most_probable
예제 #25
0
파일: bayesAgents.py 프로젝트: yibei/neu
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    maxProb = 0
    maxAssign = 0
    query = inference.inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR,
                                                     evidence,
                                                     eliminationOrder)
    for assignment in query.getAllPossibleAssignmentDicts():
        prob = query.getProbability(assignment)
        if prob > maxProb:
            maxProb = prob
            maxAssign = assignment
    return maxAssign
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    from inference import inferenceByVariableElimination
    query_factor = inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
    best_assignment = None
    prob = 0
    for assignment in query_factor.getAllPossibleAssignmentDicts():
        temp_prob = query_factor.getProbability(assignment)
        if temp_prob > prob:
            prob = temp_prob
            best_assignment = assignment
    return best_assignment
예제 #27
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE *** "
    from inference import inferenceByVariableElimination
    var = inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
    temp = None
    val = 0
    for i in var.getAllPossibleAssignmentDicts():
        x = var.getProbability(i)
        if x > val:
            val = x
            temp = i
    return temp
예제 #28
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman
    Changes to UC Berkeley CS188's base code written by Brendan Tang.

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    factor = inference.inferenceByVariableElimination(
        bayesNet, ['foodHouse', 'ghostHouse'], evidence, eliminationOrder)
    probability = float('-inf')
    d = dict()
    for assignmentDict in factor.getAllPossibleAssignmentDicts():
        p = factor.getProbability(assignmentDict)
        if p > probability:
            probability = p
            d = assignmentDict
    return d
예제 #29
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    #"*** YOUR CODE HERE ***"
    queryVariables = [FOOD_HOUSE_VAR]
    food_factor = inference.inferenceByVariableElimination(bayesNet, queryVariables, evidence, eliminationOrder)
    all_assignments = food_factor.getAllPossibleAssignmentDicts()
    best_pos = None
    best_prob = 0.0
    for assignment in all_assignments:
        cur_prob = food_factor.getProbability(assignment)
        if cur_prob > best_prob:
            best_pos = assignment[FOOD_HOUSE_VAR]
            best_prob = cur_prob
    return {FOOD_HOUSE_VAR: best_pos}
예제 #30
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        #Those should be the only rewards that you need - to get the expected value multiple the probability that you go to the FOOD_HOUSE by GAME_WON (this will depend on whether the house is left and you take the left action, etc) and probability of reaching GHOST_HOUSE by GHOST_REWARD.
        query_factor = inference.inferenceByVariableElimination(
            self.bayesNet, HOUSE_VARS, evidence, eliminationOrder)
        leftp, rightp = 0, 0
        for assignment in query_factor.getAllPossibleAssignmentDicts():
            if assignment[FOOD_HOUSE_VAR] == TOP_LEFT_VAL and assignment[
                    GHOST_HOUSE_VAR] == TOP_RIGHT_VAL:
                leftp = query_factor.getProbability(assignment)
            elif assignment[FOOD_HOUSE_VAR] == TOP_RIGHT_VAL and assignment[
                    GHOST_HOUSE_VAR] == TOP_LEFT_VAL:
                rightp = query_factor.getProbability(assignment)

        leftExpectedValue = leftp * WON_GAME_REWARD + (
            1 - leftp) * GHOST_COLLISION_REWARD
        rightExpectedValue = rightp * WON_GAME_REWARD + (
            1 - rightp) * GHOST_COLLISION_REWARD

        return leftExpectedValue, rightExpectedValue
예제 #31
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    houseMarginals = inference.inferenceByVariableElimination(bayesNet,
                                                              FOOD_HOUSE_VAR, evidence, eliminationOrder)
    best_assignment = None
    max_prob = 0
    for assignment in houseMarginals.getAllPossibleAssignmentDicts():
        cur_prob = houseMarginals.getProbability(assignment)
        if (cur_prob > max_prob):
            max_prob = cur_prob
            best_assignment = assignment

    return best_assignment
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    p = 0
    assign = 0
    query = inference.inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR,
                                                     evidence,
                                                     eliminationOrder)
    for i in query.getAllPossibleAssignmentDicts():
        current_p = query.getProbability(i)
        if current_p > p:
            p = current_p
            assign = i
    return assign
예제 #33
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    factor = inference.inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR,
                                                      evidence,
                                                      eliminationOrder)
    max_prob = 0
    location = None
    for d in factor.getAllPossibleAssignmentDicts():
        prob = factor.getProbability(d)
        if prob is not None and prob > max_prob:
            max_prob = prob
            location = d[FOOD_HOUSE_VAR]
    return {FOOD_HOUSE_VAR: location}
예제 #34
0
파일: bayesAgents.py 프로젝트: ak2411/CS188
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    houseDict = {houseVal: 0 for houseVal in HOUSE_VALS}
    prob = inference.inferenceByVariableElimination(bayesNet, FOOD_HOUSE_VAR,
                                                    evidence, eliminationOrder)
    for assignment in prob.getAllPossibleAssignmentDicts():
        houseDict[assignment[FOOD_HOUSE_VAR]] += prob.getProbability(
            assignment)
    # get key w/ max val in houseDict
    return {
        FOOD_HOUSE_VAR: max(houseDict.iterkeys(),
                            key=(lambda key: houseDict[key]))
    }
예제 #35
0
def getMostLikelyFoodHousePosition(evidence, bayesNet, eliminationOrder):
    """
    Question 7: Marginal inference for pacman

    Find the most probable position for the food house.
    First, call the variable elimination method you just implemented to obtain
    p(FoodHouse | everything else). Then, inspect the resulting probability
    distribution to find the most probable location of the food house. Return
    this.

    (This should be a very short method.)
    """
    "*** YOUR CODE HERE ***"
    # pretty straightforward
    foodHouseFactor = inference.inferenceByVariableElimination(
        bayesNet, FOOD_HOUSE_VAR, evidence, eliminationOrder)
    highestProb = -9999999999
    mostProbableHouseLocation = None
    for assignDicts in foodHouseFactor.getAllPossibleAssignmentDicts():
        probability = foodHouseFactor.getProbability(assignDicts)
        if probability > highestProb:
            highestProb = probability
            mostProbableHouseLocation = assignDicts
    return mostProbableHouseLocation
예제 #36
0
    def computeEnterValues(self, evidence, eliminationOrder):
        """
        Question 8a: Value of perfect information

        Given the evidence, compute the value of entering the left and right
        houses immediately. You can do this by obtaining the joint distribution
        over the food and ghost house positions using your inference procedure.
        The reward associated with entering each house is given in the *_REWARD
        variables at the top of the file.

        *Do not* take into account the "time elapsed" cost of traveling to each
        of the houses---this is calculated elsewhere in the code.
        """

        leftExpectedValue = 0
        rightExpectedValue = 0

        "*** YOUR CODE HERE ***"
        """
        X_POS_VAR = "xPos"
        FOOD_LEFT_VAL = "foodLeft"
        GHOST_LEFT_VAL = "ghostLeft"
        X_POS_VALS = [FOOD_LEFT_VAL, GHOST_LEFT_VAL]
        
        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]
        
        FOOD_HOUSE_VAR = "foodHouse"
        GHOST_HOUSE_VAR = "ghostHouse"
        HOUSE_VARS = [FOOD_HOUSE_VAR, GHOST_HOUSE_VAR]
        
        TOP_LEFT_VAL = "topLeft"
        TOP_RIGHT_VAL = "topRight"
        BOTTOM_LEFT_VAL = "bottomLeft"
        BOTTOM_RIGHT_VAL = "bottomRight"
        HOUSE_VALS = [TOP_LEFT_VAL, TOP_RIGHT_VAL, BOTTOM_LEFT_VAL, BOTTOM_RIGHT_VAL]
        
        OBS_VAR_TEMPLATE = "obs(%d,%d)"
        
        BLUE_OBS_VAL = "blue"
        RED_OBS_VAL = "red"
        NO_OBS_VAL = "none"
        OBS_VALS = [BLUE_OBS_VAL, RED_OBS_VAL, NO_OBS_VAL]
        
        ENTER_LEFT = 0
        ENTER_RIGHT = 1
        EXPLORE = 2
        
        First compute p(foodHouse = topLeft and ghostHouse = topRight | evidence) and 
        p(foodHouse = topRight and ghostHouse = topLeft | evidence). Then use these two 
        probabilities to compute expected values for rushing left and rushing right.
        
        GHOST_COLLISION_REWARD, WON_GAME_REWARD
        """
        leftDict = dict(evidence)
        leftDict[FOOD_HOUSE_VAR] = TOP_LEFT_VAL
        leftDict[GHOST_HOUSE_VAR] = TOP_RIGHT_VAL

        rightDict = dict(evidence)
        rightDict[FOOD_HOUSE_VAR] = TOP_RIGHT_VAL
        rightDict[GHOST_HOUSE_VAR] = TOP_LEFT_VAL

        factor = inference.inferenceByVariableElimination(
            self.bayesNet, HOUSE_VARS, evidence, eliminationOrder)

        leftFoodProb = factor.getProbability(leftDict)
        rightFoodProb = factor.getProbability(rightDict)
        leftExpectedValue = leftFoodProb * WON_GAME_REWARD + rightFoodProb * GHOST_COLLISION_REWARD
        rightExpectedValue = rightFoodProb * WON_GAME_REWARD + leftFoodProb * GHOST_COLLISION_REWARD
        return leftExpectedValue, rightExpectedValue