Beispiel #1
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, old_coins, playerScore, enemyScore
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation, mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation, mazeMap, coins + [playerLocation])

    # Calculate our score and en score :
    if playerLocation in old_coins and playerLocation not in coins:
        playerScore += 1
    if playerLocation in old_coins and playerLocation not in coins:
        enemyScore += 1

    if len(route) == 0 or route[-1] not in coins:
        winning_value, next_coin, en_best_path, pl_best_path = minmax(coins, playerScore, 0, playerLocation, enemyScore, 0, opponentLocation, 0)
        interface.debug(pl_best_path)
        interface.debug(en_best_path)
        interface.debug("going to : " + str(next_coin) + " with a probable score of : " + str(winning_value))
        route = route_matrix[playerLocation][next_coin]

    coins_which_are_close = coins_close(playerLocation, coins, dists_matrix, limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        pass
        # route = route_matrix[playerLocation][closest_coin]
        # interface.debug("NEAR : " + str(closest_coin))

    old_coins = coins
    next_pos = route.pop(0)
    # input()
    return u.direction(playerLocation, next_pos)
Beispiel #2
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, coins_to_get
    # First we update our dists and routes matrix :
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation, mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation, mazeMap, coins)

    if len(route) == 0 or route[-1] not in coins:
        coins_to_get = [c for c in coins_to_get if c in coins]
        sorted(coins_to_get, key=lambda x: dists_matrix[x][playerLocation], reverse=False)
        # probabilities = closer_probability(playerLocation, opponentLocation, coins_to_get, dists_matrix)
        # next_coin = which_coin_is_next(coins_to_get, playerLocation, opponentLocation)

        # Il y a des pièces que l'on pourrait récuperer maintenant ?
        # Une sorte de système de packets
        coins_which_are_close = coins_close(playerLocation, coins, dists_matrix)
        closest_coin = None
        for coin in coins_which_are_close:
            # If we don't already go there, and the enemy is not going there
            if coin not in coins_to_get and dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][coin]:  # TODO : magic number, we should check that with the enemy
                if closest_coin is None or dists_matrix[playerLocation][coin] < dists_matrix[playerLocation][closest_coin]:
                    closest_coin = coin
        if closest_coin is not None:
            coins_to_get.insert(0, closest_coin)
            interface.debug("Finally go to : " + str(closest_coin))

        try:
            next_coin = coins_to_get.pop(0)
        except:
            interface.debug("PHASE III")
            # Si on a plus rien, on fait un voyageur de commerce sur les pieces qui restent
            sorted(coins, key=lambda coin: dists_matrix[playerLocation][coin])
            coins_to_get = coins[:7]
            coins_to_get = voyageur_commerce(playerLocation, coins_to_get, dists_matrix)
            next_coin = coins_to_get.pop(0)

        interface.debug(coins_to_get)
        # coins_to_get.remove(next_coin)

        route = route_matrix[playerLocation][next_coin]

    # In case we pass not far from a coin :
    # Il y a des pièces que l'on pourrait récuperer maintenant ?
    # Une sorte de système de packets
    coins_which_are_close = coins_close(playerLocation, coins, dists_matrix, limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if coin not in coins_to_get and dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        coins_to_get.insert(0, route[-1])
        route = route_matrix[playerLocation][closest_coin]
        interface.debug("Finally go to : " + str(closest_coin))

    next_pos = route.pop(0)
    return u.direction(playerLocation, next_pos)
Beispiel #3
0
def determineNextMove(player_location, opponentLocation, coins):
    """Return the next direction"""
    global route, currentcoin, meta_route, best_weight, best_path, coins_to_search
    #the second test prevents the player from going to coins which have been taken by the opponent
    if currentcoin == player_location or opponentLocation in coins_to_search:
        dists_matrix, routes_matrix = u.update_dists_from_each(dist_matrix, route_matrix, player_location, mazeMap, coins)

        coins_to_search = get_n_shortest(3, coins, player_location, dist_matrix)
        ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
        may_be_lost_coins = []
        # Remove from coins_to_search the first coin which is closer to the opponent than to the player
        for c in coins_to_search:
            if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]:
                may_be_lost_coins.append(c)
        if len(may_be_lost_coins) != 0:
            coins_to_search.remove(may_be_lost_coins[0]) 
   
        best_weight = float("inf")
        best_path = []
        exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
        meta_route = [player_location]+best_path
        route = u.location_list_to_route(meta_route, route_matrix)
        currentcoin = meta_route[1]

    return u.direction(player_location, route.pop(0))
Beispiel #4
0
def determineNextMove(player_location, opponentLocation, coins):
    """Return the next direction"""
    global route, currentcoin, meta_route, best_weight, best_path, coins_to_search
    #the second test prevents the player from going to coins which have been taken by the opponent
    if currentcoin == player_location or opponentLocation in coins_to_search:
        dists_matrix, routes_matrix = u.update_dists_from_each(
            dist_matrix, route_matrix, player_location, mazeMap, coins)

        coins_to_search = get_n_shortest(3, coins, player_location,
                                         dist_matrix)
        ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
        may_be_lost_coins = []
        # Remove from coins_to_search the first coin which is closer to the opponent than to the player
        for c in coins_to_search:
            if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[
                    player_location][c]:
                may_be_lost_coins.append(c)
        if len(may_be_lost_coins) != 0:
            coins_to_search.remove(may_be_lost_coins[0])

        best_weight = float("inf")
        best_path = []
        exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
        meta_route = [player_location] + best_path
        route = u.location_list_to_route(meta_route, route_matrix)
        currentcoin = meta_route[1]

    return u.direction(player_location, route.pop(0))
Beispiel #5
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, old_coins, playerScore, enemyScore
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation,
                             mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation,
                             mazeMap, coins + [playerLocation])

    # Calculate our score and en score :
    if playerLocation in old_coins and playerLocation not in coins:
        playerScore += 1
    if playerLocation in old_coins and playerLocation not in coins:
        enemyScore += 1

    if len(route) == 0 or route[-1] not in coins:
        winning_value, next_coin, en_best_path, pl_best_path = minmax(
            coins, playerScore, 0, playerLocation, enemyScore, 0,
            opponentLocation, 0)
        interface.debug(pl_best_path)
        interface.debug(en_best_path)
        interface.debug("going to : " + str(next_coin) +
                        " with a probable score of : " + str(winning_value))
        route = route_matrix[playerLocation][next_coin]

    coins_which_are_close = coins_close(playerLocation,
                                        coins,
                                        dists_matrix,
                                        limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][
                coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][
                    coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        pass
        # route = route_matrix[playerLocation][closest_coin]
        # interface.debug("NEAR : " + str(closest_coin))

    old_coins = coins
    next_pos = route.pop(0)
    # input()
    return u.direction(playerLocation, next_pos)
Beispiel #6
0
def determineNextMove(playerLocation, opponentLocation, coins):
    
    global route, currentcoin, acc
    u.update_dists_from_each(dist_matrix, route_matrix, playerLocation, mazeMap, coins)
    if currentcoin == playerLocation:
        best_weight = float("inf")
        best_path = []
        coins_to_search = get_n_shortest(7, coins, playerLocation, dist_matrix)
        if playerLocation in coin_to_search:
            api.debug(coin_to_search)
            api.debug(playerLocation)
        meta_route = exhaustive(coins_to_search, playerLocation, [] ,0 ,dist_matrix)
        route = u.location_list_to_route(meta_route, route_matrix)
        currentcoin = meta_route[0]
        #if currentcoin == playerLocation:
            #api.debug(meta_route)
            #api.debug(route)
            #acc+=1
            #api.debug(acc)
    return u.direction(playerLocation, route.pop(0))
Beispiel #7
0
def change_way(coins, opponentLocation, player_location):
    """Return the new coin to search, coin sequence, route and the distance from the player to the first coin of the route"""
    global best_weight, best_path
    dist_matrix, route_matrix = u.update_dists_from_each(dists_matrix, routes_matrix, player_location, mazeMap, coins)
    coins_to_search = get_n_shortest(5, coins, player_location, dists_matrix)
    ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
    for c in coins_to_search:
        if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]:
            coins_to_search.remove(c)
            break
    best_weight = float("inf")
    best_path = []
    api.debug(coins_to_search)
    exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
    meta_route = [player_location] + best_path
    api.debug(meta_route)
    route = u.location_list_to_route(meta_route, route_matrix)
          
    return coins_to_search, meta_route, route, dist_matrix[player_location][meta_route[1]]
