def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        def find_player_positions(player_index):
            if player_index == 1:
                return self.pos
            pos = np.where(self.board == player_index)
            # convert pos to tuple of ints
            return tuple(ax[0] for ax in pos)

        start_time = time()
        player_positions = (find_player_positions(1), find_player_positions(2))
        self.board[self.pos] = -1

        curr_state = SearchAlgos.State(self.board.copy(), tuple(players_score),
                                       player_positions, 0, self.penalty_score,
                                       (0, 0), self.lifetime, self.initial_pos)

        heavy_player = SearchAlgos.AlphaBeta(minimax_utility, minimax_succ,
                                             None, start_time, time_limit,
                                             minimax_heuristic)
        depth = 4
        value, direction = heavy_player.search(curr_state, depth, 1)

        i = self.pos[0] + direction[0]
        j = self.pos[1] + direction[1]
        self.pos = (i, j)
        self.board[self.pos] = 1

        self.lifetime += 1
        return direction
Beispiel #2
0
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(
         self, game_time, penalty_score
     )  # keep the inheritance of the parent's (AbstractPlayer) __init__()
     #TODO: initialize more fields, if needed, and the Minimax algorithm from SearchAlgos.py
     self.penalty_score = penalty_score
     self.name = "LightAlphaBeta"
     self.location = None
     self.game_board = None
     self.rival_location = None
     self.rival_points = 0
     self.points = 0
     self.time_ended = False
     self.start_time = 0
     self.fruit_locations = dict()
     self.fruit_life_time = None
     self.best_fruit_value = 0
     self.fruits_in_game = False
     self.best_fruit_location = None
     self.moves_available = []
     self.moves_available_count = 0
     self.rival_moves_available = []
     self.rival_moves_available_count = 0
     self.min_dimention = None
     self.fruits_concentration = None
     # self.init_concentration_dict()
     self.search_algos = SearchAlgos.AlphaBeta(self.utility, None,
                                               self.make_move, self.is_goal)
Beispiel #3
0
 def make_move(self, time_limit, players_score):
     """Make move with this Player.
     input:
         - time_limit: float, time limit for a single turn.
     output:
         - direction: tuple, specifing the Player's movement, chosen from self.directions
     """
     # print("start computing Heavy alpha-beta move")  # printing for self test
     if self.first_player == -1:
         self.first_player = True
     state = utils.State(copy.deepcopy(self.board), self.pos,
                         self.rival_pos, players_score, self.penalty_score,
                         self.turns_till_fruit_gone,
                         self.fruits_on_board_dict, self.first_player)
     search_algo = SearchAlgos.AlphaBeta(utils.utility, utils.succ,
                                         utils.perform_move, utils.goal)
     best_move = search_algo.search(
         state, 3, True)  # always with max depth=3 (or 2 in other test)
     if best_move[1] is None:
         exit(0)
     # print("Heavy alpha-beta choose the move: ", best_move)  # printing for self test
     self.board[self.pos] = -1
     tmp1 = best_move[1]
     self.pos = (self.pos[0] + tmp1[0], self.pos[1] + tmp1[1])
     self.board[self.pos] = 1
     self.turns_till_fruit_gone -= 1
     return best_move[1]
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(self, game_time, penalty_score) # keep the inheritance of the parent's (AbstractPlayer) __init__()
     self.board = None
     self.pos = None
     self.alphabeta = SearchAlgos.AlphaBeta(utility=self.utility, succ=self.succ, perform_move=None, goal=self.goal,
                                        heuristic=self.heuristic)
     self.players_score = None
Beispiel #5
0
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(
         self, game_time, penalty_score
     )  # keep the inheritance of the parent's (AbstractPlayer) __init__()
     self.alphaBeta = SearchAlgos.AlphaBeta(self.utility, self.succ,
                                            self.perform_move, self.goal,
                                            self.heuristic_function,
                                            self.revert_move)
