def getMovement(layoutText, agentIndex, difficulty):
    
    gameState = GameState()
    
    numGhosts = layoutText.count('G')

    layoutText = layoutText.split('\n')
    layoutText = filter(None,layoutText)

    layout = Layout(layoutText)

    gameState.initialize(layout,numGhosts);
    
    if difficulty == 1:
        agent = ReflexAgent()
        agent.index = agentIndex
        return agent.getAction(gameState)
    elif difficulty == 2:
        agent = MinimaxAgent()
        agent.index = agentIndex
        return agent.getAction(gameState)
    elif difficulty == 2:
        agent = AlphaBetaAgent()
        agent.index = agentIndex
        agent.depth = 4
        return agent.getAction(gameState)
    else:
        agent = RandomAgent()
        agent.index = agentIndex
        return agent.getAction(gameState)
Exemple #2
0
 def __init__(self,
              ghostAgents,
              display,
              rules,
              layout,
              percentRandomize=0.5):
     Game.__init__(self, ghostAgents, display, rules)
     initState = GameState()
     initState.initialize(layout, len(ghostAgents), percentRandomize)
     self.state = initState
     self.initialState = initState.deepCopy()
class MultiAgentSearchAgent(Agent):
    """
        This class provides some common elements to all of your
        multi-agent searchers.  Any methods defined here will be available
        to the MinimaxPacmanAgent, AlphaBetaPacmanAgent & ExpectimaxPacmanAgent.

        You *do not* need to make any changes here, but you can if you want to
        add functionality to all your adversarial search agents.  Please do not
        remove anything, however.

        Note: this is an abstract class: one that should not be instantiated.  It's
        only partially specified, and designed to be extended.  Agent (game.py)
        is another abstract class.
    """
    
    moveToAction = {   Directions.WEST:  PacmanAction.WEST,
                       Directions.EAST:  PacmanAction.EAST,
                       Directions.NORTH: PacmanAction.NORTH,
                       Directions.SOUTH: PacmanAction.SOUTH,
                       Directions.STOP:  PacmanAction.STOP,}
    actionToMovement = {PacmanAction.WEST:  Directions.WEST,
                        PacmanAction.EAST:  Directions.EAST,
                        PacmanAction.NORTH: Directions.NORTH,
                        PacmanAction.SOUTH: Directions.SOUTH,
                        PacmanAction.STOP:  Directions.STOP}

    def __init__(self, evalFn = 'scoreEvaluationFunction', depth = '2'):
        self.index = 0 # Pacman is always agent index 0
        self.evaluationFunction = scoreEvaluationFunction
        self.depth = int(depth)
        self.gameState = GameState()

        self.initializeGameState()

        rospy.Subscriber("/pacman_interface/agent_action", AgentAction, self.agentActionCallback)
        rospy.Service('get_action', PacmanGetAction, self.getAction)

        rospy.spin()

    def initializeGameState(self):
        rospy.wait_for_service('pacman_inialize_game_state')
        try:
            getInitializationInfo = rospy.ServiceProxy('pacman_inialize_game_state', PacmanInitializationInfo)
            initInfo = getInitializationInfo()

            thisLayout = layout.getLayout(initInfo.layout)
            numGhosts = initInfo.numGhosts

            self.gameState.initialize(thisLayout, numGhosts)
            print "Game initialized"
        except rospy.ServiceException, e:
            print "Service call failed: %s"%e
Exemple #4
0
 def newGame(self,
             layout,
             pacmanAgent,
             ghostAgents,
             display,
             quiet=False,
             catchExceptions=False):
     from pacman import GameState
     agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()]
     initState = GameState()
     initState.initialize(layout, len(ghostAgents))
     game = GameExtended(agents,
                         display,
                         self,
                         catchExceptions=catchExceptions)
     game.state = initState
     self.initialState = initState.deepCopy()
     self.quiet = quiet
     return game
Exemple #5
0
def getMovement(layoutText, agentIndex, difficulty):

    gameState = GameState()

    numGhosts = layoutText.count('G')

    layoutText = layoutText.split('\n')
    layoutText = filter(None, layoutText)

    layout = Layout(layoutText)

    gameState.initialize(layout, numGhosts)

    if difficulty == 1:
        agent = ReflexAgent()
        agent.index = agentIndex
        direction = agent.getAction(gameState)
    elif difficulty == 2:
        agent = MinimaxAgent()
        agent.index = agentIndex
        direction = agent.getAction(gameState)
    elif difficulty == 3:
        agent = AlphaBetaAgent()
        agent.index = agentIndex
        agent.depth = 3
        direction = agent.getAction(gameState)
    else:
        agent = RandomAgent()
        agent.index = agentIndex
        direction = agent.getAction(gameState)

    if direction == 'North':
        return 1
    elif direction == 'South':
        return 2
    elif direction == 'West':
        return 3
    else:
        return 4
% % %%  %%% % %     % % %%%  %% % %
%         %             %         %
%  %%%%%  %%%%%%% %%%%%%%  %%%%%  %
% %%   % %      % %      % %   %% %
%                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""

numGhosts = layoutText.count('G')

layoutText = layoutText.split('\n')
layoutText = filter(None,layoutText)

layout = Layout(layoutText)

x.initialize(layout,numGhosts);
agent = MinimaxAgent()
agent.index=1

randomAgent = RandomAgent()
randomAgent.index = 1

reflexAgent = ReflexAgent()
reflexAgent.index = 1

minimaxAgent = MinimaxAgent()
minimaxAgent.depth = 2
minimaxAgent.index = 1


alphabetaAgent = AlphaBetaAgent()
def generateGameState(args): 
    layout =  generateLayout(args)
    gameState = GameState()
    numGhostAgents = 1
    gameState.initialize(layout, numGhostAgents)
    return gameState
def generateAllStates(length, ghostNum = 1): #length of all possible spaces. Do not set the ghost num
    allStatesWithoutP = []
    for k in range(0, 4**length):
        layout = util.base10toN(k, 4, length)
        allStatesWithoutP.append(layout)

    allValidStates = []

    for k in allStatesWithoutP: 
        zerocount = 0
        for x in range(0, len(k)):
            if k[x] == "0":
                zerocount += 1
        if zerocount == (ghostNum+1):
            allValidStates.append(k)

    allLayouts = []

    for k in allValidStates: #hardcoded for only ONE GHOST!!
        tempstring1 = ""
        tempstring2 = ""
        switcher = True
        for x in range(0, len(k)):
            if k[x] == "0":
                if switcher:
                    tempstring1 += "4"
                    tempstring2 += "5"
                else:
                    tempstring1 += "5"
                    tempstring2 += "4"
                switcher = False
            else:
                tempstring1 += k[x]
                tempstring2 += k[x]
        allLayouts.append(tempstring1)
        allLayouts.append(tempstring2)
    for k in range(0, len(allLayouts)):
        state = allLayouts[k]
        newstate = "%"
        for x in range(0, len(state)):
            if state[x] == "1":
                newstate+=" "
            elif state[x] == "2":
                newstate += "."
            elif state[x] == "3":
                newstate+= "o"
            elif state[x] == "4":
                newstate+= "P"
            elif state[x] == "5":
                newstate+= "G"
        newstate+= "%"
        layouttext = []
        layouttext.append("%"*(length+2)) #HARDCODE
        layouttext.append(newstate)
        layouttext.append("%"*(length+2)) #HARDCODE
        allLayouts[k] = layouttext
        #print layouttext

    allStates = []
    for k in range(0, len(allLayouts)):
        layout = Layout(allLayouts[k])
        gameState = GameState()
        gameState.initialize(layout, 1) #ghost hardcoded
        allStates.append(gameState)
    return allStates
def generateGameState(args):
    layout = generateLayout(args)
    gameState = GameState()
    numGhostAgents = 1
    gameState.initialize(layout, numGhostAgents)
    return gameState
def generateAllStates(
        length,
        ghostNum=1):  #length of all possible spaces. Do not set the ghost num
    allStatesWithoutP = []
    for k in range(0, 4**length):
        layout = util.base10toN(k, 4, length)
        allStatesWithoutP.append(layout)

    allValidStates = []

    for k in allStatesWithoutP:
        zerocount = 0
        for x in range(0, len(k)):
            if k[x] == "0":
                zerocount += 1
        if zerocount == (ghostNum + 1):
            allValidStates.append(k)

    allLayouts = []

    for k in allValidStates:  #hardcoded for only ONE GHOST!!
        tempstring1 = ""
        tempstring2 = ""
        switcher = True
        for x in range(0, len(k)):
            if k[x] == "0":
                if switcher:
                    tempstring1 += "4"
                    tempstring2 += "5"
                else:
                    tempstring1 += "5"
                    tempstring2 += "4"
                switcher = False
            else:
                tempstring1 += k[x]
                tempstring2 += k[x]
        allLayouts.append(tempstring1)
        allLayouts.append(tempstring2)
    for k in range(0, len(allLayouts)):
        state = allLayouts[k]
        newstate = "%"
        for x in range(0, len(state)):
            if state[x] == "1":
                newstate += " "
            elif state[x] == "2":
                newstate += "."
            elif state[x] == "3":
                newstate += "o"
            elif state[x] == "4":
                newstate += "P"
            elif state[x] == "5":
                newstate += "G"
        newstate += "%"
        layouttext = []
        layouttext.append("%" * (length + 2))  #HARDCODE
        layouttext.append(newstate)
        layouttext.append("%" * (length + 2))  #HARDCODE
        allLayouts[k] = layouttext
        #print layouttext

    allStates = []
    for k in range(0, len(allLayouts)):
        layout = Layout(allLayouts[k])
        gameState = GameState()
        gameState.initialize(layout, 1)  #ghost hardcoded
        allStates.append(gameState)
    return allStates
