1+ import random
12from agents import Direction
23from agents import Agent
3- from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment
4+ from agents import ReflexVacuumAgent , ModelBasedVacuumAgent , TrivialVacuumEnvironment , compare_agents ,\
5+ RandomVacuumAgent
6+
7+
8+ random .seed ("aima-python" )
49
510
611def test_move_forward ():
@@ -50,6 +55,19 @@ def test_add():
5055 assert l2 .direction == Direction .D
5156
5257
58+ def test_RandomVacuumAgent () :
59+ # create an object of the RandomVacuumAgent
60+ agent = RandomVacuumAgent ()
61+ # create an object of TrivialVacuumEnvironment
62+ environment = TrivialVacuumEnvironment ()
63+ # add agent to the environment
64+ environment .add_thing (agent )
65+ # run the environment
66+ environment .run ()
67+ # check final status of the environment
68+ assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
69+
70+
5371def test_ReflexVacuumAgent () :
5472 # create an object of the ReflexVacuumAgent
5573 agent = ReflexVacuumAgent ()
@@ -76,6 +94,23 @@ def test_ModelBasedVacuumAgent() :
7694 assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
7795
7896
97+ def test_compare_agents () :
98+ environment = TrivialVacuumEnvironment
99+ agents = [ModelBasedVacuumAgent , ReflexVacuumAgent ]
100+
101+ result = compare_agents (environment , agents )
102+ performance_ModelBasedVacummAgent = result [0 ][1 ]
103+ performance_ReflexVacummAgent = result [1 ][1 ]
104+
105+ # The performance of ModelBasedVacuumAgent will be at least as good as that of
106+ # ReflexVacuumAgent, since ModelBasedVacuumAgent can identify when it has
107+ # reached the terminal state (both locations being clean) and will perform
108+ # NoOp leading to 0 performance change, whereas ReflexVacuumAgent cannot
109+ # identify the terminal state and thus will keep moving, leading to worse
110+ # performance compared to ModelBasedVacuumAgent.
111+ assert performance_ReflexVacummAgent <= performance_ModelBasedVacummAgent
112+
113+
79114def test_Agent ():
80115 def constant_prog (percept ):
81116 return percept
0 commit comments