def assign_fitness(amount_of_games, population): # print('Assigning fitness to the population') agent_idx = population.get_fitless_agent_idx() while agent_idx != -1: GA_agent = population.pop[agent_idx] # print('Calculating fitness for agent: ', agent_idx) # print(GA_agent) players = [LudoPlayerGenetic(GA_agent)] + [ StandardLudoPlayers.LudoPlayerRandom() for _ in range(3) ] for id, player in enumerate(players): player.id = id n = amount_of_games fitness = np.zeros(4, dtype=np.int) # print(agent_idx) for i in range(n): np.random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() fitness[players[winner].id] += 1 population.fitness_add(fitness_score=fitness[0] / amount_of_games, index_of_chromosomes=agent_idx) agent_idx = population.get_fitless_agent_idx()
def normal_game(number_of_runs=100): GA_player = population.pop[population.get_best_agent()] # population.pop[population.get_random_agent(1)[0]] players = [LudoPlayerGenetic(GA_player)] + [StandardLudoPlayers.LudoPlayerRandom() for _ in range(3)] for i, player in enumerate(players): player.id = i # print(player) score = np.zeros(4, dtype=np.int) n = number_of_runs start_time = time.time() for i in range(n): ''' for idx in range(4): if players[idx].id == 0: players[idx].load_agent(population.pop[population.get_best_agent()]) ''' np.random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() score[players[winner].id] += 1 # print('Game ', i, ' done') duration = time.time() - start_time print('win distribution:', score / number_of_runs) print('games per second:', n / duration)
def eval_population_worker(queue: mp.Queue, games_per_chromosome, task_counter_queue: mp.Queue, Opponent): while True: population_path = queue.get() folder_path = os.path.dirname(population_path) folder_name = os.path.basename(folder_path) player_name = folder_name.split("+")[0] Player = get_ga_player(player_name) population = np.load(population_path) N = min(len(population), 20) population_idx = np.random.choice(np.arange(len(population)), N, replace=False) population = population[population_idx] save_matrix = np.empty((2, N), np.float) save_matrix[0] = population_idx scores = save_matrix[1] for i, chromosome in enumerate(population): players = [Player(chromosome)] + [Opponent() for _ in range(3)] win_count = 0 for _ in range(games_per_chromosome): random.shuffle(players) game = LudoGame(players) winner = players[game.play_full_game()] if isinstance(winner, Player): win_count += 1 scores[i] = win_count / games_per_chromosome generation_str = os.path.basename(population_path).split(".")[0] scores_path = get_score_file_path(folder_path, generation_str, Opponent.name) assert not os.path.exists(scores_path), "Scores already exists: {}".format(scores_path) np.save(scores_path, save_matrix) task_counter_queue.put(('finished', population_path))
def tournament(chromosomes, Player, game_count): players = [Player(chromosome) for chromosome in chromosomes] while len(players) < 4: players.append(LudoPlayerRandom()) tournament_player_ids = {} for i, player in enumerate(players): tournament_player_ids[player] = i win_rates = np.zeros(4) for _ in range(game_count): random.shuffle(players) game = LudoGame(players) winner = players[game.play_full_game()] win_rates[tournament_player_ids[winner]] += 1 ranked_player_ids = np.argsort(-win_rates) for id in ranked_player_ids: if id < len(chromosomes): return id
def evaluate_agent(agent, amount_games, use_random=True): if use_random: rndPlayer = LudoPlayerRandom() else: rndPlayer = SemiSmartPlayer() players = [agent, agent, rndPlayer, rndPlayer] for i, player in enumerate(players): player.id = i wins = [0, 0, 0, 0] for i in range(amount_games): random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() wins[players[winner].id] += 1 return wins[1]
def tournament(_players, game_count): progress_bar = ProgressBar(widgets=[Percentage()], maxval=game_count).start() players = [player for player in _players] tournament_player_ids = {} for i, player in enumerate(players): tournament_player_ids[player] = i win_rates = np.zeros(4) for i in range(game_count): random.shuffle(players) game = LudoGame(players) winner = players[game.play_full_game()] win_rates[tournament_player_ids[winner]] += 1 progress_bar.update(i + 1) progress_bar.finish() return win_rates / game_count
def normal_play(): players = [] players = [LudoPlayerRandom() for _ in range(3)] epsilon = 0.05 # discount_factor = 0.5 # learning_rate = 0.25 # parameters = [epsilon, discount_factor, learning_rate] t1 = LudoPlayerQLearningAction(parameters, chosenPolicy="greedy", QtableName='QTable', RewardName='Reward') players.append(t1) for i, player in enumerate(players): player.id = i # selv tildele atributter uden defineret i klassen score = [0, 0, 0, 0] n = 1000 start_time = time.time() tqdm_1 = tqdm(range(n), ascii=True) for i in tqdm_1: tqdm_1.set_description_str(f"win rates {np.around(score/np.sum(score),decimals=2)*100}") random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() score[players[winner].id] += 1 for player in players: # Saving reward for QLearning player if type(player)==LudoPlayerQLearningAction: player.append_reward() for player in players: if type(player)==LudoPlayerQLearningAction: player.saveQTable() player.saveReward() duration = time.time() - start_time print('win distribution percentage', (score/np.sum(score))*100) print('win distribution:', score)
def play_tournament(self, chromosome_ids, game_count): flat_pop = self.get_flat_pop() chromosomes = flat_pop[chromosome_ids] players = [self.Player(chromosome) for chromosome in chromosomes] tournament_player_ids = {} for i, player in enumerate(players): tournament_player_ids[player] = i win_rates = np.zeros(4) for _ in range(game_count): random.shuffle(players) game = LudoGame(players) winner = players[game.play_full_game()] win_rates[tournament_player_ids[winner]] += 1 ranked_chromosome_ids = chromosome_ids[np.argsort(-win_rates)] children = self.recombine(*flat_pop[ranked_chromosome_ids[:2]]) children = [self.mutate(child) for child in children] children = [self.Player.normalize(child) for child in children] flat_pop[ranked_chromosome_ids[2:]] = children self.total_game_count += game_count self.cur_tournament_count += 1 self.progress_bar.update(self.cur_tournament_count)
def main(): global GA GA1 = GAPlayer() GA2 = GAPlayer() randomAgent = LudoPlayerRandom() print(GA.getPopulation(0)) for t in range(0, 100): print("Test:", t) players = [GA1, GA1, GA2, GA2] for i, player in enumerate(players): player.id = i GA1.index = 0 GA2.index = 1 score = [0, 0, 0, 0] for _ in tqdm(range(1000)): random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() score[players[winner].id] += 1 print("Wins: ", score)
from pyludo import LudoGame from pyludo.StandardLudoPlayers import LudoPlayerRandom, LudoPlayerFast, LudoPlayerAggressive, LudoPlayerDefensive import random import time players = [ LudoPlayerRandom(), LudoPlayerFast(), LudoPlayerAggressive(), LudoPlayerDefensive(), ] scores = {} for player in players: scores[player.name] = 0 n = 1000 start_time = time.time() for i in range(n): random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() scores[players[winner].name] += 1 print('Game ', i, ' done') duration = time.time() - start_time print('win distribution:', scores) print('games per second:', n / duration)
from pyludo import LudoGame, LudoPlayerRandom, LudoVisualizerStep import pyglet game = LudoGame([LudoPlayerRandom() for _ in range(4)], info=True) window = LudoVisualizerStep(game) print('use left and right arrow to progress game') pyglet.app.run()
def evaluate_agents(agents): players = agents ludoGame = LudoGame(players) return ludoGame.play_full_game()
def main(): global GA GA1 = GAPlayer() GA2 = GAPlayer() GA3 = GAPlayer() GA4 = GAPlayer() randomAgent = LudoPlayerRandom() n = 50 generations = 100 for g in range(0, generations): print("Generation:", g) GA.exportPopulation() players = [GA1, GA2, GA3, GA4] for i, player in enumerate(players): player.id = i a = np.zeros(pops) a += n for _ in tqdm(range(int(n * pops * 0.25))): try: idx = np.random.choice(pops, 4, replace=False, p=np.divide(a, np.sum(a))) except: break a[idx] -= 1 GA1.index = idx[0] GA2.index = idx[1] GA3.index = idx[2] GA4.index = idx[3] #print(a) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() GA.populationScores[players[winner].index] += 1 print(a) print(GA.populationScores) GA.populationScores = np.square(GA.populationScores) players = [GA1, randomAgent, randomAgent, randomAgent] for i, player in enumerate(players): player.id = i if True: score = [0, 0, 0, 0] print(np.argmax(GA.populationScores)) GA1.index = np.argmax(GA.populationScores) for i in tqdm(range(1000)): random.shuffle(players) ludoGame = LudoGame(players) winner = ludoGame.play_full_game() score[players[winner].id] += 1 GA.exportWeigths(GA1.index, score[0]) print('win distribution:', score) f = open("output.txt", "a+") f.write(str(score[0]) + "\n") f.close() print("\t*Mutation*\t") GA.mutate()
from pyludo import LudoGame, LudoVisualizerStep from pyludo.StandardLudoPlayers import LudoPlayerRandom, LudoPlayerFast, LudoPlayerAggressive, LudoPlayerDefensive import pyglet players = [ LudoPlayerRandom(), LudoPlayerFast(), LudoPlayerAggressive(), LudoPlayerDefensive(), ] game = LudoGame(players, info=True) window = LudoVisualizerStep(game) print('use left and right arrow to progress game') pyglet.app.run()