Exemple #11
0
class PacmanTask(Task):
    def __init__(self, layout, agents, display, state_repr='stack',
                 muteAgents=False, catchExceptions=False):
        '''
        state_repr: state representation, possible values ['stack', 'k-frames', 'dict']
            'stack' - stack walls, food, ghost and pacman representation into a 4D tensor.
            'dict' - return the raw dict representation keys=['walls', 'food', 'ghost', 'pacman'], values are matrix/tensor.
            'k-frames' - instead of directional descriptors for pacman and ghost, use static descriptors and capture past k frames.
        '''
        # parse state representation.
        self.state_repr = state_repr
        self.layout = layout
        self.agents = agents
        self.display = display
        self.muteAgents = muteAgents
        self.catchExceptions = catchExceptions

        if self.state_repr.endswith('frames'):
            bar_pos = self.state_repr.rfind('frames')
            self.state_k = int(self.state_repr[:bar_pos-1])
            self.state_history = []
        self.init_state = GameState()
        self.init_state.initialize(layout, len(agents))
        self.game_rule = ClassicGameRules(timeout=100)
        self.myagent = GameNoWaitAgent()
        self.init_game = Game([self.myagent] + agents[:layout.getNumGhosts()],
                        display,
                        self.game_rule,
                        catchExceptions = catchExceptions)
        self.init_game.state = self.init_state

        # action mapping.
        self.all_actions = Actions._directions.keys()
        self.action_to_dir = {action_i: action
            for (action_i, action) in enumerate(self.all_actions)}
        self.dir_to_action = {action: action_i
            for (action_i, action) in enumerate(self.all_actions)}

        def start_game():
            self.game = self.init_game.deepCopy()
            self.game.init()

        self.start_game = start_game
        start_game()

    def deep_copy(self):
        agents = list(self.agents)
        task = PacmanTask(self.layout, agents, self.display, self.state_repr, self.muteAgents, self.catchExceptions)
        task.game = self.game.deepCopy()
        task.myagent = self.myagent # TODO: agents not deep copy.
        return task

    @property
    def curr_state_dict(self):
        return self.game.state.data.array()

    @property
    def curr_state(self):
        if self.state_repr == 'dict':
            return self.curr_state_dict
        elif self.state_repr == 'stack':
            state_dict = self.curr_state_dict
            if len(state_dict.get('ghosts')) == 0:
                ghost_stacked = np.zeros_like(state_dict['pacman'])
            else:
                ghost_stacked = np.sum(state_dict['ghosts'], axis=0)
            state = np.array(
                [
                    state_dict['food'],
                    state_dict['wall']
                ]
                +
                [
                    state_dict['pacman'][:, :, i] for i in range(4)
                ]
                +
                [
                    ghost_stacked[:, :, i] for i in range(4)
                ]
            )
            return state
        elif hasattr(self, 'state_k'):
            state_history = self.state_history + [self.curr_state_dict]
            def stack_direction(state_agent):
                return np.sum(state_agent, axis=2)
            state = []
            k = 0
            for hist_dict in state_history[::-1]:
                state.extend([
                        hist_dict['food'],
                        hist_dict['wall'],
                        stack_direction(hist_dict['pacman'])

                    ]
                        +
                    sum(
                        [
                                [stack_direction(ghost) for ghost in hist_dict['ghosts']]
                        ], []
                    )
                )
                k += 1
            state = np.array(state)
            frame_dim = state.shape[0] / k
            for ki in range(k, self.state_k + 1):
                state = np.concatenate((state, np.zeros_like(state[:frame_dim, :, :])), axis=0)
            return state

    def is_end(self):
        return self.game.gameOver

    @property
    def num_actions(self):
        return len(self.all_actions)

    @property
    def valid_actions(self):
        dirs = self.game.state.getLegalPacmanActions()
        return [self.dir_to_action[dir] for dir in dirs]

    def step(self, action):
        if hasattr(self, 'state_k'): # if we use past frames.
            self.state_history.append(self.curr_state_dict)
            if len(self.state_history) > self.state_k:
                self.state_history = self.state_history[-self.state_k:]

        if action not in self.valid_actions: # TODO: hack.
            action = self.dir_to_action[Directions.STOP]

        # convert action to direction.
        direction = self.action_to_dir[action]
        old_score = self.game.state.data.score

        # run the game using the direction.
        self.myagent.next_action = direction
        self.game.run_one()
        new_score = self.game.state.data.score
        reward = new_score - old_score

        if self.is_end():
            self.game.finalize()

        return reward

    def reset(self):
        self.start_game()

    @property
    def state_shape(self):
        return self.curr_state.shape

    def __str__(self):
        return str(self.game.state)

    def __repr__(self):
        return str(self.game.state)