예제 #1
0
 def next_action(self, percept, random_generator):
     if {"x": 0, "y": 0, "looks_like": "dirt"} in percept["entities"]:
         return PickUp()
     if percept["position"]["x"] == 1 and not self.moved:
         return Move(direction="right")
     if percept["position"]["x"] == 2 and not self.moved:
         return Move(direction="left")
     return DoNothing()
    def next_action(self, percept, random_generator):
        dirt_count = len(
            [e for e in percept["entities"] if e["looks_like"] == "dirt"])

        if {"x": 0, "y": 0, "looks_like": "dirt"} in percept["entities"]:
            return PickUp()
        if percept["position"]["x"] == 1 and dirt_count > 0:
            return Move(direction="right")
        if percept["position"]["x"] == 2 and dirt_count > 0:
            return Move(direction="left")
        return DoNothing()
예제 #3
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")}
예제 #4
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"))
    def next_action(self, percept, random_generator):
        if {"x": 0, "y": 0, "looks_like": "dirt"} in percept["entities"]:
            return PickUp()

        targets = set()
        obstructions = set()

        for key in self.passable:
            coords = key[1:-1].split(",")
            x = int(coords[0])
            y = int(coords[1])
            if self.passable[key]:
                for pos in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
                    if str(pos) not in self.passable:
                        targets.add(pos)
            else:
                obstructions.add((x, y))
        if not targets:
            return DoNothing()
        my_x = percept["position"]["x"]
        my_y = percept["position"]["y"]
        problem = PathfindingProblem((my_x, my_y), targets, obstructions, [])
        try:
            plan = a_star_graph(problem, get_heuristic(problem))
            return Move(direction=plan[0])
        except NoSolutionError:
            return DoNothing()
예제 #6
0
    def next_action(self, percept, random_generator):
        if "position" not in percept:
            # We are picked up
            return DoNothing()

        obstacles = [(e["x"], e["y"]) for e in percept["entities"]
                     if e["looks_like"] == "wall"
                     or e["looks_like"] == "water"]
        players = [(e["x"], e["y"]) for e in percept["entities"]
                   if e["looks_like"] == "player"]
        missionaries = [(e["x"], e["y"]) for e in percept["entities"]
                        if e["looks_like"] == "coin"]
        cannibals = [(e["x"], e["y"]) for e in percept["entities"]
                     if e["looks_like"] == "evilCoin"]

        try:
            problem = PathfindingProblem(
                (0, 0), players, obstacles, [])
            a_star_graph(problem, get_heuristic(problem))
            return DoNothing()
        except NoSolutionError:
            pass

        reachable_missionary_count = 0
        for missionary in missionaries:
            try:
                problem = PathfindingProblem(
                    (0, 0), [missionary], obstacles, [])
                a_star_graph(problem, get_heuristic(problem))
                reachable_missionary_count += 1
            except NoSolutionError:
                pass

        reachable_cannibal_count = 0
        for cannibal in cannibals:
            try:
                problem = PathfindingProblem((0, 0), [cannibal], obstacles, [])
                a_star_graph(problem, get_heuristic(problem))
                reachable_cannibal_count += 1
            except NoSolutionError:
                pass

        if (reachable_cannibal_count + 1 > reachable_missionary_count
                and reachable_missionary_count > 0):
            if (-1, 0) in missionaries:
                return Attack(direction="left")
            elif (1, 0) in missionaries:
                return Attack(direction="right")
            elif (0, -1) in missionaries:
                return Attack(direction="down")
            elif (0, 1) in missionaries:
                return Attack(direction="up")
            problem = PathfindingProblem((0, 0), missionaries, obstacles, [])
            plan = a_star_graph(problem, get_heuristic(problem))
            return Move(direction=plan[0])
        return DoNothing()
예제 #7
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 == {}
예제 #8
0
def test_move_direction(direction, x, y):
    system = MovementSystem()
    actions = {"a": Move(direction=direction)}
    position_components = PositionDict({"a": Position(x=0, y=0)})
    blocks_movement_components = {}
    tags = {}
    swappable_components = {}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=x, y=y)
예제 #9
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 == {}
예제 #10
0
def test_blocker_cannot_move_into_others_space():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0)
    })
    blocks_movement_components = {"a": BlocksMovement()}
    tags = {}
    swappable_components = {}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=0, y=0)
예제 #11
0
def test_swap():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0)
    })
    blocks_movement_components = {}
    tags = {}
    swappable_components = {"b": Swappable()}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=1, y=0)
    assert position_components["b"] == Position(x=0, y=0)
예제 #12
0
def test_blocks_movement_right_tags():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0)
    })
    blocks_movement_components = {
        "b": BlocksMovement(passableForTags=["pass"])
    }
    tags = {"a": ["pass"]}
    swappable_components = {}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=1, y=0)
예제 #13
0
    def next_action(self, percept, random_generator):
        if (self.manual_pickup and {"x": 0, "y": 0, "looks_like": "coin"}
                in percept["entities"]):
            return PickUp()

        walls = [(e["x"], e["y"]) for e in percept["entities"]
                 if e["looks_like"] == "wall"]
        coins = [(e["x"], e["y"]) for e in percept["entities"]
                 if e["looks_like"] == "coin"]
        if not coins:
            return DoNothing()
        problem = PathfindingProblem((0, 0), coins, walls, [])
        try:
            plan = a_star_graph(problem, get_heuristic(problem))
            return Move(direction=plan[0])
        except NoSolutionError:
            return DoNothing()
예제 #14
0
def test_cannot_swap_if_other_entity_is_blocked():
    system = MovementSystem()
    actions = {"a": Move(direction="right")}
    position_components = PositionDict({
        "a": Position(x=0, y=0),
        "b": Position(x=1, y=0),
        "c": Position(x=0, y=0)
    })
    blocks_movement_components = {
        "c": BlocksMovement(passableForTags=["pass"])
    }
    tags = {"a": ["pass"]}
    swappable_components = {"b": Swappable()}
    system.move_entities(actions, position_components,
                         blocks_movement_components, tags,
                         swappable_components)
    assert position_components["a"] == Position(x=0, y=0)
    assert position_components["b"] == Position(x=1, y=0)
    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])
예제 #16
0
 def next_action(self, percept, random_generator):
     if {"x": 0, "y": 0, "looks_like": "dirt"} in percept["entities"]:
         return PickUp()
     direction = random_generator.choice(["up", "down", "left", "right"])
     return Move(direction=direction)
예제 #17
0
 def next_action(self, percept, random_generator):
     direction = random_generator.choice(["up", "down",
                                          "left", "right"])
     return Move(direction=direction)
예제 #18
0
 def next_action(self, percept, random_generator):
     if self.plan:
         return Move(direction=self.plan[0])
     return DoNothing()