67
12
Richard Dawkins in his book The Blind Watchmaker, describes a Weasel program. The algorithm can be described as follows:
Start with a random string of 28 characters. Valid characters are all upppercase letters, and space.
Make 100 copies of that string, with a 5% chance per character of that character being replaced with a random character.
Compare each new string with the target "METHINKS IT IS LIKE A WEASEL", and give each a score according to the number of letters in the string which are correct and in the correct position.
If any of the new strings has a perfect score (28), halt.
Choose the highest-scoring string from step 3. How you work out a tie is up to you, but only one string may be chosen. Take the chosen string and go to step 2.
The winner will be the shortest code snippet to get to the correct answer while printing the highest-scoring string of each generation in the following format:
If people could help by checking other peoples answers would be very helpful!
When do we print the string? Does the originally generated string get printed, or only after running the algorithm on it once? (If it's the latter, I can save a lot of code) – Justin – 2015-08-24T16:59:45.410
4Which characters are allowed? Unicode? Lowercase? – Oriol – 2014-01-03T17:42:04.010
4Ah, I love Dawkins. Beauty, and feasibility of evolution shown in a simple algorithm. – Cruncher – 2014-01-03T18:16:16.207
May step 4 be better replaced with "step 1.5) If the new strings has a perfect score (28), halt" and "step 4) Take the highest scoring string, and go to step 1.5."? That is, if the initial random string is a winner, need we fan out? – Rob Starling – 2014-01-03T18:21:11.190
@RobStarling ah, good point. This algorithm seems to assume that you'll never random the target. – Cruncher – 2014-01-03T18:22:54.483
1I'm a bit confused as to the order of operations here. Is the intent that we make 100 new strings, based on the original string? Or is it 100 new strings, with the first string being based on the original, and each subsequent string based on the previous string? The algorithm description seems to imply the former, while the sample output appears to be of the latter. – Iszi – 2014-01-06T19:18:17.140
Also, +1 to @Oriol - do we have to use all-uppercase characters or can we do it in all-lowercase? What about mixed case? – Iszi – 2014-01-06T20:06:34.717
its the latter @lszi – Noelkd – 2014-01-06T20:29:05.260
@Oriol it's just the 26 (capital) letters, and a space. – Noelkd – 2014-01-06T20:31:28.443
@Noelkd The wording definitely implies the former. You should probably edit the question to make this more clear. It does say "Make 100 copies of this string" – Cruncher – 2014-01-07T14:19:12.487
@Cruncher it makes sense if you read it in the correct order. If you read 4. then go to 2. The "This" is refering to the string you are taking from step four. Not got any great reason to defend the steps as they were simply copied and pasted from wikipedia, if you feel it would read better with an edit feel free to make that edit. – Noelkd – 2014-01-07T16:49:07.620
@Noelkd At step 2 (whether your come from step 1 or 4) says to create 100 copies of the same string(with the random mutations). What Iszi was asking is if, you only make a copy of it once, then the other 99 are chained down from that. – Cruncher – 2014-01-07T17:01:33.963
Some solutions at Rosetta Code: Evolutionary algorithm
– zx8754 – 2014-01-07T17:17:43.873@Cruncher thanks for taking the time to explain, I really wasn't getting that. The 100 mutations are all mutations of the string you took with you into step two. So if you have a string
"hello world"
and all the mutations would be based of"hello world"
for that generation, if that makes sense? – Noelkd – 2014-01-07T17:20:48.370Yes, that makes sense, and was my interpretation of the problem. @Iszi did say
while the sample output appears to be of the latter
. I'm not sure where that conclusion came from though. – Cruncher – 2014-01-07T17:29:08.607@Cruncher & Noelkd - Maybe I'm confused as to what the output should be, then. When it says "printing each generation" does it mean we should print all 100 strings that are generated each time the algorithm loops, or only the one that is going to be fed into the next loop? Assuming that the 100 copies for each round are based off of the initial seed, the output sample appears to be of only the seed strings plus the final perfect string. – Iszi – 2014-01-07T17:44:17.657
I'd like to move this discussion to chat for some lengthier clarification.
– Iszi – 2014-01-07T17:50:32.7632The instructions are pretty clear, but what if the original string is the target? – Christian Palmstierna – 2014-01-08T15:41:34.200
How you work out a tie is up to you, but only one string may be chosen.
seems we can choose any lower score when a tie appear – l4m2 – 2018-06-01T09:57:18.030