Exemplo n.º 1
0
def betterEvaluationFunction(currentGameState):
	"""
	Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
	evaluation function (question 5).

	DESCRIPTION: <write something here so we know what you did>
	"""
	"*** YOUR CODE HERE ***"
	newPos = currentGameState.getPacmanPosition()
	newFood = currentGameState.getFood()
	newGhostStates = currentGameState.getGhostStates()
	newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
	
	ghostDis = util.manhattanDistance(newPos, newGhostStates[0].getPosition())
	disFood = dict()
	foodDis = 0
	if (len(newFood.asList())!=0):
		for f in newFood.asList():
			disFood[f] = util.manhattanDistance(newPos, f)
		minFood = min(disFood, key=disFood.get)
		foodDis = disFood[minFood]
		
	return -0.05*foodDis-len(newFood.asList())-2**(2-ghostDis)
	#return currentGameState.getScore()
	util.raiseNotDefined()
Exemplo n.º 2
0
    def isGoalState(self, state):
        """
          state: Search state

        Returns True if and only if the state is a valid goal state
        """
        util.raiseNotDefined()
Exemplo n.º 3
0
 def predict(self, X):
     """
     X is a vector that we're supposed to make a prediction about.
     Semantically, a return value <0 means class -1 and a return
     value >=0 means class +1
     """
     util.raiseNotDefined()
Exemplo n.º 4
0
    def terminalTest(self, state):
        """
          state: Search state

        Returns True if and only if the state is a valid goal state.
        """
        util.raiseNotDefined()
Exemplo n.º 5
0
 def computeQValueFromValues(self, state, action):
     """
       Compute the Q-value of action in state from the
       value function stored in self.values.
     """
     "*** YOUR CODE HERE ***"
     util.raiseNotDefined()
def uniformCostSearch(problem):
    """Search the node of least total cost first."""
    "*** YOUR CODE HERE ***"
    heap = util.PriorityQueue()
    start = problem.getStartState()
    heap.push(start, 0)

    came_from = {}
    actions_cost = {}
    came_from[start] = None
    actions_cost[start] = 0
    
    while not heap.isEmpty():
        state = heap.pop()

        if problem.isGoalState(state):
            return recontruct_actions(came_from, start, state)

        for nextState, action, cost in problem.getSuccessors(state):
            newcost = actions_cost[state] + cost
            if nextState not in actions_cost or actions_cost[nextState] > newcost:
                actions_cost[nextState] = newcost
                priority = newcost
                heap.push(nextState, priority)
                came_from[nextState] = (state, action)

    return []
    
    util.raiseNotDefined()
def aStarSearch(problem, heuristic=nullHeuristic):
    """Search the node that has the lowest combined cost and heuristic first."""
    "*** YOUR CODE HERE ***"

    heap = util.PriorityQueue()
    start = problem.getStartState()
    heap.push(start, 0)

    came_from = {}
    actions_cost = {}
    came_from[start] = None
    actions_cost[start] = 0
        
    while not heap.isEmpty():
        state = heap.pop()

        if problem.isGoalState(state):
            return recontruct_actions(came_from, start, state)

        for nextState, action, cost in problem.getSuccessors(state):
            newcost = actions_cost[state] + cost
            if nextState not in actions_cost or actions_cost[nextState] > newcost:
                actions_cost[nextState] = newcost
                priority = newcost + heuristic(nextState, problem)
                heap.push(nextState, priority)
                came_from[nextState] = (state, action)

    return []
    util.raiseNotDefined()
Exemplo n.º 8
0
def aStarSearch(problem, heuristic=nullHeuristic):
    "Search the node that has the lowest combined cost and heuristic first."
    "*** YOUR CODE HERE ***"
    toBeExpanded = util.PriorityQueue()
    toBeExpanded.push((problem.getStartState(),[],0),0)
    
    alreadyExpanded = set()
    
    while not toBeExpanded.isEmpty():
        current = toBeExpanded.pop()
        
        currentState = current[0]
        currentMoves = current[1]
        currentCost = current[2]
        
        if(currentState in alreadyExpanded):
            continue
    
        alreadyExpanded.add(currentState)
        
        """if the state is goal state, then task finished"""
        if problem.isGoalState(currentState):
            return currentMoves
        
        successors = problem.getSuccessors(currentState)
        for successor, action, stepCost in successors:
            toBeExpanded.push((successor,currentMoves+[action],currentCost+stepCost),currentCost+stepCost+heuristic(successor,problem))
        
    return []
    
    util.raiseNotDefined()
Exemplo n.º 9
0
 def getFeatures(self, state, action):    
   """
     Returns a dict from features to counts
     Usually, the count will just be 1.0 for
     indicator functions.  
   """
   util.raiseNotDefined()
Exemplo n.º 10
0
 def getQValue(self, state, action):
   """
     Should return Q(state,action) = w * featureVector
     where * is the dotProduct operator
   """
   "*** YOUR CODE HERE ***"
   util.raiseNotDefined()
 def getBeliefDistribution(self):
   """
   Return the agent's current belief state, a distribution over
   ghost locations conditioned on all evidence and time passage.
   """
   "*** YOUR CODE HERE ***"
   util.raiseNotDefined()
Exemplo n.º 12
0
def uniformCostSearch(problem):
    "Search the node of least total cost first. "
    "*** YOUR CODE HERE ***"
    node = [problem.getStartState(),'',1,[]]
    frontier = util.PriorityQueue()
    frontier.push(node,0)
    explored = set()
    explored.add(node[0])
    found = False
    while not found:
        if frontier.isEmpty():
            return []
        node = frontier.pop()
        if problem.isGoalState(node[0]):
            found = True
            solution = node[3]
        explored.add(node[0])
        children = problem.getSuccessors(node[0])
        for child in children:
            if child[0] not in explored:#(explored or frontier[:][0]):
                current_path = list(node[3])
                current_path.append(child[1])
                child = list(child)
                child.append(current_path)
                #explored.add(child[0])
                frontier.push(child, problem.getCostOfActions(current_path))
            #elif len([True for item in frontier.heap if child[0] in item[1]])>0:
            #    print 'child: '+str(child)
            #    print 'frontier items:'+ frontier
    return solution

    util.raiseNotDefined()
Exemplo n.º 13
0
def breadthFirstSearch(problem):
  """
  Search the shallowest nodes in the search tree first.
  [2nd Edition: p 73, 3rd Edition: p 82]
  """
  "*** YOUR CODE HERE ***"
  util.raiseNotDefined()
Exemplo n.º 14
0
def breadthFirstSearch(problem):
    """
    Search the shallowest nodes in the search tree first.
    """

    from util import Queue
    from game import Directions
    
    actions = []
    
    frontier = Queue()
    frontier.push((problem.getStartState(), [], 0))
    visited = []

    while (frontier.isEmpty() == False):
        (currentS, currentP, currentC) = frontier.pop()
        if (problem.isGoalState(currentS) == True):
            actions = currentP
            break
        if (visited.count(currentS) == 0):
            visited.append(currentS)
            successors = problem.getSuccessors(currentS)
            for i in range(0,len(successors)):
                (neighbor, direction, cost) = successors[i]
                if (visited.count(neighbor) == 0):
                    frontier.push((neighbor, (currentP +[direction]), (currentC + cost)))
    print actions
    return actions

    util.raiseNotDefined()
Exemplo n.º 15
0
def aStarSearch(problem, heuristic=nullHeuristic):

    from util import PriorityQueue
    from game import Directions
    
    actions = []
    
    frontier = PriorityQueue()
    frontier.push((problem.getStartState(), [], 0), 0)
    visited = []

    while (frontier.isEmpty() == False):
        (currentS, currentP, currentC) = frontier.pop()
        if (problem.isGoalState(currentS) == True):
            actions = currentP
            break
        if (visited.count(currentS) == 0):
            visited.append(currentS)
            successors = problem.getSuccessors(currentS)
            for i in range(0,len(successors)):
                (neighbor, direction, cost) = successors[i]
                if (visited.count(neighbor) == 0):
                    frontier.push((neighbor, (currentP +[direction]), (currentC + cost)), (currentC + cost + heuristic(neighbor, problem)))
                
    return actions


    
    util.raiseNotDefined()
Exemplo n.º 16
0
 def isGoalState(self, state):
     
     
     isGoalState = False
     
     if(state == self.startingPosition):
         return False
     elif(type(state) is tuple and type(state[0][1]) is not int and len(state[0][1]) > 0):
         for aGoal in self.corners:
             if(state[0][1].__contains__(aGoal)):
                 isGoalState = True
             else:
                 isGoalState = False
                 return isGoalState
     elif(type(state) is tuple and len(state) == 2 and  type(state[1]) is tuple):
         for aGoal in self.corners:
             if(state[1].__contains__(aGoal)):
                 isGoalState = True
             else:
                 isGoalState = False
                 return isGoalState
     else:
         return False
     
     
     self.goalState = state
     return isGoalState
     util.raiseNotDefined()
Exemplo n.º 17
0
    def observe(self, observation, gameState):
        """
        Update beliefs based on the given distance observation. Make
        sure to handle the special case where all particles have weight
        0 after reweighting based on observation. If this happens,
        resample particles uniformly at random from the set of legal
        positions (self.legalPositions).

        A correct implementation will handle two special cases:
          1) When a ghost is captured by Pacman, **all** particles should be updated so
             that the ghost appears in its prison cell, self.getJailPosition()

             You can check if a ghost has been captured by Pacman by
             checking if it has a noisyDistance of None (a noisy distance
             of None will be returned if, and only if, the ghost is
             captured).

          2) When all particles receive 0 weight, they should be recreated from the
             prior distribution by calling initializeUniformly. The total weight
             for a belief distribution can be found by calling totalCount on
             a Counter object

        util.sample(Counter object) is a helper method to generate a sample from
        a belief distribution

        You may also want to use util.manhattanDistance to calculate the distance
        between a particle and pacman's position.
        """

        noisyDistance = observation
        emissionModel = busters.getObservationDistribution(noisyDistance)
        pacmanPosition = gameState.getPacmanPosition()
        "*** YOUR CODE HERE ***"
        util.raiseNotDefined()
Exemplo n.º 18
0
def breadthFirstSearch(problem):
  "Search the shallowest nodes in the search tree first. [p 81]"
  "*** YOUR CODE HERE ***"
  soln = []
  explr = []
  visit = []
  fringe = util.Queue()
  node = [None, problem.getStartState(), '', 0]
  fringe.push(node)

  while not fringe.isEmpty():    
    node = parent, state, dirctn, cost = fringe.pop()
    if problem.isGoalState(state):
      visit.append(node)
      soln.append(node[2])
      break
    if not (state in explr):
      for successor in problem.getSuccessors(state):
        fringe.push([state, successor[0], successor[1], successor[2]])
      visit.append(node)
      explr.append(state)

  parentNode = visit.pop()
  while len(visit) != 1:    
    curNode = visit.pop()
    while curNode[1] != parentNode[0]:
      curNode = visit.pop()
    if curNode[0] is None:
      break
    parentNode = curNode
    soln.append(curNode[2])

  return soln[::-1]
  util.raiseNotDefined()
Exemplo n.º 19
0
    def getSuccessors(self, state):
        """
        Returns successor states, the actions they require, and a cost of 1.

         As noted in search.py:
            For a given state, this should return a list of triples, (successor,
            action, stepCost), where 'successor' is a successor to the current
            state, 'action' is the action required to get there, and 'stepCost'
            is the incremental cost of expanding to that successor
        """

        successors = []
        for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
            # Add a successor state to the successor list if the action is legal
            # Here's a code snippet for figuring out whether a new position hits a wall:
            #   x,y = currentPosition
            #   dx, dy = Actions.directionToVector(action)
            #   nextx, nexty = int(x + dx), int(y + dy)
            #   hitsWall = self.walls[nextx][nexty]
            x,y = state[0]
            dx, dy = Actions.directionToVector(action)
            netx, nety = int(x + dx), int(y + dy)
            if not self.walls[netx][nety]:
                successor_state = (netx,nety)
                corner_yet_to_visit = list(state[1])
                if corner_yet_to_visit.__contains__(successor_state) == True:
                    corner_yet_to_visit.remove(successor_state)  
                successors.append( (((netx, nety),tuple(corner_yet_to_visit)), action, 1)) 


        self._expanded += 1 # DO NOT CHANGE

        return successors
        util.raiseNotDefined()
 def isGoalState(self, state):
     """
     Returns whether this search state is a goal state of the problem.
     """
     "*** YOUR CODE HERE ***"
     return False not in state[1:]
     util.raiseNotDefined()
Exemplo n.º 21
0
 def getGhostTupleDistributionGivenObservations(self, observations):
   """
   Compute the distribution over ghost tuples, given the evidence.
   
   Note that the observations are given as a dictionary.
   """
   util.raiseNotDefined()
Exemplo n.º 22
0
    def getLegalActions(self, state):
        """
          state: Search state

        For a given state should return list of legal action
        """
        util.raiseNotDefined()
Exemplo n.º 23
0
def aStarSearch(problem, heuristic=nullHeuristic):
    "Search the node that has the lowest combined cost and heuristic first."
    "*** YOUR CODE HERE ***"
    visited = set()
    frontier = util.PriorityQueueWithFunction(lambda x: x[pathCosts] +
        heuristic(x[coordinates], problem))
    currentState = ((problem.getStartState(), [], 0))
    frontier.push(currentState)

    while frontier.isEmpty() == False:
        currentState = frontier.pop()
            
        if problem.isGoalState(currentState[coordinates]):
           return currentState[actions]
        elif currentState[coordinates] in visited:
           continue

        # Here we have the same as before but this time, we have to 
        # add to the successor's path cost to our current value for the 
        # path's up to this point
        for successor in problem.getSuccessors(currentState[coordinates]):
           frontier.push((successor[coordinates], 
            currentState[actions] + [successor[actions]], 
                currentState[pathCosts] + successor[pathCosts]))

        visited.add(currentState[coordinates])
    util.raiseNotDefined()
Exemplo n.º 24
0
    def train(self, X, Y):
        '''
        just figure out what the most frequent class is for each value of X[:,0] and store it
        '''

        ### TODO: YOUR CODE HERE
        util.raiseNotDefined()
Exemplo n.º 25
0
    def predict(self, X):
        """
        check the first feature and make a classification decision based on it
        """

        ### TODO: YOUR CODE HERE
        util.raiseNotDefined()
Exemplo n.º 26
0
    def getAction(self, state):
        """
          Compute the action to take in the current state.  With
          probability self.epsilon, we should take a random action and
          take the best policy action otherwise.  Note that if there are
          no legal actions, which is the case at the terminal state, you
          should choose None as the action.

          HINT: You might want to use util.flipCoin(prob)
          HINT: To pick randomly from a list, use random.choice(list)
        """
        # Pick Action
        legalActions = self.getLegalActions(state)
        action = None
        "*** YOUR CODE HERE ***"

        #if terminal state return None
        if len(legalActions)==0:
            return None
        #check random true or false
        
        randomOrNot= util.flipCoin(self.epsilon)
        if  randomOrNot: 
            #Chose east, west, north, south? how do I get the list? 
            return   random.choice(legalActions)
          
        else: 
            #best policy action get policy or compute action from q values? 
            return self.computeActionFromQValues(state)
        
        util.raiseNotDefined()
Exemplo n.º 27
0
    def computeActionFromQValues(self, state):
        """
          Compute the best action to take in a state.  Note that if there
          are no legal actions, which is the case at the terminal state,
          you should return None.
        """
        "*** YOUR CODE HERE ***"

        #get all the legal actions
        legalActions = self.getLegalActions(state)
        valueActionPair= []
       # Return None0 if no legal action 
        if len(legalActions)==0:
            return None

        else: 

            #Find the action that returns has maximum qvalue
            for action in legalActions: 
                # record all the value and action pairs
                valueActionPair.append((self.getQValue(state, action), action))
                #get all the best actions if two or more have the best actions
                bestActions = []
                
                for valueAndAction in valueActionPair:
                    if valueAndAction == max(valueActionPair):
                        bestActions.append(valueAndAction)

                #choose one randomly from the bestAction
                bestActionList = random.choice(bestActions)

        return bestActionList[1]
        util.raiseNotDefined()
def depthFirstSearch(problem):
    """
    Search the deepest nodes in the search tree first.

    Your search algorithm needs to return a list of actions that reaches the
    goal. Make sure to implement a graph search algorithm.

    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:

    print "Start:", problem.getStartState()
    print "Is the start a goal?", problem.isGoalState(problem.getStartState())
    print "Start's successors:", problem.getSuccessors(problem.getStartState())
    """
    "*** YOUR CODE HERE ***"
    path = []
    cost = 0
    stack = util.Stack()
    visited = set()
    now = problem.getStartState()
    stack.push((now, path, cost))
    while not stack.isEmpty():
        now, path, cost = stack.pop()
        if now in visited:
            continue
        visited.add(now)
        if problem.isGoalState(now):
            return path
        for state, direction, c in problem.getSuccessors(now):
            stack.push((state, path+[direction], cost+c))
    util.raiseNotDefined()
Exemplo n.º 29
0
 def result(self, state, action):
     """
     Given a state and an action, returns resulting state and step cost, which is
     the incremental cost of moving to that successor.
     Returns (next_state, cost)
     """
     util.raiseNotDefined()
Exemplo n.º 30
0
    def chooseAction(self, gameState):
        #print self.ghostBeliefs.__str__()
        #return Directions.STOP
        pacmanPosition = gameState.getPacmanPosition()
        legal = [a for a in gameState.getLegalPacmanActions()]
        livingGhosts = gameState.getLivingGhosts()
        l = [beliefs for i,beliefs
             in enumerate(self.ghostBeliefs)
             if livingGhosts[i+1]]
        "*** YOUR CODE HERE ***"
        n = 99999
        array = util.Counter()
        for q in l:
            x = max(q.values())
            m = n
            w = "ahem THIS ahemING oh"
            for e in q:
                if q[e] == x:
                    w = e
            n = min(n, self.distancer.getDistance(w, pacmanPosition))
            if n != m:
                array[n] = w

        for action in legal:
            successorPosition = Actions.getSuccessor(pacmanPosition, action)
            if self.distancer.getDistance(successorPosition, array[min(array.keys())]) < self.distancer.getDistance(pacmanPosition, array[min(array.keys())]):
                return action

        util.raiseNotDefined()
Exemplo n.º 31
0
def breadthFirstSearch(problem):
    "Search the shallowest nodes in the search tree first. [p 81]"
    "*** YOUR CODE HERE ***"
    util.raiseNotDefined()
Exemplo n.º 32
0
    def solve(self,
              input_train_data,
              target_train_data,
              input_val_data,
              target_val_data,
              model,
              callback=None):
        """
        Question 6.c: Optimize the model and return the intermediate losses.

        Optimize the model using minibatch stochastic gradient descent by
        running the variable updates for self.iterations iterations.

        Args:
            input_train_data: a numpy.array with shape (N, R)
            target_train_data: a numpy.array with shape (N, S)
            input_val_data: a numpy.array with shape (M, R)
            target_val_data: a numpy.array with shape (M, S)
            model: the model from which the parameters are optimized

        Returns:
            A tuple of lists, where the first list contains the training loss of
            each iteration and the second list contains the validation loss of
            each iteration. The validation loss should be computed using the
            same amount of data as the training loss, but using the validation
            data.

        N and M are the numbers of training points, respectively, and R and S
        are the dimensions for each input and target data point, respectively.

        For minibatch stochastic gradient descent, you will need to iterate
        over the data in minibatches. As before, you must use
        MinibatchIndefinitelyGenerator to iterate over the data. You will
        need to instantiate two generators (one for the training data and
        another one for the validation data) and you should do it before the
        for loop. You should read the docstring of
        MinibatchIndefinitelyGenerator in tensorflow_util.py to figure out
        how to use it. Make sure to pass in self.batch_size and self.shuffle
        when you instantiate the generator.

        Useful member variables and methods:
        self.batch_size
        self.shuffle
        session.run(...)
        generator.next()
        """
        session = tfu.get_session()
        target_ph = tf.placeholder(tf.float32,
                                   shape=(None, ) +
                                   target_train_data.shape[1:])
        placeholders = [model.input_ph, target_ph]
        train_data = [input_train_data, target_train_data]
        val_data = [input_val_data, target_val_data]
        # You may want to initialize some variables that are shared across iterations
        "*** YOUR CODE HERE ***"
        loss_tensor = self.get_loss_tensor(
            model.prediction_tensor, target_ph,
            model.get_param_vars(regularizable=True))
        updates = self.get_updates(loss_tensor,
                                   model.get_param_vars(trainable=True))
        update_ops = [
            tf.assign(old_var, new_var_or_tensor)
            for (old_var, new_var_or_tensor) in updates
        ]
        train_losses = []
        val_losses = []
        for iter_ in range(self.iterations):
            "*** YOUR CODE HERE ***"
            util.raiseNotDefined()
            # train_loss should be the loss of this iteration using only the training data that was used for the updates
            # val_loss should be the loss of this iteration using the same amount of data used for the updates, but using the validation data instead
            train_losses.append(train_loss)
            val_losses.append(val_loss)

            if callback is not None: callback(model)
            self.display_progress(iter_, train_losses, val_losses)
        return train_losses, val_losses
 def getStartState(self):
     """
     Returns the start state for the search problem.
     """
     util.raiseNotDefined()
Exemplo n.º 34
0
def aStarSearch(problem, heuristic=nullHeuristic):
    """Search the node that has the lowest combined cost and heuristic first."""
    "*** YOUR CODE HERE ***"
    util.raiseNotDefined()
Exemplo n.º 35
0
def uniformCostSearch(problem):
    """Search the node of least total cost first."""
    "*** YOUR CODE HERE ***"
    util.raiseNotDefined()
Exemplo n.º 36
0
 def update(self, state, action, nextState, reward):
     """
        Should update your weights based on transition
     """
     "*** YOUR CODE HERE ***"
     util.raiseNotDefined()
Exemplo n.º 37
0
 def getStartState(self):
     "Returns the start state (in your state space, not the full Pacman state space)"
     "*** YOUR CODE HERE ***"
     return (self.startingPosition, self.cornersVisited)
     util.raiseNotDefined()
Exemplo n.º 38
0
 def getAgent(self, index):
     "Returns the agent for the provided index."
     util.raiseNotDefined()
Exemplo n.º 39
0
 def getStartState(self):
     "Returns the start state (in your state space, not the full Pacman state space)"
     "*** YOUR CODE HERE ***"
     return self.startState
     util.raiseNotDefined()
Exemplo n.º 40
0
def depthFirstSearch(problem):
    """
    Search the deepest nodes in the search tree first.

    Your search algorithm needs to return a list of actions that reaches the
    goal. Make sure to implement a graph search algorithm.

    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:

    print("Start:", problem.getStartState())
    print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
    print("Start's successors:", problem.getSuccessors(problem.getStartState()))
    """
    "*** YOUR CODE HERE ***"

    print("Start:", problem.getStartState())
    print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
    print("Start's successors:",
          problem.getSuccessors(problem.getStartState()))

    # Find the start position
    start = problem.getStartState()

    # Create a variable to hold the current state
    current = problem.getStartState()

    # Initalize a fringe using stack properties to store states
    fringe = util.Stack()

    # Check is the start state the goal state
    if problem.isGoalState(start) is True:
        return start

    # Add the start position to the visited list
    visited = []
    visited.append(start)

    # Format start state as a tuple to append to fringe
    fringe.push((start, []))

    # Traverse to next state in fringe while fringe is not empty
    # and current state is not goal state
    while not fringe.isEmpty() and not problem.isGoalState(current):
        # Take next location in the fringe
        # Pop the location tuple and action list
        location, action = fringe.pop()

        # Add location to visited list
        visited.append(location)

        # Add location's successors to fringe
        successors = problem.getSuccessors(location)

        for i in successors:
            if i[0] not in visited:
                # Update new current state
                current = i[0]

                direction = i[1]
                # If successors have not been visited before then add to fringe
                # Add direction to action list
                fringe.push((i[0], action + [direction]))

    return action + [direction]

    util.raiseNotDefined()
    def chooseAction(self, gameState):
        """
        First computes the most likely position of each ghost that has
        not yet been captured, then chooses an action that brings
        Pacman closer to the closest ghost (according to mazeDistance!).

        To find the mazeDistance between any two positions, use:
          self.distancer.getDistance(pos1, pos2)

        To find the successor position of a position after an action:
          successorPosition = Actions.getSuccessor(position, action)

        livingGhostPositionDistributions, defined below, is a list of
        util.Counter objects equal to the position belief
        distributions for each of the ghosts that are still alive.  It
        is defined based on (these are implementation details about
        which you need not be concerned):

          1) gameState.getLivingGhosts(), a list of booleans, one for each
             agent, indicating whether or not the agent is alive.  Note
             that pacman is always agent 0, so the ghosts are agents 1,
             onwards (just as before).

          2) self.ghostBeliefs, the list of belief distributions for each
             of the ghosts (including ghosts that are not alive).  The
             indices into this list should be 1 less than indices into the
             gameState.getLivingGhosts() list.
        """
        pacmanPosition = gameState.getPacmanPosition()
        legal = [a for a in gameState.getLegalPacmanActions()]
        livingGhosts = gameState.getLivingGhosts()
        livingGhostPositionDistributions = \
            [beliefs for i, beliefs in enumerate(self.ghostBeliefs)
             if livingGhosts[i+1]]
        "*** YOUR CODE HERE ***"
        # for each ghost, find the most likely location
        # calculate the distance to that location from pacman
        # keep the min distance/ghost and then move to this ghost

        #print pacmanPosition
        #print legal
        #print "---------"
        #print pacmanPosition
        closDist = float("inf")
        chosPos = []
        for ghostDistro in livingGhostPositionDistributions:
            maxProb = 0
            maxPos = []
            for pos, prob in ghostDistro.items():
                if prob > maxProb:
                    maxPos = pos
                    maxProb = prob
            # should I be using distance after an action?
            #print "Ghost loc", maxPos
            currentDist = self.distancer.getDistance(maxPos, pacmanPosition)
            if currentDist < closDist:
                closDist = currentDist
                chosPos = maxPos
        #print "closest ghost",chosPos
        minDist = float("inf")
        bestAction = []

        # find the best action
        for action in legal:
            newPos = Actions.getSuccessor(pacmanPosition, action)
            newDist = self.distancer.getDistance(newPos, chosPos)
            if minDist > newDist:
                minDist = newDist
                bestAction = action
        #print "best action", bestAction
        #print
        return bestAction
        util.raiseNotDefined()
Exemplo n.º 42
0
 def chooseAction(self, gameState):
     """
 Override this method to make a good agent. It should return a legal action within
 the time limit (otherwise a random legal action will be chosen for you).
 """
     util.raiseNotDefined()
Exemplo n.º 43
0
 def getAction(self, state):
     """
 The Agent will receive a GameState (from either {pacman, capture, sonar}.py) and
 must return an action from Directions.{North, South, East, West, Stop}
 """
     raiseNotDefined()
Exemplo n.º 44
0
 def getDistribution(self, state):
     "Returns a Counter encoding a distribution over actions from the provided state."
     util.raiseNotDefined()
