@@ -362,7 +362,7 @@ def __add__(self, heading):
362
362
}.get (heading , None )
363
363
364
364
def move_forward (self , from_location ):
365
- x ,y = from_location
365
+ x , y = from_location
366
366
if self .direction == self .R :
367
367
return (x + 1 , y )
368
368
elif self .direction == self .L :
@@ -389,11 +389,12 @@ def __init__(self, width=10, height=10):
389
389
self .width = width
390
390
self .height = height
391
391
self .observers = []
392
- #Sets iteration start and end (no walls).
393
- self .x_start ,self .y_start = (0 ,0 )
394
- self .x_end ,self .y_end = (self .width , self .height )
392
+ # Sets iteration start and end (no walls).
393
+ self .x_start , self .y_start = (0 , 0 )
394
+ self .x_end , self .y_end = (self .width , self .height )
395
395
396
396
perceptible_distance = 1
397
+
397
398
def things_near (self , location , radius = None ):
398
399
"Return all things within radius of location."
399
400
if radius is None :
@@ -447,7 +448,7 @@ def move_to(self, thing, destination):
447
448
# for obs in self.observers:
448
449
# obs.thing_added(thing)
449
450
450
- def add_thing (self , thing , location = (1 , 1 ), exclude_duplicate_class_items = False ):
451
+ def add_thing (self , thing , location = (1 , 1 ), exclude_duplicate_class_items = False ):
451
452
'''Adds things to the world.
452
453
If (exclude_duplicate_class_items) then the item won't be added if the location
453
454
has at least one item of the same class'''
@@ -462,7 +463,7 @@ def is_inbounds(self, location):
462
463
x ,y = location
463
464
return not (x < self .x_start or x >= self .x_end or y < self .y_start or y >= self .y_end )
464
465
465
- def random_location_inbounds (self , exclude = None ):
466
+ def random_location_inbounds (self , exclude = None ):
466
467
'''Returns a random location that is inbounds (within walls if we have walls)'''
467
468
location = (random .randint (self .x_start , self .x_end ), random .randint (self .y_start , self .y_end ))
468
469
if exclude is not None :
@@ -486,14 +487,14 @@ def add_walls(self):
486
487
'''Put walls around the entire perimeter of the grid.'''
487
488
for x in range (self .width ):
488
489
self .add_thing (Wall (), (x , 0 ))
489
- self .add_thing (Wall (), (x , self .height - 1 ))
490
+ self .add_thing (Wall (), (x , self .height - 1 ))
490
491
for y in range (self .height ):
491
492
self .add_thing (Wall (), (0 , y ))
492
- self .add_thing (Wall (), (self .width - 1 , y ))
493
+ self .add_thing (Wall (), (self .width - 1 , y ))
493
494
494
- #Updates iteration start and end (with walls).
495
- self .x_start ,self .y_start = (1 ,1 )
496
- self .x_end ,self .y_end = (self .width - 1 , self .height - 1 )
495
+ # Updates iteration start and end (with walls).
496
+ self .x_start , self .y_start = (1 , 1 )
497
+ self .x_end , self .y_end = (self .width - 1 , self .height - 1 )
497
498
498
499
def add_observer (self , observer ):
499
500
"""Adds an observer to the list of observers.
@@ -662,6 +663,7 @@ class Wumpus(Agent):
662
663
class Stench (Thing ):
663
664
pass
664
665
666
+
665
667
class Explorer (Agent ):
666
668
holding = []
667
669
has_arrow = True
@@ -674,8 +676,9 @@ def can_grab(self, thing):
674
676
675
677
676
678
class WumpusEnvironment (XYEnvironment ):
677
- pit_probability = 0.2 #Probability to spawn a pit in a location. (From Chapter 7.2)
678
- #Room should be 4x4 grid of rooms. The extra 2 for walls
679
+ pit_probability = 0.2 # Probability to spawn a pit in a location. (From Chapter 7.2)
680
+ # Room should be 4x4 grid of rooms. The extra 2 for walls
681
+
679
682
def __init__ (self , agent_program , width = 6 , height = 6 ):
680
683
super (WumpusEnvironment , self ).__init__ (width , height )
681
684
self .init_world (agent_program )
@@ -690,47 +693,46 @@ def init_world(self, program):
690
693
for x in range (self .x_start , self .x_end ):
691
694
for y in range (self .y_start , self .y_end ):
692
695
if random .random () < self .pit_probability :
693
- self .add_thing (Pit (), (x ,y ), True )
694
- self .add_thing (Breeze (), (x - 1 ,y ), True )
695
- self .add_thing (Breeze (), (x ,y - 1 ), True )
696
- self .add_thing (Breeze (), (x + 1 ,y ), True )
697
- self .add_thing (Breeze (), (x ,y + 1 ), True )
696
+ self .add_thing (Pit (), (x , y ), True )
697
+ self .add_thing (Breeze (), (x - 1 , y ), True )
698
+ self .add_thing (Breeze (), (x , y - 1 ), True )
699
+ self .add_thing (Breeze (), (x + 1 , y ), True )
700
+ self .add_thing (Breeze (), (x , y + 1 ), True )
698
701
699
702
"WUMPUS"
700
- w_x , w_y = self .random_location_inbounds (exclude = (1 ,1 ))
703
+ w_x , w_y = self .random_location_inbounds (exclude = (1 , 1 ))
701
704
self .add_thing (Wumpus (lambda x : "" ), (w_x , w_y ), True )
702
705
self .add_thing (Stench (), (w_x - 1 , w_y ), True )
703
706
self .add_thing (Stench (), (w_x + 1 , w_y ), True )
704
707
self .add_thing (Stench (), (w_x , w_y - 1 ), True )
705
708
self .add_thing (Stench (), (w_x , w_y + 1 ), True )
706
709
707
710
"GOLD"
708
- self .add_thing (Gold (), self .random_location_inbounds (exclude = (1 ,1 )), True )
711
+ self .add_thing (Gold (), self .random_location_inbounds (exclude = (1 , 1 )), True )
709
712
#self.add_thing(Gold(), (2,1), True) Making debugging a whole lot easier
710
713
711
714
"AGENT"
712
- self .add_thing (Explorer (program ), (1 ,1 ), True )
715
+ self .add_thing (Explorer (program ), (1 , 1 ), True )
713
716
714
- def get_world (self , show_walls = True ):
717
+ def get_world (self , show_walls = True ):
715
718
'''returns the items in the world'''
716
719
result = []
717
- x_start ,y_start = (0 ,0 ) if show_walls else (1 ,1 )
718
- x_end ,y_end = (self .width , self .height ) if show_walls else (self .width - 1 , self .height - 1 )
720
+ x_start , y_start = (0 , 0 ) if show_walls else (1 , 1 )
721
+ x_end , y_end = (self .width , self .height ) if show_walls else (self .width - 1 , self .height - 1 )
719
722
for x in range (x_start , x_end ):
720
723
row = []
721
724
for y in range (y_start , y_end ):
722
- row .append (self .list_things_at ((x ,y )))
725
+ row .append (self .list_things_at ((x , y )))
723
726
result .append (row )
724
727
return result
725
728
726
- def percepts_from (self , agent , location , tclass = Thing ):
729
+ def percepts_from (self , agent , location , tclass = Thing ):
727
730
'''Returns percepts from a given location, and replaces some items with percepts from chapter 7.'''
728
731
thing_percepts = {
729
732
Gold : Glitter (),
730
733
Wall : Bump (),
731
734
Wumpus : Stench (),
732
- Pit : Breeze ()
733
- }
735
+ Pit : Breeze ()}
734
736
'''Agents don't need to get their percepts'''
735
737
thing_percepts [agent .__class__ ] = None
736
738
@@ -740,19 +742,19 @@ def percepts_from(self, agent, location, tclass = Thing):
740
742
741
743
742
744
result = [thing_percepts .get (thing .__class__ , thing ) for thing in self .things
743
- if thing .location == location and isinstance (thing , tclass )]
745
+ if thing .location == location and isinstance (thing , tclass )]
744
746
return result if len (result ) else [None ]
745
747
746
748
def percept (self , agent ):
747
749
'''Returns things in adjacent (not diagonal) cells of the agent.
748
750
Result format: [Left, Right, Up, Down, Center / Current location]'''
749
- x ,y = agent .location
751
+ x , y = agent .location
750
752
result = []
751
- result .append (self .percepts_from (agent , (x - 1 ,y )))
752
- result .append (self .percepts_from (agent , (x + 1 ,y )))
753
- result .append (self .percepts_from (agent , (x ,y - 1 )))
754
- result .append (self .percepts_from (agent , (x ,y + 1 )))
755
- result .append (self .percepts_from (agent , (x ,y )))
753
+ result .append (self .percepts_from (agent , (x - 1 , y )))
754
+ result .append (self .percepts_from (agent , (x + 1 , y )))
755
+ result .append (self .percepts_from (agent , (x , y - 1 )))
756
+ result .append (self .percepts_from (agent , (x , y + 1 )))
757
+ result .append (self .percepts_from (agent , (x , y )))
756
758
757
759
'''The wumpus gives out a a loud scream once it's killed.'''
758
760
wumpus = [thing for thing in self .things if isinstance (thing , Wumpus )]
@@ -781,14 +783,14 @@ def execute_action(self, agent, action):
781
783
agent .performance -= 1
782
784
elif action == 'Grab' :
783
785
things = [thing for thing in self .list_things_at (agent .location )
784
- if agent .can_grab (thing )]
786
+ if agent .can_grab (thing )]
785
787
if len (things ):
786
788
print ("Grabbing" , things [0 ].__class__ .__name__ )
787
789
if len (things ):
788
790
agent .holding .append (things [0 ])
789
791
agent .performance -= 1
790
792
elif action == 'Climb' :
791
- if agent .location == (1 ,1 ): # Agent can only climb out of (1,1)
793
+ if agent .location == (1 , 1 ): # Agent can only climb out of (1,1)
792
794
agent .performance += 1000 if Gold () in agent .holding else 0
793
795
self .delete_thing (agent )
794
796
elif action == 'Shoot' :
@@ -831,6 +833,7 @@ def is_done(self):
831
833
#Almost done. Arrow needs to be implemented
832
834
# ______________________________________________________________________________
833
835
836
+
834
837
def compare_agents (EnvFactory , AgentFactories , n = 10 , steps = 1000 ):
835
838
"""See how well each of several agents do in n instances of an environment.
836
839
Pass in a factory (constructor) for environments, and several for agents.
0 commit comments