genetic_algorithm.basic_string ============================== .. py:module:: genetic_algorithm.basic_string .. autoapi-nested-parse:: Simple multithreaded algorithm to show how the 4 phases of a genetic algorithm works (Evaluation, Selection, Crossover and Mutation) https://en.wikipedia.org/wiki/Genetic_algorithm Author: D4rkia Attributes ---------- .. autoapisummary:: genetic_algorithm.basic_string.MUTATION_PROBABILITY genetic_algorithm.basic_string.N_POPULATION genetic_algorithm.basic_string.N_SELECTED genetic_algorithm.basic_string.target_str Functions --------- .. autoapisummary:: genetic_algorithm.basic_string.basic genetic_algorithm.basic_string.crossover genetic_algorithm.basic_string.evaluate genetic_algorithm.basic_string.mutate genetic_algorithm.basic_string.select Module Contents --------------- .. py:function:: basic(target: str, genes: list[str], debug: bool = True) -> tuple[int, int, str] Verify that the target contains no genes besides the ones inside genes variable. >>> from string import ascii_lowercase >>> basic("doctest", ascii_lowercase, debug=False)[2] 'doctest' >>> genes = list(ascii_lowercase) >>> genes.remove("e") >>> basic("test", genes) Traceback (most recent call last): ... ValueError: ['e'] is not in genes list, evolution cannot converge >>> genes.remove("s") >>> basic("test", genes) Traceback (most recent call last): ... ValueError: ['e', 's'] is not in genes list, evolution cannot converge >>> genes.remove("t") >>> basic("test", genes) Traceback (most recent call last): ... ValueError: ['e', 's', 't'] is not in genes list, evolution cannot converge .. py:function:: crossover(parent_1: str, parent_2: str) -> tuple[str, str] Slice and combine two strings at a random point. >>> random.seed(42) >>> crossover("123456", "abcdef") ('12345f', 'abcde6') .. py:function:: evaluate(item: str, main_target: str) -> tuple[str, float] Evaluate how similar the item is with the target by just counting each char in the right position >>> evaluate("Helxo Worlx", "Hello World") ('Helxo Worlx', 9.0) .. py:function:: mutate(child: str, genes: list[str]) -> str Mutate a random gene of a child with another one from the list. >>> random.seed(123) >>> mutate("123456", list("ABCDEF")) '12345A' .. py:function:: select(parent_1: tuple[str, float], population_score: list[tuple[str, float]], genes: list[str]) -> list[str] Select the second parent and generate new population >>> random.seed(42) >>> parent_1 = ("123456", 8.0) >>> population_score = [("abcdef", 4.0), ("ghijkl", 5.0), ("mnopqr", 7.0)] >>> genes = list("ABCDEF") >>> child_n = int(min(parent_1[1] + 1, 10)) >>> population = [] >>> for _ in range(child_n): ... parent_2 = population_score[random.randrange(len(population_score))][0] ... child_1, child_2 = crossover(parent_1[0], parent_2) ... population.extend((mutate(child_1, genes), mutate(child_2, genes))) >>> len(population) == (int(parent_1[1]) + 1) * 2 True .. py:data:: MUTATION_PROBABILITY :value: 0.4 .. py:data:: N_POPULATION :value: 200 .. py:data:: N_SELECTED :value: 50 .. py:data:: target_str :value: 'This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!'