|
/home/cpanrun/depot/main/contrib-patched/perl/CPAN/src/AI-Gene-Sequence/blib/lib/AI/Gene/AI/Gene/Sequence.pm |
AI::Gene::Sequence
A base class for storing and mutating genetic sequences.
package Somegene; use AI::Gene::Sequence; our @ISA = qw(AI::Gene::Sequence);
my %things = ( a => [qw(a1 a2 a3 a4 a5)],
b => [qw(b1 b2 b3 b4 b5)],);
sub generate_token {
my $self = shift;
my ($type, $prev) = @_;
if ($type) {
$prev = ${ $things{$type} }[rand @{ $things{$type} }];
}
else {
$type = ('a','b')[rand 2];
$prev = ${$things{$type}}[rand @{$things{$type}}];
}
return ($type, $prev);
}
sub valid_gene {
my $self = shift;
return 0 if $_[0] =~ /(.)\1/;
return 1;
}
sub seed {
my $self = shift;
$self->[0] = 'ababab';
@{$self->[1]} = qw(A1 B1 A2 B2 A3 B3);
}
sub render {
my $self = shift;
return join(' ', @{$self->[1]});
}
# elsewhere package main;
my $gene = Somegene->new; $gene->seed; print $gene->render, "\n"; $gene->mutate(5); print $gene->render, "\n"; $gene->mutate(5); print $gene->render, "\n";
This is a class which provides generic methods for the creation and mutation of genetic sequences. Various mutations are provided as is a way to ensure that genes created by mutations remain useful (for instance, if a gene gives rise to code, it can be tested for correct syntax).
If you do not need to keep check on what sort of thing is currently occupying a slot in the gene, you would be better off using the AI::Gene::Simple class instead as this will be somewhat faster. The interface to the mutations is the same though, so if you need to change in future, then it will not be too painful.
This module should not be confused with the bioperl modules which are used to analyse DNA sequences.
It is intended that the methods in this code are inherited by other modules.
A gene is a sequence of tokens, each a member of some group of simillar tokens (they can of course all be members of a single group). This module encodes genes as a string representing token types, and an array containing the tokens themselves, this allows for arbitary data to be stored as a token in a gene.
For instance, a regular expression could be encoded as:
$self = ['ccartm',['a', 'b', '|', '[A-Z]', '\W', '*?'] ]
Using a string to indicate the sort of thing held at the corresponding part of the gene allows for a simple test of the validity of a proposed gene by using a regular expression.
To use the genetic sequences, you must write your own implementations of the following methods:
You may also want to override the following methods:
Mutation methods are all named 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
single tokens only), if a length of 0 is requested, then a random
length is chosen.
Also, if a mutation is suggested but would result in an invalid sequence, then the mutation will not be carried out. If a mutation is attempted which could corrupt your gene (copying from a region beyond the end of the gene for instance) then it will be silently skipped. Mutation methods all return the number of mutations carried out (not the number of tokens affected).
These methods all expect to be passed positive integers, undef or zero, other values could (and likely will) do something unpredictable.
mutate([num, ref to hash of probs & methods])This hash should contain keys which fit $1 in mutate_(.*)
and values indicating the weight to be given to that method.
The module will normalise this nicely, so you do not have to.
This lets you define your own mutation methods in addition to
overriding any you do not like in the module.
mutate_insert([num, pos])generate_token method.
mutate_overwrite([num, pos1, pos2, len])mutate_reverse([num, pos, len])mutate_shuffle([num, pos1, pos2, len])mutate_duplicate([num, pos1, pos2, length])mutate_remove([num, pos, length]))mutate_minor([num, pos])generate_token
method).
mutate_major([num, pos])generate_token method.
mutate_switch([num, pos1, pos2, len1, len2])The following methods are also provided, but you will probably want to overide them for your own genetic sequences.
generate_token([token type, current token])The provided version of this method returns a random character from 'a'..'z' as both the token type and token.
valid_gene(string [, posn])The provided version of this method always returns true.
clone()newrender_gene
This module was written by Alex Gough (alex@rcon.org).
For an illustration of the use of this module, see Regexgene.pm, Musicgene.pm, spamscan.pl and music.pl from the gziped distribution.
Copyright (c) 2000 Alex Gough <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.
This is very slow if you do not need to check that your mutations create valid genes, but fast if you do, thems the breaks. There is a AI::Gene::Simple class instead if this bothers you.
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 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.
|
/home/cpanrun/depot/main/contrib-patched/perl/CPAN/src/AI-Gene-Sequence/blib/lib/AI/Gene/AI/Gene/Sequence.pm |