Pro/MCE.pm

filter: Statementstmt 100% 133 / 133 Branchbran 85.7% 12 / 14 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 20 / 20 Podpod 100% 3 / 3 totaltotal 96.6% 172 / 178 CC 16 SCAR 10.0
CC
16
Cov
97%
evolve
5.0
_adopt
5.0
new
3.0
CRAP 2.7
« Pro/Crossover/OX.pm ↑ Summary
1 package AI::Genetic::Pro::MCE;
2  
3 444 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 444 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 444 ▶use base qw( AI::Genetic::Pro );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #-----------------------------------------------------------------------
7 444 ▶use Clone qw( clone );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 444 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 444 ▶use MCE( Sereal => 0 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 444 ▶use MCE::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 444 ▶use MCE::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 444 ▶use MCE::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 #-----------------------------------------------------------------------
14 444 $Storable::Deparse = 1;
15 444 $Storable::Eval = 1;
16 #-----------------------------------------------------------------------
17 444 __PACKAGE__->mk_accessors( qw(
18 _pop
19 _tpl
20 ));
21 #=======================================================================
22 sub new {
23 444 ▶ my ( $cls, $obj, $tpl ) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
24
25 444 my $self = bless $obj, $cls;
26
27 #-------------------------------------------------------------------
28 444 $self->_init_mce;
29 444 $self->_init_pop;
30
31 #-------------------------------------------------------------------
32 444 ▶ $AI::Genetic::Pro::Array::Type::Native = 1 if $self->native;
Branch: 1 branch
BranchTrueFalseTotal
if $self->native 222 222 444
33
34 #-------------------------------------------------------------------
35 444 delete $tpl->{ $_ } for qw( -history -mce -population -workers );
36 444 $self->_tpl( $tpl );
37
38 #-------------------------------------------------------------------
39 444 return $self;
40 }
41 #=======================================================================
42 sub _init_pop {
43 444 ▶ my ( $self ) = @_;
Subroutine _init_pop: called CC=1 SCAR=0.0
44
45 444 my $pop = int( $self->population / $self->workers );
46 444 my $rst = $self->population % $self->workers;
47
48 444 my @pop = ( $pop ) x $self->workers;
49 444 $pop[ 0 ] += $rst;
50
51 444 $self->_pop( \@pop );
52 }
53 #=======================================================================
54 sub _calculate_fitness_all {
55 444 ▶ my ($self) = @_;
Subroutine _calculate_fitness_all: called CC=1 SCAR=0.0
56
57 my %fit = mce_map {
58 2 ▶ $_ => $self->fitness()->( $self, $self->chromosomes->[ $_ ] )
Subroutine __ANON__: called CC=1 SCAR=0.0
59 444 } 0 .. $#{ $self->chromosomes };
60 418 $self->_fitness( \%fit );
61
62 418 return;
63 }
64 #=======================================================================
65 sub _init_mce {
66 444 ▶ my ( $self ) = @_;
Subroutine _init_mce: called CC=2 SCAR=7.0
67
68 #-------------------------------------------------------------------
69 444 ▶ $self->workers( MCE::Util::get_ncpu() ) unless $self->workers;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->workers 444 0 444
70
71 #-------------------------------------------------------------------
72 444 MCE::Map->init(
73 chunk_size => q[auto],
74 max_workers => $self->workers,
75 );
76
77 #-------------------------------------------------------------------
78 444 return;
79 }
80 #=======================================================================
81 sub init {
82 444 ▶ my ( $self, $val ) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
83
84 #-------------------------------------------------------------------
85 444 my $pop = $self->population;
86 444 $self->population( 1 );
87 444 $self->SUPER::init( $val );
88 418 $self->population( $pop );
89
90 #-------------------------------------------------------------------
91 418 my $one = shift @{ $self->chromosomes };
92 418 my $tpl = $self->_tpl;
93
94 my @lst = mce_map {
95 26 ▶ my $arg = clone( $tpl );
Subroutine __ANON__: called CC=1 SCAR=0.0
96 26 $arg->{ -population } = $_;
97 26 my $gal = AI::Genetic::Pro->new( %$arg );
98 26 $gal->init( $val );
99 26 @{ $gal->_state };
100
101 418 } @{ $self->_pop };
102
103 #-------------------------------------------------------------------
104 392 return $self->_adopt( \@lst );
105 }
106 #=======================================================================
107 sub _adopt {
108 3152 ▶ my ( $self, $lst ) = @_;
Subroutine _adopt: called CC=5 SCAR=16.1
109
110 3152 ▶ if( my $typ = $self->_package ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $typ = $self->_package) 1576 1576 3152
111 1576 for my $idx ( 0 .. $#$lst ){
112 157600 $lst->[ $idx ]->[ 0 ] = $typ->make_with_packed( $lst->[ $idx ]->[ 0 ] );
113 157600 bless $lst->[ $idx ]->[ 0 ], q[AI::Genetic::Pro::Chromosome];
114 }
115 }
116
117 3152 my ( @chr, %fit, @rhc, %tif );
118 3152 for my $sth ( @$lst ){
119 315200 push @chr, $sth->[ 0 ];
120 315200 $fit{ $#chr } = $sth->[ 1 ];
121 }
122
123 #@$lst = ( );
124
125 3152 my @idx = shuffle 0 .. $#chr;
126
127 3152 for my $i ( @idx ){
128 315200 push @rhc, $chr[ $i ];
129 315200 $tif{ $#rhc } = $fit{ $i };
130 }
131
132 3152 $self->_fitness ( \%tif );
133 3152 $self->chromosomes( \@rhc );
134
135 3152 return;
136 }
137 #=======================================================================
138 sub _chunks {
139 3150 ▶ my ( $self ) = @_;
Subroutine _chunks: called CC=3 SCAR=11.0
140
141 3150 my $cnt = 0;
142 3150 my @chk;
143
144 3150 for my $idx ( 0 .. $#{ $self->_pop } ){
145 40950 my $pos = 0;
146 40950 my %tmp = map { $pos++ => $self->_fitness->{ $_ } } $cnt .. $cnt + $self->_pop->[ $idx ] -1 ;
147 40950 my @tmp = splice @{ $self->chromosomes }, 0, $self->_pop->[ $idx ];
148 40950 $cnt += @tmp;
149
150 40950 ▶ if( $self->_package ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_package) { } 20475 20475 40950
151 push @chk, [
152 20475 [ map { ${ tied( @$_ ) } } @tmp ],
153 \%tmp,
154 ];
155 }else{
156 20475 push @chk, [
157 \@tmp,
158 \%tmp,
159 ];
160 }
161 }
162
163 3150 return \@chk;
164 }
165 #=======================================================================
166 sub evolve {
167 392 ▶ my ( $self, $generations ) = @_;
Subroutine evolve: called CC=5 SCAR=16.1
Pod documented
168  
169 392 ▶ $generations ||= -1;
Condition50%1/2
$generations ||= -1
l!l
3920
MC/DC0%0/1
$generations ||= -1
$generations
Truth table50%1/2
A
A= $generations
Aresult
11
00
170  
171 392 ▶ for(my $i = 0; $i != $generations; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $generations) 3152 0 3152
172
173 # terminate ----------------------------------------------------
174 3152 ▶ last if $self->terminate and $self->terminate->( $self );
Branch: 1 branch
BranchTrueFalseTotal
if $self->terminate and $self->terminate->($self) 2 3150 3152
Condition66%2/3
$self->terminate and $self->terminate->($self)
!ll&&!rl&&r
031502
MC/DC50%1/2
$self->terminate and $self->terminate->($self)
$self->terminate $self->terminate->($self)
Truth table66%2/3
A and B
A= $self->terminateB= $self->terminate->($self)
ABresult
0X0
100
111
175
176 # update generation --------------------------------------------
177 3150 $self->generation($self->generation + 1);
178
179 # update history -----------------------------------------------
180 3150 $self->_save_history;
181  
182 3150 my $tpl = $self->_tpl;
183 my @lst = mce_map {
184 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
185 390 ▶ my $ary = $_;
Subroutine __ANON__: called CC=3 SCAR=11.0
186 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
187 390 my $arg = clone( $tpl );
188 390 $arg->{ -population } = 1;
189 390 my $gal = AI::Genetic::Pro->new( %$arg );
190 390 $gal->init( 1 );
191 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
192 390 ▶ if( my $typ = $self->_package ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $typ = $self->_package) 195 195 390
193 195 for my $idx ( 0 .. $#{ $ary->[ 0 ] } ){
194 1500 $ary->[ 0 ][ $idx ] = $typ->make_with_packed( $ary->[ 0 ][ $idx ] );
195 1500 bless $ary->[ 0 ][ $idx ], q[AI::Genetic::Pro::Chromosome];
196 }
197 }
198 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
199 390 $gal->population ( scalar( @{ $ary->[ 0 ] } ) );
200 390 $gal->chromosomes( $ary->[ 0 ] );
201 390 $gal->_fitness ( $ary->[ 1 ] );
202 390 $gal->strict ( 0 );
203 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
204 390 $gal->evolve ( 1 );
205 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206 390 @{ $gal->_state };
207
208 3150 } $self->_chunks;
209  
210 2760 $self->_adopt( \@lst );
211 }
212  
213 2 return;
214 }
215 #=======================================================================
216 444 1;
« Pro/Crossover/OX.pm ↑ Summary