def __init__(self, quantity: int): self.individuals = [] i = 0 while i < quantity: actual = Individual() if (actual.check_viability()) and ( not self.equal_individuals(actual)): self.individuals.append(actual) i += 1
def crossover(self, parent1: Individual, parent2: Individual): # o cruzamento é feito até que se gere filhos que atendam às restrições allowed = False while not allowed: gene_parent1 = random.randint(0, 4) gene_parent2 = random.randint(0, 4) # troca-se os genes sorteados temp_value = parent1[gene_parent1] parent1[gene_parent1] = parent2[gene_parent2] parent2[gene_parent2] = temp_value if parent1.check_viability() and parent2.check_viability(): allowed = True else: temp_value = parent1[gene_parent1] parent1[gene_parent1] = parent2[gene_parent2] parent2[gene_parent2] = temp_value return