Beispiel #8
0
def change_way(coins, opponentLocation, player_location):
    """Return the new coin to search, coin sequence, route and the distance from the player to the first coin of the route"""
    global best_weight, best_path
    dist_matrix, route_matrix = u.update_dists_from_each(
        dists_matrix, routes_matrix, player_location, mazeMap, coins)
    coins_to_search = get_n_shortest(5, coins, player_location, dists_matrix)
    ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
    for c in coins_to_search:
        if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[
                player_location][c]:
            coins_to_search.remove(c)
            break
    best_weight = float("inf")
    best_path = []
    api.debug(coins_to_search)
    exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
    meta_route = [player_location] + best_path
    api.debug(meta_route)
    route = u.location_list_to_route(meta_route, route_matrix)

    return coins_to_search, meta_route, route, dist_matrix[player_location][
        meta_route[1]]
Beispiel #9
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, coins_to_get
    # First we update our dists and routes matrix :
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation,
                             mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation,
                             mazeMap, coins)

    if len(route) == 0 or route[-1] not in coins:
        coins_to_get = [c for c in coins_to_get if c in coins]
        sorted(coins_to_get,
               key=lambda x: dists_matrix[x][playerLocation],
               reverse=False)
        # probabilities = closer_probability(playerLocation, opponentLocation, coins_to_get, dists_matrix)
        # next_coin = which_coin_is_next(coins_to_get, playerLocation, opponentLocation)

        # Il y a des pièces que l'on pourrait récuperer maintenant ?
        # Une sorte de système de packets
        coins_which_are_close = coins_close(playerLocation, coins,
                                            dists_matrix)
        closest_coin = None
        for coin in coins_which_are_close:
            # If we don't already go there, and the enemy is not going there
            if coin not in coins_to_get and dists_matrix[playerLocation][
                    coin] < dists_matrix[opponentLocation][
                        coin]:  # TODO : magic number, we should check that with the enemy
                if closest_coin is None or dists_matrix[playerLocation][
                        coin] < dists_matrix[playerLocation][closest_coin]:
                    closest_coin = coin
        if closest_coin is not None:
            coins_to_get.insert(0, closest_coin)
            interface.debug("Finally go to : " + str(closest_coin))

        try:
            next_coin = coins_to_get.pop(0)
        except:
            interface.debug("PHASE III")
            # Si on a plus rien, on fait un voyageur de commerce sur les pieces qui restent
            sorted(coins, key=lambda coin: dists_matrix[playerLocation][coin])
            coins_to_get = coins[:7]
            coins_to_get = voyageur_commerce(playerLocation, coins_to_get,
                                             dists_matrix)
            next_coin = coins_to_get.pop(0)

        interface.debug(coins_to_get)
        # coins_to_get.remove(next_coin)

        route = route_matrix[playerLocation][next_coin]

    # In case we pass not far from a coin :
    # Il y a des pièces que l'on pourrait récuperer maintenant ?
    # Une sorte de système de packets
    coins_which_are_close = coins_close(playerLocation,
                                        coins,
                                        dists_matrix,
                                        limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if coin not in coins_to_get and dists_matrix[playerLocation][
                coin] < dists_matrix[opponentLocation][
                    coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][
                    coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        coins_to_get.insert(0, route[-1])
        route = route_matrix[playerLocation][closest_coin]
        interface.debug("Finally go to : " + str(closest_coin))

    next_pos = route.pop(0)
    return u.direction(playerLocation, next_pos)
Beispiel #10
0
def minmax(coins_left, score_p1, len_path_p1, loc_p1, score_p2, len_path_p2, loc_p2, turn):
    """Recursive search of the winning tree"""
    global alpha, beta
    # At the end of the tree, return the value of the leaf
    if len(coins_left) == 0 or score_p2 - 1 > coins_init_num / 2 or score_p1 - 1 > coins_init_num / 2:
        return - score_p2, None, [], []  # maximizing for p1

    # Update the map data
    u.update_dists_from_each(dists_matrix, route_matrix, loc_p1, mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, loc_p2, mazeMap, coins + [loc_p1])

    # pl_won_coin = False
    pl_last_coin, en_last_coin = [], []
    # Calculating the score for each participant
    # if loc_p1 in coins_left:
    #     score_p1 += 1
    #     pl_last_coin = [loc_p1]
    #     new_coins_left.remove(loc_p1)
    #     pl_won_coin = True
    # if loc_p2 in coins_left:
    #     score_p2 += 1
    #     en_last_coin = [loc_p2]
    #     new_coins_left.remove(loc_p2)
    # if pl_won_coin and loc_p1 == loc_p2:
    #     score_p2 += 1
    #     en_last_coin = [loc_p2]

    new_coins_left = coins_left[:]
    # Player1 turn :
    if len_path_p1 <= turn:
        # Check if he is on a coin and increase his score :
        if loc_p1 in coins_left:
            score_p1 += 1
            pl_last_coin = [loc_p1]
            new_coins_left.remove(loc_p1)

        # Player on is playing, MAXIMIZING THE OVERALL SCORE
        best_value = float('-inf')
        best_coin = None

        # Search path in all coins
        for coin in coins_left:
            len_path_p1 += dists_matrix[loc_p1][coin]
            loc_p1 = coin
            node_value, _, en_path, pl_path = minmax(new_coins_left, score_p1, len_path_p1, loc_p1, score_p2, len_path_p2, loc_p2, turn + 1)
            if node_value > best_value:
                best_value = node_value
                best_coin = coin

            if beta <= best_value:  # Algo alpha-beta
                break

        beta = min(best_value, beta)
    elif len_path_p2 <= turn:  # Player2 turn
        # Check if he is on a coin and increase his score :
        if loc_p2 in coins_left:
            score_p2 += 1
            en_last_coin = [loc_p2]
            new_coins_left.remove(loc_p2)

        # Player on is playing, MINIMZING THE OVERALL SCORE
        best_value = float('+inf')
        best_coin = None

        # Search path in all coins
        for coin in coins_left:
            len_path_p2 += dists_matrix[loc_p2][coin]
            loc_p2 = coin  # route_matrix[loc_p1][coin][0]
            # if dists_matrix[loc_p2][coin] < dists_matrix[loc_p2][loc_p1] and dists_matrix[loc_p1][coin] < dists_matrix[loc_p2][loc_p1]:
            # score_p1 += 1 / 2 ** dists_matrix[loc_p1][coin]  # * min(1, dists_matrix[loc_p2][coin] / dists_matrix[loc_p1][coin]) ** 2 # Add a sort of pb
            # else:
            #     score_p1 += 1
            node_value, _, en_path, pl_path = minmax(new_coins_left, score_p1, len_path_p1, loc_p1, score_p2, len_path_p2, loc_p2, turn + 1)
            if node_value < best_value:
                best_value = node_value
                best_coin = coin
            if alpha >= best_value:  # Algo alpha-beta
                break
        alpha = max(best_value, beta)
    else:
        next_turn = min(len_path_p1, len_path_p2)
        best_value, best_coin, en_path, pl_path = minmax(new_coins_left, score_p1, len_path_p1, loc_p1, score_p2, len_path_p2, loc_p2, next_turn)

    pl_path = pl_last_coin + pl_path
    en_path = en_last_coin + en_path
    return best_value, best_coin, en_path, pl_path
Beispiel #11
0
def minmax(coins_left, score_p1, len_path_p1, loc_p1, score_p2, len_path_p2,
           loc_p2, turn):
    """Recursive search of the winning tree"""
    global alpha, beta
    # At the end of the tree, return the value of the leaf
    if len(
            coins_left
    ) == 0 or score_p2 - 1 > coins_init_num / 2 or score_p1 - 1 > coins_init_num / 2:
        return -score_p2, None, [], []  # maximizing for p1

    # Update the map data
    u.update_dists_from_each(dists_matrix, route_matrix, loc_p1, mazeMap,
                             coins)
    u.update_dists_from_each(dists_matrix, route_matrix, loc_p2, mazeMap,
                             coins + [loc_p1])

    # pl_won_coin = False
    pl_last_coin, en_last_coin = [], []
    # Calculating the score for each participant
    # if loc_p1 in coins_left:
    #     score_p1 += 1
    #     pl_last_coin = [loc_p1]
    #     new_coins_left.remove(loc_p1)
    #     pl_won_coin = True
    # if loc_p2 in coins_left:
    #     score_p2 += 1
    #     en_last_coin = [loc_p2]
    #     new_coins_left.remove(loc_p2)
    # if pl_won_coin and loc_p1 == loc_p2:
    #     score_p2 += 1
    #     en_last_coin = [loc_p2]

    new_coins_left = coins_left[:]
    # Player1 turn :
    if len_path_p1 <= turn:
        # Check if he is on a coin and increase his score :
        if loc_p1 in coins_left:
            score_p1 += 1
            pl_last_coin = [loc_p1]
            new_coins_left.remove(loc_p1)

        # Player on is playing, MAXIMIZING THE OVERALL SCORE
        best_value = float('-inf')
        best_coin = None

        # Search path in all coins
        for coin in coins_left:
            len_path_p1 += dists_matrix[loc_p1][coin]
            loc_p1 = coin
            node_value, _, en_path, pl_path = minmax(new_coins_left, score_p1,
                                                     len_path_p1, loc_p1,
                                                     score_p2, len_path_p2,
                                                     loc_p2, turn + 1)
            if node_value > best_value:
                best_value = node_value
                best_coin = coin

            if beta <= best_value:  # Algo alpha-beta
                break

        beta = min(best_value, beta)
    elif len_path_p2 <= turn:  # Player2 turn
        # Check if he is on a coin and increase his score :
        if loc_p2 in coins_left:
            score_p2 += 1
            en_last_coin = [loc_p2]
            new_coins_left.remove(loc_p2)

        # Player on is playing, MINIMZING THE OVERALL SCORE
        best_value = float('+inf')
        best_coin = None

        # Search path in all coins
        for coin in coins_left:
            len_path_p2 += dists_matrix[loc_p2][coin]
            loc_p2 = coin  # route_matrix[loc_p1][coin][0]
            # if dists_matrix[loc_p2][coin] < dists_matrix[loc_p2][loc_p1] and dists_matrix[loc_p1][coin] < dists_matrix[loc_p2][loc_p1]:
            # score_p1 += 1 / 2 ** dists_matrix[loc_p1][coin]  # * min(1, dists_matrix[loc_p2][coin] / dists_matrix[loc_p1][coin]) ** 2 # Add a sort of pb
            # else:
            #     score_p1 += 1
            node_value, _, en_path, pl_path = minmax(new_coins_left, score_p1,
                                                     len_path_p1, loc_p1,
                                                     score_p2, len_path_p2,
                                                     loc_p2, turn + 1)
            if node_value < best_value:
                best_value = node_value
                best_coin = coin
            if alpha >= best_value:  # Algo alpha-beta
                break
        alpha = max(best_value, beta)
    else:
        next_turn = min(len_path_p1, len_path_p2)
        best_value, best_coin, en_path, pl_path = minmax(
            new_coins_left, score_p1, len_path_p1, loc_p1, score_p2,
            len_path_p2, loc_p2, next_turn)

    pl_path = pl_last_coin + pl_path
    en_path = en_last_coin + en_path
    return best_value, best_coin, en_path, pl_path