コード例 #1
0
ファイル: RandomAgent.py プロジェクト: RvonGlahn/BattleSnake
    def feel_busy(self, board: BoardState, snake: Snake, grid_map: GridMap):

        possible_actions = snake.possible_actions()
        head = snake.get_head()

        if possible_actions is None:
            return None

        actions_without_obstacle = []

        for action in possible_actions:
            next_field = head.advanced(action)
            object_at_field = grid_map.get_value_at_position(next_field)

            if board.is_out_of_bounds(next_field):
                continue

            if object_at_field == Occupant.Snake:
                continue

            actions_without_obstacle.append(action)

        if len(actions_without_obstacle) > 0:
            return np.random.choice(actions_without_obstacle)
        else:
            return None
コード例 #2
0
ファイル: RandomAgent.py プロジェクト: RvonGlahn/BattleSnake
    def move(self, game_info: GameInfo, turn: int, board: BoardState, you: Snake) -> MoveResult:

        possible_actions = you.possible_actions()

        if possible_actions is None:
            return None

        grid_map = board.generate_grid_map()

        busy_action = self.feel_busy(board, you, grid_map)
        if busy_action is not None:
            return MoveResult(direction=busy_action)

        random_action = np.random.choice(possible_actions)
        return MoveResult(direction=random_action)
コード例 #3
0
    def next_step(snake: Snake, board: BoardState, grid_map: GridMap) -> Direction:

        head = snake.get_head()
        #tail = snake.get_tail()
        middle = Position(board.height // 2, board.width // 2)
        valid = ValidActions.get_valid_actions(board, snake.possible_actions(), [snake], snake, grid_map, False)
        #for direction in Direction:
        #    if not board.is_out_of_bounds(head.advanced(direction)):
        #        if not (head.advanced(direction) in snake.get_body()):
        #            valid.append(direction)
        #if snake.get_length() > 10:
        #    for action in valid:
        #        _, path = AStar.a_star_search(head.advanced(action), tail, board, grid_map)
        #       print(len(path))
        #        end, pat = path[len(path)-1]
        #        if not end == tail:
        #            valid.remove(action)

        if middle in snake.get_body():
            need, next_direction = SoloSurvival.need_food(snake, board, grid_map, valid)
            if need:
                if next_direction in valid:
                    return next_direction
                else:
                    #print("not valid")
                    return np.random.choice(valid)
            else:
                next_direction = SoloSurvival.tail_gate(snake, board, grid_map)
                if next_direction in valid:
                    return next_direction
                else:
                    return np.random.choice(valid)

        else:
            if SoloSurvival.food_around_head(head, board):
                _, path = AStar.a_star_search(head, middle, board, grid_map)
                _, next_direction = path[0]
            else:
                _, path = AStar.a_star_search(head, middle, board, grid_map)
                _, next_direction = path[0]
        if next_direction in valid:
            return next_direction
        else:
            return np.random.choice(valid)
コード例 #4
0
    def move(self, game_info: GameInfo, turn: int, board: BoardState, you: Snake) -> MoveResult:
        if you.latency:
            print("Latency", you.latency)
        start_time = time.time()
        grid_map: GridMap[Occupant] = board.generate_grid_map()

        self.Decision.set_round(turn)
        next_action = self.Decision.decide(you, board, grid_map)

        if not next_action:
            possible_actions = you.possible_actions()
            for action in possible_actions:
                next_position = you.get_head().advanced(action)
                for snake in board.snakes:
                    if next_position == snake.get_tail() and snake.health != 100:
                        next_action = action
            if not next_action:
                next_action = np.random.choice(possible_actions)
        print("Move-DAUER: ", time.time() - start_time)
        return MoveResult(direction=next_action)
コード例 #5
0
    def hunger(snake: Snake, board: BoardState, grid_map: GridMap, food_path: List[Position],
               valid_actions: List[Direction], my_automat: SnakeAutomat) -> Tuple[Direction, List[Position]]:

        possible_actions = snake.possible_actions()
        back_up_actions = ValidActions.get_valid_actions(board, possible_actions, board.snakes, snake, grid_map, False)

        if valid_actions:
            action = np.random.choice(valid_actions)
        else:
            action = np.random.choice(back_up_actions)

        if not food_path:
            print(my_automat.reachable_food)
            food_path = Hungry.follow_food(snake, board, grid_map, my_automat.reachable_food)
        if food_path:
            print("Food_Path: ", food_path)
            if food_path[0][1] in valid_actions:
                action = food_path[0][1]
                food_path.pop(0)
        else:
            food_path = []
        print(action)

        return action, food_path
コード例 #6
0
ファイル: Anxious.py プロジェクト: RvonGlahn/BattleSnake
    def avoid_enemy(my_snake: Snake, board: BoardState, grid_map: GridMap,
                    valid_actions: List[Direction], action_plan: ActionPlan,
                    direction_depth: Dict) -> Direction:

        if not valid_actions:
            possible_actions = my_snake.possible_actions()
            valid_actions = ValidActions.get_valid_actions(board,
                                                           possible_actions,
                                                           board.snakes,
                                                           my_snake,
                                                           grid_map,
                                                           avoid_food=False)

        num_snakes = 4 - len(board.snakes)
        flood_dist = 6 if len(board.snakes) > 2 else 99

        my_head = my_snake.get_head()
        enemy_heads = [
            snake.get_head() for snake in board.snakes
            if snake.snake_id != my_snake.snake_id
        ]

        middle = Position(int(board.height / 2), int(board.width / 2))
        corners = [
            Position(0, 0),
            Position(0, board.width),
            Position(board.height, 0),
            Position(board.height, board.width)
        ]

        escape_cost_dict = action_plan.escape_lane(my_head, valid_actions)

        # TODO: Unterscheidung der Params in Late game und early game abhängig von anzahl der Schlangen
        p_head = Params_Anxious.ALPHA_DISTANCE_ENEMY_HEAD[num_snakes] * (-1)
        p_corner = Params_Anxious.BETA_DISTANCE_CORNERS[num_snakes]
        p_mid = Params_Anxious.THETA_DISTANCE_MID[num_snakes] * (-1)
        p_border = Params_Anxious.EPSILON_NO_BORDER[num_snakes]

        p_food = (Params_Anxious.GAMMA_DISTANCE_FOOD[num_snakes] * 20 /
                  my_snake.health)
        p_flood_min = Params_Anxious.OMEGA_FLOOD_FILL_MIN[num_snakes] * (-1)
        p_flood_max = Params_Anxious.OMEGA_FLOOD_FILL_MAX[num_snakes]
        p_flood_dead = Params_Anxious.OMEGA_FLOOD_DEAD[num_snakes]

        p_corridor = Params_Anxious.RHO_ESCAPE_CORRIDOR[num_snakes] * (-1)
        p_length = Params_Anxious.TAU_PATH_LENGTH[num_snakes]

        total_cost = np.array([], dtype=np.float64)
        direction_cost = np.zeros(10)

        for action in valid_actions:

            next_position = my_head.advanced(action)

            # calculate flood fill
            flood_fill_value, relevant_food = FloodFill.get_fill_stats(
                board, next_position, my_snake.snake_id, new_pos=True)
            enemy_flood = sum([
                flood_fill_value[snake.snake_id] for snake in board.snakes
                if snake.snake_id != my_snake.snake_id and
                Distance.manhattan_dist(snake.get_head(), my_head) < flood_dist
            ])

            # calculate all costs for heuristics
            direction_cost[0] = direction_depth[action] * p_length

            direction_cost[1] = escape_cost_dict[action] * p_corridor

            direction_cost[2] = ActionPlan.punish_border_fields(
                next_position, my_head, grid_map.width,
                grid_map.height) * p_border

            direction_cost[3] = sum([
                Distance.manhattan_dist(next_position, enemy_head)
                for enemy_head in enemy_heads
            ]) * p_head

            direction_cost[4] = sum([
                Distance.manhattan_dist(next_position, corner)
                for corner in corners
                if Distance.manhattan_dist(next_position, corner) < 9
            ]) * p_corner

            direction_cost[5] = Distance.manhattan_dist(next_position,
                                                        middle) * p_mid

            direction_cost[6] = flood_fill_value[
                my_snake.snake_id] * p_flood_max
            direction_cost[7] = enemy_flood * p_flood_min

            direction_cost[8] = len(relevant_food) * p_food

            # extra points for killing enemy snake
            if num_snakes == 1:

                enemy_id = [
                    snake.snake_id for snake in board.snakes
                    if snake.snake_id != my_snake.snake_id
                ][0]
                if flood_fill_value[enemy_id] < 20:
                    flood_kill_value = (20 - flood_fill_value[enemy_id]) * 1000
                    direction_cost[9] = flood_kill_value * p_flood_dead

            total_cost = np.append(total_cost, direction_cost.sum())
            direction_cost = np.zeros(10)

        if valid_actions:
            best_action = valid_actions[int(np.argmax(total_cost))]
            print(best_action)
        else:
            best_action = None

        return best_action