blib/lib/AI/ParticleSwarmOptimization/MCE.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package AI::ParticleSwarmOptimization::MCE;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 use base qw(
6 AI::ParticleSwarmOptimization
7 Class::Accessor::Fast::XS
8 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #-----------------------------------------------------------------------
10 use Clone qw( clone );
11 use List::Util qw( min shuffle );
12 use Storable;
13 use MCE ( Sereal => 0 );
14 use MCE::Map;
15 use MCE::Util;
16 #-----------------------------------------------------------------------
17 __PACKAGE__->mk_accessors( qw(
18 _pop
19 _tpl
20 _wrk
21 ));
22 #-----------------------------------------------------------------------
23 $Storable::Deparse = 1;
24 $Storable::Eval = 1;
25 #-----------------------------------------------------------------------
26 $AI::ParticleSwarmOptimization::MCE::VERSION = '1.000';
27 #=======================================================================
28 sub new {
29 my ($class, %params) = @_;
30
31 #-------------------------------------------------------------------
32 my $self = bless {}, $class;
33 $self->SUPER::setParams( %params );
34
35 #-------------------------------------------------------------------
36 $self->_init_mce( \%params );
37 $self->_init_pop( \%params );
38 $self->_init_tpl( \%params );
39
40 #-------------------------------------------------------------------
41 return $self;
42 }
43 #=======================================================================
44 sub _init_tpl {
45 my ( $self, $params ) = @_;
46
47 my $cln = clone( $params );
48 delete $cln->{ $_ } for qw(
49 -iterCount
50 -iterations
51 -numParticles
52 -workers
53
54 _pop
55 _tpl
56 _wrk
57 );
58
59 $self->_tpl( $cln );
60
61 return;
62 }
63 #=======================================================================
64 sub _init_pop {
65 my ( $self, $params ) = @_;
66
67 my $pop = int( $self->{ numParticles } / $self->_wrk );
68 my $rst = $self->{ numParticles } % $self->_wrk;
69
70 my @pop = ( $pop ) x $self->_wrk;
71 $pop[ 0 ] += $rst;
72
73 $self->_pop( \@pop );
74 }
75 #=======================================================================
76 sub _init_mce {
77 my ( $self, $params ) = @_;
78
79 #-------------------------------------------------------------------
80 $self->_wrk( $params->{ '-workers' } || MCE::Util::get_ncpu() );
81
82 #-------------------------------------------------------------------
83 MCE::Map->init(
84 chunk_size => q[auto],
85 max_workers => $self->_wrk,
86 );
87
88 #-------------------------------------------------------------------
89 return;
90 }
91 #=======================================================================
92 sub setParams {
93 my ( $self, %params ) = @_;
94
95 my $fles = __PACKAGE__->new( %params );
96
97 $self->{ $_ } = $fles->{ $_ } for keys %$fles;
98
99 return 1;
100 }
101 #=======================================================================
102 sub init {
103 my ( $self ) = @_;
104
105 #-------------------------------------------------------------------
106 my $pop = $self->{ numParticles };
107 $self->{ numParticles } = 1;
108 $self->SUPER::init();
109 $self->{ numParticles } = $pop;
110 $self->{ prtcls } = [ ];
111
112 #-------------------------------------------------------------------
113 my $cnt = 0;
114 my $tpl = $self->_tpl;
115
116 @{ $self->{ prtcls } } = map {
117 $_->{ id } = $cnt++;
118 $_
119 } mce_map {
120 my $arg = clone( $tpl );
121 $arg->{ -numParticles } = $_;
122
123 my $swm = AI::ParticleSwarmOptimization->new( %$arg );
124 $swm->init;
125
126 @{ $swm->{ prtcls } };
127
128 } @{ $self->_pop };
129
130 #-------------------------------------------------------------------
131 return 1;
132 }
133 #=======================================================================
134 sub _chunks {
135 my ( $self ) = @_;
136
137 #-------------------------------------------------------------------
138 @{ $self->{ prtcls } } = shuffle @{ $self->{ prtcls } };
139
140 #-------------------------------------------------------------------
141 my @chk;
142 for my $idx ( 0 .. $#{ $self->_pop } ){
143 #my $cnt = 0;
144 #my @tmp = map {
145 # $_->{ id } = $cnt++;
146 # $_
147 #} splice @{ $self->{ prtcls } }, 0, $self->_pop->[ $idx ];
148
149 # Faster and smaller memory consumption...
150 my $cnt = 0;
151 my @tmp = splice @{ $self->{ prtcls } }, 0, $self->_pop->[ $idx ];
152 $_->{ id } = $cnt++ for @tmp;
153  
154 push @chk, \@tmp;
155 }
156
157 #-------------------------------------------------------------------
158 return \@chk;
159 }
160 #=======================================================================
161 sub _updateVelocities {
162 my ( $self, $iter ) = @_;
163  
164 #-------------------------------------------------------------------
165 print "Iter $iter\n" if $self->{verbose} & AI::ParticleSwarmOptimization::kLogIter;
166  
167 my $tpl = $self->_tpl;
168  
169 my @lst = mce_map {
170 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171 my $ary = $_;
172 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173 my $arg = clone( $tpl );
174 $arg->{ -numParticles } = 1;
175
176 my $swm = AI::ParticleSwarmOptimization->new( %$arg );
177 $swm->init;
178 $swm->{ numParticles } = scalar( @$ary );
179 $swm->{ prtcls } = $ary;
180 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
181 $swm->_updateVelocities( $iter );
182 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183 [
184 $swm->{ prtcls },
185 $swm->{ bestBest },
186 ]
187 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
188 } $self->_chunks;
189  
190 #-------------------------------------------------------------------
191 #my $cnt = 0;
192 #@{ $self->{ prtcls } } = map {
193 # $_->{ id } = $cnt++;
194 # $_
195 #} map {
196 # @{ $_->[ 0 ] }
197 #} @lst;
198  
199 # Faster and smaller memory consumption...
200 my $cnt = 0;
201 @{ $self->{ prtcls } } = map { @{ $_->[ 0 ] } } @lst;
202 $_->{ id } = $cnt++ for @{ $self->{ prtcls } };
203
204 #-------------------------------------------------------------------
205 $self->{ bestBest } = min grep { defined $_ } map { $_->[ 1 ] } @lst;
206
207 #-------------------------------------------------------------------
208 return;
209 }
210 #=======================================================================
211 sub _moveParticles {
212 my ( $self, $iter ) = @_;
213  
214 #-------------------------------------------------------------------
215 print "Iter $iter\n" if $self->{verbose} & AI::ParticleSwarmOptimization::kLogIter;
216  
217 my $tpl = $self->_tpl;
218  
219 my @lst = mce_map {
220 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
221 my $ary = $_;
222 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
223 my $arg = clone( $tpl );
224 $arg->{ -numParticles } = 1;
225
226 my $swm = AI::ParticleSwarmOptimization->new( %$arg );
227 $swm->init;
228 $swm->{ numParticles } = scalar( @$ary );
229 $swm->{ prtcls } = $ary;
230 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
231 [
232 $swm->_moveParticles( $iter ),
233 $swm->{ prtcls }
234 ]
235 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
236 } $self->_chunks;
237  
238 #-------------------------------------------------------------------
239 #my $cnt = 0;
240 #@{ $self->{ prtcls } } = map {
241 # $_->{ id } = $cnt++;
242 # $_
243 #} map {
244 # @{ $_->[ 1 ] }
245 #} @lst;
246  
247 # Faster and smaller memory consumption...
248 my $cnt = 0;
249 @{ $self->{ prtcls } } = map { @{ $_->[ 1 ] } } @lst;
250 $_->{ id } = $cnt++ for @{ $self->{ prtcls } };
251  
252 #-------------------------------------------------------------------
253 return unless grep { defined $_ } map { $_->[ 0 ] } @lst;
254 return 1;
255 }
256 #=======================================================================
257 1;
258  
259 __END__
↑ Summary