@@ -65,7 +65,7 @@ class Agent(Thing):
65
65
"""An Agent is a subclass of Thing with one required slot,
66
66
.program, which should hold a function that takes one argument, the
67
67
percept, and returns an action. (What counts as a percept or action
68
- will depend on the specific environment in which the agent exists.)
68
+ will depend on the specific environment in which the agent exists.)
69
69
Note that 'program' is a slot, not a method. If it were a method,
70
70
then the program could 'cheat' and look at aspects of the agent.
71
71
It's not supposed to do that: the program can only look at the
@@ -87,7 +87,7 @@ def can_grab(self, thing):
87
87
"""Returns True if this agent can grab this thing.
88
88
Override for appropriate subclasses of Agent and Thing."""
89
89
return False
90
-
90
+
91
91
def TraceAgent (agent ):
92
92
"""Wrap the agent's program to print its input and output. This will let
93
93
you see what the agent is doing in the environment."""
@@ -253,7 +253,7 @@ def list_things_at(self, location, tclass=Thing):
253
253
"Return all things exactly at a given location."
254
254
return [thing for thing in self .things
255
255
if thing .location == location and isinstance (thing , tclass )]
256
-
256
+
257
257
def some_things_at (self , location , tclass = Thing ):
258
258
"""Return true if at least one of the things at location
259
259
is an instance of class tclass (or a subclass)."""
@@ -297,7 +297,7 @@ class XYEnvironment(Environment):
297
297
def __init__ (self , width = 10 , height = 10 ):
298
298
super (XYEnvironment , self ).__init__ ()
299
299
update (self , width = width , height = height , observers = [])
300
-
300
+
301
301
def things_near (self , location , radius = None ):
302
302
"Return all things within radius of location."
303
303
if radius is None : radius = self .perceptible_distance
@@ -343,7 +343,7 @@ def move_to(self, thing, destination):
343
343
thing .location = destination
344
344
for o in self .observers :
345
345
o .thing_moved (thing )
346
-
346
+
347
347
def add_thing (self , thing , location = (1 , 1 )):
348
348
super (XYEnvironment , self ).add_thing (thing , location )
349
349
thing .holding = []
@@ -356,7 +356,7 @@ def delete_thing(self, thing):
356
356
# Any more to do? Thing holding anything or being held?
357
357
for obs in self .observers :
358
358
obs .thing_deleted (thing )
359
-
359
+
360
360
def add_walls (self ):
361
361
"Put walls around the entire perimeter of the grid."
362
362
for x in range (self .width ):
@@ -367,14 +367,14 @@ def add_walls(self):
367
367
self .add_thing (Wall (), (self .width - 1 , y ))
368
368
369
369
def add_observer (self , observer ):
370
- """Adds an observer to the list of observers.
370
+ """Adds an observer to the list of observers.
371
371
An observer is typically an EnvGUI.
372
-
372
+
373
373
Each observer is notified of changes in move_to and add_thing,
374
374
by calling the observer's methods thing_moved(thing)
375
375
and thing_added(thing, loc)."""
376
376
self .observers .append (observer )
377
-
377
+
378
378
def turn_heading (self , heading , inc ):
379
379
"Return the heading to the left (inc=+1) or right (inc=-1) of heading."
380
380
return turn_heading (heading , inc )
@@ -388,11 +388,11 @@ class Wall(Obstacle):
388
388
pass
389
389
390
390
#______________________________________________________________________________
391
- ## Vacuum environment
391
+ ## Vacuum environment
392
392
393
393
class Dirt (Thing ):
394
394
pass
395
-
395
+
396
396
class VacuumEnvironment (XYEnvironment ):
397
397
"""The environment of [Ex. 2.12]. Agent perceives dirty or clean,
398
398
and bump (into obstacle) or not; 2D discrete world of unknown size;
@@ -440,9 +440,9 @@ def __init__(self):
440
440
loc_B : random .choice (['Clean' , 'Dirty' ])}
441
441
442
442
def thing_classes (self ):
443
- return [Wall , Dirt , ReflexVacuumAgent , RandomVacuumAgent ,
443
+ return [Wall , Dirt , ReflexVacuumAgent , RandomVacuumAgent ,
444
444
TableDrivenVacuumAgent , ModelBasedVacuumAgent ]
445
-
445
+
446
446
def percept (self , agent ):
447
447
"Returns the agent's location, and the location status (Dirty/Clean)."
448
448
return (agent .location , self .status [agent .location ])
@@ -485,16 +485,16 @@ def thing_classes(self):
485
485
486
486
## Needs a lot of work ...
487
487
488
-
488
+
489
489
#______________________________________________________________________________
490
490
491
491
def compare_agents (EnvFactory , AgentFactories , n = 10 , steps = 1000 ):
492
492
"""See how well each of several agents do in n instances of an environment.
493
493
Pass in a factory (constructor) for environments, and several for agents.
494
- Create n instances of the environment, and run each agent in copies of
494
+ Create n instances of the environment, and run each agent in copies of
495
495
each one for steps. Return a list of (agent, average-score) tuples."""
496
496
envs = [EnvFactory () for i in range (n )]
497
- return [(A , test_agent (A , steps , copy .deepcopy (envs )))
497
+ return [(A , test_agent (A , steps , copy .deepcopy (envs )))
498
498
for A in AgentFactories ]
499
499
500
500
def test_agent (AgentFactory , steps , envs ):
@@ -527,7 +527,7 @@ def score(env):
527
527
## give is a range of expected scores. If this test fails, it does
528
528
## not necessarily mean something is wrong.
529
529
>>> envs = [TrivialVacuumEnvironment() for i in range(100)]
530
- >>> def testv(A): return test_agent(A, 4, copy.deepcopy(envs))
530
+ >>> def testv(A): return test_agent(A, 4, copy.deepcopy(envs))
531
531
>>> 7 < testv(ModelBasedVacuumAgent) < 11
532
532
True
533
533
>>> 5 < testv(ReflexVacuumAgent) < 9
@@ -551,12 +551,12 @@ class EnvGUI(tk.Tk, object):
551
551
def __init__ (self , env , title = 'AIMA GUI' , cellwidth = 50 , n = 10 ):
552
552
553
553
# Initialize window
554
-
554
+
555
555
super (EnvGUI , self ).__init__ ()
556
556
self .title (title )
557
557
558
558
# Create components
559
-
559
+
560
560
canvas = EnvCanvas (self , env , cellwidth , n )
561
561
toolbar = EnvToolbar (self , env , canvas )
562
562
for w in [canvas , toolbar ]:
@@ -569,14 +569,14 @@ def __init__(self, parent, env, canvas):
569
569
super (EnvToolbar , self ).__init__ (parent , relief = 'raised' , bd = 2 )
570
570
571
571
# Initialize instance variables
572
-
572
+
573
573
self .env = env
574
574
self .canvas = canvas
575
575
self .running = False
576
576
self .speed = 1.0
577
577
578
578
# Create buttons and other controls
579
-
579
+
580
580
for txt , cmd in [('Step >' , self .env .step ),
581
581
('Run >>' , self .run ),
582
582
('Stop [ ]' , self .stop ),
@@ -608,7 +608,7 @@ def background_run(self):
608
608
delay_sec = 1.0 / max (self .speed , 1.0 ) # avoid division by zero
609
609
ms = int (1000.0 * delay_sec ) # seconds to milliseconds
610
610
self .after (ms , self .background_run )
611
-
611
+
612
612
def list_things (self ):
613
613
print "Things in the environment:"
614
614
for thing in self .env .things :
@@ -621,4 +621,4 @@ def list_agents(self):
621
621
622
622
def set_speed (self , speed ):
623
623
self .speed = float (speed )
624
-
624
+
0 commit comments