diff --git a/agents.py b/agents.py index 047eb3fd6..ff049aa4a 100644 --- a/agents.py +++ b/agents.py @@ -85,10 +85,10 @@ def __init__(self, program=None): self.bump = False self.holding = [] self.performance = 0 - if program is None: + if program is None or not isinstance(program, collections.Callable): + print("Can't find a valid program for {}, falling back to default.".format(self.__class__.__name__)) def program(percept): return eval(input('Percept={}; action? '.format(percept))) - assert isinstance(program, collections.Callable) self.program = program def can_grab(self, thing): @@ -297,12 +297,14 @@ def add_thing(self, thing, location=None): for it. (Shouldn't need to override this.""" if not isinstance(thing, Thing): thing = Agent(thing) - assert thing not in self.things, "Don't add the same thing twice" - thing.location = location if location is not None else self.default_location(thing) - self.things.append(thing) - if isinstance(thing, Agent): - thing.performance = 0 - self.agents.append(thing) + if thing in self.things: + print("Can't add the same thing twice") + else: + thing.location = location if location is not None else self.default_location(thing) + self.things.append(thing) + if isinstance(thing, Agent): + thing.performance = 0 + self.agents.append(thing) def delete_thing(self, thing): """Remove a thing from the environment.""" diff --git a/tests/test_agents.py b/tests/test_agents.py index 0162a78b8..699e317f7 100644 --- a/tests/test_agents.py +++ b/tests/test_agents.py @@ -1,4 +1,5 @@ from agents import Direction +from agents import Agent from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment @@ -65,3 +66,9 @@ def test_ModelBasedVacuumAgent() : # check final status of the environment assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'} +def test_Agent(): + def constant_prog(percept): + return percept + agent = Agent(constant_prog) + result = agent.program(5) + assert result == 5