| 1 | package POE::Component::Algorithm::Evolutionary; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use lib qw( ../../../../../Algorithm-Evolutionary/lib ../../../../../../Algorithm-Evolutionary/lib ../Algorithm-Evolutionary/lib ); #For development and perl syntax mode | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 4 | ▶ | use version; our $VERSION = qv('0.2.1'); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 4 | ▶ | use POE; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 4 | ▶ | use Algorithm::Evolutionary; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | |||||||||||||
| 15 | 0 | ▶ | sub AUTOLOAD { | ||||||||||
|
Subroutine
AUTOLOAD: not called CC=5 SCAR=34.0
| |||||||||||||
| 16 | 0 | my $self = shift; | |||||||||||
| 17 | 0 | our $AUTOLOAD; | |||||||||||
| 18 | 0 | my ($method) = ($AUTOLOAD =~ /::(\w+)$/); | |||||||||||
| 19 | 0 | ▶ | return if !$self->{'session'}; # Before creation or after destruction | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 0 | my $heap = $self->{'session'}->get_heap(); | |||||||||||
| 21 | 0 | ▶ | if ( $method =~ /^set_(\w+)/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 0 | my $instanceVar = $1; | |||||||||||
| 23 | 0 | ▶ | if (defined ($heap->{$instanceVar})) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | 0 | $heap->{$instanceVar} = shift; | |||||||||||
| 25 | } | ||||||||||||
| 26 | } else { | ||||||||||||
| 27 | 0 | my $instanceVar = lcfirst($method); | |||||||||||
| 28 | 0 | ▶ | if (defined ($heap->{$instanceVar})) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 0 | return $heap->{$instanceVar}; | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | } | ||||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | |||||||||||||
| 36 | # Module implementation here | ||||||||||||
| 37 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 38 | 0 | my $class = shift; | |||||||||||
| 39 | 0 | my %args = @_; | |||||||||||
| 40 | |||||||||||||
| 41 | 0 | my $options = {}; | |||||||||||
| 42 | 0 | for my $option ( qw( Fitness Creator Single_Step Terminator Alias ) ) { | |||||||||||
| 43 | 0 | ▶ | $options->{lc($option)} = $args{$option} || croak "$option required"; | ||||||||||
|
Condition0%0/2
$args{$option} || croak("$option required")
MC/DC0%0/1
$args{$option} || croak("$option required")
$args{$option}
Truth table0%0/2
A
A= $args{$option}
| |||||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 0 | for my $option ( qw( Replacer After_Step ) ) { | |||||||||||
| 47 | 0 | $options->{lc($option)} = $args{$option}; | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | 0 | my $self = { alias => $options->{'alias' }}; | |||||||||||
| 51 | 0 | bless $self, $class; | |||||||||||
| 52 | |||||||||||||
| 53 | my $session = POE::Session->create(inline_states => { _start => \&start, | ||||||||||||
| 54 | generation => \&generation, | ||||||||||||
| 55 | after_step => \&after_step, | ||||||||||||
| 56 | finish => \&finishing}, | ||||||||||||
| 57 | 0 | args => [$options->{'alias'}, $self, $options] | |||||||||||
| 58 | ); | ||||||||||||
| 59 | 0 | $self->{'session'} = $session; | |||||||||||
| 60 | 0 | return $self; | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 0 | ▶ | sub _start_base { | ||||||||||
|
Subroutine
_start_base: not called CC=2 SCAR=17.9
| |||||||||||||
| 64 | 0 | my ($kernel, $heap, $alias, $self, $options )= | |||||||||||
| 65 | @_[KERNEL, HEAP, ARG0, ARG1, ARG2 ]; | ||||||||||||
| 66 | 0 | $kernel->alias_set($alias); | |||||||||||
| 67 | 0 | for my $option ( keys %$options ) { | |||||||||||
| 68 | 0 | $heap->{$option} = $options->{$option}; | |||||||||||
| 69 | } | ||||||||||||
| 70 | 0 | $heap->{'self'} = $self; | |||||||||||
| 71 | 0 | my @pop; | |||||||||||
| 72 | 0 | $options->{'creator'}->apply( \@pop ); | |||||||||||
| 73 | 0 | map( $_->evaluate($options->{'fitness'}), @pop ); | |||||||||||
| 74 | 0 | $heap->{'population'} = \@pop; | |||||||||||
| 75 | |||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | # Create stuff and get ready to go | ||||||||||||
| 79 | 0 | ▶ | sub start { | ||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 80 | 0 | _start_base( @_ ); | |||||||||||
| 81 | 0 | $_[KERNEL]->yield('generation'); | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | |||||||||||||
| 85 | 0 | ▶ | sub new_population { | ||||||||||
|
Subroutine
new_population: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 86 | 0 | my ($kernel, $heap, $new_population ) = @_[KERNEL, HEAP, ARG0]; | |||||||||||
| 87 | 0 | ▶ | if ( $heap->{'replacer'} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | $heap->{'replacer'}->apply($heap->{'population'}, $new_population ); | |||||||||||
| 89 | } else { | ||||||||||||
| 90 | 0 | splice( @{$heap->{'population'}}, -@{$new_population} ); | |||||||||||
| 91 | 0 | push @{$heap->{'population'}}, @{$new_population} ; | |||||||||||
| 92 | } | ||||||||||||
| 93 | 0 | $kernel->yield('generation'); | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | sub after_step { | ||||||||||
|
Subroutine
after_step: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 97 | 0 | my ($kernel, $heap, $arg ) = @_[KERNEL, HEAP, ARG0]; | |||||||||||
| 98 | 0 | ▶ | if ( $heap->{'after_step'} ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 0 | ▶ | if ( ref $heap->{'after_step'} eq 'CODE' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | $heap->{'after_step'}->( $heap->{'population'}, $arg ); | |||||||||||
| 101 | } else { | ||||||||||||
| 102 | 0 | $heap->{'after_step'}->apply( $heap->{'population'}, $arg ); | |||||||||||
| 103 | } | ||||||||||||
| 104 | } | ||||||||||||
| 105 | 0 | $kernel->yield('generation'); | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | #Evolve population | ||||||||||||
| 109 | 0 | ▶ | sub generation { | ||||||||||
|
Subroutine
generation: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 110 | 0 | my ($kernel, $heap ) = @_[KERNEL, HEAP]; | |||||||||||
| 111 | 0 | $heap->{'single_step'}->apply( $heap->{'population'} ); | |||||||||||
| 112 | 0 | ▶ | if ( ! $heap->{'terminator'}->apply( $heap->{'population'} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 0 | $kernel->yield( 'finish' ); | |||||||||||
| 114 | } else { | ||||||||||||
| 115 | 0 | $kernel->yield( 'after_step' ); | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | #Finish here | ||||||||||||
| 121 | 0 | ▶ | sub finishing { | ||||||||||
|
Subroutine
finishing: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 122 | 0 | my ($kernel, $heap ) = @_[KERNEL, HEAP]; | |||||||||||
| 123 | print "Best is:\n\t ",$heap->{'population'}->[0]->asString()," Fitness: ", | ||||||||||||
| 124 | 0 | $heap->{'population'}->[0]->Fitness(),"\n"; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 4 | "Don't look further" ; # Magic true value required at end of module | |||||||||||
| 128 | __END__ | ||||||||||||