Exemplo n.º 45
0
 def getQValue(self, state, action):
     """
 Should return Q(state,action)
 """
     util.raiseNotDefined()
Exemplo n.º 46
0
 def isGoalState(self, state):
     """
     Returns whether this search state is a goal state of the problem.
     """
     "*** YOUR CODE HERE ***"
     util.raiseNotDefined()
Exemplo n.º 47
0
    def getAction(self, gameState):
        """
          Returns the minimax action from the current gameState using self.depth
          and self.evaluationFunction.

          Here are some method calls that might be useful when implementing minimax.

          gameState.getLegalActions(agentIndex):
            Returns a list of legal actions for an agent
            agentIndex=0 means Pacman, ghosts are >= 1

          gameState.generateSuccessor(agentIndex, action):
            Returns the successor game state after an agent takes an action

          gameState.getNumAgents():
            Returns the total number of agents in the game
        """
        """ run pacman.py -p MinimaxAgent -l minimaxClassic -a depth=4 
            run autograder.py -q q2 --no-graphics """
        "[Project 3] YOUR CODE HERE"

        def max_agent(state, depth):
            if state.isWin() or state.isLose():
                return state.getScore()
            best_score = float("-inf")
            score = best_score
            best_action = Directions.STOP
            for action in state.getLegalActions(0):
                score = exp_agent(state.generateSuccessor(0, action), depth, 1)
                if score > best_score:
                    best_score = score
                    best_action = action
            if depth == 0:
                return best_action
            else:
                return best_score

        def exp_agent(state, depth, ghost):
            if state.isLose() or state.isWin():
                return state.getScore()
            next_ghost = (ghost + 1) % state.getNumAgents()
            actions = state.getLegalActions(ghost)
            best_score = float("inf")
            score = best_score
            for action in actions:
                if next_ghost == 0:
                    if depth == self.depth - 1:
                        score = self.evaluationFunction(
                            state.generateSuccessor(ghost, action))
                    else:
                        score = max_agent(
                            state.generateSuccessor(ghost, action), depth + 1)
                else:
                    score = exp_agent(state.generateSuccessor(ghost, action),
                                      depth, next_ghost)
                if score < best_score:
                    best_score = score
            return best_score

        return max_agent(gameState, 0)
        util.raiseNotDefined()
Exemplo n.º 48
0
 def getAction(self, state):
     """
 state: can call state.getLegalActions()
 Choose an action and return it.   
 """
     util.raiseNotDefined()
Exemplo n.º 49
0
 def getGhostStartStates(self):
     """
     Returns a list containing the start state for each ghost.
     Only used in problems that use ghosts (FoodGhostPlanningProblem)
     """
     util.raiseNotDefined()
Exemplo n.º 50
0
 def update(self, state, action, nextState, reward):
     """
   This class will call this function, which you write, after
   observing a transition and reward
 """
     util.raiseNotDefined()
Exemplo n.º 51
0
    def chooseAction(self, gameState):
        """
        First computes the most likely position of each ghost that has
        not yet been captured, then chooses an action that brings
        Pacman closer to the closest ghost (according to mazeDistance!).

        To find the mazeDistance between any two positions, use:
          self.distancer.getDistance(pos1, pos2)

        To find the successor position of a position after an action:
          successorPosition = Actions.getSuccessor(position, action)

        livingGhostPositionDistributions, defined below, is a list of
        util.Counter objects equal to the position belief
        distributions for each of the ghosts that are still alive.  It
        is defined based on (these are implementation details about
        which you need not be concerned):

          1) gameState.getLivingGhosts(), a list of booleans, one for each
             agent, indicating whether or not the agent is alive.  Note
             that pacman is always agent 0, so the ghosts are agents 1,
             onwards (just as before).

          2) self.ghostBeliefs, the list of belief distributions for each
             of the ghosts (including ghosts that are not alive).  The
             indices into this list should be 1 less than indices into the
             gameState.getLivingGhosts() list.
        """
        pacmanPosition = gameState.getPacmanPosition()
        legal = [a for a in gameState.getLegalPacmanActions()]
        livingGhosts = gameState.getLivingGhosts()
        livingGhostPositionDistributions = [
            beliefs for i, beliefs in enumerate(self.ghostBeliefs)
            if livingGhosts[i + 1]
        ]
        best_pos = None
        best_dist = maxint
        for distrib in livingGhostPositionDistributions:
            best_prob = 0.0
            ghost_pos = None
            ghost_dist = maxint
            for pos, prob in distrib.iteritems():
                if prob > best_prob:
                    best_prob = prob
                    ghost_dist = self.distancer.getDistance(
                        pacmanPosition, pos)
                    ghost_pos = pos
                if prob == best_prob:
                    dist = self.distancer.getDistance(pacmanPosition, pos)
                    if dist < ghost_dist:
                        ghost_pos = pos
                        ghost_dist = dist

                # if prob >= best_prob:
                # best_prob = prob
                # dist = self.distancer.getDistance(pacmanPosition, pos)
                # if dist < best_dist:
                # best_dist = dist
                # best_pos = pos
            if ghost_dist < best_dist:
                best_dist = ghost_dist
                best_pos = ghost_pos
            #print "best_prob", best_prob
        #print "best pos", best_pos
        best_action = None
        best_dist = maxint
        for action in legal:
            pos = Actions.getSuccessor(pacmanPosition, action)
            dist = self.distancer.getDistance(pos, best_pos)
            if dist < best_dist:
                best_dist = dist
                best_action = action
        return best_action
        print livingGhostPositionDistributions
        "*** YOUR CODE HERE ***"
        util.raiseNotDefined()
Exemplo n.º 52
0
 def getGoalState(self):
     """
     Returns goal state for problem. Note only defined for problems that have
     a unique goal state such as PositionPlanningProblem
     """
     util.raiseNotDefined()
Exemplo n.º 53
0
 def getCost(self, state, action):
     """
     Given a state and an action, returns step cost, which is the incremental cost 
     of moving to that successor.
     """
     util.raiseNotDefined()
    def getBeliefDistribution(self):
        "*** YOUR CODE HERE ***"

        util.raiseNotDefined()
Exemplo n.º 55
0
 def getActions(self, state):
     """
     Given a state, returns available actions.
     Returns a list of actions
     """
     util.raiseNotDefined()
Exemplo n.º 56
0
 def getAction(self, gameState):
     """
       Returns the minimax action using self.depth and self.evaluationFunction
     """
     "*** YOUR CODE HERE ***"
     util.raiseNotDefined()
Exemplo n.º 57
0
 def chooseAction(self, gameState):
   """
   Override this method to make a good agent.
   """
   util.raiseNotDefined()  
Exemplo n.º 58
0
 def getResult(self, state, action):
     """
     Given a state and an action, returns resulting state.
     """
     util.raiseNotDefined()
Exemplo n.º 59
0
 def getWeights(self, gameState, action):
     util.raiseNotDefined()
Exemplo n.º 60
0
    def getAction(self, gameState):
        """
        Returns the minimax action using self.depth and self.evaluationFunction
        """
        "*** YOUR CODE HERE ***"

        def maxValue(gameState, depth, a, b):
            pacmanAct = gameState.getLegalActions(0)

            # Neu khong con duong di hoac thang hoac thua hoac dat den do sau can thiet thi return
            if len(pacmanAct) == 0 or gameState.isWin() or gameState.isLose(
            ) or depth == self.depth:
                return (self.evaluationFunction(gameState), None)

            maxEvaluation = -(float("inf"))
            maxAction = None
            for action in pacmanAct:
                tmpEvaluation, tmpAction = minValue(
                    gameState.generateSuccessor(0, action), 1, depth, a, b)
                if tmpEvaluation > maxEvaluation:
                    maxEvaluation = tmpEvaluation
                    maxAction = action
                if maxEvaluation > b:
                    return maxEvaluation, maxAction
                a = max(a, maxEvaluation)

            return maxEvaluation, maxAction

        ##Toi thieu hoa gia tri cua ghost
        def minValue(gameState, ghostIndex, depth, a, b):
            ghostAction = gameState.getLegalActions(ghostIndex)
            if len(ghostAction) == 0:
                return self.evaluationFunction(gameState), None

            minEvalution = float("inf")
            minAction = None
            for action in ghostAction:
                # neu la con ma cuoi cung thi goi ham max
                if ghostIndex == gameState.getNumAgents() - 1:
                    tmpEvalution, tmpAction = maxValue(
                        gameState.generateSuccessor(ghostIndex, action),
                        depth + 1, a, b)
                # neu khong thi goi nhung con ma khac
                else:
                    tmpEvalution, tmpAction = minValue(
                        gameState.generateSuccessor(ghostIndex, action),
                        ghostIndex + 1, depth, a, b)
                if (tmpEvalution < minEvalution):
                    minAction = action
                    minEvalution = tmpEvalution
                if minEvalution < a:
                    return minEvalution, minAction
                b = min(b, minEvalution)

            return (minEvalution, minAction)

        a = -(float('inf'))
        b = float('inf')
        maxAction = maxValue(gameState, 0, a, b)[1]
        return maxAction
        util.raiseNotDefined()