Java “Hello World!” Genetic Algorithm (GA).

Just finished a Java application that evolves the phrase “Hello World!”, or any other arbitrary phrase for that matter.

I was to some extend inspired by this post that I found, by a guy who did the same thing in C++.

Using his parameters for a start, the application is able to evolve the phrase “Hello World!” within 70-80 epochs using these settings/methodology:

  • Population size: 1024
  • Fixed chromosome lenght (same as the target phrase: “Hello World!”)
  • 10% elite
  • 25% chance of mutation on non-elite members of the population
  • Pair-bred crossover method, using a single point crossover, producing two children for each set of parents

The ever so important fitness function looks like this:

/* Calculate fitness */
 for(int i=0;i<population.length;i++){
    population[i].fitness = 0;
       for(int j=0;j<population[i].s.length();j++){
       population[i].fitness += Math.abs((int)(target.charAt(j))-(int)(population[i].s.charAt(j)));
    }
 }

The source code can be found here: http://www.havnemark.dk/misc/GAHelloWorld.zip

It is a netbeans project, but should easily import to Eclipse. And the project only consists of two classes, so adapting it to any other IDE should be simple.

Sample run, generates this output:

run:
Now running Java Hello World Genetic Algorithm.
Single threaded application, written by:
Kim H. Rasmussen - Uni. Southern Denmark, April 2012

Iteration: 0 Best Fitness: 530 Text: (j]$=hh15L>k
Iteration: 1 Best Fitness: 162 Text: VQnc^=Qr`siA
Iteration: 3 Best Fitness: 127 Text: NIo9n!Eptnr!
Iteration: 8 Best Fitness: 113 Text: :om_r/VawVX$
Iteration: 10 Best Fitness: 108 Text: BcnYf$Ife}`*
Iteration: 11 Best Fitness: 96 Text: Kmrch4JinkU%
Iteration: 13 Best Fitness: 92 Text: >f|t]!UoseV/
Iteration: 15 Best Fitness: 87 Text: Hl`Uu!Yid^d#
Iteration: 16 Best Fitness: 84 Text: Bfigz(Mid^i
Iteration: 17 Best Fitness: 71 Text: Hgnih%_xim+
Iteration: 19 Best Fitness: 70 Text: Sq~mx!Soovd
Iteration: 21 Best Fitness: 59 Text: Bpfmz&Tooug!
Iteration: 22 Best Fitness: 56 Text: C`dqm!^g~lb"
Iteration: 23 Best Fitness: 55 Text: Iqtlg#Koun^!
Iteration: 26 Best Fitness: 51 Text: Kgfp]!Rrnlb$
Iteration: 28 Best Fitness: 49 Text: FUelo!Koun^!
Iteration: 30 Best Fitness: 44 Text: Dihjk#\pmug!
Iteration: 31 Best Fitness: 30 Text: Ejeoo"Soqnb"
Iteration: 33 Best Fitness: 27 Text: Bfigm!Vlsmg!
Iteration: 43 Best Fitness: 25 Text: Ealgo"Ypnmd$
Iteration: 46 Best Fitness: 23 Text: Hikkl$Tkplc!
Iteration: 48 Best Fitness: 21 Text: Hglgo"Ylpkc$
Iteration: 49 Best Fitness: 18 Text: Hglni!Voqne#
Iteration: 51 Best Fitness: 16 Text: Hgloo"Soqke#
Iteration: 52 Best Fitness: 15 Text: Gfkir!Torkd
Iteration: 56 Best Fitness: 14 Text: Hfkmn"Upsmf
Iteration: 57 Best Fitness: 13 Text: Hglno"Tpsle"
Iteration: 59 Best Fitness: 11 Text: Hflmo"[mrmd!
Iteration: 64 Best Fitness: 9 Text: Hflmo!Tpsle!
Iteration: 65 Best Fitness: 8 Text: Hflmo"Vnqlc!
Iteration: 70 Best Fitness: 7 Text: Hfkmo Uoslc!
Iteration: 74 Best Fitness: 5 Text: Helno!Vosld!
Iteration: 85 Best Fitness: 4 Text: Helmo Uoqld!
Iteration: 88 Best Fitness: 2 Text: Hello Voqld!
Iteration: 95 Best Fitness: 1 Text: Hello Vorld!
Best Fitness: 0 Text: Hello World!

Solution found!
BUILD SUCCESSFUL (total time: 0 seconds)