r/dailyprogrammer_ideas • • Apr 08 '15
Submitted! [Intermediate] Hello World Genetic or Evolutionary Algorithm
Description
Use either an Evolutionary or Genetic Algorithm to evolve a solution to the fitness functions provided!
Input description
The input string should be the target string you want to evolve the initial random solution into.
The target string (and therefore input) will be
'Hello, world!'
However, you want your program to initialize the process by randomly generating a string of the same length as the input. The only thing you want to use the input for is to determine the fitness of your function, so you don't want to just cheat by printing out the input string!
Output description The ideal output of the program will be the evolutions of the population until the program reaches 'Hello, world!' (if your algorithm works correctly). You want your algorithm to be able to turn the random string from the initial generation to the output phrase as quickly as possible!
Gen: 1 | Fitness: 219 | JAmYv'&L_Cov1
Gen: 2 | Fitness: 150 | Vlrrd:VnuBc
Gen: 4 | Fitness: 130 | JPmbj6ljThT
Gen: 5 | Fitness: 105 | :^mYv'&oj\jb(
Gen: 6 | Fitness: 100 | Ilrrf,(sluBc
Gen: 7 | Fitness: 68 | Iilsj6lrsgd
Gen: 9 | Fitness: 52 | Iildq-(slusc
Gen: 10 | Fitness: 41 | Iildq-(vnuob
Gen: 11 | Fitness: 38 | Iilmh'&wmsjb
Gen: 12 | Fitness: 33 | Iilmh'&wmunb!
Gen: 13 | Fitness: 27 | Iildq-wmsjd#
Gen: 14 | Fitness: 25 | Ihnlr,(wnunb!
Gen: 15 | Fitness: 22 | Iilmj-wnsjb!
Gen: 16 | Fitness: 21 | Iillq-&wmsjd#
Gen: 17 | Fitness: 16 | Iillq,wmsjd!
Gen: 19 | Fitness: 14 | Igllq,wmsjd!
Gen: 20 | Fitness: 12 | Igllq,wmsjd!
Gen: 22 | Fitness: 11 | Igllq,wnsld#
Gen: 23 | Fitness: 10 | Igllq,wmsld!
Gen: 24 | Fitness: 8 | Igllq,wnsld!
Gen: 27 | Fitness: 7 | Igllq,!wosld!
Gen: 30 | Fitness: 6 | Igllo,!wnsld!
Gen: 32 | Fitness: 5 | Hglln,!wosld!
Gen: 34 | Fitness: 4 | Igllo,world!
Gen: 36 | Fitness: 3 | Hgllo,world!
Gen: 37 | Fitness: 2 | Iello,!world!
Gen: 40 | Fitness: 1 | Hello,!world!
Gen: 77 | Fitness: 0 | Hello, world!
Elapsed time is 0.069605 seconds.
Notes/Hints
One of the hardest parts of making an evolutionary or genetic algorithm is deciding what a decent fitness function is, or the way we go about evaluating how good each individual (or potential solution) really is.
One possible fitness function is The Hamming Distance
Bonus
As a bonus make your algorithm able to accept any input string and still evaluate the function efficiently (the longer the string you input the lower your mutation rate you'll have to use, so consider using scaling mutation rates, but don't cheat and scale the rate of mutation with fitness instead scale it to size of the input string!)
Bonus EXTRA HARD MODE
Using the principles you learned from the hello world problem, write an algorithm to find the maximum of the much harder multivariable function:
f(x,y) = (e-x2 - y2+sqrt(5) sin2 (x3 )+2 cos2 (2x+3 y))/(1 + x2 + y2 )
where -3<=x<=3 and -3<=y<=3
or you can check out the function On Wolfram Here
My solution in Matlab can be found in my Github Repository
Hint: By representing the solutions (or genomes) in binary instead of cartesian (x,y) coordinates you will be able to perform the evolutionary and genetic operations on the genomes more easily!
Finally
Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas
2
u/[deleted] Apr 08 '15 edited Apr 08 '15
I haven't figured out spoiler tags yet, so dont look below unless you want to see my code.
Program Spoilers
I did my solution in Matlab and included a number of different selection and crossover schemes so it is easier to learn how they interact with each other. I also vectorized the code so there aren't any for loops outside of the generational loop.
2
u/jnazario Apr 09 '15 edited Apr 09 '15
playing around in scala:
1
u/lukz Apr 09 '15
Your output starts with initial configuration of 13 characters. But in your challenge description you never mention that input value should be exactly 13 characters long. I think you should put that information into the challenge description to make it more complete.
1
u/Fawzors Apr 20 '15
2
u/uncleozzy Apr 22 '15
Long past the freshness date at this point, but here's some ugly C. I've never coded in C on anything approaching a regular basis, so ... yeah, this ain't pretty.
It uses three-parent inheritance to build each generation, selecting a character from the most-fit parent if it matches either of the other two parents at the same position, otherwise choosing the character from the third parent.
The mutation rate starts at 18% and scales down by 1% for every three characters in the input string, to a minimum of 2.5%. I ran a medium-length string (the second one below) repeatedly to come up with those values, as well as to tweak the size of the population. I'm sure there's plenty of room for real optimization there.
It runs at reasonable speed for strings of various lengths:
Code is below:
2
u/weekendblues Apr 27 '15
I gave this a try in rustc 1.0.0-beta (9854143cb 2015-04-02) (built 2015-04-02) just for fun. I'm sure there are ways to do this, but it still converges on "Hello, World!" in 30-60 milliseconds on my machine. Here's the code:
Since characters in rust are UTF-32 encoded, I narrowed things down a little bit, limiting my random characters to 0x20 through 0x7e. There's some weird looking typecasting in there, but that's just the way it goes in rust right now. I will say, I'm not error handing at all to make sure no one enters a "UTF-32" string outside of ascii characters in the first place, but for the purposes of this exercise I'm not particularly concerned with that. Here's some output: