AI-Genetic

 view release on metacpan or  search on metacpan

Genetic.pm  view on Meta::CPAN

              [qw/red big thin/],
              [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.

Genetic/Individual.pm  view on Meta::CPAN


=item o bitvector

One argument is expected which is an anonymous list of genes:

  AI::Genetic::IndBitVector->new([0, 1, 1, 0, 1, 0]);

=item o listvector

Two arguments are expected. The first is an anonymous list of
genes, the second is an anonymous list of lists of possible gene values,
similar to the argument of I<newRandom>.

  AI::Genetic::IndListVector->new(
    [qw/red medium fat/],   # genes
    [  # possible values
     [qw/red blue green/],
     [qw/big medium small/],
     [qw/very_fat fat fit thin very_thin/],
    ]);

=item o rangevector

Two arguments are expected. The first is an anonymous list of
genes, the second is an anonymous list of lists of possible gene values,
similar to the argument of I<newRandom>.

  AI::Genetic::IndListVector->new(
    [3, 14, 4],   # genes
    [   # possible values
     [1, 5],
     [0, 20],
     [4, 9],
    ]);

Genetic/OpCrossover.pm  view on Meta::CPAN

# both parents have to be of same length.

sub vectorTwoPoint {
  my ($prob, $mom, $dad) = @_;

  return 0 if rand > $prob;

  # get first index from 1 to $#{$dad}-1
  my $ind1 = 1 + int rand($#{$dad} - 1);

  # get second index from $ind1 to $#{$dad}
  my $ind2 = $ind1 + 1 + int rand($#{$dad} - $ind1);
  my @c1 = (@$mom[0 .. $ind1 - 1],
	    @$dad[$ind1 .. $ind2 - 1],
	    @$mom[$ind2 .. $#{$dad}]);

  my @c2 = (@$dad[0 .. $ind1 - 1],
	    @$mom[$ind1 .. $ind2 - 1],
	    @$dad[$ind2 .. $#{$dad}]);

  return (\@c1, \@c2);

Genetic/OpCrossover.pm  view on Meta::CPAN


In single point crossover, a point is selected along the choromosomes of both parents.
The chromosomes are then split at that point, and the head of one parent chromosome is
joined with the tail of the other and vice versa, creating two child chromosomes.
The following method is defined:

=over

=item B<vectorSinglePoint>(I<Xprob, parent1, parent2>)

The first argument is the crossover rate. The second and third arguments are anonymous
lists that define the B<genes>
of the parents (not AI::Genetic::Individual objects, but the return value of the I<genes()>
method in scalar context).
If mating occurs, two anonymous lists of genes are returned corresponding to the two
new children. If no mating occurs, 0 is returned.

=back

=item Two Point

In two point crossover, two points are selected along the choromosomes of both parents.
The chromosomes are then cut at those points, and the middle parts are swapped,
creating two child chromosomes. The following method is defined:

=over

=item B<vectorTwoPoint>(I<Xprob, parent1, parent2>)

The first argument is the crossover rate. The second and third arguments are anonymous
lists that define the B<genes>
of the parents (not AI::Genetic::Individual objects, but the return value of the I<genes()>
method in scalar context).
If mating occurs, two anonymous lists of genes are returned corresponding to the two
new children. If no mating occurs, 0 is returned.

=back

=item Uniform

In uniform crossover, two child chromosomes are created by looking at each gene in both
parents, and randomly selecting which one to go with each child.
The following method is defined:

=over

=item B<vectorUniform>(I<Xprob, parent1, parent2>)

The first argument is the crossover rate. The second and third arguments are anonymous
lists that define the B<genes>
of the parents (not AI::Genetic::Individual objects, but the return value of the I<genes()>
method in scalar context).
If mating occurs, two anonymous lists of genes are returned corresponding to the two
new children. If no mating occurs, 0 is returned.

=back

=back

README  view on Meta::CPAN

    have a higher chance of mating with other individuals. When two
    individuals mate, they swap some of their genes, resulting in an
    individual that has properties from both of its "parents". Every now and
    then, a mutation occurs where some gene randomly changes value,
    resulting in a different individual. If all is well defined, after a few
    generations, the population should converge on a "good-enough" solution
    to the problem being tackled.

    A GA implementation runs for a discrete number of time steps called
    *generations*. What happens during each generation can vary greatly
    depending on the strategy being used (See the section on "STRATEGIES"
    for more info). Typically, a variation of the following happens at each
    generation:

    1. Selection
        Here the performance of all the individuals is evaluated based on
        the fitness function, and each is given a specific fitness value.
        The higher the value, the bigger the chance of an individual passing
        its genes on in future generations through mating (crossover).

    2. Crossover

README  view on Meta::CPAN

                Defaults to 100.

        -crossover
                This defines the crossover rate. Defaults to 0.95.

        -mutation
                This defines the mutation rate. Defaults to 0.05.

        *-fitness*
                This defines a fitness function. It expects a reference to a
                subroutine. More details are given in the section on
                "FITNESS FUNCTION".

        *-type* This defines the type of the genome. Currently, AI::Genetic
                supports only three types:

                *bitvector*
                    Individuals of this type have genes that are bits. Each
                    gene can be in one of two possible states, on or off.

                *listvector*

README  view on Meta::CPAN

                subroutine. It expects a subroutine reference. This sub will
                be called at the end of each generation with one argument:
                the AI::Genetic object. Evolution terminates if the sub
                returns a true value.

    *$ga*->createStrategy(*strategy_name*, *sub_ref*)
        This method allows the creation of a custom-made strategy to be used
        during evolution. It expects a unique strategy name, and a
        subroutine reference as arguments. The subroutine will be called
        with one argument: the AI::Genetic object. It is expected to alter
        the population at each generation. See the section on "STRATEGIES"
        for more information.

    *$ga*->init(*initArgs*)
        This method initializes the population with random individuals. It
        MUST be called before any call to *evolve()* or *inject()*. As a
        side effect, any already existing individuals in the population are
        deleted. It expects one argument, which depends on the type of
        individuals:

        o   For bitvectors, the argument is simply the length of the

README  view on Meta::CPAN

                      [qw/red big thin/],
                      [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.



( run in 0.943 second using v1.01-cache-2.11-cpan-39bf76dae61 )