def test_set_strategies_true(): """ Check that set_strategies function is working correctly """ player_1 = Agent(1, 2, True) # 1 symbol of the agent, 2 symbol of the opponent strategies = [0.5, 0.6, 0.8, 0.5] player_1.set_strategies(strategies) assert(player_1.strategies == strategies)
def init_population(self,population_a): self.population.clear() for index in range(population_a): new_agent = Agent(1,2,True) new_agent.set_strategies(self.get_random_probabilities()) new_individual = IndividualByAgent(new_agent) self.population.append(new_individual)
def config_players(self): parser = argparse.ArgumentParser(description='Configuración del ' + 'Juego.') parser.add_argument("-gt", "--game-type", type=int, help="Define el tipo de juego. ") parser.add_argument("-a1c", "--agent1c", type=str, help="Características del agente 1") parser.add_argument("-a2c", "--agent2c", type=str, help="Características del agente 2") parser.add_argument("-p1c", "--agentec", type=str, help="Características del agente") args = parser.parse_args() # 0 -> H vs A, 1 -> A vs A # player 2 symbol = 2, and oponent = 1 self.player_2 = Agent(2, 1) if (args.game_type == 0): self.player_2.set_strategies(eval(args.agentec)) self.player_1 = Human() else: self.player_2.set_strategies(eval(args.agent1c)) self.player_1 = Agent(1, 2) self.player_1.set_strategies(eval(args.agent2c))
def test_get_strategy_0(): """ Check that get_strategy is working correctly """ b = [[1, 0, 1, 2, 1, 1, 0], [2, 0, 0, 2, 1, 2, 0], [1, 0, 0, 1, 2, 2, 0], [2, 0, 0, 0, 1, 0, 0], [2, 0, 0, 0, 1, 0, 0], [1, 0, 0, 0, 2, 0, 0]] board = Board() board.board = b player_1 = Agent(1, 2, True) # 1 symbol of the agent, 2 symbol of the opponent player_1.set_strategies([0,0,0,0]) var_action = player_1.get_strategy(board) array_result = [[0, 1, 1], [1, 2, 2], [2, 2, 2], [3, 2, 2], [4, 1, 1], [5, 3, 3], [6, 2, 2]] result = True for x in var_action: position = array_result[x.position][0] amount = array_result[x.position][1] strategies_number = array_result[x.position][2] print(x.position,x.amount, x.strategies_number) if not(x.position == position and x.amount == amount and strategies_number == x.strategies_number): result = False assert(result)
def test_get_column_number(): """ Check that get_column_number is working correctly """ player_1 = Agent(1, 2, True) # 1 symbol of the agent, 2 symbol of the opponent array_number = [] for i in range(0,7): var_number = Number(i) if(i == 2): var_number.increase_strategy() var_number.increase_strategy() var_number.increase_amount() array_number.append(var_number) col = player_1.get_column_number(array_number) col_result = 2 assert(col == col_result)
def test_get_array_number(): """ Check that get_column_number is working correctly """ array_result = [[0, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0], [4, 0, 0], [5, 0, 0], [6, 0, 0]] player_1 = Agent(1, 2, True) # 1 symbol of the agent, 2 symbol of the opponent var_action = player_1.get_array_number() result = True for x in var_action: position = array_result[x.position][0] amount = array_result[x.position][1] strategies_number = array_result[x.position][2] if not(x.position == position and x.amount == amount and strategies_number == x.strategies_number): result = False assert(result)
def test_next_action_block(): """ Check that next_action is working correctly """ array = [[1, 1, 1, 2, 1, 0, 0], [2, 1, 1, 1, 2, 0, 0], [0, 0, 1, 2, 2, 0, 0], [0, 0, 0, 2, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] board = Board() board.set_board(array) player_1 = Agent(1, 2, True) # 1 symbol of the agent, 2 symbol of the opponent player_2 = Agent(2, 1, True) # 2 symbol of the agent, 1 symbol of the opponent col_result = player_2.next_action(board) col = 2 assert(col == col_result)
def init_population(self, population_a): self.population.clear() for index in range(population_a): new_agent_1 = Agent(1, 2) new_agent_2 = Agent(2, 1) new_agent_1.set_strategies(self.get_random_probabilities()) new_agent_2.set_strategies(self.get_random_probabilities()) new_individual = Individual(new_agent_1, 0, new_agent_2, 0) self.population.append(new_individual)
def get_first_best_agent(self, agent_possibilities, agent_cross, min_games=cf.MIN_GAMES): for strategies in agent_possibilities: agent = Agent(1, 2, True) agent.strategies = list(strategies) #parent 1 game_1, ties = self.get_won_games_agent(agent, self.agent) game_2, ties = self.get_won_games_agent(agent, agent_cross.agent) if game_1 >= min_games and game_2 >= min_games: return IndividualByAgent(agent) # If doesn't exist any good agent return any parent if self.won_games >= agent_cross.won_games: return IndividualByAgent(self.agent) else: return IndividualByAgent(agent_cross.agent)
def test_get_winner_generation(): array_real.append(Individual.fit_agents) array_real.append(Individual.get_winner_agent) #Fake agent agent = Agent(1, 2) agent.strategies = "STRATEGIES" # Change functions Individual.fit_agents = lambda _: 1 Individual.get_winner_agent = lambda _: [agent, 0, 0] test_genetics.population[0] = Individual(agent, 0, agent, 0) result = test_genetics.get_winner_from_generation() assert (result == "STRATEGIES") #recovery methods Individual.get_winner_agent = array_real.pop() Individual.fit_agents = array_real.pop()
class Game_Controller(): def __init__(self): self.player_1 = Player() self.player_2 = Player() # Methods------------------------- # @Method: CONFIG_PLAYERS # @Description: def config_players(self): parser = argparse.ArgumentParser(description='Configuración del ' + 'Juego.') parser.add_argument("-gt", "--game-type", type=int, help="Define el tipo de juego. ") parser.add_argument("-a1c", "--agent1c", type=str, help="Características del agente 1") parser.add_argument("-a2c", "--agent2c", type=str, help="Características del agente 2") parser.add_argument("-p1c", "--agentec", type=str, help="Características del agente") args = parser.parse_args() # 0 -> H vs A, 1 -> A vs A # player 2 symbol = 2, and oponent = 1 self.player_2 = Agent(2, 1) if (args.game_type == 0): self.player_2.set_strategies(eval(args.agentec)) self.player_1 = Human() else: self.player_2.set_strategies(eval(args.agent1c)) self.player_1 = Agent(1, 2) self.player_1.set_strategies(eval(args.agent2c)) # Methods------------------------- # @Method: GET_CONFIG_PLAYERS # @Description: Return the players, and the game type def get_config_players(self): if (isinstance(self.player_1, Human)): return [self.player_1, self.player_2, 0] else: return [self.player_1, self.player_2, 1] def play_game(self): players = self.get_config_players() self.game = Game(players[0], players[1]) self.game.play_game()
def test_win_false(): """ Check that win function is working correctly """ array = [[1, 1, 1, 2, 1, 0, 0], [2, 1, 2, 1, 2, 0, 0], [0, 0, 1, 2, 2, 0, 0], [0, 0, 0, 2, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] board = Board() board.set_board(array) player_1 = Agent(1, 2, True) # 1 symbol of the agent, 2 symbol of the opponent player_2 = Agent(2, 1, True) # 2 symbol of the agent, 1 symbol of the opponent col_to_win_result = player_1.win(board, 1) col_to_win = -1 col_to_win_result_2 = player_2.win(board, 2) col_to_win_2 = -1 assert(col_to_win == col_to_win_result) assert(col_to_win_2 == col_to_win_result_2)
def test_block_true(): """ Check that block function is working correctly """ array = [[1, 1, 1, 2, 1, 0, 0], [2, 1, 1, 1, 2, 0, 0], [0, 0, 1, 2, 2, 0, 0], [0, 0, 0, 2, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] board = Board() board.set_board(array) player_1 = Agent(1, 2, True) # 1 symbol of the agent, 2 symbol of the opponent player_2 = Agent(2, 1, True) # 2 symbol of the agent, 1 symbol of the opponent col_to_block_result = player_1.block(board, 2) col_to_block = 4 col_to_block_result_2 = player_2.block(board, 1) col_to_block_2 = 2 assert(col_to_block == col_to_block_result) assert(col_to_block_2 == col_to_block_result_2)
def __init__(self): self.player_1 = Player() self.player_2 = Player()
# Project: Connect 4 # Author : Katerine Molina Sanchez # E-mail: [email protected] # Version: 0.0.0 #IMPORT SECTION from model.genetics.genetics_algorithm_by_agent import GeneticAlgorithmByAgent from model.genetics.individual_by_agent import IndividualByAgent from model.game.agent import Agent from config.config import Config as cf from model.genetics.game_genetics import GameGenetics import random as rd # Fake agents agent_1 = Agent(1,2) agent_2 = Agent(1,2) test_individual = IndividualByAgent(agent_1) test_individual_1 = IndividualByAgent(agent_2) population = 10 generation = population test_genetics = GeneticAlgorithmByAgent(population,generation) array_stubs = [] array_real = [] # GET_RANDOM_PROBABILITIES_TEST-------------------------------- def test_get_random_probabilities():
# Project: Connect 4 # Author : Katerine Molina Sanchez # E-mail: [email protected] # Version: 0.0.0 #IMPORT SECTION from model.genetics.individual_by_agent import IndividualByAgent from config.config import Config as cf from model.genetics.game_genetics import GameGenetics from model.game.agent import Agent import random import itertools # Fake agents agent = Agent(1, 2, True) test_individual = IndividualByAgent(agent) test_individual_1 = IndividualByAgent(agent) array_stubs = [] array_real = [] def test_initialize_individual(): individual = IndividualByAgent(agent) assert (individual.agent == agent) # MUTATE AGENT TESTS---------------------------------------------
# Package: tests.model_tests.game # Description: tests of structure game # Artificial Intelligence, II Semester 2018 # Project: Connect 4 # Author : Mariana Rojas S. # E-mail: [email protected] # Version: 0.0.0 # IMPORT SECTION from model.game.game import Game from model.game.human import Human from model.game.agent import Agent player_1 = Human() player_2 = Agent(2, 1, True) # 2 symbol of the agent, 1 symbol of the opponent game = Game(player_1, player_2) game.board.created_board() def test_play_game(): """ Check that game is working correctly """ pass def test_turn_player_1(): """ Check that turn of the player is working correctly """ turn = "Turno del Jugador 1" turn_result = game.turn("1")