示例#1
0
def test_ignore_irrelevant_tags():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={
                "tagOne": 1,
                "tagTwo": 2
            }
        )
    }
    position_components = PositionDict({
        "counter": Position(x=0, y=0),
        "tag_holder": Position(x=0, y=0)
    })
    tags_components = {"tag_holder": [
        "tagOne", "tagTwo", "tagTwo", "tagThree"]}
    label_components = LabelDict({"p": "player"})
    scores = system.get_constant_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components
    )
    assert scores == {"p": 1}
示例#2
0
def test_multiple_counters_add_score():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter_one": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={}
        ),
        "counter_two": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={}
        ),
    }
    position_components = PositionDict({
        "counter_one": Position(x=0, y=0),
        "counter_two": Position(x=1, y=0)
    })
    tags_components = {}
    label_components = LabelDict({"p": "player"})
    scores = system.get_constant_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components
    )
    assert scores == {"p": 2}
示例#3
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)
示例#4
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 == {}
示例#5
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 == {}
示例#6
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 == {}
示例#7
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")}
示例#8
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": []})
示例#9
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"))
示例#10
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()
示例#11
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)
示例#12
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)
示例#13
0
def test_no_tags_always_give_score():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter": CountTagsScore(
            addTo="player",
            scoreType="constant",
            score=1,
            tags={}
        )
    }
    position_components = PositionDict({
        "counter": Position(x=0, y=0)
    })
    tags_components = {}
    label_components = LabelDict({"p": "player"})
    scores = system.get_constant_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components
    )
    assert scores == {"p": 1}
示例#14
0
def test_additive_score_type_no_effect_if_no_score():
    system = CountTagsScoreSystem()
    count_tags_score_components = {
        "counter": CountTagsScore(
            addTo="player",
            scoreType="additive",
            score=1,
            tags={}
        )
    }
    position_components = PositionDict({
        "counter": Position(x=0, y=0)
    })
    tags_components = {}
    label_components = LabelDict({"p": "player"})
    score_components = {}
    system.add_tag_scores(
        count_tags_score_components, position_components, tags_components,
        label_components, score_components
    )
    assert score_components == {}