1
- """Implement Agents and Environments (Chapters 1-2).
1
+ """
2
+ Implement Agents and Environments. (Chapters 1-2)
2
3
3
4
The class hierarchies are as follows:
4
5
23
24
EnvToolbar ## contains buttons for controlling EnvGUI
24
25
25
26
EnvCanvas ## Canvas to display the environment of an EnvGUI
26
-
27
27
"""
28
28
29
- # TO DO:
29
+ # TODO
30
30
# Implement grabbing correctly.
31
31
# When an object is grabbed, does it still have a location?
32
32
# What if it is released?
33
33
# What if the grabbed or the grabber is deleted?
34
34
# What if the grabber moves?
35
- #
36
35
# Speed control in GUI does not have any effect -- fix it.
37
36
38
37
from utils4e import distance_squared , turn_heading
@@ -90,8 +89,7 @@ def __init__(self, program=None):
90
89
self .holding = []
91
90
self .performance = 0
92
91
if program is None or not isinstance (program , collections .Callable ):
93
- print ("Can't find a valid program for {}, falling back to default." .format (
94
- self .__class__ .__name__ ))
92
+ print ("Can't find a valid program for {}, falling back to default." .format (self .__class__ .__name__ ))
95
93
96
94
def program (percept ):
97
95
return eval (input ('Percept={}; action? ' .format (percept )))
@@ -122,10 +120,13 @@ def new_program(percept):
122
120
123
121
124
122
def TableDrivenAgentProgram (table ):
125
- """This agent selects an action based on the percept sequence.
123
+ """
124
+ [Figure 2.7]
125
+ This agent selects an action based on the percept sequence.
126
126
It is practical only for tiny domains.
127
127
To customize it, provide as table a dictionary of all
128
- {percept_sequence:action} pairs. [Figure 2.7]"""
128
+ {percept_sequence:action} pairs.
129
+ """
129
130
percepts = []
130
131
131
132
def program (percept ):
@@ -154,7 +155,10 @@ def RandomAgentProgram(actions):
154
155
155
156
156
157
def SimpleReflexAgentProgram (rules , interpret_input ):
157
- """This agent takes action based solely on the percept. [Figure 2.10]"""
158
+ """
159
+ [Figure 2.10]
160
+ This agent takes action based solely on the percept.
161
+ """
158
162
159
163
def program (percept ):
160
164
state = interpret_input (percept )
@@ -166,7 +170,10 @@ def program(percept):
166
170
167
171
168
172
def ModelBasedReflexAgentProgram (rules , update_state , trainsition_model , sensor_model ):
169
- """This agent takes action based on the percept and state. [Figure 2.12]"""
173
+ """
174
+ [Figure 2.12]
175
+ This agent takes action based on the percept and state.
176
+ """
170
177
171
178
def program (percept ):
172
179
program .state = update_state (program .state , program .action , percept , trainsition_model , sensor_model )
@@ -219,7 +226,9 @@ def TableDrivenVacuumAgent():
219
226
220
227
221
228
def ReflexVacuumAgent ():
222
- """A reflex agent for the two-state vacuum environment. [Figure 2.8]
229
+ """
230
+ [Figure 2.8]
231
+ A reflex agent for the two-state vacuum environment.
223
232
>>> agent = ReflexVacuumAgent()
224
233
>>> environment = TrivialVacuumEnvironment()
225
234
>>> environment.add_thing(agent)
@@ -333,8 +342,7 @@ def run(self, steps=1000):
333
342
334
343
def list_things_at (self , location , tclass = Thing ):
335
344
"""Return all things exactly at a given location."""
336
- return [thing for thing in self .things
337
- if thing .location == location and isinstance (thing , tclass )]
345
+ return [thing for thing in self .things if thing .location == location and isinstance (thing , tclass )]
338
346
339
347
def some_things_at (self , location , tclass = Thing ):
340
348
"""Return true if at least one of the things at location
@@ -437,13 +445,13 @@ def move_forward(self, from_location):
437
445
"""
438
446
x , y = from_location
439
447
if self .direction == self .R :
440
- return ( x + 1 , y )
448
+ return x + 1 , y
441
449
elif self .direction == self .L :
442
- return ( x - 1 , y )
450
+ return x - 1 , y
443
451
elif self .direction == self .U :
444
- return ( x , y - 1 )
452
+ return x , y - 1
445
453
elif self .direction == self .D :
446
- return ( x , y + 1 )
454
+ return x , y + 1
447
455
448
456
449
457
class XYEnvironment (Environment ):
@@ -498,7 +506,7 @@ def execute_action(self, agent, action):
498
506
agent .holding .pop ()
499
507
500
508
def default_location (self , thing ):
501
- return ( random .choice (self .width ), random .choice (self .height ) )
509
+ return random .choice (self .width ), random .choice (self .height )
502
510
503
511
def move_to (self , thing , destination ):
504
512
"""Move a thing to a new location. Returns True on success or False if there is an Obstacle.
@@ -724,7 +732,7 @@ def percept(self, agent):
724
732
status = ('Dirty' if self .some_things_at (
725
733
agent .location , Dirt ) else 'Clean' )
726
734
bump = ('Bump' if agent .bump else 'None' )
727
- return ( status , bump )
735
+ return status , bump
728
736
729
737
def execute_action (self , agent , action ):
730
738
agent .bump = False
@@ -753,12 +761,11 @@ def __init__(self):
753
761
loc_B : random .choice (['Clean' , 'Dirty' ])}
754
762
755
763
def thing_classes (self ):
756
- return [Wall , Dirt , ReflexVacuumAgent , RandomVacuumAgent ,
757
- TableDrivenVacuumAgent , ModelBasedVacuumAgent ]
764
+ return [Wall , Dirt , ReflexVacuumAgent , RandomVacuumAgent , TableDrivenVacuumAgent , ModelBasedVacuumAgent ]
758
765
759
766
def percept (self , agent ):
760
767
"""Returns the agent's location, and the location status (Dirty/Clean)."""
761
- return ( agent .location , self .status [agent .location ])
768
+ return agent .location , self .status [agent .location ]
762
769
763
770
def execute_action (self , agent , action ):
764
771
"""Change agent's location and/or location's status; track performance.
@@ -994,8 +1001,7 @@ def is_done(self):
994
1001
print ("Death by {} [-1000]." .format (explorer [0 ].killed_by ))
995
1002
else :
996
1003
print ("Explorer climbed out {}."
997
- .format (
998
- "with Gold [+1000]!" if Gold () not in self .things else "without Gold [+0]" ))
1004
+ .format ("with Gold [+1000]!" if Gold () not in self .things else "without Gold [+0]" ))
999
1005
return True
1000
1006
1001
1007
# TODO: Arrow needs to be implemented
0 commit comments