Created
January 20, 2014 19:55
-
-
Save grondilu/8527805 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constant target = "Hello world!"; | |
constant mutate_chance = .08; | |
constant alphabet = map &chr, ^128; # ASCII characters | |
constant C = 100; | |
sub mutate { | |
$^code.comb.map( | |
{ (rand < mutate_chance ?? alphabet.pick !! $_) x (1 xx 10, 0, 2).pick } | |
).join | |
} | |
sub fitness($code) { | |
my $string = .Str given try { | |
EVAL $code; | |
CATCH { return 0 } | |
} | |
( [+] $string.comb Zeq state @ = target.comb ) | |
/ [+] 1, grep * > 1, $code.comb.BagHash.values; | |
} | |
loop ( | |
my $parent = "say 'Hello world!'"; | |
$parent ne target; | |
$parent = max :by(&fitness), mutate($parent) xx C | |
) { printf "%6d: '%s'\n", (state $)++, $parent } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment