This is a Java
program, run in the terminal, that generates a target word/phrase using Genetic Algorithms
This program uses ANSI Escape Sequences
meaning that Windows Command Prompt (cmd) CANNOT OUTPUT CORRECTLY!
If you are using Windows, there are a number of terminal emulators available that do support ANSI Escape Sequences
, the easiest of which to get is:
These are some terms that are necessary to understand in order to use this program:
Target
The word or phrase that the program will try to generate using genetic algorithms
Word
Each
Word
has the same number of letters as thetarget
word/phraseEvery
Word
has afitness
based on the letters it contains
Fitness
If a letter in a given
Word
is the same letter and in the same spot as in thetarget
, it is worth 1fitness
Fitness
for eachWord
is calculated by summing up each letter'sfitness
Example:
target
= cat
Word
= cac
Fitness
= 2 (1 for the c and 1 for the a and 0 for the second c)
Population
A collection of
Words
used to generate newWords
A
Population
knows:The highest
fitness
within the populationThe word that has the highest
fitness
in the populationThe average
fitness
in the population
Generation
Each
Generation
a newPopulation
is createdThis new
Population
gets itsWords
by mutating theWords
of the oldPopulation
It does this by picking 2
Words
from the oldPopulation
asparents
and creating a newWord
(thechild
) from thoseparents
Every
child
has a 50% chance to inherit a letter from eachparent
(50% to inherit from parent 1 and 50% to inherit from parent 2)Every
child
also has a user determined percent chance to have each lettermutate
A
mutation
replaces that letter with a random letter and no inheritance happens for that letter
java wordGenerator.WordGenerator
If this command does not work, you probably need to define the classpath for the JVM to use.
While in the JavaWordGenerator
folder use the command:
java -cp . wordGenerator.WordGenerator
or java --classpath . wordGenerator.WordGenerator
It will ask you to input some values:
Population size
The number of words to be mutated in a
Population
Population size
is anint
Mutation Percentage
The percentage each letter in a given word has to
mutate
(randomly change to another letter)
Mutation Percentage
is entered as anint
which is interpreted as a percentageex:
Mutation Percentage
= 10 -> 10% chance for a letter to mutate
Target
The word/phrase that this program will try to generate
is a
String
and can include A-Z, a-z, 0-9, spaces, and symbols
Input: java wordGenerator.WordGenerator
Output: Please enter population size
Input: 100
Output: Please enter the percentage for the mutation to occour
Input: 1
Output: Please enter target word or phrase
Input: Hello World
Output:
Generation: 559
Top Word: Hello World
Fitness: 11
Average Fitness: 9.880000
Elapsed time: 0.8557480000000001 second(s)