Genetic Word Algorithm - danielwilczak101/EasyGA GitHub Wiki
EasyGA can be used to create and experiment with words and sentences of various degrees of complexity.

This problem utilizes the genetic algorithm to create a word or sentence that user has inputted. The goal is that the algorithm will continue to generate until it matches the inputted string. Depending on the quality of the users CPU it may take several minutes for the GA to finish evolving into the inputted string, but given enough time it should sort itself out. The only exception is if the algorithm takes more than 1 million generations to evolve, at this point it will stop.
Here are two examples of the algorithm in action. The first example is with a short and relatively simple word 'EasyGA', the second is slightly more complex being 'import EasyGA'.
It is clear to see that as more characters are added, the complexity increases and so more generations must pass until the algorithm finishes.
Here is the code for the program.
import EasyGA
import random
#Create the Genetic Algorithm
ga = EasyGA.GA()
#User inputs a word that the GA will attempt to evolve into
word = input("Please enter a word or sentence (Use only standard characters such as letters, spaces and, punctuation marks): \n")
ga.chromosome_length = len(word)
ga.fitness_goal = len(word)
ga.population_size = 50
ga.generation_goal = 1000000
def alpha_organizer(chromosome):
return sum(
1
for gene, letter
in zip(chromosome, word)
if gene.value == letter
)
ga.fitness_function_impl = alpha_organizer
ga.adapt_population_flag = False
ga.adapt_rate = 0
# Creates random genes utilizing the characters below
ga.gene_impl = lambda: random.choice(["A","a","B","b","C","c","D","d","E","e",
"F","f","G","g","H","h","I","i","J","j",
"K","k","L","l","M","m","N","n","O","o",
"P","p","Q","q","R","r","S","s","T","t",
"U","u","V","v","W","w","X","x","Y","y",
"Z","z"," ",".","!","?"])
ga.evolve()
#Print your default genetic algorithm
ga.print_generation()
ga.print_population()
#Prints a graph of the genetic algorithm
ga.graph.highest_value_chromosome()
ga.graph.show()