genetic_algorithm.basic_string¶
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¶
Functions¶
|
Verify that the target contains no genes besides the ones inside genes variable. |
|
Slice and combine two strings at a random point. |
|
Evaluate how similar the item is with the target by just |
|
Mutate a random gene of a child with another one from the list. |
|
Select the second parent and generate new population |
Module Contents¶
- genetic_algorithm.basic_string.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
- genetic_algorithm.basic_string.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’)
- genetic_algorithm.basic_string.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)
- genetic_algorithm.basic_string.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’
- genetic_algorithm.basic_string.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
- genetic_algorithm.basic_string.MUTATION_PROBABILITY = 0.4¶
- genetic_algorithm.basic_string.N_POPULATION = 200¶
- genetic_algorithm.basic_string.N_SELECTED = 50¶
- genetic_algorithm.basic_string.target_str = 'This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!'¶