Пример #1
0
def test_move_correctly_modifies_agent_state(data: st.DataObject) -> None:
    """ Makes sure they actually move or STAY. """
    # TODO: Handle out-of-bounds errors.
    # TODO: Consider making the environment toroidal.
    env = data.draw(bst.envs())
    env.reset()
    old_locations: Dict[int, Tuple[int, int]] = {}
    for agent_id, agent in env.agents.items():
        old_locations[agent_id] = agent.pos
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))
    executed_dict = env._move(tuple_action_dict)

    # TODO: Consider making ``env.LEFT``, etc tuples which can be added to existing
    # positions rather than just integers.
    for agent_id, action in executed_dict.items():
        agent = env.agents[agent_id]
        move = action[0]
        old_pos = old_locations[agent_id]
        if move == env.UP or move == env.DOWN:
            assert agent.pos[0] == old_pos[0]
        if move == env.LEFT or move == env.RIGHT:
            assert agent.pos[1] == old_pos[1]
        if move == env.UP:
            assert agent.pos[1] == old_pos[1] + 1
        if move == env.DOWN:
            assert agent.pos[1] == old_pos[1] - 1
        if move == env.RIGHT:
            assert agent.pos[0] == old_pos[0] + 1
        if move == env.LEFT:
            assert agent.pos[0] == old_pos[0] - 1
        if move == env.STAY:
            assert agent.pos == old_pos
Пример #2
0
def test_consume_removes_nothing_else(data: st.DataObject) -> None:
    """ Otherwise, food should remain in place. """
    env = data.draw(bst.envs())
    env.reset()
    food_obj_type_id = env.obj_type_ids["food"]
    agent_food_positions: Dict[int, Tuple[int, int]] = {}
    for agent_id, agent in env.agents.items():
        if env.grid[agent.pos + (food_obj_type_id,)] == 1:
            agent_food_positions[agent_id] = agent.pos
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))

    eating_positions: List[Tuple[int, int]] = []
    for agent_id, pos in agent_food_positions.items():
        if tuple_action_dict[agent_id][1] == env.EAT:
            eating_positions.append(pos)

    food_positions: Set[Tuple[int, int]] = set()
    for x in range(env.width):
        for y in range(env.height):
            if env.grid[(x, y) + (food_obj_type_id,)] == 1:
                food_positions.add((x, y))
    persistent_food_positions = food_positions - set(eating_positions)

    env._consume(tuple_action_dict)

    for pos in persistent_food_positions:
        assert env.grid[pos + (food_obj_type_id,)] == 1
Пример #3
0
def test_mate_adds_children_to_agents(data: st.DataObject) -> None:
    """ Makes sure child ids get added to ``env.agents``. """
    env = data.draw(bst.envs())
    env.reset()
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))
    child_ids = env._mate(tuple_action_dict)
    for child_id in child_ids:
        assert child_id in env.agents
Пример #4
0
def test_mate_makes_num_agents_nondecreasing(data: st.DataObject) -> None:
    """ Makes sure ``len(agents)`` is nondecreasing. """
    env = data.draw(bst.envs())
    env.reset()
    old_num_agents = len(env.agents)
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))
    env._mate(tuple_action_dict)
    assert old_num_agents <= len(env.agents)
Пример #5
0
def test_mate_children_are_new(data: st.DataObject) -> None:
    """ Makes sure children are new. """
    env = data.draw(bst.envs())
    env.reset()
    old_agent_memory_addresses = [id(agent) for agent in env.agents.values()]
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))
    child_ids = env._mate(tuple_action_dict)
    for child_id in child_ids:
        assert id(env.agents[child_id]) not in old_agent_memory_addresses
Пример #6
0
def test_move_holds_other_actions_invariant(data: st.DataObject) -> None:
    """ Makes sure the returned action dict only modifies move subaction space. """
    env = data.draw(bst.envs())
    env.reset()

    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))
    executed_dict = env._move(tuple_action_dict)

    pairs = zip(list(tuple_action_dict.values()), list(executed_dict.values()))
    for attempted_action, executed_action in pairs:
        assert attempted_action[1:] == executed_action[1:]
Пример #7
0
def test_move_only_changes_to_stay(data: st.DataObject) -> None:
    """ Makes sure the returned action dict only changes to STAY if at all. """
    env = data.draw(bst.envs())
    env.reset()

    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))
    executed_dict = env._move(tuple_action_dict)

    pairs = zip(list(tuple_action_dict.values()), list(executed_dict.values()))
    for attempted_action, executed_action in pairs:
        if attempted_action[0] != executed_action[0]:
            assert executed_action[0] == env.STAY
Пример #8
0
def test_consume_makes_agent_health_nondecreasing(data: st.DataObject) -> None:
    """ Tests that agent.health in the correct direction. """
    env = data.draw(bst.envs())
    env.reset()
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))

    old_healths: Dict[int, float] = {}
    for agent_id, agent in env.agents.items():
        old_healths[agent_id] = agent.health

    env._consume(tuple_action_dict)

    for agent_id, agent in env.agents.items():
        assert old_healths[agent_id] <= agent.health
Пример #9
0
def test_consume_decreases_num_foods_correctly(data: st.DataObject) -> None:
    """ The ``env.num_foods`` attribute is decremented properly. """
    env = data.draw(bst.envs())
    env.reset()
    food_obj_type_id = env.obj_type_ids["food"]
    agent_food_positions: Dict[int, Tuple[int, int]] = {}
    for agent_id, agent in env.agents.items():
        if env.grid[agent.pos + (food_obj_type_id,)] == 1:
            agent_food_positions[agent_id] = agent.pos
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))

    eating_positions: List[Tuple[int, int]] = []
    for agent_id, pos in agent_food_positions.items():
        if tuple_action_dict[agent_id][1] == env.EAT:
            eating_positions.append(pos)

    old_num_foods = env.num_foods
    env._consume(tuple_action_dict)
    assert old_num_foods - env.num_foods == len(eating_positions)
Пример #10
0
def test_consume_removes_food_when_appropriate(data: st.DataObject) -> None:
    """ If the action is ``EAT`` and there's food, should disappear. """
    env = data.draw(bst.envs())
    env.reset()
    food_obj_type_id = env.obj_type_ids["food"]
    agent_food_positions: Dict[int, Tuple[int, int]] = {}
    for agent_id, agent in env.agents.items():
        if env.grid[agent.pos + (food_obj_type_id,)] == 1:
            agent_food_positions[agent_id] = agent.pos
    tuple_action_dict = data.draw(bst.tuple_action_dicts(env=env))

    eating_positions: List[Tuple[int, int]] = []
    for agent_id, pos in agent_food_positions.items():
        if tuple_action_dict[agent_id][1] == env.EAT:
            eating_positions.append(pos)

    env._consume(tuple_action_dict)

    for pos in eating_positions:
        assert env.grid[pos + (food_obj_type_id,)] == 0