Skip to content

Commit 0c222e1

Browse files
ad71norvig
authored andcommitted
Notebook updates (aimacode#942)
* Added notebook section for DTAgentProgram * Updated README.md * Minor * Added TODO to fix pomdp tests * Added notebook section on AC3 * Added doctests to agents.py * Fixed pomdp tests * Fixed a doctest * Fixed pomdp test * Added doctests for rl.py * Fixed NameError in rl.py doctests * Fixed NameError in rl.py doctests * Minor fixes * Minor fixes * Fixed ImportErrors * Fixed all doctests
1 parent 7140ac1 commit 0c222e1

File tree

6 files changed

+2293
-568
lines changed

6 files changed

+2293
-568
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
8888
| 5.3 | Minimax-Decision | `minimax_decision` | [`games.py`][games] | Done | Included |
8989
| 5.7 | Alpha-Beta-Search | `alphabeta_search` | [`games.py`][games] | Done | Included |
9090
| 6 | CSP | `CSP` | [`csp.py`][csp] | Done | Included |
91-
| 6.3 | AC-3 | `AC3` | [`csp.py`][csp] | Done | |
91+
| 6.3 | AC-3 | `AC3` | [`csp.py`][csp] | Done | Included |
9292
| 6.5 | Backtracking-Search | `backtracking_search` | [`csp.py`][csp] | Done | Included |
9393
| 6.8 | Min-Conflicts | `min_conflicts` | [`csp.py`][csp] | Done | Included |
9494
| 6.11 | Tree-CSP-Solver | `tree_csp_solver` | [`csp.py`][csp] | Done | Included |
@@ -118,7 +118,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
118118
| 11.8 | Angelic-Search | `angelic_search` | [`planning.py`][planning] | Done | Included |
119119
| 11.10 | Doubles-tennis | `double_tennis_problem` | [`planning.py`][planning] | Done | Included |
120120
| 13 | Discrete Probability Distribution | `ProbDist` | [`probability.py`][probability] | Done | Included |
121-
| 13.1 | DT-Agent | `DTAgent` | [`probability.py`][probability] | | |
121+
| 13.1 | DT-Agent | `DTAgent` | [`probability.py`][probability] | Done | Included |
122122
| 14.9 | Enumeration-Ask | `enumeration_ask` | [`probability.py`][probability] | Done | Included |
123123
| 14.11 | Elimination-Ask | `elimination_ask` | [`probability.py`][probability] | Done | Included |
124124
| 14.13 | Prior-Sample | `prior_sample` | [`probability.py`][probability] | Done | Included |
@@ -133,7 +133,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
133133
| 17.7 | Policy-Iteration | `policy_iteration` | [`mdp.py`][mdp] | Done | Included |
134134
| 17.9 | POMDP-Value-Iteration | `pomdp_value_iteration` | [`mdp.py`][mdp] | Done | Included |
135135
| 18.5 | Decision-Tree-Learning | `DecisionTreeLearner` | [`learning.py`][learning] | Done | Included |
136-
| 18.8 | Cross-Validation | `cross_validation` | [`learning.py`][learning] | | |
136+
| 18.8 | Cross-Validation | `cross_validation` | [`learning.py`][learning]\* | | |
137137
| 18.11 | Decision-List-Learning | `DecisionListLearner` | [`learning.py`][learning]\* | | |
138138
| 18.24 | Back-Prop-Learning | `BackPropagationLearner` | [`learning.py`][learning] | Done | Included |
139139
| 18.34 | AdaBoost | `AdaBoost` | [`learning.py`][learning] | Done | Included |

agents.py

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ def program(percept):
131131

132132

133133
def RandomAgentProgram(actions):
134-
"""An agent that chooses an action at random, ignoring all percepts."""
134+
"""An agent that chooses an action at random, ignoring all percepts.
135+
>>> list = ['Right', 'Left', 'Suck', 'NoOp']
136+
>>> program = RandomAgentProgram(list)
137+
>>> agent = Agent(program)
138+
>>> environment = TrivialVacuumEnvironment()
139+
>>> environment.add_thing(agent)
140+
>>> environment.run()
141+
>>> environment.status == {(1, 0): 'Clean' , (0, 0): 'Clean'}
142+
True
143+
"""
135144
return lambda percept: random.choice(actions)
136145

137146
# ______________________________________________________________________________
@@ -171,7 +180,14 @@ def rule_match(state, rules):
171180

172181

173182
def RandomVacuumAgent():
174-
"""Randomly choose one of the actions from the vacuum environment."""
183+
"""Randomly choose one of the actions from the vacuum environment.
184+
>>> agent = RandomVacuumAgent()
185+
>>> environment = TrivialVacuumEnvironment()
186+
>>> environment.add_thing(agent)
187+
>>> environment.run()
188+
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
189+
True
190+
"""
175191
return Agent(RandomAgentProgram(['Right', 'Left', 'Suck', 'NoOp']))
176192

177193

@@ -192,7 +208,14 @@ def TableDrivenVacuumAgent():
192208

193209

194210
def ReflexVacuumAgent():
195-
"""A reflex agent for the two-state vacuum environment. [Figure 2.8]"""
211+
"""A reflex agent for the two-state vacuum environment. [Figure 2.8]
212+
>>> agent = ReflexVacuumAgent()
213+
>>> environment = TrivialVacuumEnvironment()
214+
>>> environment.add_thing(agent)
215+
>>> environment.run()
216+
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
217+
True
218+
"""
196219
def program(percept):
197220
location, status = percept
198221
if status == 'Dirty':
@@ -205,7 +228,14 @@ def program(percept):
205228

206229

207230
def ModelBasedVacuumAgent():
208-
"""An agent that keeps track of what locations are clean or dirty."""
231+
"""An agent that keeps track of what locations are clean or dirty.
232+
>>> agent = ModelBasedVacuumAgent()
233+
>>> environment = TrivialVacuumEnvironment()
234+
>>> environment.add_thing(agent)
235+
>>> environment.run()
236+
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
237+
True
238+
"""
209239
model = {loc_A: None, loc_B: None}
210240

211241
def program(percept):
@@ -342,6 +372,22 @@ def __init__(self, direction):
342372
self.direction = direction
343373

344374
def __add__(self, heading):
375+
"""
376+
>>> d = Direction('right')
377+
>>> l1 = d.__add__(Direction.L)
378+
>>> l2 = d.__add__(Direction.R)
379+
>>> l1.direction
380+
'up'
381+
>>> l2.direction
382+
'down'
383+
>>> d = Direction('down')
384+
>>> l1 = d.__add__('right')
385+
>>> l2 = d.__add__('left')
386+
>>> l1.direction == Direction.L
387+
True
388+
>>> l2.direction == Direction.R
389+
True
390+
"""
345391
if self.direction == self.R:
346392
return{
347393
self.R: Direction(self.D),
@@ -364,6 +410,16 @@ def __add__(self, heading):
364410
}.get(heading, None)
365411

366412
def move_forward(self, from_location):
413+
"""
414+
>>> d = Direction('up')
415+
>>> l1 = d.move_forward((0, 0))
416+
>>> l1
417+
(0, -1)
418+
>>> d = Direction(Direction.R)
419+
>>> l1 = d.move_forward((0, 0))
420+
>>> l1
421+
(1, 0)
422+
"""
367423
x, y = from_location
368424
if self.direction == self.R:
369425
return (x + 1, y)
@@ -940,14 +996,30 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
940996
"""See how well each of several agents do in n instances of an environment.
941997
Pass in a factory (constructor) for environments, and several for agents.
942998
Create n instances of the environment, and run each agent in copies of
943-
each one for steps. Return a list of (agent, average-score) tuples."""
999+
each one for steps. Return a list of (agent, average-score) tuples.
1000+
>>> environment = TrivialVacuumEnvironment
1001+
>>> agents = [ModelBasedVacuumAgent, ReflexVacuumAgent]
1002+
>>> result = compare_agents(environment, agents)
1003+
>>> performance_ModelBasedVacummAgent = result[0][1]
1004+
>>> performance_ReflexVacummAgent = result[1][1]
1005+
>>> performance_ReflexVacummAgent <= performance_ModelBasedVacummAgent
1006+
True
1007+
"""
9441008
envs = [EnvFactory() for i in range(n)]
9451009
return [(A, test_agent(A, steps, copy.deepcopy(envs)))
9461010
for A in AgentFactories]
9471011

9481012

9491013
def test_agent(AgentFactory, steps, envs):
950-
"""Return the mean score of running an agent in each of the envs, for steps"""
1014+
"""Return the mean score of running an agent in each of the envs, for steps
1015+
>>> def constant_prog(percept):
1016+
... return percept
1017+
...
1018+
>>> agent = Agent(constant_prog)
1019+
>>> result = agent.program(5)
1020+
>>> result == 5
1021+
True
1022+
"""
9511023
def score(env):
9521024
agent = AgentFactory()
9531025
env.add_thing(agent)

0 commit comments

Comments
 (0)