AI-Gene-Sequence
view release on metacpan or search on metacpan
AI/Gene/Sequence.pm view on Meta::CPAN
else {
splice(@{$self->[1]}, $pos2, 0,
splice(@{$self->[1]}, $pos1, $len) );
}
$rt++;
}
return $rt;
}
# These are intended to be overriden, simple versions are
# provided for the sake of testing.
# Generates things to make up genes
# can be called with a token type to produce, or with none.
# if called with a token type, it will also be passed the original
# token as the second argument.
# should return a two element list of the token type followed by the token itself.
sub generate_token {
my $self = shift;
my $token_type = $_[0];
AI/Gene/Sequence.pm view on Meta::CPAN
# this will let you have a look, if it starts being odd.
sub render_gene {
my $self = shift;
my $return = "$self\n";
$return .= $self->[0] . "\n";
$return .= (join ',', @{$self->[1]}). "\n";
return $return;
}
# used for testing
sub _test_dump {
my $self = shift;
my @rt = ($self->[0], join('',@{$self->[1]}));
return @rt;
}
1;
__END__;
=pod
AI/Gene/Sequence.pm view on Meta::CPAN
print $gene->render, "\n";
$gene->mutate(5);
print $gene->render, "\n";
=head1 DESCRIPTION
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 I<bioperl> modules
which are used to analyse DNA sequences.
AI/Gene/Sequence.pm view on Meta::CPAN
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.
=head2 Using the module
To use the genetic sequences, you must write your own
implementations of the following methods:
=over 4
AI/Gene/Simple.pm view on Meta::CPAN
else {
splice(@{$self->[0]}, $pos2, 0,
splice(@{$self->[0]}, $pos1, $len) );
}
$rt++;
}
return $rt;
}
# These are intended to be overriden, simple versions are
# provided for the sake of testing.
# Generates things to make up genes
# can be called with a token type to produce, or with none.
# if called with a token type, it will also be passed the original
# token as the second argument.
# should return a two element list of the token type followed by the token itself.
sub generate_token {
my $self = shift;
my $token_type = $_[0];
AI/Gene/Simple.pm view on Meta::CPAN
# You need some way to use the gene you've made and mutated, but
# this will let you have a look, if it starts being odd.
sub render_gene {
my $self = shift;
my $return = "$self\n";
$return .= (join ',', @{$self->[0]}). "\n";
return $return;
}
# used for testing
sub _test_dump {
my $self = shift;
my $rt = (join('',@{$self->[0]}));
return $rt;
}
1;
__END__;
=pod
0.13 Sat Dec 30 23:00:00 2000
Added: mutate_reverse method added to both Sequence and Simple
BUGFIXES: modified makefile to ensure sensible version of perl
removed eval in mutate method and _normalise
0.12 Fri Dec 29 19:00:00 2000
Added: Genetics::Gene::Simple package added, with tests (tsimp.t)
BUGFIXES: require 5.6.0 lines.
documentation made clearer.
0.11 Thu Dec 28 21:00:00 2000
Added: Extensive test suite (tgene.t)
mutate_overwrite added
BUGFIXES: Most methods have more gene length related sanity checking.
So long as positive integers are used as args, then there should
be no fatal errors through missing the end of substrings.
mutate when called with ref of probs only worked with keys
of generic probs hash, this is now fixed.
0.10 Wed Dec 27 21:00:00 2000
Makefile.PL
MANIFEST
AI/Gene/Sequence.pm
AI/Gene/Simple.pm
demo/Regexgene.pm
demo/Musicgene.pm
demo/spamscan.pl
demo/music.pl
t/tgene.t
t/tsimp.t
test.pl
META.yml Module meta-data (added by MakeMaker)
demo/music.pl view on Meta::CPAN
use strict;
use warnings;
use Musicgene;
# make something to start from
my @seeds;
for (0..9) {
$seeds[$_] = Musicgene->new(20);
print "$_ : ", ($seeds[$_]->_test_dump)[0], "\n";
$seeds[$_]->write_file('music'.$_.'.mid');
}
print "Enter number to retain (0-9):";
while (<>) {
chomp;
last if /\D/;
$seeds[0] = $seeds[$_];
$seeds[0]->write_file('music0.mid');
print "\n0: ", ($seeds[0]->_test_dump)[0], "\n";
for (1..9) {
$seeds[$_] = $seeds[0]->clone; # make some children
$seeds[$_]->mutate(5); # modify them a bit
$seeds[$_]->write_file('music'.$_.'.mid');
print "$_: ", ($seeds[$_]->_test_dump)[0], "\n";
}
print "Enter number to retain (0-9):";
}
use strict;
use warnings;
# Gtest is a small package used to test the AI::Gene::Sequence
# package. It provides a generate_token method and a seed_gene
# method, the first is highly deterministic (so tests of a module
# which hinge on randomness can work) and the second sets up a gene
# ready for a test.
# Also is a new method, which creates the gene and seeds it and
# 'd' and 'g' methods, which return (stringified) versions of the
# sequence ($self->[0]) and gene (@{$self->[1]}) respectively.
package GTest;
our (@ISA);
use AI::Gene::Sequence;
@ISA = qw(AI::Gene::Sequence);
return $self->[0];
}
sub g {
my $self = shift;
return join('', @{$self->[1]});
}
package main;
use Test;
# see above for a small package ( GTest ) used to test G::G::S
BEGIN {plan tests => 111, todo =>[]}
my $hammer = 30; # set big to bash at methods with randomness
{ # test1
# first of all, does our testing package behave
my $gene = GTest->new;
die "$0: Broken render" unless $gene->d eq 'abcdefghij'
and $gene->g eq 'abcdefghij';
die "$0: Broken generate" unless ($gene->generate_token('a'))[1] eq 'A'
and ($gene->generate_token())[0] eq 'n';
ok(1);
}
my $main = GTest->new;
{ print "# clone\n";
my $gene = $main->clone;
use strict;
use warnings;
# GtestS is a small package used to test the AI::Gene::Simple
# package. It provides a generate_token method and a seed_gene
# method, the first is highly deterministic (so tests of a module
# which hinge on randomness can work) and the second sets up a gene
# ready for a test.
# Also is a new method, which creates the gene and seeds it and
# a 'd' method, which returns a stringified version of $self->[0]
package GTestS;
our (@ISA);
use AI::Gene::Simple;
@ISA = qw(AI::Gene::Simple);
sub new {
return $prev;
}
sub d {
my $self = shift;
return join('',@{$self->[0]});
}
package main;
use Test;
# see above for a small package ( GTest ) used to test G::G::S
BEGIN {plan tests => 101, todo =>[]}
my $hammer = 30; # set big to bash at methods with randomness
{ # test1
# first of all, does our testing package behave
my $gene = GTestS->new;
die "$0: Broken render" unless $gene->d eq 'abcdefghij';
die "$0: Broken generate" unless $gene->generate_token('a') eq 'A'
and $gene->generate_token eq 'N';
ok(1);
}
my $main = GTestS->new;
{ print "# clone\n";
my $gene = $main->clone;
ok($gene->d, $main->d);
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)
BEGIN { $| = 1; print "1..1\n"; }
END {print "not ok 1\n" unless $loaded;}
use AI::Gene::Sequence;
use AI::Gene::Simple;
$loaded = 1;
print "ok 1\n";
######################### End of black magic.
# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):
( run in 0.292 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )