Agents & Environments
Implement Agents and Environments. (Chapters 1-2)
The class hierarchies are as follows:
Thing ## A physical object that can exist in an environment
Agent
Wumpus
Dirt
Wall
...
Environment ## An environment holds objects, runs simulations
XYEnvironment
VacuumEnvironment
WumpusEnvironment
An agent program is a callable instance, taking percepts and choosing actions:
SimpleReflexAgentProgram
...
The GUI helpers are:
EnvGUI ## A window with a graphical representation of the Environment
EnvToolbar ## contains buttons for controlling EnvGUI
EnvCanvas ## Canvas to display the environment of an EnvGUI
>>> a = ReflexVacuumAgent()
>>> a.program((loc_A, 'Clean'))
'Right'
>>> a.program((loc_B, 'Clean'))
'Left'
>>> a.program((loc_A, 'Dirty'))
'Suck'
>>> a.program((loc_A, 'Dirty'))
'Suck'
>>> e = TrivialVacuumEnvironment()
>>> e.add_thing(ModelBasedVacuumAgent())
>>> e.run(5)
- class agents.Thing[source]
Bases:
objectThis represents any physical object that can appear in an Environment. You subclass Thing to get the things you want. Each thing can have a .__name__ slot (used for output only).
- class agents.Agent(program=None)[source]
Bases:
ThingAn Agent is a subclass of Thing with one required instance attribute (aka slot), .program, which should hold a function that takes one argument, the percept, and returns an action. (What counts as a percept or action will depend on the specific environment in which the agent exists.) Note that ‘program’ is a slot, not a method. If it were a method, then the program could ‘cheat’ and look at aspects of the agent. It’s not supposed to do that: the program can only look at the percepts. An agent program that needs a model of the world (and of the agent itself) will have to build and maintain its own model. There is an optional slot, .performance, which is a number giving the performance measure of the agent in its environment.
- agents.TraceAgent(agent)[source]
Wrap the agent’s program to print its input and output. This will let you see what the agent is doing in the environment.
- agents.TableDrivenAgentProgram(table)[source]
[Figure 2.7] This agent selects an action based on the percept sequence. It is practical only for tiny domains. To customize it, provide as table a dictionary of all {percept_sequence:action} pairs.
- agents.RandomAgentProgram(actions)[source]
An agent that chooses an action at random, ignoring all percepts. >>> list = [‘Right’, ‘Left’, ‘Suck’, ‘NoOp’] >>> program = RandomAgentProgram(list) >>> agent = Agent(program) >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1, 0): ‘Clean’ , (0, 0): ‘Clean’} True
- agents.SimpleReflexAgentProgram(rules, interpret_input)[source]
[Figure 2.10] This agent takes action based solely on the percept.
- agents.ModelBasedReflexAgentProgram(rules, update_state, model)[source]
[Figure 2.12] This agent takes action based on the percept and state.
- agents.RandomVacuumAgent()[source]
Randomly choose one of the actions from the vacuum environment. >>> agent = RandomVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- agents.TableDrivenVacuumAgent()[source]
Tabular approach towards vacuum world as mentioned in [Figure 2.3] >>> agent = TableDrivenVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- agents.ReflexVacuumAgent()[source]
[Figure 2.8] A reflex agent for the two-state vacuum environment. >>> agent = ReflexVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- agents.ModelBasedVacuumAgent()[source]
An agent that keeps track of what locations are clean or dirty. >>> agent = ModelBasedVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- class agents.Environment[source]
Bases:
objectAbstract class representing an Environment. ‘Real’ Environment classes inherit from this. Your Environment will typically need to implement:
percept: Define the percept that an agent sees. execute_action: Define the effects of executing an action; also update the agent.performance slot.
The environment keeps a list of .things and .agents (which is a subset of .things). Each agent has a .performance slot, initialized to 0. Each thing has a .location slot, even though some environments may not need this.
- step()[source]
Run the environment for one time step. If the actions and exogenous changes are independent, this method will do. If there are interactions between them, you’ll need to override this method.
- list_things_at(location, tclass=<class 'agents.Thing'>)[source]
Return all things exactly at a given location.
- some_things_at(location, tclass=<class 'agents.Thing'>)[source]
Return true if at least one of the things at location is an instance of class tclass (or a subclass).
- class agents.Direction(direction)[source]
Bases:
objectA direction class for agents that want to move in a 2D plane.
Usage:
d = Direction("down") # to change directions: d = d + "right" or d = d + Direction.R # both do the same thing
Note that the argument to __add__ must be a string and not a Direction object, and it can only be ‘right’ or ‘left’.
- R = 'right'
- L = 'left'
- U = 'up'
- D = 'down'
- class agents.XYEnvironment(width=10, height=10)[source]
Bases:
EnvironmentThis class is for environments on a 2D plane, with locations labelled by (x, y) points, either discrete or continuous.
Agents perceive things within a radius. Each agent in the environment has a .location slot which should be a location such as (0, 1), and a .holding slot, which should be a list of things that are held.
- perceptible_distance = 1
- execute_action(agent, action)[source]
Apply a motion or manipulation action for the agent. Supports turning (‘TurnRight’/’TurnLeft’), moving one step (‘Forward’, setting agent.bump on a collision), grabbing a grabbable thing at the agent’s location (‘Grab’), and dropping the last held thing (‘Release’).
- move_to(thing, destination)[source]
Move a thing to a new location. Returns True on success or False if there is an Obstacle. If thing is holding anything, they move with him.
- add_thing(thing, location=None, exclude_duplicate_class_items=False)[source]
Add things to the world. If (exclude_duplicate_class_items) then the item won’t be added if the location has at least one item of the same class.
- is_inbounds(location)[source]
Checks to make sure that the location is inbounds (within walls if we have walls)
- random_location_inbounds(exclude=None)[source]
Returns a random location that is inbounds (within walls if we have walls)
- class agents.Obstacle[source]
Bases:
ThingSomething that can cause a bump, preventing an agent from moving into the same square it’s in.
- class agents.Wall[source]
Bases:
ObstacleAn impassable Obstacle forming the boundary or interior walls of a grid.
- class agents.GraphicEnvironment(width=10, height=10, boundary=True, color={}, display=False)[source]
Bases:
XYEnvironmentAn XYEnvironment that visualises itself in a Jupyter notebook using an ipythonblocks BlockGrid, colouring each cell according to the class of the last thing placed there.
- get_world()[source]
Returns all the items in the world in a format understandable by the ipythonblocks BlockGrid.
- run(steps=1000, delay=1)[source]
Run the Environment for given number of time steps, but update the GUI too.
- reveal()[source]
Display the BlockGrid for this world - the last thing to be added at a location defines the location color.
- class agents.ContinuousWorld(width=10, height=10)[source]
Bases:
EnvironmentModel for Continuous World
- class agents.PolygonObstacle(coordinates)[source]
Bases:
ObstacleAn Obstacle in a ContinuousWorld whose shape is a polygon described by a list of vertex coordinates.
- class agents.VacuumEnvironment(width=10, height=10)[source]
Bases:
XYEnvironmentThe environment of [Ex. 2.12]. Agent perceives dirty or clean, and bump (into obstacle) or not; 2D discrete world of unknown size; performance measure is 100 for each dirt cleaned, and -1 for each turn taken.
- class agents.TrivialVacuumEnvironment[source]
Bases:
EnvironmentThis environment has two locations, A and B. Each can be Dirty or Clean. The agent perceives its location and the location’s status. This serves as an example of how to implement a simple Environment.
- class agents.Gold[source]
Bases:
ThingThe gold the explorer is trying to grab in the Wumpus World.
- class agents.Glitter[source]
Bases:
ThingPercept indicating that gold is in the explorer’s current room.
- class agents.Arrow[source]
Bases:
ThingThe single arrow the explorer can shoot to try to kill the wumpus.
- class agents.Scream[source]
Bases:
ThingPercept heard throughout the world when the wumpus is killed.
- class agents.Wumpus(program=None)[source]
Bases:
AgentThe wumpus: a monster that kills the explorer sharing its room.
- screamed = False
- class agents.Explorer(program=None)[source]
Bases:
AgentThe agent that explores the Wumpus World, seeking gold while avoiding pits and the wumpus.
- holding = []
- has_arrow = True
- killed_by = ''
- direction = <agents.Direction object>
- class agents.WumpusEnvironment(agent_program, width=6, height=6)[source]
Bases:
XYEnvironmentA grid implementation of the Wumpus World from Chapter 7: a cave of rooms surrounded by walls, holding pits, the wumpus and gold, in which an Explorer agent perceives stench, breeze, glitter, bump and scream.
- pit_probability = 0.2
- percepts_from(agent, location, tclass=<class 'agents.Thing'>)[source]
Return percepts from a given location, and replaces some items with percepts from chapter 7.
- percept(agent)[source]
Return things in adjacent (not diagonal) cells of the agent. Result format: [Left, Right, Up, Down, Center / Current location]
- agents.compare_agents(EnvFactory, AgentFactories, n=10, steps=1000)[source]
See how well each of several agents do in n instances of an environment. Pass in a factory (constructor) for environments, and several for agents. Create n instances of the environment, and run each agent in copies of each one for steps. Return a list of (agent, average-score) tuples. >>> environment = TrivialVacuumEnvironment >>> agents = [ModelBasedVacuumAgent, ReflexVacuumAgent] >>> result = compare_agents(environment, agents) >>> performance_ModelBasedVacuumAgent = result[0][1] >>> performance_ReflexVacuumAgent = result[1][1] >>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent True
- agents.test_agent(AgentFactory, steps, envs)[source]
Return the mean score of running an agent in each of the envs, for steps >>> def constant_prog(percept): … return percept … >>> agent = Agent(constant_prog) >>> result = agent.program(5) >>> result == 5 True
Implement Agents and Environments. (Chapters 1-2)
The class hierarchies are as follows:
Thing ## A physical object that can exist in an environment
Agent
Wumpus
Dirt
Wall
...
Environment ## An environment holds objects, runs simulations
XYEnvironment
VacuumEnvironment
WumpusEnvironment
An agent program is a callable instance, taking percepts and choosing actions:
SimpleReflexAgentProgram
...
EnvGUI ## A window with a graphical representation of the Environment
EnvToolbar ## contains buttons for controlling EnvGUI
EnvCanvas ## Canvas to display the environment of an EnvGUI
>>> a = ReflexVacuumAgent()
>>> a.program((loc_A, 'Clean'))
'Right'
>>> a.program((loc_B, 'Clean'))
'Left'
>>> a.program((loc_A, 'Dirty'))
'Suck'
>>> a.program((loc_A, 'Dirty'))
'Suck'
>>> e = TrivialVacuumEnvironment()
>>> e.add_thing(ModelBasedVacuumAgent())
>>> e.run(5)
- class agents4e.Thing[source]
Bases:
objectThis represents any physical object that can appear in an Environment. You subclass Thing to get the things you want. Each thing can have a .__name__ slot (used for output only).
- class agents4e.Agent(program=None)[source]
Bases:
ThingAn Agent is a subclass of Thing with one required slot, .program, which should hold a function that takes one argument, the percept, and returns an action. (What counts as a percept or action will depend on the specific environment in which the agent exists.) Note that ‘program’ is a slot, not a method. If it were a method, then the program could ‘cheat’ and look at aspects of the agent. It’s not supposed to do that: the program can only look at the percepts. An agent program that needs a model of the world (and of the agent itself) will have to build and maintain its own model. There is an optional slot, .performance, which is a number giving the performance measure of the agent in its environment.
- agents4e.TraceAgent(agent)[source]
Wrap the agent’s program to print its input and output. This will let you see what the agent is doing in the environment.
- agents4e.TableDrivenAgentProgram(table)[source]
[Figure 2.7] This agent selects an action based on the percept sequence. It is practical only for tiny domains. To customize it, provide as table a dictionary of all {percept_sequence:action} pairs.
- agents4e.RandomAgentProgram(actions)[source]
An agent that chooses an action at random, ignoring all percepts. >>> list = [‘Right’, ‘Left’, ‘Suck’, ‘NoOp’] >>> program = RandomAgentProgram(list) >>> agent = Agent(program) >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1, 0): ‘Clean’ , (0, 0): ‘Clean’} True
- agents4e.SimpleReflexAgentProgram(rules, interpret_input)[source]
[Figure 2.10] This agent takes action based solely on the percept.
- agents4e.ModelBasedReflexAgentProgram(rules, update_state, transition_model, sensor_model)[source]
[Figure 2.12] This agent takes action based on the percept and state.
- agents4e.RandomVacuumAgent()[source]
Randomly choose one of the actions from the vacuum environment. >>> agent = RandomVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- agents4e.TableDrivenVacuumAgent()[source]
Tabular approach towards vacuum world as mentioned in [Figure 2.3] >>> agent = TableDrivenVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- agents4e.ReflexVacuumAgent()[source]
[Figure 2.8] A reflex agent for the two-state vacuum environment. >>> agent = ReflexVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- agents4e.ModelBasedVacuumAgent()[source]
An agent that keeps track of what locations are clean or dirty. >>> agent = ModelBasedVacuumAgent() >>> environment = TrivialVacuumEnvironment() >>> environment.add_thing(agent) >>> environment.run() >>> environment.status == {(1,0):’Clean’ , (0,0) : ‘Clean’} True
- class agents4e.Environment[source]
Bases:
objectAbstract class representing an Environment. ‘Real’ Environment classes inherit from this. Your Environment will typically need to implement:
percept: Define the percept that an agent sees. execute_action: Define the effects of executing an action. Also update the agent.performance slot.
The environment keeps a list of .things and .agents (which is a subset of .things). Each agent has a .performance slot, initialized to 0. Each thing has a .location slot, even though some environments may not need this.
- step()[source]
Run the environment for one time step. If the actions and exogenous changes are independent, this method will do. If there are interactions between them, you’ll need to override this method.
- list_things_at(location, tclass=<class 'agents4e.Thing'>)[source]
Return all things exactly at a given location.
- some_things_at(location, tclass=<class 'agents4e.Thing'>)[source]
Return true if at least one of the things at location is an instance of class tclass (or a subclass).
- class agents4e.Direction(direction)[source]
Bases:
objectA direction class for agents that want to move in a 2D plane
Usage:
d = Direction("down") To change directions: d = d + "right" or d = d + Direction.R #Both do the same thing Note that the argument to __add__ must be a string and not a Direction object. Also, it (the argument) can only be right or left.
- R = 'right'
- L = 'left'
- U = 'up'
- D = 'down'
- class agents4e.XYEnvironment(width=10, height=10)[source]
Bases:
EnvironmentThis class is for environments on a 2D plane, with locations labelled by (x, y) points, either discrete or continuous.
Agents perceive things within a radius. Each agent in the environment has a .location slot which should be a location such as (0, 1), and a .holding slot, which should be a list of things that are held.
- perceptible_distance = 1
- execute_action(agent, action)[source]
Apply a motion action for the agent. Supports turning (‘TurnRight’/’TurnLeft’), moving one step (‘Forward’, setting agent.bump on a collision) and dropping the last held thing (‘Release’).
- move_to(thing, destination)[source]
Move a thing to a new location. Returns True on success or False if there is an Obstacle. If thing is holding anything, they move with him.
- add_thing(thing, location=None, exclude_duplicate_class_items=False)[source]
Add things to the world. If (exclude_duplicate_class_items) then the item won’t be added if the location has at least one item of the same class.
- is_inbounds(location)[source]
Checks to make sure that the location is inbounds (within walls if we have walls)
- random_location_inbounds(exclude=None)[source]
Returns a random location that is inbounds (within walls if we have walls)
- class agents4e.Obstacle[source]
Bases:
ThingSomething that can cause a bump, preventing an agent from moving into the same square it’s in.
- class agents4e.Wall[source]
Bases:
ObstacleAn impassable Obstacle forming the boundary or interior walls of a grid.
- class agents4e.GraphicEnvironment(width=10, height=10, boundary=True, color={}, display=False)[source]
Bases:
XYEnvironmentAn XYEnvironment that visualises itself in a Jupyter notebook using an ipythonblocks BlockGrid, colouring each cell according to the class of the last thing placed there.
- get_world()[source]
Returns all the items in the world in a format understandable by the ipythonblocks BlockGrid.
- run(steps=1000, delay=1)[source]
Run the Environment for given number of time steps, but update the GUI too.
- reveal()[source]
Display the BlockGrid for this world - the last thing to be added at a location defines the location color.
- class agents4e.ContinuousWorld(width=10, height=10)[source]
Bases:
EnvironmentModel for Continuous World
- class agents4e.PolygonObstacle(coordinates)[source]
Bases:
ObstacleAn Obstacle in a ContinuousWorld whose shape is a polygon described by a list of vertex coordinates.
- class agents4e.VacuumEnvironment(width=10, height=10)[source]
Bases:
XYEnvironmentThe environment of [Ex. 2.12]. Agent perceives dirty or clean, and bump (into obstacle) or not; 2D discrete world of unknown size; performance measure is 100 for each dirt cleaned, and -1 for each turn taken.
- class agents4e.TrivialVacuumEnvironment[source]
Bases:
EnvironmentThis environment has two locations, A and B. Each can be Dirty or Clean. The agent perceives its location and the location’s status. This serves as an example of how to implement a simple Environment.
- class agents4e.Gold[source]
Bases:
ThingThe gold the explorer is trying to grab in the Wumpus World.
- class agents4e.Glitter[source]
Bases:
ThingPercept indicating that gold is in the explorer’s current room.
- class agents4e.Arrow[source]
Bases:
ThingThe single arrow the explorer can shoot to try to kill the wumpus.
- class agents4e.Scream[source]
Bases:
ThingPercept heard throughout the world when the wumpus is killed.
- class agents4e.Wumpus(program=None)[source]
Bases:
AgentThe wumpus: a monster that kills the explorer sharing its room.
- screamed = False
- class agents4e.Explorer(program=None)[source]
Bases:
AgentThe agent that explores the Wumpus World, seeking gold while avoiding pits and the wumpus.
- holding = []
- has_arrow = True
- killed_by = ''
- direction = <agents4e.Direction object>
- class agents4e.WumpusEnvironment(agent_program, width=6, height=6)[source]
Bases:
XYEnvironmentA grid implementation of the Wumpus World from Chapter 7: a cave of rooms surrounded by walls, holding pits, the wumpus and gold, in which an Explorer agent perceives stench, breeze, glitter, bump and scream.
- pit_probability = 0.2
- percepts_from(agent, location, tclass=<class 'agents4e.Thing'>)[source]
Return percepts from a given location, and replaces some items with percepts from chapter 7.
- percept(agent)[source]
Return things in adjacent (not diagonal) cells of the agent. Result format: [Left, Right, Up, Down, Center / Current location]
- agents4e.compare_agents(EnvFactory, AgentFactories, n=10, steps=1000)[source]
See how well each of several agents do in n instances of an environment. Pass in a factory (constructor) for environments, and several for agents. Create n instances of the environment, and run each agent in copies of each one for steps. Return a list of (agent, average-score) tuples. >>> environment = TrivialVacuumEnvironment >>> agents = [ModelBasedVacuumAgent, ReflexVacuumAgent] >>> result = compare_agents(environment, agents) >>> performance_ModelBasedVacuumAgent = result[0][1] >>> performance_ReflexVacuumAgent = result[1][1] >>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent True