AI-ParticleSwarmOptimization-Pmap
view release on metacpan or search on metacpan
1.007 Mon, 09 Jan 2023 22:08:55 +0100
- First release with Dist::Zilla.
1.006 Thu, 05 Jan 2023 11:16:26 +0100
- Fix: MANIFEST
1.005 Mon, 17 Oct 2022 18:51:37 +0200
- Fix in a range of randomness. Thanks to Mario Roy :-)
1.004 Sun, 16 Oct 2022 16:19:22 +0200
- Fix of testing set.
1.003 Sun, 16 Oct 2022 16:13:19 +0200
- Fix of testing set.
1.002 Fri, 07 Oct 2022 18:19:01 +0200
- Fix of version number.
1.001 Fri, 07 Oct 2022 14:12:56 +0200
- Fix in a POD.
1.000 Fri, 07 Oct 2022 10:33:30 +0200
- First release.
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;
}
Description
This module is enhancement of on original AI::ParticleSwarmOptimization
to support multi-core processing with use of Pmap. Below you can find
original documentation of that module, but with one difference. There
is new parameter "-workers", which one can use to define of number of
parallel processes that will be used during computations.
of dimensions.
-posMax: number, optional
Maximum coordinate value for any dimension in the hyper space.
Defaults to 100.
-posMin: number, optional
Minimum coordinate value for any dimension in the hyper space.
Defaults to --posMax (if -posMax is negative -posMin should be set
more negative).
-randSeed: number, optional
Seed for the random number generator. Useful if you want to rerun
an optimization, perhaps for benchmarking or test purposes.
-randStartVelocity: boolean, optional
Set true to initialize particles with a random velocity. Otherwise
particle velocity is set to 0 on initalization.
A range based on 1/100th of --posMax - -posMin is used for the
initial speed in each dimension of the velocity vector if a random
start velocity is used.
-stallSpeed: positive number, optional
Speed below which a particle is considered to be stalled and is
repositioned to a new random location with a new initial speed.
Defaults to 10% of the number of iterations (-iterations).
-exitPlateauBurnin: number, optional
Determines how many iterations to run before checking for plateaus.
Defaults to 50% of the number of iterations (-iterations).
-verbose: flags, optional
If set to a non-zero value -verbose determines the level of
diagnostic print reporting that is generated during optimization.
The following constants may be bitwise ored together to set logging
options:
* kLogBetter
prints particle details when its fit becomes bebtter than its
previous best.
* kLogStall
prints particle details when its velocity reaches 0 or falls
Shows the current iteration number.
* kLogDetail
Shows additional details for some of the other logging options.
* kLogIterDetail
Shorthand for kLogIter | kLogIterDetail
setParams (%parameters)
Set or change optimization parameters. See -new above for a
description of the parameters that may be supplied.
init ()
Reinitialize the optimization. init () will be called during the
first call to optimize () if it hasn't already been called.
optimize ()
example/PSOTest-MultiCore.pl view on Meta::CPAN
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;
}
#=======================================================================
++$|;
#-----------------------------------------------------------------------
#my $pso = AI::ParticleSwarmOptimization->new( # Single-core
#my $pso = AI::ParticleSwarmOptimization::MCE->new( # Multi-core
my $pso = AI::ParticleSwarmOptimization::Pmap->new( # Multi-core
-fitFunc => \&calcFit,
-dimensions => 10,
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
} 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;
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
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;
}
=head1 Description
This module is enhancement of on original AI::ParticleSwarmOptimization to support
multi-core processing with use of Pmap. Below you can find original documentation
of that module, but with one difference. There is new parameter "-workers", which
one can use to define of number of parallel processes that will be used during
computations.
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
Number of particles in the swarm. Defaults to 10 times the number of dimensions.
=item I<-posMax>: number, optional
Maximum coordinate value for any dimension in the hyper space. Defaults to 100.
=item I<-posMin>: number, optional
Minimum coordinate value for any dimension in the hyper space. Defaults to
-I<-posMax> (if I<-posMax> is negative I<-posMin> should be set more negative).
=item I<-randSeed>: number, optional
Seed for the random number generator. Useful if you want to rerun an
optimization, perhaps for benchmarking or test purposes.
=item I<-randStartVelocity>: boolean, optional
Set true to initialize particles with a random velocity. Otherwise particle
velocity is set to 0 on initalization.
A range based on 1/100th of -I<-posMax> - I<-posMin> is used for the initial
speed in each dimension of the velocity vector if a random start velocity is
used.
=item I<-stallSpeed>: positive number, optional
Speed below which a particle is considered to be stalled and is repositioned to
a new random location with a new initial speed.
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
Defaults to 10% of the number of iterations (I<-iterations>).
=item I<-exitPlateauBurnin>: number, optional
Determines how many iterations to run before checking for plateaus.
Defaults to 50% of the number of iterations (I<-iterations>).
=item I<-verbose>: flags, optional
If set to a non-zero value I<-verbose> determines the level of diagnostic print
reporting that is generated during optimization.
The following constants may be bitwise ored together to set logging options:
=over 4
=item * kLogBetter
prints particle details when its fit becomes bebtter than its previous best.
=item * kLogStall
prints particle details when its velocity reaches 0 or falls below the stall
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
Shows additional details for some of the other logging options.
=item * kLogIterDetail
Shorthand for C<kLogIter | kLogIterDetail>
=back
=back
=item B<setParams (%parameters)>
Set or change optimization parameters. See I<-new> above for a description of
the parameters that may be supplied.
=item B<init ()>
Reinitialize the optimization. B<init ()> will be called during the first call
to B<optimize ()> if it hasn't already been called.
=item B<optimize ()>
t/01_pso_multi.t view on Meta::CPAN
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 1.077 second using v1.01-cache-2.11-cpan-49f99fa48dc )