Algorithm-Genetic-Diploid

 view release on metacpan or  search on metacpan

lib/Algorithm/Genetic/Diploid/Individual.pm  view on Meta::CPAN

package Algorithm::Genetic::Diploid::Individual;
use strict;
use List::Util qw'sum shuffle';
use Algorithm::Genetic::Diploid::Base;
use base 'Algorithm::Genetic::Diploid::Base';

my $log = __PACKAGE__->logger;

=head1 NAME

Algorithm::Genetic::Diploid::Individual - an individual that reproduces sexually

=head1 METHODS

=over

=item new

Constructor takes named arguments, sets a default, empty list of chromosomes and
a default child count of zero

=cut

sub new {
	shift->SUPER::new(
		'chromosomes' => [],
		'child_count' => 0,
		@_,
	);
}

=item child_count

Getter for the number of children

=cut

sub child_count {
	shift->{'child_count'};
}

# private method to increment 
# child count after breeding
sub _increment_cc { shift->{'child_count'}++ }

=item chromosomes

Getter and setter for the list of chromosomes

=cut

sub chromosomes {
	my $self = shift;
	if ( @_ ) {
		$log->debug("assigning new chromosomes: @_");
		$self->{'chromosomes'} = \@_;
	}
	return @{ $self->{'chromosomes'} }
}

=item meiosis

Meiosis produces a gamete, i.e. n chromosomes that have mutated and recombined

=cut

sub meiosis {
	my $self = shift;
	
	# this is basically mitosis: cloning of chromosomes
	my @chro = map { $_->clone } $self->chromosomes;
	$log->debug("have cloned ".scalar(@chro)." chromosomes (meiosis II)");
	
	# create pairs of homologous chromosomes, i.e. metafase
	my @pairs;
	for my $i ( 0 .. $#chro - 1 ) {
		for my $j ( ( $i + 1 ) .. $#chro ) {
			if ( $chro[$i]->number == $chro[$j]->number ) {
				push @pairs, [ $chro[$i], $chro[$j] ];
			}	
		}
	}
	
	# recombination happens during metafase
	for my $pair ( @pairs ) {



( run in 1.137 second using v1.01-cache-2.11-cpan-39bf76dae61 )