@@ -362,7 +362,7 @@ def __add__(self, heading):
362362 }.get (heading , None )
363363
364364 def move_forward (self , from_location ):
365- x ,y = from_location
365+ x , y = from_location
366366 if self .direction == self .R :
367367 return (x + 1 , y )
368368 elif self .direction == self .L :
@@ -389,11 +389,12 @@ def __init__(self, width=10, height=10):
389389 self .width = width
390390 self .height = height
391391 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 )
395395
396396 perceptible_distance = 1
397+
397398 def things_near (self , location , radius = None ):
398399 "Return all things within radius of location."
399400 if radius is None :
@@ -447,7 +448,7 @@ def move_to(self, thing, destination):
447448 # for obs in self.observers:
448449 # obs.thing_added(thing)
449450
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 ):
451452 '''Adds things to the world.
452453 If (exclude_duplicate_class_items) then the item won't be added if the location
453454 has at least one item of the same class'''
@@ -462,7 +463,7 @@ def is_inbounds(self, location):
462463 x ,y = location
463464 return not (x < self .x_start or x >= self .x_end or y < self .y_start or y >= self .y_end )
464465
465- def random_location_inbounds (self , exclude = None ):
466+ def random_location_inbounds (self , exclude = None ):
466467 '''Returns a random location that is inbounds (within walls if we have walls)'''
467468 location = (random .randint (self .x_start , self .x_end ), random .randint (self .y_start , self .y_end ))
468469 if exclude is not None :
@@ -486,14 +487,14 @@ def add_walls(self):
486487 '''Put walls around the entire perimeter of the grid.'''
487488 for x in range (self .width ):
488489 self .add_thing (Wall (), (x , 0 ))
489- self .add_thing (Wall (), (x , self .height - 1 ))
490+ self .add_thing (Wall (), (x , self .height - 1 ))
490491 for y in range (self .height ):
491492 self .add_thing (Wall (), (0 , y ))
492- self .add_thing (Wall (), (self .width - 1 , y ))
493+ self .add_thing (Wall (), (self .width - 1 , y ))
493494
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 )
497498
498499 def add_observer (self , observer ):
499500 """Adds an observer to the list of observers.
@@ -662,6 +663,7 @@ class Wumpus(Agent):
662663class Stench (Thing ):
663664 pass
664665
666+
665667class Explorer (Agent ):
666668 holding = []
667669 has_arrow = True
@@ -674,8 +676,9 @@ def can_grab(self, thing):
674676
675677
676678class 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+
679682 def __init__ (self , agent_program , width = 6 , height = 6 ):
680683 super (WumpusEnvironment , self ).__init__ (width , height )
681684 self .init_world (agent_program )
@@ -690,47 +693,46 @@ def init_world(self, program):
690693 for x in range (self .x_start , self .x_end ):
691694 for y in range (self .y_start , self .y_end ):
692695 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 )
698701
699702 "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 ))
701704 self .add_thing (Wumpus (lambda x : "" ), (w_x , w_y ), True )
702705 self .add_thing (Stench (), (w_x - 1 , w_y ), True )
703706 self .add_thing (Stench (), (w_x + 1 , w_y ), True )
704707 self .add_thing (Stench (), (w_x , w_y - 1 ), True )
705708 self .add_thing (Stench (), (w_x , w_y + 1 ), True )
706709
707710 "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 )
709712 #self.add_thing(Gold(), (2,1), True) Making debugging a whole lot easier
710713
711714 "AGENT"
712- self .add_thing (Explorer (program ), (1 ,1 ), True )
715+ self .add_thing (Explorer (program ), (1 , 1 ), True )
713716
714- def get_world (self , show_walls = True ):
717+ def get_world (self , show_walls = True ):
715718 '''returns the items in the world'''
716719 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 )
719722 for x in range (x_start , x_end ):
720723 row = []
721724 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 )))
723726 result .append (row )
724727 return result
725728
726- def percepts_from (self , agent , location , tclass = Thing ):
729+ def percepts_from (self , agent , location , tclass = Thing ):
727730 '''Returns percepts from a given location, and replaces some items with percepts from chapter 7.'''
728731 thing_percepts = {
729732 Gold : Glitter (),
730733 Wall : Bump (),
731734 Wumpus : Stench (),
732- Pit : Breeze ()
733- }
735+ Pit : Breeze ()}
734736 '''Agents don't need to get their percepts'''
735737 thing_percepts [agent .__class__ ] = None
736738
@@ -740,19 +742,19 @@ def percepts_from(self, agent, location, tclass = Thing):
740742
741743
742744 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 )]
744746 return result if len (result ) else [None ]
745747
746748 def percept (self , agent ):
747749 '''Returns things in adjacent (not diagonal) cells of the agent.
748750 Result format: [Left, Right, Up, Down, Center / Current location]'''
749- x ,y = agent .location
751+ x , y = agent .location
750752 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 )))
756758
757759 '''The wumpus gives out a a loud scream once it's killed.'''
758760 wumpus = [thing for thing in self .things if isinstance (thing , Wumpus )]
@@ -781,14 +783,14 @@ def execute_action(self, agent, action):
781783 agent .performance -= 1
782784 elif action == 'Grab' :
783785 things = [thing for thing in self .list_things_at (agent .location )
784- if agent .can_grab (thing )]
786+ if agent .can_grab (thing )]
785787 if len (things ):
786788 print ("Grabbing" , things [0 ].__class__ .__name__ )
787789 if len (things ):
788790 agent .holding .append (things [0 ])
789791 agent .performance -= 1
790792 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)
792794 agent .performance += 1000 if Gold () in agent .holding else 0
793795 self .delete_thing (agent )
794796 elif action == 'Shoot' :
@@ -831,6 +833,7 @@ def is_done(self):
831833 #Almost done. Arrow needs to be implemented
832834# ______________________________________________________________________________
833835
836+
834837def compare_agents (EnvFactory , AgentFactories , n = 10 , steps = 1000 ):
835838 """See how well each of several agents do in n instances of an environment.
836839 Pass in a factory (constructor) for environments, and several for agents.
0 commit comments