Skip to content

Commit 5f2e3e6

Browse files
rajatjain1997antmarakis
authored andcommitted
Added test cases for agents.py (aimacode#1057)
* Added WumpusWorld testcases Added: - Testcases in test_agents.py Modified: - Duplicate walls are not added in corners now * Tests for VacuumEnvironment and WumpusEnvironment Added: - Test cases for Explorer actions in WumpusEnvironment - Test cases for VacuumEnvironment Modified: - VacuumAgent correctly disables bump percept when agent sucks dirt after bumping into a wall * Added spaces in tuples to comply with PEP8
1 parent ed67915 commit 5f2e3e6

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

agents.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def add_walls(self):
543543
for x in range(self.width):
544544
self.add_thing(Wall(), (x, 0))
545545
self.add_thing(Wall(), (x, self.height - 1))
546-
for y in range(self.height):
546+
for y in range(1, self.height-1):
547547
self.add_thing(Wall(), (0, y))
548548
self.add_thing(Wall(), (self.width - 1, y))
549549

@@ -714,6 +714,7 @@ def percept(self, agent):
714714
return (status, bump)
715715

716716
def execute_action(self, agent, action):
717+
agent.bump = False
717718
if action == 'Suck':
718719
dirt_list = self.list_things_at(agent.location, Dirt)
719720
if dirt_list != []:

tests/test_agents.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment, compare_agents,\
55
RandomVacuumAgent, TableDrivenVacuumAgent, TableDrivenAgentProgram, RandomAgentProgram, \
66
SimpleReflexAgentProgram, ModelBasedReflexAgentProgram, rule_match
7+
from agents import Wall, Gold, Explorer, Thing, Bump, Glitter, WumpusEnvironment, Pit, \
8+
VacuumEnvironment, Dirt
79

810

911
random.seed("aima-python")
@@ -264,3 +266,109 @@ def constant_prog(percept):
264266
agent = Agent(constant_prog)
265267
result = agent.program(5)
266268
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy