Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5187c0e

Browse files
antmarakisnorvig
authored andcommitted
Agent Tests (aimacode#560)
* add RandomVacuumAgent + CompareAgents * set random.seed
1 parent 51ae91b commit 5187c0e

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

tests/test_agents.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import random
12
from agents import Direction
23
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")
49

510

611
def 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+
5371
def 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+
79114
def test_Agent():
80115
def constant_prog(percept):
81116
return percept

0 commit comments

Comments
 (0)