Ok. A semi-working GA implementation. This fitness function was the best I could think of, and suggestions are very welcome. Also, sorry for not DRYing the code. That's what happens when you get me excited :)

You will, again, want to redirect stderr to /dev/null :). With 50 generations, it took about 17 seconds wall-clock time to run on my machine.

Update: Hmm. One thing I notice is that things like '<text>' or q~<text>~ get generated a lot. I've subtracted points in my fitness function for that sort of ``cheating'', but as I do so, the GA just ``finds'' more ways to ``cheat''. As it is, unless I can think of some more sophisticated fitness function (one that somehow calculates actual usefulness of the code), the current one will just keep growing as I try to (clumsily) parse the perl. (And we all know that goes. It isn't "Nothing but perl and jagh can parse Perl", is it? :)

Update2: Fixed the mess that was the call to $ga->init(). Thank you, jdporter.

Update3: Tweaked the fitness function. I'm also putting the code at my site, to avoid spamming PM with trivial updates as I play. Anything significant will end up back here.

#!/usr/bin/perl -w use strict; sub fitness { my ($chars) = @_; my $s = join '', @$chars; my $f = system('perl', '-ce', $s); ## zero and an immediate return if it fails to compile if ($f == 0) { $ret = 10; } else { return 0; } ## quotes and POD is cheating. Comment char isn't included, ## so don't bother with it. $ret-=10 if (substr($s, 0, 1) eq '='); #subtract if it's a POD $ret-=10 if ($s =~ /^q(.).*?(\1)$/); #q() string? $ret-=10 if ($s =~ /^([`'"]).*(\1)$/); # quoted strings or backtick +s ## Spaces at the start and end are a lesser form of cheating $ret-=2 if ($s =~ /^ /); $ret-=2 if ($s =~ / $/); $ret++ if ($s =~ /\w \w/); # space separated words are kinda cool. if ($ret < 0) { $ret = 0 }; return $ret; } use AI::Genetic; my $ga = new AI::Genetic( -fitness => \&fitness, -type => 'listvector', ); my @chars = ( 32..34,36..64,91..126 ); $_ = chr($_) for @chars; $ga->init([ map { [@chars] } 1..20 ]); $ga->evolve('randomTwoPoint', 50); my $chars = $ga->getFittest->genes();; print "Best score = ", $ga->getFittest->score(), ".\n"; my $s = join '',@$chars; print "$s\n";

In reply to GA perl (was: Brute force perl) by jagh
in thread Brute force perl by jagh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.