Beispiel #6
0
    def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        # print("start computing Global AB move")  # printing for self test
        start_time = time.time()
        allowed_time = min(self.time_for_curr_iter, time_limit)

        if self.first_player == -1:
            self.first_player = True

        state = utils.State(copy.deepcopy(self.board), self.pos,
                            self.rival_pos, players_score, self.penalty_score,
                            self.turns_till_fruit_gone,
                            self.fruits_on_board_dict, self.first_player,
                            time.time() + allowed_time - self.extra_safe_time)
        search_algo = SearchAlgos.AlphaBeta(utils.utility, utils.succ,
                                            utils.perform_move, utils.goal)
        depth = 1
        best_move = None, None

        while depth <= self.max_turns:
            try:
                best_move = search_algo.search(state, depth, True)
            except TimeoutError:
                break
            depth += 1

        if best_move[1] is None:
            # print("something went wrong,.. im out... probably not enough time for at least depth=1")  # printing for self test
            exit(0)

        # print("depth is : ", depth - 1)   # printing for self test
        if self.time_for_each_iter is not None:
            # print("my turn is: ", self.my_turn)   # printing for self test
            self.time_for_curr_iter += self.time_for_each_iter[
                self.my_turn] - (time.time() - start_time)
            self.my_turn -= 1
        else:
            self.time_for_curr_iter += (self.time_for_curr_iter /
                                        self.risk_factor1) - (time.time() -
                                                              start_time)
        # print("current iter took: ", time.time()-start_time)   # printing for self test
        # print("next iter will take: ", self.time_for_curr_iter)
        # print("Global AB choose the move: ", best_move)
        self.max_turns -= 1
        self.board[self.pos] = -1
        tmp1 = best_move[1]
        self.pos = (self.pos[0] + tmp1[0], self.pos[1] + tmp1[1])
        self.board[self.pos] = 1
        self.turns_till_fruit_gone -= 1
        return best_move[1]
Beispiel #7
0
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(self, game_time,
                             penalty_score)  # keep the inheritance of the parent's (AbstractPlayer) __init__()
     # TODO: initialize more fields, if needed, and the AlphaBeta algorithm from SearchAlgos.py
     self.board = None  # player1 is my player
     self.pos_players = None  # tuple [0]-player1 pos, [1] -player2 pos
     self.strategy = sa.AlphaBeta(utility=self.utility, succ=utils.State.succ, retriveLast=utils.State.retriveLast,
                                goal=utils.State.goal)
     self.num_of_turns = (0, 0)  # tuple [0]-player1 turns, [1] -player2 turns
     self.fruits_on_board = None
     self.little_edge = 0
    def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        global play_simple_player

        def find_player_positions(player_index):
            if player_index == 1:
                return self.pos
            pos = np.where(self.board == player_index)
            return tuple(ax[0] for ax in pos)

        start_time = time()
        player_positions = (find_player_positions(1), find_player_positions(2))
        self.board[self.pos] = -1

        if not play_simple_player:
            play_simple_player = not is_enemy_reachable(
                self.board, self.pos) and self.lifetime >= 2 * min(
                    len(self.board[0]), len(self.board))

        curr_state = SearchAlgos.State(self.board.copy(), tuple(players_score),
                                       player_positions, 0, self.penalty_score,
                                       (0, 0), self.lifetime)

        time_limit = self.calculate_turn_time_limit(
            int(self.lifetime / 2) - 1 + self.lifetime % 2)
        global_alphabeta = SearchAlgos.AlphaBeta(minimax_utility, minimax_succ,
                                                 None, start_time, time_limit,
                                                 minimax_heuristic)

        depth = 1
        value, direction = global_alphabeta.search(curr_state, depth, 1)

        while not global_alphabeta.developed_whole_tree:
            try:
                global_alphabeta.developed_whole_tree = True
                value, direction = global_alphabeta.search(
                    curr_state, depth, 1)
                depth += 1
            except SearchAlgos.Interrupted:
                break
        i = self.pos[0] + direction[0]
        j = self.pos[1] + direction[1]
        self.pos = (i, j)
        self.board[self.pos] = 1

        self.lifetime += 1
        return direction
Beispiel #9
0
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(
         self, game_time, penalty_score
     )  # keep the inheritance of the parent's (AbstractPlayer) __init__()
     #TODO: initialize more fields, if needed, and the AlphaBeta algorithm from SearchAlgos.py
     self.board = None
     self.pos = None
     self.flag = True
     self.time_frame = None
     self.global_time = game_time
     self.alphabeta = SearchAlgos.AlphaBeta(utility=self.utility,
                                            succ=self.succ,
                                            perform_move=None,
                                            goal=self.goal,
                                            heuristic=self.heuristic)
     self.players_score = None
Beispiel #10
0
    def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        # print("start computing alpha-beta move")  # printing for self test
        if self.first_player == -1:
            self.first_player = True

        state = utils.State(copy.deepcopy(self.board), self.pos,
                            self.rival_pos, players_score, self.penalty_score,
                            self.turns_till_fruit_gone,
                            self.fruits_on_board_dict, self.first_player,
                            time.time() + time_limit - .015)
        search_algo = SearchAlgos.AlphaBeta(utils.utility, utils.succ,
                                            utils.perform_move, utils.goal)
        depth = 1
        best_move = None, None

        while depth <= self.max_turns:
            try:
                best_move = search_algo.search(state, depth, True)
            except TimeoutError:
                break
            depth += 1

        if best_move[1] is None:
            # print("something went wrong,.. im out")  # printing for self test
            exit(0)
        # print("depth is : ", depth - 1)  # printing for self test
        # print("alpha-beta choose the move: ", best_move)  # printing for self test
        self.board[self.pos] = -1
        tmp1 = best_move[1]
        self.pos = (self.pos[0] + tmp1[0], self.pos[1] + tmp1[1])
        self.board[self.pos] = 1
        self.turns_till_fruit_gone -= 1
        return best_move[1]
Beispiel #11
0
    def set_game_params(self, board):
        """Set the game parameters needed for this player.
        This function is called before the game starts.
        (See GameWrapper.py for more info where it is called)
        input:
            - board: np.array, a 2D matrix of the board.
        No output is expected.
        """
        self.board = board  # need to set my pos, the rival pos, all the grey area and all fruits
        self.max_turns = len(board) * len(board[0]) - 2
        self.turns_till_fruit_gone = min(len(board), len(board[0])) * 2
        for r, row in enumerate(board):
            for c, num in enumerate(row):
                if num == -1:
                    self.max_turns -= 1
                if num == 1:
                    self.pos = (r, c)  # this my pos
                elif num == 2:
                    self.rival_pos = (r, c)
                elif num > 2:
                    self.fruits_on_board_dict[(
                        r, c
                    )] = num  # need to do this manually only for this player

        self.time_for_curr_iter = (-2 / 3) * self.game_time / ((
            (1 / 3)**self.max_turns) - 1)

        min_iter_time = time.time()
        state = utils.State(copy.deepcopy(self.board), self.pos,
                            self.rival_pos, [0, 0], self.penalty_score,
                            self.turns_till_fruit_gone,
                            self.fruits_on_board_dict, True)
        search_algo = SearchAlgos.AlphaBeta(utils.utility, utils.succ,
                                            utils.perform_move, utils.goal)
        search_algo.search(state, 2, True)

        min_iter_time = (time.time() - min_iter_time) * 1.25

        if min_iter_time == 0:
            min_iter_time = 0.0022

        self.my_turn = int((1 + self.max_turns) / 2)
        tmp_time = self.time_for_curr_iter
        tmp_depth = self.my_turn
        while tmp_depth and tmp_time > min_iter_time:  # check every iter is possible for at least depth=1
            tmp_time = tmp_time / self.risk_factor1
            tmp_depth -= 1

        if tmp_time < min_iter_time:  # not every iter is possible for at least depth=1. plan B for time sharing:
            avg_time = self.game_time / self.my_turn
            self.time_for_each_iter = {}
            index_left = self.my_turn
            index_right = 0
            exchange_tmp = avg_time - min_iter_time
            while index_left >= index_right and exchange_tmp > 0:  # exchange time between the latest iter and the first
                self.time_for_each_iter[index_left] = avg_time + exchange_tmp
                self.time_for_each_iter[
                    index_right] = min_iter_time + self.extra_safe_time
                index_right += 1
                index_left -= 1
                min_iter_time *= self.risk_factor2
                exchange_tmp = avg_time - (min_iter_time +
                                           self.extra_safe_time)

            while index_left >= index_right:
                self.time_for_each_iter[index_left] = avg_time
                self.time_for_each_iter[index_right] = avg_time
                index_right += 1
                index_left -= 1

            self.time_for_curr_iter = self.time_for_each_iter[self.my_turn]
            self.my_turn -= 1