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