AI-Genetic

 view release on metacpan or  search on metacpan

Genetic.pm  view on Meta::CPAN

              [qw/blue small fat/],
             );

this adds 5 new individuals, 2 with the specified genetic coding, and 3 randomly
generated.

=item I<$ga>-E<gt>B<evolve>(I<strategy>, ?I<num_generations>?)

This method causes the GA to evolve the population using the specified strategy.
A strategy name has to be specified as the first argument. The second argument
is optional and specifies the number of generations to evolve. It defaults to
1. See L</"STRATEGIES"> for more information on the default strategies.

Each generation consists of the following steps:

=over

=item o

The population is sorted according to the individuals' fitnesses.

=item o

Genetic.pm  view on Meta::CPAN

=item o

If a termination subroutine is given, it is executed and the return value is
checked. Evolution terminates if this sub returns a true value.

=back

=item I<$ga>-E<gt>B<getFittest>(?I<N>?)

This returns the I<N> fittest individuals. If not specified,
I<N> defaults to 1. As a side effect, it sorts the population by
fitness score. The actual AI::Genetic::Individual objects are returned.
You can use the C<genes()> and C<score()> methods to get the genes and the
scores of the individuals. Please check L<AI::Genetic::Individual> for details.

=item I<$ga>-E<gt>B<sortPopulation>

This method sorts the population according to fitness function. The results
are cached for speed.

=item I<$ga>-E<gt>B<sortIndividuals>(?[I<ListOfIndividuals>]?)

Genetic/Defaults.pm  view on Meta::CPAN


package AI::Genetic::Defaults;

use strict;
use AI::Genetic::OpSelection;
use AI::Genetic::OpCrossover;
use AI::Genetic::OpMutation;

1;

# this implements the default strategies.

sub rouletteSinglePoint {
  # initialize the roulette wheel
  AI::Genetic::OpSelection::initWheel($_[0]->people);

  push @_ => 'vectorSinglePoint', 'rouletteUnique';
  goto &genericStrategy;
}

sub rouletteTwoPoint {

Genetic/Individual.pm  view on Meta::CPAN

    $self->{GENES}  = $genes;
    $self->{CALCED} = 0;

  } else {          # new mode. Just call newRandom.
    goto &newRandom;
  }

  return $self;
}

# should create default methods.
# those are the only three needed.
sub newRandom   {}
sub newSpecific {}
sub genes       {}

# the following methods shouldn't be overridden.
sub fitness {
  my ($self, $sub) = @_;

  $self->{FITFUNC} = $sub if $sub;

Genetic/Individual.pm  view on Meta::CPAN

=over

=item I<$individual>                -E<gt>B<new(options)>

=item I<AI::Genetic::IndBitVector>  -E<gt>B<new(options)>

=item I<AI::Genetic::IndListVector> -E<gt>B<new(options)>

=item I<AI::Genetic::IndRangeVector>-E<gt>B<new(options)>

This is the default constructor. It can be called as an instance method or
as a class method. In both cases it returns a new individual of
the proper type.

If called as an instance method, it expects one argument
which defines the genes of the individual. All other attributes, like
fitness function, class, etc, will be copied from the calling
instance.

If called as a class method, then it calls I<newSpecific()>. See below
for details.

Genetic/OpMutation.pm  view on Meta::CPAN


This package implements a few mutation mechanisms that can be used in user-defined
strategies. The methods in this class are to be called as static class methods,
rather than instance methods, which means you must call them as such:

  AI::Genetic::OpCrossover::MethodName(arguments)

=head1 CLASS METHODS

There is really one kind of mutation operator implemented in this class, but it
implemented for the three default individuals types. Each gene of an individual
is looked at separately to decide whether it will be mutated or not. Mutation is
decided based upon the mutation rate (or probability). If a mutation is to happen,
then the value of the gene is switched to some other possible value.

For the case of I<bitvectors>, an ON gene switches to an OFF gene.

For the case of I<listvectors>, a gene's value is replaced by another one from
the possible list of values.

For the case of I<rangevectors>, a gene's value is replaced by another one from

Genetic/OpSelection.pm  view on Meta::CPAN

=over

=item B<initWheel>(I<population>)

This method initializes the roulette wheel. It expects an anonymous list of individuals,
as returned by the I<people()> method as described in L<AI::Genetic/CLASS METHODS>.
This B<must> be called only once.

=item B<roulette>(?I<N>?)

This method selects I<N> individuals. I<N> defaults to 2. Note that the same individual
can be selected multiple times.

=item B<rouletteUnique>(?I<N>?)

This method selects I<N> unique individuals. I<N> defaults to 2. Any individual
can be selected only once per call to this method.

=back

=item Tournament

Here, I<N> individuals are randomly selected, and the fittest one of
them is returned. The following method is defined:

=over

=item B<tournament>(?I<N>?)

I<N> defaults to 2. Note that only one individual is returned per call to this
method.

=back

=item Random

Here, I<N> individuals are randomly selected and returned.
The following method is defined:

=over

=item B<random>(?I<N>?)

I<N> defaults to 1.

=back

=item Fittest

Here, the fittest I<N> individuals of the whole population are returned.
The following method is defined:

=over

=item B<topN>(?I<N>?)

I<N> defaults to 1.

=back

=back

=head1 AUTHOR

Written by Ala Qumsieh I<aqumsieh@cpan.org>.

=head1 COPYRIGHTS

README  view on Meta::CPAN

                      [qw/blue small fat/],
                     );

        this adds 5 new individuals, 2 with the specified genetic coding,
        and 3 randomly generated.

    *$ga*->evolve(*strategy*, ?*num_generations*?)
        This method causes the GA to evolve the population using the
        specified strategy. A strategy name has to be specified as the first
        argument. The second argument is optional and specifies the number
        of generations to evolve. It defaults to 1. See the section on
        "STRATEGIES" for more information on the default strategies.

        Each generation consists of the following steps:

        o   The population is sorted according to the individuals'
            fitnesses.

        o   The subroutine corresponding to the named strategy is called
            with one argument, the AI::Genetic object. This subroutine is
            expected to alter the object itself.

        o   If a termination subroutine is given, it is executed and the
            return value is checked. Evolution terminates if this sub
            returns a true value.

    *$ga*->getFittest(?*N*?)
        This returns the *N* fittest individuals. If not specified, *N*
        defaults to 1. As a side effect, it sorts the population by fitness
        score. The actual AI::Genetic::Individual objects are returned. You
        can use the "genes()" and "score()" methods to get the genes and the
        scores of the individuals. Please check the AI::Genetic::Individual
        manpage for details.

    *$ga*->sortPopulation
        This method sorts the population according to fitness function. The
        results are cached for speed.

    *$ga*->sortIndividuals(?[*ListOfIndividuals*]?)



( run in 0.414 second using v1.01-cache-2.11-cpan-0a6323c29d9 )