From 2d46c5699d06648ac9887027ba62c57557388c5f Mon Sep 17 00:00:00 2001 From: Rajat Jain <1997.rajatjain@gmail.com> Date: Thu, 14 Feb 2019 17:51:47 +0530 Subject: [PATCH 1/3] Added WumpusWorld testcases Added: - Testcases in test_agents.py Modified: - Duplicate walls are not added in corners now --- agents.py | 2 +- tests/test_agents.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/agents.py b/agents.py index f7ccb255b..d9e1f7e8d 100644 --- a/agents.py +++ b/agents.py @@ -540,7 +540,7 @@ def add_walls(self): for x in range(self.width): self.add_thing(Wall(), (x, 0)) self.add_thing(Wall(), (x, self.height - 1)) - for y in range(self.height): + for y in range(1, self.height-1): self.add_thing(Wall(), (0, y)) self.add_thing(Wall(), (self.width - 1, y)) diff --git a/tests/test_agents.py b/tests/test_agents.py index dd390fc89..61d90a489 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -4,6 +4,7 @@ from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment, compare_agents,\ RandomVacuumAgent, TableDrivenVacuumAgent, TableDrivenAgentProgram, RandomAgentProgram, \ SimpleReflexAgentProgram, ModelBasedReflexAgentProgram, rule_match +from agents import Wall, Gold, Explorer, Thing, Bump, Glitter, WumpusEnvironment, Pit random.seed("aima-python") @@ -264,3 +265,54 @@ def constant_prog(percept): agent = Agent(constant_prog) result = agent.program(5) assert result == 5 + +def test_WumpusEnvironment(): + def constant_prog(percept): + return percept + # Initialize Wumpus Environment + w = WumpusEnvironment(constant_prog) + + #Check if things are added properly + assert len([x for x in w.things if isinstance(x, Wall)]) == 20 + assert any(map(lambda x: isinstance(x, Gold), w.things)) + assert any(map(lambda x: isinstance(x, Explorer), w.things)) + assert not any(map(lambda x: not isinstance(x,Thing), w.things)) + + #Check that gold and wumpus are not present on (1,1) + assert not any(map(lambda x: isinstance(x, Gold) or isinstance(x,WumpusEnvironment), + w.list_things_at((1,1)))) + + #Check if w.get_world() segments objects correctly + assert len(w.get_world()) == 6 + for row in w.get_world(): + assert len(row) == 6 + + #Start the game! + agent = [x for x in w.things if isinstance(x, Explorer)][0] + gold = [x for x in w.things if isinstance(x, Gold)][0] + pit = [x for x in w.things if isinstance(x, Pit)][0] + + assert w.is_done()==False + + #Check Walls + agent.location = (1,2) + percepts = w.percept(agent) + assert len(percepts) == 5 + assert any(map(lambda x: isinstance(x,Bump), percepts[0])) + + #Check Gold + agent.location = gold.location + percepts = w.percept(agent) + assert any(map(lambda x: isinstance(x,Glitter), percepts[4])) + agent.location = (gold.location[0], gold.location[1]+1) + percepts = w.percept(agent) + assert not any(map(lambda x: isinstance(x,Glitter), percepts[4])) + + #Check agent death + agent.location = pit.location + assert w.in_danger(agent) == True + assert agent.alive == False + assert agent.killed_by == Pit.__name__ + assert agent.performance == -1000 + + assert w.is_done()==True \ No newline at end of file From 674f95ff64eac039e1613663bbac712209c5cd73 Mon Sep 17 00:00:00 2001 From: Rajat Jain <1997.rajatjain@gmail.com> Date: Fri, 15 Feb 2019 16:35:07 +0530 Subject: [PATCH 2/3] 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 --- agents.py | 1 + tests/test_agents.py | 58 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/agents.py b/agents.py index d9e1f7e8d..331813c37 100644 --- a/agents.py +++ b/agents.py @@ -719,6 +719,7 @@ def percept(self, agent): return (status, bump) def execute_action(self, agent, action): + agent.bump = False if action == 'Suck': dirt_list = self.list_things_at(agent.location, Dirt) if dirt_list != []: diff --git a/tests/test_agents.py b/tests/test_agents.py index 61d90a489..d847b426a 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -4,7 +4,8 @@ from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment, compare_agents,\ RandomVacuumAgent, TableDrivenVacuumAgent, TableDrivenAgentProgram, RandomAgentProgram, \ SimpleReflexAgentProgram, ModelBasedReflexAgentProgram, rule_match -from agents import Wall, Gold, Explorer, Thing, Bump, Glitter, WumpusEnvironment, Pit +from agents import Wall, Gold, Explorer, Thing, Bump, Glitter, WumpusEnvironment, Pit, \ + VacuumEnvironment, Dirt random.seed("aima-python") @@ -266,6 +267,32 @@ def constant_prog(percept): result = agent.program(5) assert result == 5 +def test_VacuumEnvironment(): + # Initialize Vacuum Environment + v = VacuumEnvironment(6,6) + #Get an agent + agent = ModelBasedVacuumAgent() + agent.direction = Direction(Direction.R) + v.add_thing(agent) + v.add_thing(Dirt(), location=(2,1)) + + # Check if things are added properly + assert len([x for x in v.things if isinstance(x, Wall)]) == 20 + assert len([x for x in v.things if isinstance(x, Dirt)]) == 1 + + #Let the action begin! + assert v.percept(agent) == ("Clean", "None") + v.execute_action(agent, "Forward") + assert v.percept(agent) == ("Dirty", "None") + v.execute_action(agent, "TurnLeft") + v.execute_action(agent, "Forward") + assert v.percept(agent) == ("Dirty", "Bump") + v.execute_action(agent, "Suck") + assert v.percept(agent) == ("Clean", "None") + old_performance = agent.performance + v.execute_action(agent, "NoOp") + assert old_performance == agent.performance + def test_WumpusEnvironment(): def constant_prog(percept): return percept @@ -315,4 +342,33 @@ def constant_prog(percept): assert agent.killed_by == Pit.__name__ assert agent.performance == -1000 + assert w.is_done()==True + +def test_WumpusEnvironmentActions(): + def constant_prog(percept): + return percept + # Initialize Wumpus Environment + w = WumpusEnvironment(constant_prog) + + agent = [x for x in w.things if isinstance(x, Explorer)][0] + gold = [x for x in w.things if isinstance(x, Gold)][0] + pit = [x for x in w.things if isinstance(x, Pit)][0] + + agent.location = (1,1) + assert agent.direction.direction == "right" + w.execute_action(agent, 'TurnRight') + assert agent.direction.direction == "down" + w.execute_action(agent, 'TurnLeft') + assert agent.direction.direction == "right" + w.execute_action(agent, 'Forward') + assert agent.location == (2,1) + + agent.location = gold.location + w.execute_action(agent, 'Grab') + assert agent.holding == [gold] + + agent.location = (1,1) + w.execute_action(agent, 'Climb') + assert not any(map(lambda x: isinstance(x, Explorer), w.things)) + assert w.is_done()==True \ No newline at end of file From ce71d58427d88c6bc6afbf186c453e79d1962af1 Mon Sep 17 00:00:00 2001 From: Rajat Jain <1997.rajatjain@gmail.com> Date: Sat, 16 Feb 2019 07:59:43 +0530 Subject: [PATCH 3/3] Added spaces in tuples to comply with PEP8 --- tests/test_agents.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_agents.py b/tests/test_agents.py index d847b426a..3c133c32a 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -307,7 +307,7 @@ def constant_prog(percept): #Check that gold and wumpus are not present on (1,1) assert not any(map(lambda x: isinstance(x, Gold) or isinstance(x,WumpusEnvironment), - w.list_things_at((1,1)))) + w.list_things_at((1, 1)))) #Check if w.get_world() segments objects correctly assert len(w.get_world()) == 6 @@ -322,7 +322,7 @@ def constant_prog(percept): assert w.is_done()==False #Check Walls - agent.location = (1,2) + agent.location = (1, 2) percepts = w.percept(agent) assert len(percepts) == 5 assert any(map(lambda x: isinstance(x,Bump), percepts[0])) @@ -354,20 +354,20 @@ def constant_prog(percept): gold = [x for x in w.things if isinstance(x, Gold)][0] pit = [x for x in w.things if isinstance(x, Pit)][0] - agent.location = (1,1) + agent.location = (1, 1) assert agent.direction.direction == "right" w.execute_action(agent, 'TurnRight') assert agent.direction.direction == "down" w.execute_action(agent, 'TurnLeft') assert agent.direction.direction == "right" w.execute_action(agent, 'Forward') - assert agent.location == (2,1) + assert agent.location == (2, 1) agent.location = gold.location w.execute_action(agent, 'Grab') assert agent.holding == [gold] - agent.location = (1,1) + agent.location = (1, 1) w.execute_action(agent, 'Climb') assert not any(map(lambda x: isinstance(x, Explorer), w.things))