18 lines
447 B
Plaintext
18 lines
447 B
Plaintext
target = "METHINKS IT IS LIKE A WEASEL";
|
|
alphabet = Append[CharacterRange["A", "Z"], " "];
|
|
fitness = HammingDistance[target, #] &;
|
|
mutate[str_String, rate_ : 0.01] := StringReplace[
|
|
str,
|
|
_ /; RandomReal[] < rate :> RandomChoice[alphabet]
|
|
]
|
|
|
|
mutationRate = 0.02; c = 100;
|
|
NestWhileList[
|
|
First@MinimalBy[
|
|
Thread[mutate[ConstantArray[#, c], mutationRate]],
|
|
fitness
|
|
] &,
|
|
mutate[target, 1],
|
|
fitness@# > 0 &
|
|
] // ListAnimate
|