4
4
from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents ,\
5
5
RandomVacuumAgent , TableDrivenVacuumAgent , TableDrivenAgentProgram , RandomAgentProgram , \
6
6
SimpleReflexAgentProgram , ModelBasedReflexAgentProgram , rule_match
7
+ from agents import Wall , Gold , Explorer , Thing , Bump , Glitter , WumpusEnvironment , Pit , \
8
+ VacuumEnvironment , Dirt
7
9
8
10
9
11
random .seed ("aima-python" )
@@ -264,3 +266,109 @@ def constant_prog(percept):
264
266
agent = Agent (constant_prog )
265
267
result = agent .program (5 )
266
268
assert result == 5
269
+
270
+ def test_VacuumEnvironment ():
271
+ # Initialize Vacuum Environment
272
+ v = VacuumEnvironment (6 ,6 )
273
+ #Get an agent
274
+ agent = ModelBasedVacuumAgent ()
275
+ agent .direction = Direction (Direction .R )
276
+ v .add_thing (agent )
277
+ v .add_thing (Dirt (), location = (2 ,1 ))
278
+
279
+ # Check if things are added properly
280
+ assert len ([x for x in v .things if isinstance (x , Wall )]) == 20
281
+ assert len ([x for x in v .things if isinstance (x , Dirt )]) == 1
282
+
283
+ #Let the action begin!
284
+ assert v .percept (agent ) == ("Clean" , "None" )
285
+ v .execute_action (agent , "Forward" )
286
+ assert v .percept (agent ) == ("Dirty" , "None" )
287
+ v .execute_action (agent , "TurnLeft" )
288
+ v .execute_action (agent , "Forward" )
289
+ assert v .percept (agent ) == ("Dirty" , "Bump" )
290
+ v .execute_action (agent , "Suck" )
291
+ assert v .percept (agent ) == ("Clean" , "None" )
292
+ old_performance = agent .performance
293
+ v .execute_action (agent , "NoOp" )
294
+ assert old_performance == agent .performance
295
+
296
+ def test_WumpusEnvironment ():
297
+ def constant_prog (percept ):
298
+ return percept
299
+ # Initialize Wumpus Environment
300
+ w = WumpusEnvironment (constant_prog )
301
+
302
+ #Check if things are added properly
303
+ assert len ([x for x in w .things if isinstance (x , Wall )]) == 20
304
+ assert any (map (lambda x : isinstance (x , Gold ), w .things ))
305
+ assert any (map (lambda x : isinstance (x , Explorer ), w .things ))
306
+ assert not any (map (lambda x : not isinstance (x ,Thing ), w .things ))
307
+
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 ))))
311
+
312
+ #Check if w.get_world() segments objects correctly
313
+ assert len (w .get_world ()) == 6
314
+ for row in w .get_world ():
315
+ assert len (row ) == 6
316
+
317
+ #Start the game!
318
+ agent = [x for x in w .things if isinstance (x , Explorer )][0 ]
319
+ gold = [x for x in w .things if isinstance (x , Gold )][0 ]
320
+ pit = [x for x in w .things if isinstance (x , Pit )][0 ]
321
+
322
+ assert w .is_done ()== False
323
+
324
+ #Check Walls
325
+ agent .location = (1 , 2 )
326
+ percepts = w .percept (agent )
327
+ assert len (percepts ) == 5
328
+ assert any (map (lambda x : isinstance (x ,Bump ), percepts [0 ]))
329
+
330
+ #Check Gold
331
+ agent .location = gold .location
332
+ 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 )
335
+ percepts = w .percept (agent )
336
+ assert not any (map (lambda x : isinstance (x ,Glitter ), percepts [4 ]))
337
+
338
+ #Check agent death
339
+ agent .location = pit .location
340
+ assert w .in_danger (agent ) == True
341
+ assert agent .alive == False
342
+ assert agent .killed_by == Pit .__name__
343
+ assert agent .performance == - 1000
344
+
345
+ assert w .is_done ()== True
346
+
347
+ def test_WumpusEnvironmentActions ():
348
+ def constant_prog (percept ):
349
+ return percept
350
+ # Initialize Wumpus Environment
351
+ w = WumpusEnvironment (constant_prog )
352
+
353
+ agent = [x for x in w .things if isinstance (x , Explorer )][0 ]
354
+ gold = [x for x in w .things if isinstance (x , Gold )][0 ]
355
+ pit = [x for x in w .things if isinstance (x , Pit )][0 ]
356
+
357
+ agent .location = (1 , 1 )
358
+ assert agent .direction .direction == "right"
359
+ w .execute_action (agent , 'TurnRight' )
360
+ assert agent .direction .direction == "down"
361
+ w .execute_action (agent , 'TurnLeft' )
362
+ assert agent .direction .direction == "right"
363
+ w .execute_action (agent , 'Forward' )
364
+ assert agent .location == (2 , 1 )
365
+
366
+ agent .location = gold .location
367
+ w .execute_action (agent , 'Grab' )
368
+ assert agent .holding == [gold ]
369
+
370
+ agent .location = (1 , 1 )
371
+ w .execute_action (agent , 'Climb' )
372
+ assert not any (map (lambda x : isinstance (x , Explorer ), w .things ))
373
+
374
+ assert w .is_done ()== True
0 commit comments