def determineNextMove(playerLocation, opponentLocation, coins): global route, currentcoin, old_coins, meta_route, meta_route_len, next_coin # api.debug("Update routes...") t = time() t0 = t # update for our location update_dists_from_each(dists_matrix, routes_matrix, playerLocation, mazeMap, coins) # update the oponent location try: update_dists_from_each(dists_matrix, routes_matrix, opponentLocation, mazeMap, coins) except: pass # api.debug("Time : " + str(time() - t)) meta_route = [c for c in meta_route if c in coins] # api.debug("Calc init route...") t = time() if playerLocation in old_coins: next_coin = meta_route.pop(0) route = location_list_to_route([playerLocation, next_coin], routes_matrix) # api.debug("Time : " + str(time() - t)) next_move = route.pop(0) # Discard the first element to avoid back and forth for _ in range(3000): if len(meta_route) > 2: meta_route_len, meta_route_len = opt_algorithm(meta_route, meta_route_len, dists_matrix) t_tot = time() - t0 # api.debug("TOTAL TIME : " + str(t_tot)) if t_tot > .1: api.debug("/!\OVER_SHOT : +" + str(t_tot - .1)) old_coins = coins return u.direction(playerLocation, next_move)
def initialisationTurn(mazeWidth, mazeHeight, mazeMap, preparationTime, turnTime, playerLocation, opponentLocation, coins): """Function called once at the begining of the game""" global route_table, packages, best_weight, best_path, route fill_packages(coins) current_package = packages.pop(0) exhaustive(current_package, playerLocation, [], 0, (route_table, dists)) api.debug(best_path)
def evaluate_route(route, coins_dists): route_score = 0 for i in range(len(route) - 1): l1, l2 = route[i], route[i + 1] if l1 == l2: api.debug(route) route_score += coins_dists[l1][l2] return route_score
def evaluate_route(route, coins_dists): route_score = 0 for i in range(len(route) - 1): l1, l2 = route[i], route[i+1] if l1 == l2: api.debug(route) route_score += coins_dists[l1][l2] return route_score
def determineNextMove(playerLocation, opponentLocation, coins): """Function called at each turn, must return the next move of the player""" global packages, route_table, best_path, best_weight, route if len(best_path) == 0: current_package = packages.pop(0) exhaustive(current_package, playerLocation, [], 0, (route_table, dists)) api.debug(best_path) return u.direction(playerLocation, best_path.pop(0))
def init_game(mazeWidth, mazeHeight, maze_map, preparationTime, turnTime, playerLocation, opponentLocation, coins): TURNS = 1 api.debug("Distances...") t = time.time() all_good_dists, all_good_routes = dists_from_each(coins + [playerLocation], maze_map) api.debug(time.time() - t) api.debug("Population...") population = generate_population(playerLocation, coins, all_good_dists) for i in range(TURNS + 1): population = update_pop(population, coins, all_good_dists) score, route = population[0] if i % 100 == 0: api.debug("%s : %s" % (i, score)) api.debug("Start...")
def opt_algorithm(route, route_len, all_dists): key1 = randrange(0, len(route) - 1) key2 = randrange(key1 + 1, len(route)) part1 = route[:key1] part2 = route[key1:key2] part3 = route[key2:] new_route = part1 + [k for k in reversed(part2)] + part3 # api.debug(key) # api.debug(route) new_route_len = evaluate_route(new_route, all_dists) if route_len > new_route_len: api.debug("FROM " + str(route_len) + " TO " + str(new_route_len)) return new_route, new_route_len else: return route, route_len
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)
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]]
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))
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)
def initialisationTurn(mazeWidth, mazeHeight, maze_map, timeAllowed, playerLocation, opponentLocation, coins): global dists_matrix, route_matrix, coins_probability, coins_to_get dists_matrix, route_matrix = u.dists_from_each( coins + [playerLocation, opponentLocation], maze_map) coins_probability = get_probability_coins(playerLocation, opponentLocation, coins, dists_matrix, tests=100) interface.debug(coins_probability) # First : we go to the 11 in the middle coins_phase1 = [ coin for coin in coins if coins_probability[coin]['player1'] > .6 ] sorted(coins_phase1, key=lambda x: coins_probability[x]['player1'], reverse=False) coins_to_get = coins_phase1[:10] coins_to_get = voyageur_commerce(playerLocation, coins_to_get, dists_matrix) # Then we get to the ones close to us : # coins_phase2 = coins_phase1[7:16] # coins_phase2 = voyageur_commerce(coins_phase1[-1], coins_phase2, dists_matrix) # coins_to_get += coins_phase2 interface.debug(len(coins_to_get)) interface.debug(coins_to_get)
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]]
def fill_packages(coins): """Fill packages, also create the route table from any coin to any coin """ global packages, route_table, dists used = [] dists, route_table = u.dists_from_each(coins + [playerLocation], mazeMap) dists_list = sorted([d for di in dists for d in di]) quart_dist = dists_list[len(dists_list) // 4] for c in coins: for k in coins: if dists[c][ k] < quart_dist and k not in used and c not in used and k != c: used.append(c) used.append(k) packages[c] = [c] packages[c].append(k) for c in coins: if c not in used: packages[c] = [c] packages = [packages[p] for p in packages] packages = sorted(packages, key=lambda x: len(x) / dists[playerLocation][x[0]]) api.debug(packages)
def determineNextMove(playerLocation, opponentLocation, coins): global route, currentcoin, old_coins, meta_route, meta_route_len, next_coin # api.debug("Update routes...") t = time() t0 = t # update for our location update_dists_from_each(dists_matrix, routes_matrix, playerLocation, mazeMap, coins) # update the oponent location try: update_dists_from_each(dists_matrix, routes_matrix, opponentLocation, mazeMap, coins) except: pass # api.debug("Time : " + str(time() - t)) meta_route = [c for c in meta_route if c in coins] # api.debug("Calc init route...") t = time() if playerLocation in old_coins: next_coin = meta_route.pop(0) route = location_list_to_route([playerLocation, next_coin], routes_matrix) # api.debug("Time : " + str(time() - t)) next_move = route.pop( 0) # Discard the first element to avoid back and forth for _ in range(3000): if len(meta_route) > 2: meta_route_len, meta_route_len = opt_algorithm( meta_route, meta_route_len, dists_matrix) t_tot = time() - t0 # api.debug("TOTAL TIME : " + str(t_tot)) if t_tot > .1: api.debug("/!\OVER_SHOT : +" + str(t_tot - .1)) old_coins = coins return u.direction(playerLocation, next_move)
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)
def initialisationTurn(mazeWidth, mazeHeight, maze_map, timeAllowed, playerLocation, opponentLocation, coins): global dists_matrix, route_matrix, coins_probability, coins_to_get dists_matrix, route_matrix = u.dists_from_each(coins + [playerLocation, opponentLocation], maze_map) coins_probability = get_probability_coins(playerLocation, opponentLocation, coins, dists_matrix, tests=100) interface.debug(coins_probability) # First : we go to the 11 in the middle coins_phase1 = [coin for coin in coins if coins_probability[coin]['player1'] > .6] sorted(coins_phase1, key=lambda x: coins_probability[x]['player1'], reverse=False) coins_to_get = coins_phase1[:10] coins_to_get = voyageur_commerce(playerLocation, coins_to_get, dists_matrix) # Then we get to the ones close to us : # coins_phase2 = coins_phase1[7:16] # coins_phase2 = voyageur_commerce(coins_phase1[-1], coins_phase2, dists_matrix) # coins_to_get += coins_phase2 interface.debug(len(coins_to_get)) interface.debug(coins_to_get)
meta_route_len, meta_route_len = opt_algorithm( meta_route, meta_route_len, dists_matrix) t_tot = time() - t0 # api.debug("TOTAL TIME : " + str(t_tot)) if t_tot > .1: api.debug("/!\OVER_SHOT : +" + str(t_tot - .1)) old_coins = coins return u.direction(playerLocation, next_move) ######## ######## EXECUTION ######## api.debug("Calc all dists...") t = time() t0 = t locs = [playerLocation] + coins if opponentLocation != (-1, -1): locs.append(opponentLocation) dists_matrix, routes_matrix = dists_from_each(locs, mazeMap) api.debug("Time : " + str(time() - t)) api.debug("Calc init route...") t = time() meta_route, meta_route_len = path_from_nearest(playerLocation, opponentLocation, coins, dists_matrix) next_coin = meta_route.pop(0)
for _ in range(3000): if len(meta_route) > 2: meta_route_len, meta_route_len = opt_algorithm(meta_route, meta_route_len, dists_matrix) t_tot = time() - t0 # api.debug("TOTAL TIME : " + str(t_tot)) if t_tot > .1: api.debug("/!\OVER_SHOT : +" + str(t_tot - .1)) old_coins = coins return u.direction(playerLocation, next_move) ######## ######## EXECUTION ######## api.debug("Calc all dists...") t = time() t0 = t locs = [playerLocation] + coins if opponentLocation != (-1, -1): locs.append(opponentLocation) dists_matrix, routes_matrix = dists_from_each(locs, mazeMap) api.debug("Time : " + str(time() - t)) api.debug("Calc init route...") t = time() meta_route, meta_route_len = path_from_nearest(playerLocation, opponentLocation, coins, dists_matrix) next_coin = meta_route.pop(0) api.debug("Time : " + str(time() - t))
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)