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