AI-Gene-Sequence
view release on metacpan or search on metacpan
AI/Gene/Sequence.pm view on Meta::CPAN
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 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.
=head2 Using the module
To use the genetic sequences, you must write your own
implementations of the following methods:
=over 4
=item generate_token
AI/Gene/Sequence.pm view on Meta::CPAN
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
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.
=over 4
AI/Gene/Sequence.pm view on Meta::CPAN
This module was written by Alex Gough (F<alex@rcon.org>).
=head1 SEE ALSO
For an illustration of the use of this module, see Regexgene.pm,
Musicgene.pm, spamscan.pl and music.pl from the gziped distribution.
=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
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,
AI/Gene/Simple.pm view on Meta::CPAN
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
AI/Gene/Simple.pm view on Meta::CPAN
=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.
--- #YAML:1.0
name: AI-Gene-Sequence
version: 0.22
abstract: ~
author: []
license: unknown
distribution_type: module
configure_requires:
ExtUtils::MakeMaker: 0
build_requires:
ExtUtils::MakeMaker: 0
requires: {}
no_index:
directory:
- t
- inc
generated_by: ExtUtils::MakeMaker version 6.55_02
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
demo/Musicgene.pm view on Meta::CPAN
$VERSION = 0.01;
@ISA = qw(Exporter AI::Gene::Sequence);
@EXPORT = ();
%EXPORT_TAGS = ();
@EXPORT_OK = qw();
}
our @EXPORT_OK;
our @chords = ([qw(A C G)], [qw(A C E)]); # type c
our @octaves = (3..10); # type o
our @notes = ('A'..'G', 'rest'); # type n
our @lengths = (qw(hn qn), ''); # type l
sub new {
my $class = shift;
my $self = ['',[]];
bless $self, ref($class) || $class;
$self->mutate_insert($_[0]) if $_[0];
return $self;
}
demo/Regexgene.pm view on Meta::CPAN
our @modifiers = qw( * *? + +? ?? );
our @char_types = qw( \w \W \d \D \s \S .);
our @ranges = qw( [A-Z] [a-z] [0-9] );
our @chars = ((0..9,'a'..'z','A'..'Z','_'),
(map '\\'.chr, 32..47, 58..64, 91..94, 96, 123..126));
=head2 Constructor
As we want to be able to fill our regular expression at the same
time as we create it and because we will want to nest sequences
we will need some way to know how deep we are, then a different
B<new> method is needed.
If called as an object method (C<$obj->new>), this decreases the depth
count by one from the invoking object. It also adds tokens to the regular
expression and uses the B<valid_gene> method to ensure we stay sane
from the start.
As can be seen, we use array offsets above $self->[1] to store information
which is specific to our implementation.
=cut
sub new {
my $gene = ['',[], ref($_[0]) ? $_[0]->[2]-1 : 3 ]; # limit recursion
bless $gene, ref($_[0]) || $_[0];
demo/Regexgene.pm view on Meta::CPAN
}
else {
$rt[1] = $chars[rand@chars];
}
;last SWITCH}; # grouping
die "Unknown type of regex token ($type)";
}
return @rt[0,1];
}
# returns true if a valid regex, otherwise false, ignores optional posn arg
=head2 valid_gene
Because we have restricted ourselves to simple regular
expressions we only need to make sure that modifers and alternation
do not follow or precede things they should not.
Note that we do not use the current version of the gene in $self->[0].
This is because it is the un-mutated version, if we do not accept the
mutation then $self will be left alone by our calling methods.
That said, if you want to use $self->[0] then you can, but it would
be unwise to modify it here.
=cut
demo/Regexgene.pm view on Meta::CPAN
return undef;
}
else {
return 1;
}
}
=head2
Having created a way to create, modify and verify our genetically
encoded regular expressions, we could do with some way to actually
use them. This method retuns a non compiled regular expression and
calls itself recursively when it finds nested genes.
=cut
sub regex {
my $self = shift;
my $rt;
warn "$0: empty gene turned into empty regex" unless scalar @{$self->[1]};
foreach (@{$self->[1]}) {
$rt .= ref($_) ? '(?:'. $_->regex .')' : $_;
}
return $rt;
}
=head1 AUTHOR
Alex Gough (F<alex@rcon.org>).
=head1 COPYRIGHT
Copyright (c) 2001 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.
=cut
1;
__END__;
demo/spamscan.pl view on Meta::CPAN
#!/usr/bin/perl -w
# spamscan.pl by Alex Gough, 2001, (alex@rcon.org)
# This is a quick illustration of the Regexgene pseudo- module which
# is itself an illustration of the AI::Gene::Sequence module.
#
# It will run for ever, printing out dots or regular expressions
# which are quite good at spotting spam.
use strict;
use warnings;
use Regexgene;
my $num_mutates = 3;
# read in our passes and failures.
my (@wanted, @spam);
while (<DATA>) {
if (1../^$/) { push @wanted, $_;}
else { push @spam, $_}
}
print "Best score possible is: ", scalar(@spam), "\n";
my $regex = seed_match(); # start off with something quite good
my $best_yet = 0;
my $temp = 1;
while (1) {
my $child = $regex->clone; # copy the parent
$child->mutate($num_mutates); # change it slightly
my $rex = $child->regex;
$rex = qr/$rex/;
my $score = 0; # see if the kid is better
$score += grep {$_ =~ $rex} @spam; # we don't want spam
$score -= grep {$_ =~ $rex} @wanted; # but we do want our mail
if ($score > $best_yet) {
$regex = $child; # and so progress is made
$best_yet = $score;
print "\n* $best_yet ", $regex->regex, "\n";
}
print '.' unless ($temp++ % 80);
}
sub seed_match {
my $regex;
TWIDDLE: while (1) {
$regex = Regexgene->new(5);
demo/spamscan.pl view on Meta::CPAN
Groovy.
You know you aren't working when:
The saga continues...
At last, the cycle is complete
Phone works again
Warm, glowy feeling.
Mmmmm Free Time
Hmmm.
broken
Pedantry and the feminine perspective since 1840...
E-mail addresses
The domain
Disarming Baptists
Stats for, er, a while...
End-of-year party...
Nasty
The Joy of Work
Sild in tomato sauce
Someone I admire and deeply love
I am Mike, he is Bob
Windows 95 CD
demo/spamscan.pl view on Meta::CPAN
Re: Your Business
Re: Your Business
Lenders COMPETE for your MORTAGE Loan!!! -rnqyxoyj
A LITTLE MONEY CAN GO A LONG WAYS
Detective software
Click Here and get a Brand New Free Satellite ...
Eliminate BAD c r e d i t!
FWD:FWD:Target your market with search engine traffic for $0.25 -hoxrgck
** It is fun, it is legal and it works***
|||- - Professional Direct Email Marketers Club - -||| kljh
"A dream come true offering major bank credit cards at 5.9% interest!!
..//..pres./ We have foreigners who want to buy or finance your business/speak to them right now...
Make Money For The Holidays Now!! 30231
Make Money While You Sleep!!!!
Make Money While You Sleep!!!!
Untold Real Estate Info.
Pirate SOFTWARE
You Decide: Is Age-Reversal Possible? 29348
Open Letter Matthias Rath
90 % of the people in your city and state need this service,,become a credit card....
Get Out of Line... 23048
=?big5?B?d29ya6FJoUk=?=
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);
sub new {
my $class = shift;
my $self = ['',[]];
bless $self, $class;
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 1.145 second using v1.01-cache-2.11-cpan-49f99fa48dc )