diff --git a/agents.py b/agents.py index f7ccb255b..331813c37 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)) @@ -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 dd390fc89..3c133c32a 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -4,6 +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, \ + VacuumEnvironment, Dirt random.seed("aima-python") @@ -264,3 +266,109 @@ def constant_prog(percept): agent = Agent(constant_prog) 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 + # 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 + +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
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: