Пример #1
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()
Пример #2
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)
Пример #3
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)
    def next_action(self, percept, random_generator):
        if not self.plan:
            return DoNothing()

        # Parse percept
        missionaries = set((e["x"], e["y"]) for e in percept["entities"]
                           if e["looks_like"] == "coin")
        cannibals = set((e["x"], e["y"]) for e in percept["entities"]
                        if e["looks_like"] == "evilCoin")
        grass = set((e["x"], e["y"]) for e in percept["entities"]
                    if e["looks_like"] == "grass")
        dirt = set((e["x"], e["y"]) for e in percept["entities"]
                   if e["looks_like"] == "dirt")
        empty_shore = (grass | dirt) - missionaries - cannibals
        walls = set((e["x"], e["y"]) for e in percept["entities"]
                    if e["looks_like"] == "wall")
        water = set((e["x"], e["y"]) for e in percept["entities"]
                    if e["looks_like"] == "water")
        goal = self.plan[0]
        # First, ensure inventory is correct
        too_many_missionaries = percept["inventory"].count(
            "coin") > goal.missionaries
        too_many_cannibals = percept["inventory"].count(
            "evilCoin") > goal.cannibals
        if too_many_missionaries or too_many_cannibals:

            if (0, 0) in empty_shore:
                if too_many_missionaries:
                    return Drop(index=percept["inventory"].index("coin"))
                if too_many_cannibals:
                    return Drop(index=percept["inventory"].index("evilCoin"))

            problem = PathfindingProblem((0, 0), empty_shore, walls | water,
                                         [])
            return Move(
                direction=a_star_graph(problem, get_heuristic(problem))[0])

        too_few_missionaries = percept["inventory"].count(
            "coin") < goal.missionaries
        if too_few_missionaries:
            if (0, 0) in missionaries:
                return PickUp()
            problem = PathfindingProblem((0, 0), missionaries, walls | water,
                                         [])
            return Move(
                direction=a_star_graph(problem, get_heuristic(problem))[0])

        too_few_cannibals = percept["inventory"].count(
            "evilCoin") < goal.cannibals
        if too_few_cannibals:
            if (0, 0) in cannibals:
                return PickUp()
            problem = PathfindingProblem((0, 0), cannibals, walls | water, [])
            return Move(
                direction=a_star_graph(problem, get_heuristic(problem))[0])

        # Then, go to the correct shore
        if goal.destination == "dirt":
            problem = PathfindingProblem((0, 0), dirt, walls, [])
            return Move(
                direction=a_star_graph(problem, get_heuristic(problem))[0])
        else:
            problem = PathfindingProblem((0, 0), grass, walls, [])
            return Move(
                direction=a_star_graph(problem, get_heuristic(problem))[0])
Пример #5
0
 def next_action(self, percept, random_generator):
     if self.plan:
         return Move(direction=self.plan[0])
     return DoNothing()