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: object

This 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).

is_alive()[source]

Things that are ‘alive’ should return true.

show_state()[source]

Display the agent’s internal state. Subclasses should override.

display(canvas, x, y, width, height)[source]

Display an image of this Thing on the canvas.

class agents.Agent(program=None)[source]

Bases: Thing

An 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.

can_grab(thing)[source]

Return True if this agent can grab this thing. Override for appropriate subclasses of Agent and Thing.

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.rule_match(state, rules)[source]

Find the first rule that matches 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: object

Abstract 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.

thing_classes()[source]

Return the list of Thing subclasses that may appear in this environment.

percept(agent)[source]

Return the percept that the agent sees at this point. (Implement this.)

execute_action(agent, action)[source]

Change the world to reflect this action. (Implement this.)

default_location(thing)[source]

Default location to place a new thing with unspecified location.

exogenous_change()[source]

If there is spontaneous change in the world, override this.

is_done()[source]

By default, we’re done when we can’t find a live agent.

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.

run(steps=1000)[source]

Run the Environment for given number of time steps.

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).

add_thing(thing, location=None)[source]

Add a thing to the environment, setting its location. For convenience, if thing is an agent program we make a new agent for it. (Shouldn’t need to override this.)

delete_thing(thing)[source]

Remove a thing from the environment.

class agents.Direction(direction)[source]

Bases: object

A 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'
move_forward(from_location)[source]
>>> d = Direction('up')
>>> l1 = d.move_forward((0, 0))
>>> l1
(0, -1)
>>> d = Direction(Direction.R)
>>> l1 = d.move_forward((0, 0))
>>> l1
(1, 0)
class agents.XYEnvironment(width=10, height=10)[source]

Bases: Environment

This 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
things_near(location, radius=None)[source]

Return all things within radius of location.

percept(agent)[source]

By default, agent perceives things within a default radius.

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’).

default_location(thing)[source]

Return a random inbounds location that contains no Obstacle.

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)

delete_thing(thing)[source]

Deletes thing, and everything it is holding (if thing is an agent)

add_walls()[source]

Put walls around the entire perimeter of the grid.

add_observer(observer)[source]

Adds an observer to the list of observers. An observer is typically an EnvGUI.

Each observer is notified of changes in move_to and add_thing, by calling the observer’s methods thing_moved(thing) and thing_added(thing, loc).

turn_heading(heading, inc)[source]

Return the heading to the left (inc=+1) or right (inc=-1) of heading.

class agents.Obstacle[source]

Bases: Thing

Something that can cause a bump, preventing an agent from moving into the same square it’s in.

class agents.Wall[source]

Bases: Obstacle

An 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: XYEnvironment

An 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.

update(delay=1)[source]

Pause for delay seconds, then redraw the world in the GUI.

reveal()[source]

Display the BlockGrid for this world - the last thing to be added at a location defines the location color.

draw_world()[source]

Repaint the BlockGrid, colouring each occupied cell by the class of the last thing at that location using the configured color mapping.

conceal()[source]

Hide the BlockGrid for this world

class agents.ContinuousWorld(width=10, height=10)[source]

Bases: Environment

Model for Continuous World

add_obstacle(coordinates)[source]

Add a PolygonObstacle defined by the given list of vertex coordinates.

class agents.PolygonObstacle(coordinates)[source]

Bases: Obstacle

An Obstacle in a ContinuousWorld whose shape is a polygon described by a list of vertex coordinates.

class agents.Dirt[source]

Bases: Thing

A piece of dirt that a vacuum agent can clean up.

class agents.VacuumEnvironment(width=10, height=10)[source]

Bases: XYEnvironment

The 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.

thing_classes()[source]

Return the Thing/Agent classes that may populate the vacuum world.

percept(agent)[source]

The percept is a tuple of (‘Dirty’ or ‘Clean’, ‘Bump’ or ‘None’). Unlike the TrivialVacuumEnvironment, location is NOT perceived.

execute_action(agent, action)[source]

Carry out the agent’s action. ‘Suck’ removes dirt at the agent’s location and adds 100 to its performance; movement actions are delegated to the XYEnvironment, and every action other than ‘NoOp’ costs 1 performance point.

class agents.TrivialVacuumEnvironment[source]

Bases: Environment

This 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.

thing_classes()[source]

Return the Thing/Agent classes that may populate this vacuum world.

percept(agent)[source]

Returns the agent’s location, and the location status (Dirty/Clean).

execute_action(agent, action)[source]

Change agent’s location and/or location’s status; track performance. Score 10 for each dirt cleaned; -1 for each move.

default_location(thing)[source]

Agents start in either location at random.

class agents.Gold[source]

Bases: Thing

The gold the explorer is trying to grab in the Wumpus World.

class agents.Bump[source]

Bases: Thing

Percept signalling that the explorer walked into a wall.

class agents.Glitter[source]

Bases: Thing

Percept indicating that gold is in the explorer’s current room.

class agents.Pit[source]

Bases: Thing

A pit that kills the explorer if entered.

class agents.Breeze[source]

Bases: Thing

Percept felt in rooms adjacent to a Pit.

class agents.Arrow[source]

Bases: Thing

The single arrow the explorer can shoot to try to kill the wumpus.

class agents.Scream[source]

Bases: Thing

Percept heard throughout the world when the wumpus is killed.

class agents.Wumpus(program=None)[source]

Bases: Agent

The wumpus: a monster that kills the explorer sharing its room.

screamed = False
class agents.Stench[source]

Bases: Thing

Percept smelled in rooms adjacent to the Wumpus.

class agents.Explorer(program=None)[source]

Bases: Agent

The agent that explores the Wumpus World, seeking gold while avoiding pits and the wumpus.

holding = []
has_arrow = True
killed_by = ''
direction = <agents.Direction object>
can_grab(thing)[source]

Explorer can only grab gold

class agents.WumpusEnvironment(agent_program, width=6, height=6)[source]

Bases: XYEnvironment

A 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
init_world(program)[source]

Spawn items in the world based on probabilities from the book

get_world(show_walls=True)[source]

Return the items in the world

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]

execute_action(agent, action)[source]

Modify the state of the environment based on the agent’s actions. Performance score taken directly out of the book.

in_danger(agent)[source]

Check if Explorer is in danger (Pit or Wumpus), if he is, kill him

is_done()[source]

The game is over when the Explorer is killed or if he climbs out of the cave only at (1,1).

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: object

This 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).

is_alive()[source]

Things that are ‘alive’ should return true.

show_state()[source]

Display the agent’s internal state. Subclasses should override.

display(canvas, x, y, width, height)[source]

Display an image of this Thing on the canvas.

class agents4e.Agent(program=None)[source]

Bases: Thing

An 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.

can_grab(thing)[source]

Return True if this agent can grab this thing. Override for appropriate subclasses of Agent and Thing.

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.rule_match(state, rules)[source]

Find the first rule that matches 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: object

Abstract 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.

thing_classes()[source]

Return the list of Thing subclasses that may appear in this environment.

percept(agent)[source]

Return the percept that the agent sees at this point. (Implement this.)

execute_action(agent, action)[source]

Change the world to reflect this action. (Implement this.)

default_location(thing)[source]

Default location to place a new thing with unspecified location.

exogenous_change()[source]

If there is spontaneous change in the world, override this.

is_done()[source]

By default, we’re done when we can’t find a live agent.

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.

run(steps=1000)[source]

Run the Environment for given number of time steps.

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).

add_thing(thing, location=None)[source]

Add a thing to the environment, setting its location. For convenience, if thing is an agent program we make a new agent for it. (Shouldn’t need to override this.)

delete_thing(thing)[source]

Remove a thing from the environment.

class agents4e.Direction(direction)[source]

Bases: object

A 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'
move_forward(from_location)[source]
>>> d = Direction('up')
>>> l1 = d.move_forward((0, 0))
>>> l1
(0, -1)
>>> d = Direction(Direction.R)
>>> l1 = d.move_forward((0, 0))
>>> l1
(1, 0)
class agents4e.XYEnvironment(width=10, height=10)[source]

Bases: Environment

This 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
things_near(location, radius=None)[source]

Return all things within radius of location.

percept(agent)[source]

By default, agent perceives things within a default radius.

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’).

default_location(thing)[source]

Return a random inbounds location that contains no Obstacle.

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)

delete_thing(thing)[source]

Deletes thing, and everything it is holding (if thing is an agent)

add_walls()[source]

Put walls around the entire perimeter of the grid.

add_observer(observer)[source]

Adds an observer to the list of observers. An observer is typically an EnvGUI.

Each observer is notified of changes in move_to and add_thing, by calling the observer’s methods thing_moved(thing) and thing_added(thing, loc).

turn_heading(heading, inc)[source]

Return the heading to the left (inc=+1) or right (inc=-1) of heading.

class agents4e.Obstacle[source]

Bases: Thing

Something that can cause a bump, preventing an agent from moving into the same square it’s in.

class agents4e.Wall[source]

Bases: Obstacle

An 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: XYEnvironment

An 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.

update(delay=1)[source]

Pause for delay seconds, then redraw the world in the GUI.

reveal()[source]

Display the BlockGrid for this world - the last thing to be added at a location defines the location color.

draw_world()[source]

Repaint the BlockGrid, colouring each occupied cell by the class of the last thing at that location using the configured color mapping.

conceal()[source]

Hide the BlockGrid for this world

class agents4e.ContinuousWorld(width=10, height=10)[source]

Bases: Environment

Model for Continuous World

add_obstacle(coordinates)[source]

Add a PolygonObstacle defined by the given list of vertex coordinates.

class agents4e.PolygonObstacle(coordinates)[source]

Bases: Obstacle

An Obstacle in a ContinuousWorld whose shape is a polygon described by a list of vertex coordinates.

class agents4e.Dirt[source]

Bases: Thing

A piece of dirt that a vacuum agent can clean up.

class agents4e.VacuumEnvironment(width=10, height=10)[source]

Bases: XYEnvironment

The 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.

thing_classes()[source]

Return the Thing/Agent classes that may populate the vacuum world.

percept(agent)[source]

The percept is a tuple of (‘Dirty’ or ‘Clean’, ‘Bump’ or ‘None’). Unlike the TrivialVacuumEnvironment, location is NOT perceived.

execute_action(agent, action)[source]

Carry out the agent’s action. ‘Suck’ removes dirt at the agent’s location and adds 100 to its performance; movement actions are delegated to the XYEnvironment, and every action other than ‘NoOp’ costs 1 performance point.

class agents4e.TrivialVacuumEnvironment[source]

Bases: Environment

This 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.

thing_classes()[source]

Return the Thing/Agent classes that may populate this vacuum world.

percept(agent)[source]

Returns the agent’s location, and the location status (Dirty/Clean).

execute_action(agent, action)[source]

Change agent’s location and/or location’s status; track performance. Score 10 for each dirt cleaned; -1 for each move.

default_location(thing)[source]

Agents start in either location at random.

class agents4e.Gold[source]

Bases: Thing

The gold the explorer is trying to grab in the Wumpus World.

class agents4e.Bump[source]

Bases: Thing

Percept signalling that the explorer walked into a wall.

class agents4e.Glitter[source]

Bases: Thing

Percept indicating that gold is in the explorer’s current room.

class agents4e.Pit[source]

Bases: Thing

A pit that kills the explorer if entered.

class agents4e.Breeze[source]

Bases: Thing

Percept felt in rooms adjacent to a Pit.

class agents4e.Arrow[source]

Bases: Thing

The single arrow the explorer can shoot to try to kill the wumpus.

class agents4e.Scream[source]

Bases: Thing

Percept heard throughout the world when the wumpus is killed.

class agents4e.Wumpus(program=None)[source]

Bases: Agent

The wumpus: a monster that kills the explorer sharing its room.

screamed = False
class agents4e.Stench[source]

Bases: Thing

Percept smelled in rooms adjacent to the Wumpus.

class agents4e.Explorer(program=None)[source]

Bases: Agent

The agent that explores the Wumpus World, seeking gold while avoiding pits and the wumpus.

holding = []
has_arrow = True
killed_by = ''
direction = <agents4e.Direction object>
can_grab(thing)[source]

Explorer can only grab gold

class agents4e.WumpusEnvironment(agent_program, width=6, height=6)[source]

Bases: XYEnvironment

A 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
init_world(program)[source]

Spawn items in the world based on probabilities from the book

get_world(show_walls=True)[source]

Return the items in the world

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]

execute_action(agent, action)[source]

Modify the state of the environment based on the agent’s actions. Performance score taken directly out of the book.

in_danger(agent)[source]

Check if Explorer is in danger (Pit or Wumpus), if he is, kill him

is_done()[source]

The game is over when the Explorer is killed or if he climbs out of the cave only at (1,1).

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

agents4e.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