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

Skip to content

Commit 08ed75d

Browse files
committed
Moved agent-program definition back into the constructors to get rid of all the copying between parameters, instance vars, and nonlocals. Fixed nonlocal state-var bug in ReflexAgentWithState.
1 parent cd8147d commit 08ed75d

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

agents.py

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,11 @@ class Agent(Object):
7474
the performance measure of the agent in its environment."""
7575

7676
def __init__(self):
77-
self.program = self.make_agent_program()
7877
self.alive = True
7978
self.bump = False
80-
81-
def make_agent_program(self):
82-
8379
def program(percept):
8480
return raw_input('Percept=%s; action? ' % percept)
85-
return program
81+
self.program = program
8682

8783
def can_grab(self, obj):
8884
"""Returns True if this agent can grab this object.
@@ -111,29 +107,21 @@ def __init__(self, table):
111107
"Supply as table a dictionary of all {percept_sequence:action} pairs."
112108
## The agent program could in principle be a function, but because
113109
## it needs to store state, we make it a callable instance of a class.
114-
self.table = table
115110
super(TableDrivenAgent, self).__init__()
116-
117-
def make_agent_program(self):
118-
table = self.table
119111
percepts = []
120112
def program(percept):
121113
percepts.append(percept)
122114
action = table.get(tuple(percepts))
123115
return action
124-
return program
116+
self.program = program
125117

126118

127119
class RandomAgent(Agent):
128120
"An agent that chooses an action at random, ignoring all percepts."
129121

130122
def __init__(self, actions):
131-
self.actions = actions
132123
super(RandomAgent, self).__init__()
133-
134-
def make_agent_program(self):
135-
actions = self.actions
136-
return lambda percept: random.choice(actions)
124+
self.program = lambda percept: random.choice(actions)
137125

138126

139127
#______________________________________________________________________________
@@ -143,12 +131,13 @@ def make_agent_program(self):
143131
class ReflexVacuumAgent(Agent):
144132
"A reflex agent for the two-state vacuum environment. [Fig. 2.8]"
145133

146-
def make_agent_program(self):
134+
def __init__(self):
135+
super(ReflexVacuumAgent, self).__init__()
147136
def program((location, status)):
148137
if status == 'Dirty': return 'Suck'
149138
elif location == loc_A: return 'Right'
150139
elif location == loc_B: return 'Left'
151-
return program
140+
self.program = program
152141

153142
def RandomVacuumAgent():
154143
"Randomly choose one of the actions from the vacuum environment."
@@ -175,19 +164,16 @@ class ModelBasedVacuumAgent(Agent):
175164
"An agent that keeps track of what locations are clean or dirty."
176165

177166
def __init__(self):
178-
self.model = {loc_A: None, loc_B: None}
179167
super(ModelBasedVacuumAgent, self).__init__()
180-
181-
def make_agent_program(self):
182-
model = self.model
168+
model = {loc_A: None, loc_B: None}
183169
def program((location, status)):
184170
"Same as ReflexVacuumAgent, except if everything is clean, do NoOp"
185171
model[location] = status ## Update the model here
186172
if model[loc_A] == model[loc_B] == 'Clean': return 'NoOp'
187173
elif status == 'Dirty': return 'Suck'
188174
elif location == loc_A: return 'Right'
189175
elif location == loc_B: return 'Left'
190-
return program
176+
self.program = program
191177

192178
#______________________________________________________________________________
193179

@@ -468,36 +454,23 @@ class SimpleReflexAgent(Agent):
468454
"""This agent takes action based solely on the percept. [Fig. 2.13]"""
469455

470456
def __init__(self, rules, interpret_input):
471-
self.rules = rules
472-
self.interpret_input = interpret_input
473457
super(SimpleReflexAgent, self).__init__()
474-
475-
def make_agent_program(self):
476-
rules = self.rules
477-
interpret_input = self.interpret_input
478458
def program(percept):
479459
state = interpret_input(percept)
480460
rule = rule_match(state, rules)
481461
action = rule.action
482462
return action
483-
return program
463+
self.program = program
484464

485465
class ReflexAgentWithState(Agent):
486466
"""This agent takes action based on the percept and state. [Fig. 2.16]"""
487467

488468
def __init__(self, rules, update_state):
489-
self.rules = rules
490-
self.update_state = update_state
491469
super(ReflexAgentWithState, self).__init__()
492-
493-
def make_agent_program(self):
494-
rules = self.rules
495-
update_state = self.update_state
496-
state = None
497-
action = None
470+
state = [None]
498471
def program(percept):
499-
state = update_state(state, action, percept)
500-
rule = rule_match(state, rules)
472+
state[0] = update_state(state[0], action, percept)
473+
rule = rule_match(state[0], rules)
501474
action = rule.action
502475
return action
503476
return program

logic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,8 @@ def WalkSAT(clauses, p=0.5, max_flips=10000):
744744

745745
class PLWumpusAgent(agents.Agent):
746746
"An agent for the wumpus world that does logical inference. [Fig. 7.19]"""
747-
def make_agent_program(self):
747+
def __init__(self):
748+
agents.Agent.__init__(self)
748749
KB = FolKB() ## shouldn't this be a propositional KB? ***
749750
x, y, orientation = 1, 1, (1, 0)
750751
visited = set() ## squares already visited

search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ def expand(self, problem):
9090

9191
class SimpleProblemSolvingAgent(agents.Agent):
9292
"""Abstract framework for problem-solving agent. [Fig. 3.1]"""
93-
def make_agent_program(self):
93+
def __init__(self):
9494
# Some nits in this code:
9595
# - We're requiring state to be a list so it can be
9696
# updated as a nonlocal variable.
9797
# - The agent program calls self.foo() methods,
9898
# against the usual policy that agent programs
9999
# be self-contained.
100+
agents.Agent.__init__(self)
100101
state = []
101102
seq = []
102103

@@ -109,7 +110,7 @@ def program(percept):
109110
if not seq: return None
110111
return seq.pop(0)
111112

112-
return program
113+
self.program = program
113114
#______________________________________________________________________________
114115
## Uninformed Search algorithms
115116

0 commit comments

Comments
 (0)