Implementing Genetic algorithm in Ruby

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:

numbers.rb (3.72 KB)

numbers.pl (5.74 KB)

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];

                     rand($count) < $weights->[$i];

}

    unless (defined $sample) {
       $sample = $weights->[0];
    }

return $sample;
}

For the ruby version : you have at least a problem with fitness

def fitness(dna)
  dna / 256

        dna / 256.0 # otherwise ruby can use integer /

end

#sample an array of weighted elements
def sample(weights)
  count = sample = 0

  weights.each_index do |i|
    count += weights[i]
    sample = i if rand(count[i]) != 0

                              rand(count) < weights[i]

  end

Guy Decoux

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. :slight_smile:

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.

Phil

Here's a little bit of code that I hacked up overnight for a string based GA.

It lacks quite a lot, like a crossover method for example, but it go quite some way to getting the basic structure up and running.

I will continue to hack away at it over the week.

ga.rb (2.73 KB)

"Unfortunately my lack of knowledge in genetic"

Unfortunately I misread this as "my lack of knowledge is genetic", but
I am running on a caffeine shortage :slight_smile:

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. :slight_smile:

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.

Phil