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