def reproduce(self, parent1: Agent, parent2: Agent) -> [Agent, Agent]: """ :param parent1: an Agent to generate a couple of children with another Agent :param parent2: an Agent to generate a couple of children with another Agent :return: a listf of 2 children (new Agents) with the new combination of the parents (Agents) genes (params) """ random.seed() parent1_params = parent1.percent_first_move, \ parent1.percent_second_move, \ parent1.percent_third_move, \ parent1.percent_fourth_move \ parent2_params = parent2.percent_first_move, \ parent2.percent_second_move, \ parent2.percent_third_move, \ parent2.percent_fourth_move index = random.randint(1, 3) child1_params = parent1_params[:index] + parent2_params[index:] child2_params = parent2_params[:index] + parent1_params[index:] child1 = Agent("1", get_random_name(), 0, 0, 0, 0) child2 = Agent("2", get_random_name(), 0, 0, 0, 0) child1.percent_first_move, \ child1.percent_second_move, \ child1.percent_third_move, \ child1.percent_fourth_move = child1_params child2.percent_first_move, \ child2.percent_second_move, \ child2.percent_third_move, \ child2.percent_fourth_move = child2_params return [child1, child2]
def test_agents_play_a_game(): agent1 = Agent("1", get_random_name(), 0, 0, 0, 0) agent2 = Agent("2", get_random_name(), 0, 0, 0, 0) players = [agent1, agent2] game = Connect4('cls') game.level = 4 game.default() game.game_mode = game.game_modes[0] [agent1, agent2] = game.logic_play(players=players) assert ( (agent1.record[agent1.WINS] == 1 or agent1.record[agent1.DRAWS] == 1 or agent1.record[agent1.LOSSES] == 1) and (agent2.record[agent2.WINS] == 1 or agent2.record[agent2.DRAWS] == 1 or agent2.record[agent2.LOSSES] == 1))
def test_reproduce(): trainer = GeneticTrainer(10, 100) parent1 = Agent("1", get_random_name(), 15, 30, 60, 90) parent2 = Agent("2", get_random_name(), 5, 20, 50, 80) child1, child2 = trainer.reproduce(parent1, parent2) parent1_params, parent2_params = [15, 30, 60, 90], [5, 20, 50, 80] child1_params = [ child1.percent_first_move, child1.percent_second_move, child1.percent_third_move, child1.percent_fourth_move ] child2_params = [ child2.percent_first_move, child2.percent_second_move, child2.percent_third_move, child2.percent_fourth_move, ] assert (parent1_params != child1_params and parent2_params != child2_params)
def test_mutate(): trainer = GeneticTrainer(10, 100) individual = Agent("1", get_random_name(), 0.14, 0.33, 0.67, 0.96) original_params = [ individual.percent_first_move, individual.percent_second_move, individual.percent_third_move, individual.percent_fourth_move ] mutated = trainer.mutate(individual, 100) mutated_params = [ mutated.percent_first_move, mutated.percent_second_move, mutated.percent_third_move, mutated.percent_fourth_move ] assert (original_params != mutated_params)
def generate_population(self, population_size) -> list: """ :param population_size: amount of individuals to be created for the new population :return: return the randomly generated population in order to use it if required, if not, it's still kept in the local attribute self.population """ for i in range(0, population_size): random.seed() first_percent = round(random.uniform(0, 0.25), 2) second_percent = round(random.uniform(first_percent, 0.5), 2) third_percent = round(random.uniform(second_percent, 0.75), 2) fourth_percent = round(random.uniform(third_percent, 1), 2) self.population.append( Agent("1", get_random_name(), first_percent, second_percent, third_percent, fourth_percent)) return self.population
def test_make_pairs(): agent1 = Agent("1", get_random_name(), 0, 0, 0, 0) agent2 = Agent("2", get_random_name(), 0, 0, 0, 0) agent3 = Agent("1", get_random_name(), 0, 0, 0, 0) agent4 = Agent("2", get_random_name(), 0, 0, 0, 0) agent5 = Agent("1", get_random_name(), 0, 0, 0, 0) agent6 = Agent("2", get_random_name(), 0, 0, 0, 0) agents = [agent1, agent2, agent3, agent4, agent5, agent6] couples_in_a_row = [] for agent01, agent02 in make_pairs(agents): couples_in_a_row.append(agent01) couples_in_a_row.append(agent02) assert (agents == couples_in_a_row)