def create_genotype(parents: List[ByteGenotype]) -> ByteGenotype: """ Randomly select bits of the parent Genotypes to create a new child Genotype :param parents: A list of all the possible parents for the child Genotype """ child_byte_array = bytearray() for i in range(len(parents[0])): parent = choice(parents) child_byte_array.append(parent[i]) child = ByteGenotype(child_byte_array) child.mutate(MUTATION_RATE) return child
def crossover_and_mutate(parent1: ByteGenotype, parent2: ByteGenotype) -> ByteGenotype: child_array = [] for i in range(len(parent1)): parent_choice = randint(0, 1) parent: ByteGenotype if parent_choice == 0: parent = parent1 else: parent = parent2 child_array.append(parent[i]) child = ByteGenotype(bytearray(child_array)) child.mutate(MUTATION_RATE) return child
def _choose_genotypes_from_sorted_unique_list( self, genotypes: List[ByteGenotype]) -> List[ByteGenotype]: """ Takes in all the unique Genotypes from the population and selects some to keep :param genotypes: A sorted list of unique Genotypes """ splitting_point = math.floor(self._M / 2) good = [genotypes[i] for i in range(self._M - splitting_point)] # bad = [genotypes[i] for i in range(-1, -splitting_point-1, -1)] bad = ByteGenotype.generate_random_population( splitting_point, len(genotypes[0])) #insert garbage Genotypes return good + bad
def main(): # string_to_find = "hello world" # number_of_primes = 50 # size_of_primes_genotype = 16 black_jack_genotype_length = PlayerParameters.number_of_parameters( ) * 4 # 4 bytes per float fitness_calculator = make_FitnessCalculator(Application.BLACK_JACK, [1000, 20]) population = ByteGenotype.generate_random_population( 20, black_jack_genotype_length) simple_manager = mitosis.SimpleManager(population, fitness_calculator, "simple_mitosis_manager") truncation_manager = mitosis.TruncationManager(population, fitness_calculator, "truncation_manager") brute_force_manager = mitosis.BruteForce(population, fitness_calculator, "brute_force_manager") simple_meiosis_manager = meiosis.SimpleManager(population, fitness_calculator, "simple_meiosis_manager") multiple_children_manager = meiosis.MultipleChildrenManager( population, fitness_calculator, "multiple_children_manager") diversity_manager = meiosis.DiversityManager(population, fitness_calculator, "diversity_manager") tournament_manager = meiosis.TournamentManager(population, fitness_calculator, "tournament_manager") managers = [ simple_meiosis_manager, multiple_children_manager, diversity_manager, brute_force_manager ] optimizer = EvolutionaryOptimizer(managers, True) name_to_reports = optimizer.run_many_lifecycles(5000) rep.generate_many_reports(LifecycleReport.header(), name_to_reports, {}, 1) solutions: List[Tuple[str, any]] =\ [(elem[0], fitness_calculator.converter.convert(elem[1][-1].solution)) for elem in name_to_reports.items()] print("Found solutions:") for elem in solutions: print("From", elem[0]) print("Generated word:", elem[1])
def produce_offspring(self, population: List[ByteGenotype]) -> Tuple[List[ByteGenotype], List[ByteGenotype]]: children = ByteGenotype.generate_random_population(20, len(self._best_genotype)) return [self._best_genotype], children