1
+ import random
1
2
from agents import Direction
2
3
from 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" )
4
9
5
10
6
11
def test_move_forward ():
@@ -50,6 +55,19 @@ def test_add():
50
55
assert l2 .direction == Direction .D
51
56
52
57
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
+
53
71
def test_ReflexVacuumAgent () :
54
72
# create an object of the ReflexVacuumAgent
55
73
agent = ReflexVacuumAgent ()
@@ -76,6 +94,23 @@ def test_ModelBasedVacuumAgent() :
76
94
assert environment .status == {(1 ,0 ):'Clean' , (0 ,0 ) : 'Clean' }
77
95
78
96
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
+
79
114
def test_Agent ():
80
115
def constant_prog (percept ):
81
116
return percept
0 commit comments