AI-ParticleSwarmOptimization-Pmap
view release on metacpan or search on metacpan
-workers => 4,
);
my $fitValue = $pso->optimize ();
my ($best) = $pso->getBestParticles (1);
my ($fit, @values) = $pso->getParticleBestPos ($best);
printf "Fit %.4f at (%s)\n",
$fit, join ', ', map {sprintf '%.4f', $_} @values;
sub calcFit {
my @values = @_;
my $offset = int (-@values / 2);
my $sum;
select( undef, undef, undef, 0.01 ); # Simulation of heavy processing...
$sum += ($_ - $offset++) ** 2 for @values;
return $sum;
}
elements. User provided parameters are passed as the first
parameters to the fitness function when it is called:
my $pso = AI::ParticleSwarmOptimization::Pmap->new(
-fitFunc => [\&calcFit, $context],
-dimensions => 3,
);
...
sub calcFit {
my ($context, @values) = @_;
...
return $fitness;
}
In addition to any user provided parameters the list of values
representing the current particle position in the hyperspace is
passed in. There is one value per hyperspace dimension.
-inertia: positive or zero number, optional
example/PSOTest-MultiCore.pl view on Meta::CPAN
use strict;
use warnings;
use lib '../lib/';
#-----------------------------------------------------------------------
#use AI::ParticleSwarmOptimization;
#use AI::ParticleSwarmOptimization::MCE;
use AI::ParticleSwarmOptimization::Pmap;
use Data::Dumper; $::Data::Dumper::Sortkeys = 1;
#=======================================================================
sub calcFit {
my @values = @_;
my $offset = int (-@values / 2);
my $sum;
select( undef, undef, undef, 0.01 ); # Simulation of heavy processing...
$sum += ($_ - $offset++) ** 2 for @values;
return $sum;
}
#=======================================================================
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
$AI::ParticleSwarmOptimization::Pmap::VERSION = '1.008';
use strict;
use warnings;
use base qw(
AI::ParticleSwarmOptimization
);
#-----------------------------------------------------------------------
use List::Util qw( min );
use Parallel::parallel_map;
#=======================================================================
sub _initParticles {
my ($self) = @_;
@{ $self->{ prtcls } } = map {
my %prt = ( id => $_ );
$self->_initParticle( \%prt );
\%prt;
} 0 .. $self->{ numParticles } - 1;
}
#=======================================================================
# Thanks Mario :-)
my $_pid = $$;
my $_srn = 0;
#-----------------------------------------------------------------------
sub _randInRange {
my ($self, $min, $max) = @_;
if( $$ != $_pid and not $_srn ){
$self->{ rndGen }->set_seed( abs( $$ ) );
$_srn = 1;
}
return $min + $self->{ rndGen }->rand( $max - $min );
}
#=======================================================================
sub _moveParticles {
my ($self, $iter) = @_;
print "Iter $iter\n" if $self->{verbose} & AI::ParticleSwarmOptimization::kLogIter;
my @lst = grep { defined $_ } parallel_map {
my $prtcl = $_;
#---------------------------------------------------------------
@{$prtcl->{currPos}} = @{$prtcl->{nextPos}};
$prtcl->{currFit} = $prtcl->{nextFit};
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
@{ $self->{ prtcls } } = map { $_->[ 0 ] } @lst;
$self->{ bestBest } = min map { $_->{ bestFit } } @{ $self->{ prtcls } };
my @fit = map { $_->[ 1 ] } grep { defined $_->[ 1 ] } @lst;
return scalar( @fit ) ? ( sort { $a <=> $b } @fit )[ 0 ] : undef;
}
#=======================================================================
sub _updateVelocities {
my ($self, $iter) = @_;
@{ $self->{ prtcls } } = parallel_map {
my $prtcl = $_;
#---------------------------------------------------------------
my $bestN = $self->{prtcls}[$self->_getBestNeighbour ($prtcl)];
my $velSq;
for my $d (0 .. $self->{dimensions} - 1) {
my $meFactor =
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
-workers => 4,
);
my $fitValue = $pso->optimize ();
my ($best) = $pso->getBestParticles (1);
my ($fit, @values) = $pso->getParticleBestPos ($best);
printf "Fit %.4f at (%s)\n",
$fit, join ', ', map {sprintf '%.4f', $_} @values;
sub calcFit {
my @values = @_;
my $offset = int (-@values / 2);
my $sum;
select( undef, undef, undef, 0.01 ); # Simulation of heavy processing...
$sum += ($_ - $offset++) ** 2 for @values;
return $sum;
}
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
the fitness function as following elements. User provided parameters are passed
as the first parameters to the fitness function when it is called:
my $pso = AI::ParticleSwarmOptimization::Pmap->new(
-fitFunc => [\&calcFit, $context],
-dimensions => 3,
);
...
sub calcFit {
my ($context, @values) = @_;
...
return $fitness;
}
In addition to any user provided parameters the list of values representing the
current particle position in the hyperspace is passed in. There is one value per
hyperspace dimension.
=item I<-inertia>: positive or zero number, optional
t/01_pso_multi.t view on Meta::CPAN
my $fitValue = $pso->optimize ();
my ( $best ) = $pso->getBestParticles (1);
my ( $fit, @values ) = $pso->getParticleBestPos ($best);
my $iters = $pso->getIterationCount();
ok ( $fit > 0, 'Computations');
sub calcFit {
my @values = @_;
my $offset = int (-@values / 2);
my $sum;
$sum += ($_ - $offset++)**2 for @values;
return $sum;
}
( run in 0.766 second using v1.01-cache-2.11-cpan-a5abf4f5562 )