@@ -74,15 +74,11 @@ class Agent(Object):
74
74
the performance measure of the agent in its environment."""
75
75
76
76
def __init__ (self ):
77
- self .program = self .make_agent_program ()
78
77
self .alive = True
79
78
self .bump = False
80
-
81
- def make_agent_program (self ):
82
-
83
79
def program (percept ):
84
80
return raw_input ('Percept=%s; action? ' % percept )
85
- return program
81
+ self . program = program
86
82
87
83
def can_grab (self , obj ):
88
84
"""Returns True if this agent can grab this object.
@@ -111,29 +107,21 @@ def __init__(self, table):
111
107
"Supply as table a dictionary of all {percept_sequence:action} pairs."
112
108
## The agent program could in principle be a function, but because
113
109
## it needs to store state, we make it a callable instance of a class.
114
- self .table = table
115
110
super (TableDrivenAgent , self ).__init__ ()
116
-
117
- def make_agent_program (self ):
118
- table = self .table
119
111
percepts = []
120
112
def program (percept ):
121
113
percepts .append (percept )
122
114
action = table .get (tuple (percepts ))
123
115
return action
124
- return program
116
+ self . program = program
125
117
126
118
127
119
class RandomAgent (Agent ):
128
120
"An agent that chooses an action at random, ignoring all percepts."
129
121
130
122
def __init__ (self , actions ):
131
- self .actions = actions
132
123
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 )
137
125
138
126
139
127
#______________________________________________________________________________
@@ -143,12 +131,13 @@ def make_agent_program(self):
143
131
class ReflexVacuumAgent (Agent ):
144
132
"A reflex agent for the two-state vacuum environment. [Fig. 2.8]"
145
133
146
- def make_agent_program (self ):
134
+ def __init__ (self ):
135
+ super (ReflexVacuumAgent , self ).__init__ ()
147
136
def program ((location , status )):
148
137
if status == 'Dirty' : return 'Suck'
149
138
elif location == loc_A : return 'Right'
150
139
elif location == loc_B : return 'Left'
151
- return program
140
+ self . program = program
152
141
153
142
def RandomVacuumAgent ():
154
143
"Randomly choose one of the actions from the vacuum environment."
@@ -175,19 +164,16 @@ class ModelBasedVacuumAgent(Agent):
175
164
"An agent that keeps track of what locations are clean or dirty."
176
165
177
166
def __init__ (self ):
178
- self .model = {loc_A : None , loc_B : None }
179
167
super (ModelBasedVacuumAgent , self ).__init__ ()
180
-
181
- def make_agent_program (self ):
182
- model = self .model
168
+ model = {loc_A : None , loc_B : None }
183
169
def program ((location , status )):
184
170
"Same as ReflexVacuumAgent, except if everything is clean, do NoOp"
185
171
model [location ] = status ## Update the model here
186
172
if model [loc_A ] == model [loc_B ] == 'Clean' : return 'NoOp'
187
173
elif status == 'Dirty' : return 'Suck'
188
174
elif location == loc_A : return 'Right'
189
175
elif location == loc_B : return 'Left'
190
- return program
176
+ self . program = program
191
177
192
178
#______________________________________________________________________________
193
179
@@ -468,36 +454,23 @@ class SimpleReflexAgent(Agent):
468
454
"""This agent takes action based solely on the percept. [Fig. 2.13]"""
469
455
470
456
def __init__ (self , rules , interpret_input ):
471
- self .rules = rules
472
- self .interpret_input = interpret_input
473
457
super (SimpleReflexAgent , self ).__init__ ()
474
-
475
- def make_agent_program (self ):
476
- rules = self .rules
477
- interpret_input = self .interpret_input
478
458
def program (percept ):
479
459
state = interpret_input (percept )
480
460
rule = rule_match (state , rules )
481
461
action = rule .action
482
462
return action
483
- return program
463
+ self . program = program
484
464
485
465
class ReflexAgentWithState (Agent ):
486
466
"""This agent takes action based on the percept and state. [Fig. 2.16]"""
487
467
488
468
def __init__ (self , rules , update_state ):
489
- self .rules = rules
490
- self .update_state = update_state
491
469
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 ]
498
471
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 )
501
474
action = rule .action
502
475
return action
503
476
return program
0 commit comments