Algorithm-Evolve
view release on metacpan or search on metacpan
lib/Algorithm/Evolve/Util.pm view on Meta::CPAN
C<aaaaaa> and C<bbbbbb> would produce the two genes C<abbaaa> and C<baabbb>
(the two "cuts" here were after the 1st and 3rd positions).
=item C<str_agreement( $string1, $string2 )>
=item C<arr_agreement( \@array1, \@array2 )>
Returns the number of positions in which the two genes agree. Does not enforce
that they have the same size, even though the result is somewhat meaningless
in that case.
String gene comparison is done in a non-unicode-friendly way. To override this
and use a (slower) unicode-friendly string comparison, set
C<$Algorithm::Evolve::Util::UNICODE_STRINGS> to a true value.
In array genes, the comparison of individual elements is done with C<eq>.
Note that this is the Hamming metric, and not the edit distance metric. Edit
distance may be an interesting fitness to use as well. There are at least two
modules (L<Text::Levenshtein|Text::Levenshtein> and
L<Text::LevenshteinXS|Text::LevenshteinXS>) that I know of which calculate the
edit distance of two strings.
=item C<str_mutate( $string1 [, $num [, \@alphabet ]] )>
=item C<arr_mutate( \@array1 [, $num [, \@alphabet ]] )>
Returns a random mutation of the gene according to the given alphabet
(defaulting to {0,1}). If C<$num> is less than 1, it performs I<probabilistic
mutation>, with each position having a C<$num> probability of being mutated. If
C<$num> is greater than or equal to 1, it performs I<N-point mutation>: exactly
C<$num> positions are chosen at random and mutated. C<$num> defaults to 1. A
convenient rule of thumb is start with a mutation rate of 1/gene_length.
A mutation will always change the character in question: an 'a' will never be
chosen to replace an existing 'a' in a mutation. The following identity holds
for N-point mutations:
str_agreement( str_mutate($some_string, $n, \@alph), $some_string )
== length($some_string) - $n;
The alphabet for a string gene should consist of only single characters unless
you know what you're doing. Conceivably, you can implement an 'add' and 'remove'
mutation by using an alphabet that contains strings with length != 1. But this
seems a little hackish to me. For array genes, the alphabet can be just about
anything meaningful to you.
=item C<str_random( $size [, \@alphabet ] )>
=item C<arr_random( $size [, \@alphabet ] )>
Returns a random gene of the given size over the given alphabet, defaulting to
{0,1}.
=back
=head1 SEE ALSO
L<Algorithm::Evolve|Algorithm::Evolve>
F<StringEvolver.pm> in the F<examples/> directory uses the utilities in
Algorithm::Evolve::Util to implement a completely generic simple string
evolver critter class in very few lines of code.
=head1 AUTHOR
Algorithm::Evolve is written by Mike Rosulek E<lt>mike@mikero.comE<gt>. Feel
free to contact me with comments, questions, patches, or whatever.
=head1 COPYRIGHT
Copyright (c) 2003 Mike Rosulek. All rights reserved. This module is free
software; you can redistribute it and/or modify it under the same terms as Perl
itself.
( run in 0.540 second using v1.01-cache-2.11-cpan-df04353d9ac )