Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5eb77e6

Browse files
committed
Started converting to 3rd edition. agents.py converted plus a little extra cleanup.
1 parent bfb657f commit 5eb77e6

File tree

11 files changed

+57
-36
lines changed

11 files changed

+57
-36
lines changed

agents.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ def program(percept):
122122
return random.choice(actions)
123123
Agent.__init__(self, program)
124124

125+
#______________________________________________________________________________
126+
127+
class SimpleReflexAgent(Agent):
128+
"This agent takes action based solely on the percept. [Fig. 2.10]"
129+
130+
def __init__(self, rules, interpret_input):
131+
def program(percept):
132+
state = interpret_input(percept)
133+
rule = rule_match(state, rules)
134+
action = rule.action
135+
return action
136+
Agent.__init__(self, program)
137+
138+
class ModelBasedReflexAgent(Agent):
139+
"This agent takes action based on the percept and state. [Fig. 2.12]"
140+
141+
def __init__(self, rules, update_state):
142+
def program(percept):
143+
program.state = update_state(program.state, program.action, percept)
144+
rule = rule_match(program.state, rules)
145+
action = rule.action
146+
return action
147+
program.state = program.action = None
148+
Agent.__init__(self, program)
149+
150+
def rule_match(state, rules):
151+
"Find the first rule that matches state."
152+
for rule in rules:
153+
if rule.matches(state):
154+
return rule
125155

126156
#______________________________________________________________________________
127157

@@ -165,7 +195,7 @@ class ModelBasedVacuumAgent(Agent):
165195
def __init__(self):
166196
model = {loc_A: None, loc_B: None}
167197
def program((location, status)):
168-
"Same as ReflexVacuumAgent, except if everything is clean, do NoOp"
198+
"Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
169199
model[location] = status ## Update the model here
170200
if model[loc_A] == model[loc_B] == 'Clean': return 'NoOp'
171201
elif status == 'Dirty': return 'Suck'
@@ -446,37 +476,6 @@ def default_location(self, object):
446476
"Agents start in either location at random."
447477
return random.choice([loc_A, loc_B])
448478

449-
#______________________________________________________________________________
450-
451-
class SimpleReflexAgent(Agent):
452-
"""This agent takes action based solely on the percept. [Fig. 2.13]"""
453-
454-
def __init__(self, rules, interpret_input):
455-
def program(percept):
456-
state = interpret_input(percept)
457-
rule = rule_match(state, rules)
458-
action = rule.action
459-
return action
460-
Agent.__init__(self, program)
461-
462-
class ReflexAgentWithState(Agent):
463-
"""This agent takes action based on the percept and state. [Fig. 2.16]"""
464-
465-
def __init__(self, rules, update_state):
466-
def program(percept):
467-
program.state = update_state(program.state, program.action, percept)
468-
rule = rule_match(program.state, rules)
469-
action = rule.action
470-
return action
471-
program.state = program.action = None
472-
Agent.__init__(self, program)
473-
474-
def rule_match(state, rules):
475-
"Find the first rule that matches state."
476-
for rule in rules:
477-
if rule.matches(state):
478-
return rule
479-
480479
#______________________________________________________________________________
481480
## The Wumpus World
482481

@@ -589,7 +588,8 @@ def __init__(self, parent, env, canvas):
589588

590589
# Create buttons and other controls
591590

592-
for txt, cmd in [('Step >', self.env.step), ('Run >>', self.run),
591+
for txt, cmd in [('Step >', self.env.step),
592+
('Run >>', self.run),
593593
('Stop [ ]', self.stop),
594594
('List objects', self.list_objects),
595595
('List agents', self.list_agents)]:

csp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""CSP (Constraint Satisfaction Problems) problems and solvers. (Chapter 5)."""
22

3+
# (Written for the second edition of AIMA; expect some discrepanciecs
4+
# from the third edition until this gets reviewed.)
5+
36
from __future__ import generators
47
from utils import *
58
import search

games.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Games, or Adversarial Search. (Chapter 6)
22
33
"""
4+
# (Written for the second edition of AIMA; expect some discrepanciecs
5+
# from the third edition until this gets reviewed.)
46

57
from utils import *
68
import random

learning.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Learn to estimate functions from examples. (Chapters 18-20)"""
22

3+
# (Written for the second edition of AIMA; expect some discrepanciecs
4+
# from the third edition until this gets reviewed.)
5+
36
from utils import *
47
import agents, random, operator
58

logic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
unify Do unification of two FOL sentences
2424
diff, simp Symbolic differentiation and simplification
2525
"""
26+
# (Written for the second edition of AIMA; expect some discrepanciecs
27+
# from the third edition until this gets reviewed.)
2628

2729
from __future__ import generators
2830
import re

mdp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
dictionary of {state:number} pairs. We then define the value_iteration
77
and policy_iteration algorithms."""
88

9+
# (Written for the second edition of AIMA; expect some discrepanciecs
10+
# from the third edition until this gets reviewed.)
11+
912
from utils import *
1013

1114
class MDP:

nlp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""A chart parser and some grammars. (Chapter 22)"""
22

3+
# (Written for the second edition of AIMA; expect some discrepanciecs
4+
# from the third edition until this gets reviewed.)
5+
36
from utils import *
47

58
#______________________________________________________________________________

planning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Planning (Chapters 11-12)
1+
"""Planning (Chapters 10-11)
22
"""
33

44
from __future__ import generators

probability.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Probability models. (Chapter 13-15)
22
"""
3+
# (Written for the second edition of AIMA; expect some discrepanciecs
4+
# from the third edition until this gets reviewed.)
35

46
from utils import *
57
from logic import extend

search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
then create problem instances and solve them with calls to the various search
55
functions."""
66

7-
# The future is here, but if you're still in the past, uncomment next line
8-
# from __future__ import generators
7+
# (Written for the second edition of AIMA; expect some discrepanciecs
8+
# from the third edition until this gets reviewed.)
99

1010
from utils import *
1111
import agents

text.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
Then we show a very simple Information Retrieval system, and an example
55
working on a tiny sample of Unix manual pages."""
66

7+
# (Written for the second edition of AIMA; expect some discrepanciecs
8+
# from the third edition until this gets reviewed.)
9+
710
from utils import *
811
from math import log, exp
912
import re, probability, string, search

0 commit comments

Comments
 (0)