Beispiel #1
0
 def __init__(self, entities=None, **kwargs):
     super().__init__(**kwargs)
     # Entities
     self.entity_ids = []
     # Components
     self.position_components = PositionDict()
     self.ai_components = {}
     self.perception_components = {}
     self.score_components = {}
     self.blocks_movement_components = {}
     self.swappable_components = {}
     self.pickupper_components = {}
     self.inventory_components = {}
     self.pickup_components = {}
     self.looks_like_components = {}
     self.tags_components = {}
     self.label_components = LabelDict()
     self.vulnerable_components = {}
     self.count_tags_score_components = {}
     self.actions_components = {}
     # Systems
     self.percept_system = PerceptSystem()
     self.action_system = ActionSystem()
     self.tag_system = TagSystem()
     self.movement_system = MovementSystem()
     self.pick_up_system = PickUpSystem()
     self.drop_system = DropSystem()
     self.attack_system = AttackSystem()
     self.count_tags_score_system = CountTagsScoreSystem()
     if entities is not None:
         for identifier, entity in entities.items():
             entity_obj = entity if isinstance(entity, Entity) else Entity(
                 **entity)
             self.add_entity(entity_obj, entity_id=identifier)
Beispiel #2
0
def test_no_action_penalty_if_no_score_component():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai_components = {"a": MockAI(Move(direction="right"))}
    percepts = {}
    actions_components = {"a": {"move": ActionDetails(cost=3)}}
    score_components = {}
    label_components = LabelDict({})
    system.get_actions(ai_components, percepts, actions_components,
                       score_components, label_components, random_generator)
    assert score_components == {}
Beispiel #3
0
def test_only_include_allowed_actions():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai_components = {"a": MockAI(Move(direction="right"))}
    percepts = {}
    actions_components = {}
    score_components = {}
    label_components = LabelDict({})
    actions = system.get_actions(ai_components, percepts, actions_components,
                                 score_components, label_components,
                                 random_generator)
    assert actions == {}
Beispiel #4
0
def test_do_not_include_do_nothing():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai_components = {"a": MockAI(DoNothing())}
    percepts = {}
    actions_components = {"a": {"none": ActionDetails()}}
    score_components = {}
    label_components = LabelDict({})
    actions = system.get_actions(ai_components, percepts, actions_components,
                                 score_components, label_components,
                                 random_generator)
    assert actions == {}
Beispiel #5
0
def test_get_action_from_ai():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai_components = {"a": MockAI(Move(direction="right"))}
    percepts = {}
    actions_components = {"a": {"move": ActionDetails()}}
    score_components = {}
    label_components = LabelDict({})
    actions = system.get_actions(ai_components, percepts, actions_components,
                                 score_components, label_components,
                                 random_generator)
    assert actions == {"a": Move(direction="right")}
Beispiel #6
0
def test_call_update_state_percept():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai = FullMockAI(DoNothing())
    ai_components = {"a": ai}
    percepts = {"a": {"entities": []}}
    actions_components = {}
    score_components = {}
    label_components = LabelDict({})
    with mock.patch.object(ai, "update_state_percept") as update_state_percept:
        system.get_actions(ai_components, percepts, actions_components,
                           score_components, label_components,
                           random_generator)
        assert update_state_percept.call_args == mock.call({"entities": []})
Beispiel #7
0
def test_call_update_state_action():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai = FullMockAI(Move(direction="right"))
    ai_components = {"a": ai}
    percepts = {}
    actions_components = {"a": {"move": ActionDetails()}}
    score_components = {}
    label_components = LabelDict({})
    with mock.patch.object(ai, "update_state_action") as update_state_action:
        system.get_actions(ai_components, percepts, actions_components,
                           score_components, label_components,
                           random_generator)
        assert update_state_action.call_args == mock.call(
            Move(action_type="move", direction="right"))
Beispiel #8
0
def test_do_profiling():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai_components = {"a": MockAI(DoNothing())}
    percepts = {}
    actions_components = {}
    score_components = {}
    label_components = LabelDict({"a": "player"})
    time_profiling.start()
    memory_profiling.start()
    system.get_actions(ai_components, percepts, actions_components,
                       score_components, label_components, random_generator)
    time_profiling.stop()
    memory_profiling.stop()
    assert "player" in time_profiling.get_result()["contexts"]
    assert "player" in memory_profiling.get_result()
Beispiel #9
0
def test_call_next_action_with_percept_and_generator():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai = FullMockAI(DoNothing())
    ai_components = {"a": ai}
    percepts = {"a": {"entities": []}}
    actions_components = {}
    score_components = {}
    label_components = LabelDict({})
    with mock.patch.object(ai, "next_action",
                           wraps=ai.next_action) as next_action:
        system.get_actions(ai_components, percepts, actions_components,
                           score_components, label_components,
                           random_generator)
        assert next_action.call_args == mock.call({"entities": []},
                                                  random_generator)
Beispiel #10
0
def test_no_percept_means_empty_dict():
    system = ActionSystem()
    random_generator = random.Random(123)
    ai = FullMockAI(DoNothing())
    ai_components = {"a": ai}
    percepts = {}
    actions_components = {}
    score_components = {}
    label_components = LabelDict({})
    with mock.patch.object(ai, "update_state_percept") as update_state_percept:
        with mock.patch.object(ai, "next_action",
                               wraps=ai.next_action) as next_action:
            system.get_actions(ai_components, percepts, actions_components,
                               score_components, label_components,
                               random_generator)
            assert update_state_percept.call_args == mock.call({})
            assert next_action.call_args == mock.call({}, random_generator)