11import random
2- from agents import Direction
2+
33from agents import Agent
4- from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents ,\
5- RandomVacuumAgent , TableDrivenVacuumAgent , TableDrivenAgentProgram , RandomAgentProgram , \
6- SimpleReflexAgentProgram , ModelBasedReflexAgentProgram , rule_match
4+ from agents import Direction
5+ from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents , \
6+ RandomVacuumAgent , TableDrivenVacuumAgent , TableDrivenAgentProgram , RandomAgentProgram , \
7+ SimpleReflexAgentProgram , ModelBasedReflexAgentProgram
78from agents import Wall , Gold , Explorer , Thing , Bump , Glitter , WumpusEnvironment , Pit , \
8- VacuumEnvironment , Dirt
9-
9+ VacuumEnvironment , Dirt
1010
1111random .seed ("aima-python" )
1212
@@ -58,12 +58,12 @@ def test_add():
5858 assert l2 .direction == Direction .D
5959
6060
61- def test_RandomAgentProgram () :
62- #create a list of all the actions a vacuum cleaner can perform
61+ def test_RandomAgentProgram ():
62+ # create a list of all the actions a vacuum cleaner can perform
6363 list = ['Right' , 'Left' , 'Suck' , 'NoOp' ]
6464 # create a program and then an object of the RandomAgentProgram
6565 program = RandomAgentProgram (list )
66-
66+
6767 agent = Agent (program )
6868 # create an object of TrivialVacuumEnvironment
6969 environment = TrivialVacuumEnvironment ()
@@ -72,10 +72,10 @@ def test_RandomAgentProgram() :
7272 # run the environment
7373 environment .run ()
7474 # check final status of the environment
75- assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
75+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
7676
7777
78- def test_RandomVacuumAgent () :
78+ def test_RandomVacuumAgent ():
7979 # create an object of the RandomVacuumAgent
8080 agent = RandomVacuumAgent ()
8181 # create an object of TrivialVacuumEnvironment
@@ -85,7 +85,7 @@ def test_RandomVacuumAgent() :
8585 # run the environment
8686 environment .run ()
8787 # check final status of the environment
88- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
88+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
8989
9090
9191def test_TableDrivenAgent ():
@@ -109,22 +109,22 @@ def test_TableDrivenAgent():
109109 # create an object of TrivialVacuumEnvironment
110110 environment = TrivialVacuumEnvironment ()
111111 # initializing some environment status
112- environment .status = {loc_A :'Dirty' , loc_B :'Dirty' }
112+ environment .status = {loc_A : 'Dirty' , loc_B : 'Dirty' }
113113 # add agent to the environment
114114 environment .add_thing (agent )
115115
116116 # run the environment by single step everytime to check how environment evolves using TableDrivenAgentProgram
117- environment .run (steps = 1 )
118- assert environment .status == {(1 ,0 ): 'Clean' , (0 ,0 ): 'Dirty' }
117+ environment .run (steps = 1 )
118+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Dirty' }
119119
120- environment .run (steps = 1 )
121- assert environment .status == {(1 ,0 ): 'Clean' , (0 ,0 ): 'Dirty' }
120+ environment .run (steps = 1 )
121+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Dirty' }
122122
123- environment .run (steps = 1 )
124- assert environment .status == {(1 ,0 ): 'Clean' , (0 ,0 ): 'Clean' }
123+ environment .run (steps = 1 )
124+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
125125
126126
127- def test_ReflexVacuumAgent () :
127+ def test_ReflexVacuumAgent ():
128128 # create an object of the ReflexVacuumAgent
129129 agent = ReflexVacuumAgent ()
130130 # create an object of TrivialVacuumEnvironment
@@ -134,31 +134,31 @@ def test_ReflexVacuumAgent() :
134134 # run the environment
135135 environment .run ()
136136 # check final status of the environment
137- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
137+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
138138
139139
140140def test_SimpleReflexAgentProgram ():
141141 class Rule :
142-
142+
143143 def __init__ (self , state , action ):
144144 self .__state = state
145145 self .action = action
146-
146+
147147 def matches (self , state ):
148148 return self .__state == state
149-
149+
150150 loc_A = (0 , 0 )
151151 loc_B = (1 , 0 )
152-
152+
153153 # create rules for a two state Vacuum Environment
154154 rules = [Rule ((loc_A , "Dirty" ), "Suck" ), Rule ((loc_A , "Clean" ), "Right" ),
155- Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
156-
155+ Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
156+
157157 def interpret_input (state ):
158158 return state
159-
159+
160160 # create a program and then an object of the SimpleReflexAgentProgram
161- program = SimpleReflexAgentProgram (rules , interpret_input )
161+ program = SimpleReflexAgentProgram (rules , interpret_input )
162162 agent = Agent (program )
163163 # create an object of TrivialVacuumEnvironment
164164 environment = TrivialVacuumEnvironment ()
@@ -167,7 +167,7 @@ def interpret_input(state):
167167 # run the environment
168168 environment .run ()
169169 # check final status of the environment
170- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
170+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
171171
172172
173173def test_ModelBasedReflexAgentProgram ():
@@ -185,7 +185,7 @@ def matches(self, state):
185185
186186 # create rules for a two-state vacuum environment
187187 rules = [Rule ((loc_A , "Dirty" ), "Suck" ), Rule ((loc_A , "Clean" ), "Right" ),
188- Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
188+ Rule ((loc_B , "Dirty" ), "Suck" ), Rule ((loc_B , "Clean" ), "Left" )]
189189
190190 def update_state (state , action , percept , model ):
191191 return percept
@@ -203,7 +203,7 @@ def update_state(state, action, percept, model):
203203 assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
204204
205205
206- def test_ModelBasedVacuumAgent () :
206+ def test_ModelBasedVacuumAgent ():
207207 # create an object of the ModelBasedVacuumAgent
208208 agent = ModelBasedVacuumAgent ()
209209 # create an object of TrivialVacuumEnvironment
@@ -213,10 +213,10 @@ def test_ModelBasedVacuumAgent() :
213213 # run the environment
214214 environment .run ()
215215 # check final status of the environment
216- assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
216+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ) : 'Clean' }
217217
218218
219- def test_TableDrivenVacuumAgent () :
219+ def test_TableDrivenVacuumAgent ():
220220 # create an object of the TableDrivenVacuumAgent
221221 agent = TableDrivenVacuumAgent ()
222222 # create an object of the TrivialVacuumEnvironment
@@ -226,10 +226,10 @@ def test_TableDrivenVacuumAgent() :
226226 # run the environment
227227 environment .run ()
228228 # check final status of the environment
229- assert environment .status == {(1 , 0 ):'Clean' , (0 , 0 ):'Clean' }
229+ assert environment .status == {(1 , 0 ): 'Clean' , (0 , 0 ): 'Clean' }
230230
231231
232- def test_compare_agents () :
232+ def test_compare_agents ():
233233 environment = TrivialVacuumEnvironment
234234 agents = [ModelBasedVacuumAgent , ReflexVacuumAgent ]
235235
@@ -263,90 +263,96 @@ def test_TableDrivenAgentProgram():
263263def test_Agent ():
264264 def constant_prog (percept ):
265265 return percept
266+
266267 agent = Agent (constant_prog )
267268 result = agent .program (5 )
268269 assert result == 5
269270
271+
270272def test_VacuumEnvironment ():
271273 # Initialize Vacuum Environment
272- v = VacuumEnvironment (6 ,6 )
273- #Get an agent
274+ v = VacuumEnvironment (6 , 6 )
275+ # Get an agent
274276 agent = ModelBasedVacuumAgent ()
275277 agent .direction = Direction (Direction .R )
276278 v .add_thing (agent )
277- v .add_thing (Dirt (), location = (2 ,1 ))
279+ v .add_thing (Dirt (), location = (2 , 1 ))
278280
279281 # Check if things are added properly
280282 assert len ([x for x in v .things if isinstance (x , Wall )]) == 20
281283 assert len ([x for x in v .things if isinstance (x , Dirt )]) == 1
282284
283- #Let the action begin!
285+ # Let the action begin!
284286 assert v .percept (agent ) == ("Clean" , "None" )
285287 v .execute_action (agent , "Forward" )
286288 assert v .percept (agent ) == ("Dirty" , "None" )
287289 v .execute_action (agent , "TurnLeft" )
288290 v .execute_action (agent , "Forward" )
289291 assert v .percept (agent ) == ("Dirty" , "Bump" )
290292 v .execute_action (agent , "Suck" )
291- assert v .percept (agent ) == ("Clean" , "None" )
293+ assert v .percept (agent ) == ("Clean" , "None" )
292294 old_performance = agent .performance
293295 v .execute_action (agent , "NoOp" )
294296 assert old_performance == agent .performance
295297
298+
296299def test_WumpusEnvironment ():
297300 def constant_prog (percept ):
298301 return percept
302+
299303 # Initialize Wumpus Environment
300304 w = WumpusEnvironment (constant_prog )
301305
302- #Check if things are added properly
306+ # Check if things are added properly
303307 assert len ([x for x in w .things if isinstance (x , Wall )]) == 20
304308 assert any (map (lambda x : isinstance (x , Gold ), w .things ))
305309 assert any (map (lambda x : isinstance (x , Explorer ), w .things ))
306- assert not any (map (lambda x : not isinstance (x ,Thing ), w .things ))
310+ assert not any (map (lambda x : not isinstance (x , Thing ), w .things ))
307311
308- #Check that gold and wumpus are not present on (1,1)
309- assert not any (map (lambda x : isinstance (x , Gold ) or isinstance (x ,WumpusEnvironment ),
310- w .list_things_at ((1 , 1 ))))
312+ # Check that gold and wumpus are not present on (1,1)
313+ assert not any (map (lambda x : isinstance (x , Gold ) or isinstance (x , WumpusEnvironment ),
314+ w .list_things_at ((1 , 1 ))))
311315
312- #Check if w.get_world() segments objects correctly
316+ # Check if w.get_world() segments objects correctly
313317 assert len (w .get_world ()) == 6
314318 for row in w .get_world ():
315319 assert len (row ) == 6
316320
317- #Start the game!
321+ # Start the game!
318322 agent = [x for x in w .things if isinstance (x , Explorer )][0 ]
319323 gold = [x for x in w .things if isinstance (x , Gold )][0 ]
320324 pit = [x for x in w .things if isinstance (x , Pit )][0 ]
321325
322- assert w .is_done ()== False
326+ assert w .is_done () == False
323327
324- #Check Walls
328+ # Check Walls
325329 agent .location = (1 , 2 )
326330 percepts = w .percept (agent )
327331 assert len (percepts ) == 5
328- assert any (map (lambda x : isinstance (x ,Bump ), percepts [0 ]))
332+ assert any (map (lambda x : isinstance (x , Bump ), percepts [0 ]))
329333
330- #Check Gold
334+ # Check Gold
331335 agent .location = gold .location
332336 percepts = w .percept (agent )
333- assert any (map (lambda x : isinstance (x ,Glitter ), percepts [4 ]))
334- agent .location = (gold .location [0 ], gold .location [1 ]+ 1 )
337+ assert any (map (lambda x : isinstance (x , Glitter ), percepts [4 ]))
338+ agent .location = (gold .location [0 ], gold .location [1 ] + 1 )
335339 percepts = w .percept (agent )
336- assert not any (map (lambda x : isinstance (x ,Glitter ), percepts [4 ]))
340+ assert not any (map (lambda x : isinstance (x , Glitter ), percepts [4 ]))
337341
338- #Check agent death
342+ # Check agent death
339343 agent .location = pit .location
340344 assert w .in_danger (agent ) == True
341345 assert agent .alive == False
342346 assert agent .killed_by == Pit .__name__
343347 assert agent .performance == - 1000
344348
345- assert w .is_done ()== True
349+ assert w .is_done () == True
350+
346351
347352def test_WumpusEnvironmentActions ():
348353 def constant_prog (percept ):
349354 return percept
355+
350356 # Initialize Wumpus Environment
351357 w = WumpusEnvironment (constant_prog )
352358
@@ -371,4 +377,4 @@ def constant_prog(percept):
371377 w .execute_action (agent , 'Climb' )
372378 assert not any (map (lambda x : isinstance (x , Explorer ), w .things ))
373379
374- assert w .is_done ()== True
380+ assert w .is_done () == True
0 commit comments