Hi, I took an interest in genetic algorithm after reading this article:
The code listing is here:
To gain a better understanding, I tried the convert the code to Ruby, and on the way I should be able to understand how it works.
Unfortunately my lack of knowledge in genetic and Perl means I couldn't quite get the code working. When it runs, each generation doesn't seem to improve. It would be great if someone could take a look, compare it to the original Perl version and see where I've gone wrong, and the fix require to get it working. The code's attached to this post.
Robo
p.s. the link was part one of the series, part two and three are here:
Unfortunately my lack of knowledge in genetic and Perl means I couldn't
quite get the code working. When it runs, each generation doesn't seem
to improve. It would be great if someone could take a look, compare it
to the original Perl version and see where I've gone wrong, and the fix
require to get it working. The code's attached to this post.
First the P version seems wrong. Perhaps best if you find a better
algorithm.
At least there are some basic errors
my @sorted_population = sort { $a->{fitness} $b->{fitness} } @$pop_ref;
<=>
missing
my $pop_size = scalar @$population; # population size
# create the weights array: select only survivors from the population,
# then use map to have only the fitness come through
my @weights = map { $_->{fitness} } grep { $_->{survived} } @$population;
# if we have less than 2 survivors, we're in trouble
die "Population size $pop_size is too small" if $pop_size < 2;
this algorithm seems wrong : be carefull with it
$count += $weights->[$i];
$sample = $i if rand $count [$i];
To gain a better understanding, I tried the convert the code to Ruby,
and on the way I should be able to understand how it works.
Unfortunately my lack of knowledge in genetic and Perl means I couldn't
quite get the code working. When it runs, each generation doesn't seem
to improve. It would be great if someone could take a look, compare it
to the original Perl version and see where I've gone wrong, and the fix
require to get it working. The code's attached to this post.
That's a lot of code.
If you're interested I've got some Ruby code I wrote for a GA class I took
a couple of years ago. At least I know it works (as in there is
improvement from generation to generation). email me if you're interested
and I'll try to dig it up.
Unfortunately I misread this as "my lack of knowledge is genetic", but
I am running on a caffeine shortage
I don't have time to work with your code right now, but generally
speaking throwing some classes and object methods in there would
improve the readability quite a bit -- I do understand you were trying
a more-or-less direct port.
I've frequently looked at Perl code on developerworks and thought it
could be improved significantly with better modularity. Developer
works does not do a good job of technical editing, IMHO, so they turn
into a bit of a "submit article, get $$$" farm. You might want to
search perlmonks.org for better Perl examples, or you could look at
some of the genetic algorithm stuff already on CPAN.
···
On Wed, 15 Dec 2004 00:27:26 +0900, Phil Tomson <ptkwt@aracnet.com> wrote:
In article <YsCvd.14841$3U4.403540@news02.tsnz.net>,
Robo <robo@mars.com> wrote:
>-=-=-=-=-=-
>
>Hi, I took an interest in genetic algorithm after reading this article:
>
>IBM Developer
>
>The code listing is here:
>
>IBM Developer
>
>To gain a better understanding, I tried the convert the code to Ruby,
>and on the way I should be able to understand how it works.
>
>Unfortunately my lack of knowledge in genetic and Perl means I couldn't
>quite get the code working. When it runs, each generation doesn't seem
>to improve. It would be great if someone could take a look, compare it
>to the original Perl version and see where I've gone wrong, and the fix
>require to get it working. The code's attached to this post.
>
>
That's a lot of code.
If you're interested I've got some Ruby code I wrote for a GA class I took
a couple of years ago. At least I know it works (as in there is
improvement from generation to generation). email me if you're interested
and I'll try to dig it up.