in reply to So close...
I may have completely misunderstood what you are trying to do with this code, but if what I think you are doing is near enough right then you would be much better to use an array of hash than your current data structure. Consider:
use strict; use warnings; use constant popCt => 10; use constant seed => 20; use constant germrate => .5; use constant removal => 8; use constant iterations => 20; main (); sub main { my @garden; my $id = 1; # Create first generation push @garden, individual (\$id) for 1 .. popCt; for ( 1 .. iterations ) { # Process a generation births (\$id, \@garden); death (\@garden); } print "$_->{id}: $_->{chrom1}, $_->{chrom1}\n" for @garden; } sub individual { my ($id, $chrom1, $chrom2) = @_; use constant limiter => 50; $chrom1 = int rand(limiter) unless defined $chrom1; $chrom2 = int rand(limiter) unless defined $chrom2; return {chrom1 => $chrom1, chrom2 => $chrom2, id => $$id++}; } sub births { my ($id, $garden) = @_; my $birth = germrate * rand seed; my $parents = @$garden; for ( 1 .. $birth ) { my $ranmom = int rand $parents; my $mom1 = $garden->[$ranmom]{chrom1}; my $mom2 = $garden->[$ranmom]{chrom2}; my $randad = int rand $parents; my $dad1 = $garden->[$randad]{chrom1}; my $dad2 = $garden->[$randad]{chrom2}; # Creates new seedling with genes from parents plants if (int rand 2) { push @$garden, individual ($id, $dad1, $mom2); } else { push @$garden, individual ($id, $mom1, $dad2); } } } sub death { my ($garden) = @_; # Generate number of individuals to remove my $remove = int rand removal; # Avoid killing more than half the population $remove = @$garden / 2 if $remove >= @$garden / 2; splice @$garden, int rand (@$garden), 1 for 1 .. $remove; }
Prints (for one run):
1: 44, 44 26: 32, 32 40: 44, 44 46: 44, 44 50: 44, 44 55: 6, 6 58: 44, 44 61: 6, 6 66: 6, 6 68: 44, 44 69: 6, 6 70: 6, 6 71: 6, 6 72: 6, 6 73: 6, 6 75: 32, 32 79: 6, 6 80: 6, 6 81: 6, 6 83: 6, 6 84: 44, 44 85: 32, 32 86: 6, 6 88: 6, 6
Note that I tweaked the constants a little and avoided killing off the entire population which is somewhat different than my understanding of your original code, but makes for a more interesting outcome in most cases.
|
|---|