AI-Gene-Sequence
view release on metacpan or search on metacpan
AI/Gene/Simple.pm view on Meta::CPAN
my $self = shift;
my $rt = (join('',@{$self->[0]}));
return $rt;
}
1;
__END__;
=pod
=head1 NAME
AI::Gene::Simple
=head1 SYNOPSIS
A base class for storing and mutating genetic sequences.
package Somegene;
use AI::Gene::Simple;
our @ISA = qw (AI::Gene::Simple);
sub generate_token {
my $self = shift;
my $prev = $_[0] ? $_[0] + (1-rand(1)) : rand(1)*10;
return $prev;
}
sub calculate {
my $self = shift;
my $x = $_[0];
my $rt=0;
for (0..(scalar(@{$self->[0]}) -1)) {
$rt += $self->[0][$_] * ($x ** $_);
}
return $rt;
}
sub seed {
my $self = shift;
$self->[0][$_] = rand(1) * 10 for (0..$_[0]);
return $self;
}
# ... then elsewhere
package main;
my $gene = Somegene->new;
$gene->seed(5);
print $gene->calculate(2), "\n";
$gene->mutate_minor;
print $gene->calculate(2), "\n";
$gene->mutate_major;
print $gene->calculate(2), "\n";
=head1 DESCRIPTION
This is a class which provides generic methods for the
creation and mutation of genetic sequences. Various mutations
are provided but the resulting mutations are not checked
for a correct syntax. These classes are suitable for genes
where it is only necessary to know what lies at a given
position in a gene. If you need to ensure a gene maintains
a sensible grammar, then you should use the AI::Gene::Sequence
class instead, the interfaces are the same though so you
will only need to modify your overiding classes if you need to
switch from one to the other.
A suitable use for this module might be a series of coefficients
in a polynomial expansion or notes to be played in a musical
score.
This module should not be confused with the I<bioperl> modules
which are used to analyse DNA sequences.
It is intended that the methods in this code are inherited
by other modules.
=head2 Anatomy of a gene
A gene is a linear sequence of tokens which tell some unknown
system how to behave. These methods all expect that a gene
is of the form:
[ [ 'token0', 'token1', ... ], .. other elements ignored ]
=head2 Using the module
To use the genetic sequences, you must write your own
implementations of the following methods along with some
way of turning your encoded sequence into something useful.
=over 4
=item generate_token
=back
You may also want to override the following methods:
=over 4
=item new
=item clone
=item render_gene
=back
The calling conventions for these methods are outlined below.
=head2 Mutation methods
Mutation methods are all named C<mutate_*>. In general, the
first argument will be the number of mutations required, followed
by the positions in the genes which should be affected, followed
by the lengths of sequences within the gene which should be affected.
If positions are not defined, then random ones are chosen. If
lengths are not defined, a length of 1 is assumed (ie. working on
AI/Gene/Simple.pm view on Meta::CPAN
into one of the same type (as decided by the object's C<generate_token>
method).
=item C<mutate_major([num, pos])>
This changes a single token into a token of any token type.
Token at postition I<pos>. The token is produced by the object's
C<generate_token> method.
=item C<mutate_switch([num, pos1, pos2, len1, len2])>
This takes two sequences within the gene and swaps them
into each other's position. The first starts at I<pos1>
with length I<len1> and the second at I<pos2> with length
I<len2>. If the two sequences overlap, then no mutation will
be attempted.
=back
The following methods are also provided, but you will probably
want to overide them for your own genetic sequences.
=over 4
=item C<generate_token([current token])>
This is used by the mutation methods when changing tokens or
creating new ones. It is expected to return a single token.
If a minor mutation is being attempted, then the method will
also be passed the current token.
The provided version of this method returns a random character
from 'a'..'z' as both the token type and token.
=item C<clone()>
This returns a copy of the gene as a new object. If you are using
nested genes, or other references as your tokens, then you may need
to produce your own version which will deep copy your structure.
=item C<new>
This returns an empty gene, into which you can put things. If you
want to initialise your gene, or anything useful like that, then
you will need another one of these.
=item C<render_gene>
This is useful for debugging, returns a serialised summary of the
gene.
=back
=head1 AUTHOR
This module was written by Alex Gough (F<alex@rcon.org>).
=head1 SEE ALSO
If you are encoding something which must maintain a correct
syntax (executable code, regular expressions, formal poems)
then you might be better off using AI::Gene::Sequence .
=head1 COPYRIGHT
Copyright (c) 2000 Alex Gough <F<alex@rcon.org>>. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 BUGS
Some methods will do odd things if you pass them weird values,
so try not to do that. So long as you stick to passing
positive integers or C<undef> to the methods then they should
recover gracefully.
While it is easy and fun to write genetic and evolutionary
algorithms in perl, for most purposes, it will be much slower
than if they were implemented in another more suitable language.
There are some problems which do lend themselves to an approach
in perl and these are the ones where the time between mutations
will be large, for instance, when composing music where the
selection process is driven by human whims.
=cut
( run in 0.869 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )