Testing agents

Mock games

In order to test agents, you need to set up a “mock” game, place the agents in the situation you want to test, then run the game for a few rounds to see if they behave correctly. We wrote a module, /home/student/winterschool/project/pacman/testingTools.py, that contains the function get_mock_game to set up a small-scale game. A complete example for testing a game is in /home/student/winterschool/project/pacman/test_game.py.

def get_mock_game(layout_txt, agents, first_agent=0, is_red=None, quiet=True):
    """Set up a mock game with given layout and agents.
    Input arguments:
    layout_txt -- a string with the layout of the maze (see layout.py)
    agents -- a list of instances of Agent , the agents that are going to play
              the mock game
    first_agent -- 0 if the red team starts, 1 otherwise
    is_red -- List of boolean that determines if the agents are on the
              red team or not.
              If None, the agents are assigned to the teams according
              to their position on the maze.
    quiet -- if True, the game does not output text messages
    Return (layout, game)
    layout -- an instance of layout.Layout, object representing the maze
    game -- instance of game.Game, with which you can manipulate the game

The game object can be manipulated using three methods:

  1. game._init_run(): initialize the game, must be called to start the game
  2. game._one_game_step(): run one step of the game; the first time this function is called, the agent with index 0 is moved; the second time, the agent with index 1; and so on until all agents are moved, and the agent with index 0 is moved again.
  3. game._end_run(): end the game, can be called at the end of the game to clean up objects

Obtaining game states

In order to obtain the game state from the point of view of a particular agent, you should call game_state = game.state.makeObservation(agent_index). The returned game state is then equivalent to the one received by the agent with ID agent_index on its turn, including the masking of distant enemies. Do not use game.state directly to request information about agents. The information in game.state depends on the agent that is currently moving, and so direct requests to the object is prone to introduce bugs in the tests.

Example: GoEastAgent

Imagine you want to test an agent that always goes East:

from pacman import basic_agents
from pacman.game import Directions
class GoEastAgent(basic_agents.BasicAgent):
    def choose_action(self, game_state):
        return Directions.EAST

In order to do this, we draw the game maze using symbols for walls (%), food (.), and agents (1,2 represent different agents, odd for one team and even for the other).

import unittest
from pacman import basic_agents
from test.testing_tools import get_mock_game
from goeast_agents import GoEastAgent
class GoEastTestCase(unittest.TestCase):
    def test_east(self):
        layout_txt = """%%%%%%%%
                        %     2%
                        %     .%
                        %1     %
        # BasicAgent does nothing by default
        lt, game = get_mock_game(layout_txt, [GoEastAgent(0),
        # test that the start position is correct (unnecessary in general)
        game_state = game.state.makeObservation(0)
        assert game_state.getAgentPosition(0) == (1,1)
        # test that the agent really moved east!
        game_state = game.state.makeObservation(0)
        assert game_state.getAgentPosition(0) == (2,1)
if __name__=='__main__':

‘Real life’ situations

For more realistic situations, a hand-drawn maze might not be enough. After all, you will need not only to test your agents that they not crash; you also want them to perform very well against your enemies. For testing your large-scale strategy, this will probably not be enough and you will need to use larger mazes for that. Of course, you can resort to start_game.py and just watch what happens. But then you’ll get a new maze every time which for tactical considerations might or might not be the best approach.

Before you start drawing them by hand, you can create mazes with game/create_maze.py. This is the same generator which is used to create mazes during the tournament. (When the maze is created, you can either paste it to your testing code or save it and then append the file name to start_game.pystart_game.py -l layout_file agents1.py agents2.py.

materials/testing_agents.txt · Last modified: 2010/10/06 14:42 by python-faculty
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki