def __call__(self, individual_0): """ Function call operator. Starts a Tron game and sets the strategies of the two players according to the Individual function arguments. The fitness of the winner increases by 1. :param individual_0: Individual solution :type individual_0: Individual """ print(self.__class__.__name__, '.__call__', individual_0.fitness) strategy_0 = self.decode_individual(individual_0) for game in range(self.game_repetitions): #Create Tron game instance #Pass in the AI player tron_game = TronAdversarial(rows=self.rows, bike_width=self.bike_width, draw_board=self.draw_board, strategy=strategy_0) tron_game.run() winner = tron_game.winner if winner[0] == 1: individual_0.fitness += 1 print(self.__class__.__name__, ".__call__ winner", winner, individual_0.fitness)
def __call__(self, individual_0, individual_1): """ Function call operator. Starts a Tron game and sets the strategies of the two players according to the Individual function arguments. The fitness of the winner increases by 1. :param individual_0: Individual solution :type individual_0: Individual :param individual_1: Individual solution :type individual_1: Individual """ # Decode the strategies print(self.__class__.__name__, '.__call__', individual_0.fitness, individual_1.fitness) strategy_0 = self.decode_individual(individual_0) strategy_1 = self.decode_individual(individual_1) # TODO for 2 ai players the outcome for each repetion will be the same # since there is no stochastic element for game in range(self.game_repetitions): #Create Tron game instance tron_game = TronAdversarial(rows=self.rows, bike_width=self.bike_width, draw_board=self.draw_board, strategy=None) #Set players in Tron tron_game.players[0] = PlayerAI(x=self.rows / 2, y=self.rows / 2, direction=(0, 1), color="Blue", alive=True, id_=0, canvas=tron_game.canvas, board=tron_game.board, strategy=strategy_0) tron_game.players[1] = PlayerAI(x=self.rows / 2 + 1, y=self.rows / 2 + 1, direction=(0, 1), color="Green", alive=True, id_=1, canvas=tron_game.canvas, board=tron_game.board, strategy=strategy_1) # Run the Tron game tron_game.run() # Get the winner winner = tron_game.winner # Increase the fitness of the winner if winner[0] == 0: individual_0.fitness += 1 elif winner[0] is not None: individual_1.fitness += 1 print(self.__class__.__name__, ".__call__ winner", winner, individual_0.fitness, individual_1.fitness)
def __call__(self, individual_0): """ Function call operator. Starts a Tron game and sets the strategy of the player according to the Individual function arguments. The fitness is the length of the tail. :param individual_0: Individual solution :type individual_0: Individual """ # The strategy is the executable GP-tree strategy = individual_0.genome # Play a number of games for game in range(self.game_repetitions): # Create Tron game instance tron_game = TronAdversarial(rows=self.rows, bike_width=self.bike_width, draw_board=self.draw_board, strategy=strategy) # Create an AI GP player tron_game.players[1] = \ PlayerAIGP(x=tron_game.players[1].x, y=tron_game.players[1].y, direction=tron_game.players[1].direction, color=tron_game.players[1].color, alive=tron_game.players[1].alive, id_=tron_game.players[1].ID, canvas=tron_game.players[1].canvas, board=tron_game.players[1].board, strategy=tron_game.players[1].strategy, # Set the strategy # evaluation function in # the tron player evaluator=self.evaluate_strategy) # Run the tron game tron_game.run() # Get the winner of the game winner = tron_game.winner # Get the ID of the winner if winner[0] == 1: # Increase the fitness of the AI if it is the winner individual_0.fitness += 1 print(self.__class__.__name__, ".__call__ winner", winner, individual_0.fitness)
def __call__(self, individual_0, individual_1): """ Function call operator. Starts a Tron game and sets the strategy of the player according to the Individual function arguments. The fitness is the length of the tail. :param individual_0: Individual solution :type individual_0: Individual :param individual_1: Individual solution :type individual_1: Individual """ # Set the strategies as the GP tree of the individuals self.strategies.append(individual_0.genome) self.strategies.append(individual_1.genome) # Play a number of games for game in range(self.game_repetitions): # Create Tron game instance tron_game = TronAdversarial(rows=self.rows, bike_width=self.bike_width, draw_board=self.draw_board, strategy=self.strategies[0]) # Create a list of players players = [] # Loop over the strategies and the players in he Tron game for player, strategy in zip(tron_game.players, self.strategies): # Append the players to the player list players.append( PlayerAIGP( x=player.x, y=player.y, direction=player.direction, color=player.color, alive=player.alive, id_=player.ID, canvas=player.canvas, board=player.board, strategy=strategy, # Set the strategy evaluation function in the tron player evaluator=self.evaluate_strategy)) # Set the players in the tron game tron_game.players = players # Run the tron game tron_game.run() # Get the winner winner = tron_game.winner # Check ID of the winner if winner[0] == 0: # Assign fitness individual_0.wins += 1 elif winner[0] is not None: # Assign fitness individual_1.wins += 1 else: # Punish behavior for drawing individual_0.wins -= 2 individual_1.wins -= 2 # Increase the number of games played individual_0.games += 1 individual_1.games += 1 print(self.__class__.__name__, ".__call__ winner", winner, individual_0.fitness)
def __call__(self, individual_0, individual_1): """ Function call operator. Starts a Tron game and sets the strategy of the player according to the Individual function arguments. The fitness is the length of the tail. :param individual_0: Individual solution :type individual_0: Individual :param individual_1: Individual solution :type individual_1: Individual """ # Set the strategies as the GP tree of the individuals self.strategies.append(individual_0.genome) self.strategies.append(individual_1.genome) # Play a number of games for game in range(self.game_repetitions): # Create Tron game instance tron_game = TronAdversarial(rows=self.rows, bike_width=self.bike_width, draw_board=self.draw_board, strategy=self.strategies[0]) # Create a list of players players = [] # Loop over the strategies and the players in he Tron game for player, strategy in zip(tron_game.players, self.strategies): # Append the players to the player list players.append(PlayerAIGP(x=player.x, y=player.y, direction=player.direction, color=player.color, alive=player.alive, id_=player.ID, canvas=player.canvas, board=player.board, strategy=strategy, # Set the strategy evaluation function # in the tron player evaluator=self.evaluate_strategy)) # Set the players in the tron game tron_game.players = players # Run the tron game tron_game.run() # Get the winner winner = tron_game.winner # Check ID of the winner if winner[0] == 0: # Assign fitness individual_0.wins += 1 elif winner[0] is not None: # Assign fitness individual_1.wins += 1 else: # Punish behavior for drawing individual_0.wins -= 2 individual_1.wins -= 2 # Increase the number of games played individual_0.games += 1 individual_1.games += 1 print(self.__class__.__name__, ".__call__ winner", winner, individual_0.wins, individual_1.wins)