AI-Genetic

 view release on metacpan or  search on metacpan

Genetic.pm  view on Meta::CPAN

# -population: set the population size
# -crossover:  set the crossover probability
# -mutation:   set the mutation probability
# -fitness:    set the fitness function
# -type:       set the genome type. See docs.
# -terminate:  set termination sub.

sub new {
  my ($class, %args) = @_;

  my $self = bless {
		    ADDSEL => {},   # user-defined selections
		    ADDCRS => {},   # user-defined crossovers
		    ADDMUT => {},   # user-defined mutations
		    ADDSTR => {},   # user-defined strategies
		   } => $class;

  $self->{FITFUNC}    = $args{-fitness}    || sub { 1 };
  $self->{CROSSRATE}  = $args{-crossover}  || 0.95;
  $self->{MUTPROB}    = $args{-mutation}   || 0.05;
  $self->{POPSIZE}    = $args{-population} || 100;

Genetic/IndBitVector.pm  view on Meta::CPAN

package AI::Genetic::IndBitVector;

use strict;
use base qw/AI::Genetic::Individual/;

1;

sub newRandom {
  my ($class, $length) = @_;

  my $self = bless {
		    GENES   => [],
		    SCORE   => 0,
		    FITFUNC => sub {},
		    CALCED  => 0,
		   } => $class;

  push @{$self->{GENES}} => rand > 0.5 ? 1 : 0
    for 1 .. $length;

  return $self;
}

sub newSpecific {
  my ($class, $genes) = @_;

  my $self = bless {
		    GENES   => $genes,
		    CALCED  => 0,
		    SCORE   => 0,
		    FITFUNC => sub {},
		   } => $class;

  return $self;
}

sub genes {

Genetic/IndListVector.pm  view on Meta::CPAN

package AI::Genetic::IndListVector;

use strict;
use base qw/AI::Genetic::Individual/;

1;

sub newRandom {
  my ($class, $lists) = @_;

  my $self = bless {
		    GENES   => [],
		    SCORE   => 0,
		    FITFUNC => sub {},
		    CALCED  => 0,
		    LISTS   => $lists,
		   } => $class;

  push @{$self->{GENES}} => $_->[rand @$_] for @$lists;

  return $self;
}

sub newSpecific {
  my ($class, $genes, $lists) = @_;

  my $self = bless {
		    GENES   => $genes,
		    CALCED  => 0,
		    SCORE   => 0,
		    FITFUNC => sub {},
		    LISTS   => $lists,
		   } => $class;

  return $self;
}

Genetic/IndRangeVector.pm  view on Meta::CPAN

package AI::Genetic::IndRangeVector;

use strict;
use base qw/AI::Genetic::Individual/;

1;

sub newRandom {
  my ($class, $ranges) = @_;

  my $self = bless {
		    GENES   => [],
		    SCORE   => 0,
		    FITFUNC => sub {},
		    CALCED  => 0,
		    RANGES  => $ranges,
		   } => $class;

  for my $r (@$ranges) {
    my $rand = $r->[0] + int rand($r->[1] - $r->[0] + 1);
    push @{$self->{GENES}} => $rand;
  }

  return $self;
}

sub newSpecific {
  my ($class, $genes, $ranges) = @_;

  my $self = bless {
		    GENES   => $genes,
		    CALCED  => 0,
		    SCORE   => 0,
		    FITFUNC => sub {},
		    RANGES  => $ranges,
		   } => $class;

  return $self;
}

Genetic/Individual.pm  view on Meta::CPAN

# all other individuals. It doesn't do anything
# interesting.

1;

sub new {  # hmm .. do I need this?
  my ($class, $genes) = @_;

  my $self;
  if (ref $class) { # clone mode
    $self = bless {} => ref $class;
    $self->{$_} = $class->{$_} for keys %$class;
    $self->{GENES}  = $genes;
    $self->{CALCED} = 0;

  } else {          # new mode. Genome is given
    goto &newSpecific;
  }

  return $self;
}

sub new_old {  # hmm .. do I need this?
  my ($class, $genes) = @_;

  my $self;
  if (ref $class) { # clone mode
    $self = bless {} => ref $class;
    $self->{$_} = $class->{$_} for keys %$class;
    $self->{GENES}  = $genes;
    $self->{CALCED} = 0;

  } else {          # new mode. Just call newRandom.
    goto &newRandom;
  }

  return $self;
}



( run in 0.919 second using v1.01-cache-2.11-cpan-de7293f3b23 )