view release on metacpan or search on metacpan
Revision history for Perl extension AI::ParticleSwarmOptimization::Pmap.
1.008 Mon, 09 Jan 2023 22:17:01 +0100
- Fix in POD.
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
# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.017.
Changes
LICENSE
MANIFEST
META.json
META.yml
Makefile.PL
README
dist.ini
example/PSOTest-MultiCore.pl
lib/AI/ParticleSwarmOptimization/Pmap.pm
t/00_load.t
t/01_pso_multi.t
],
"dynamic_config" : 0,
"generated_by" : "Dist::Zilla version 6.017, CPAN::Meta::Converter version 2.150010",
"license" : [
"lgpl_2_1"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : 2
},
"name" : "AI-ParticleSwarmOptimization-Pmap",
"prereqs" : {
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"runtime" : {
"requires" : {
"AI::ParticleSwarmOptimization" : "1.006",
"List::Util" : "0",
"Parallel::parallel_map" : "0.02"
}
}
},
"release_status" : "stable",
"resources" : {
"homepage" : "https://github.com/d-strzelec/AI-ParticleSwarmOptimization-Pmap",
"repository" : {
"type" : "git",
"url" : "https://github.com/d-strzelec/AI-ParticleSwarmOptimization-Pmap.git",
"web" : "https://github.com/d-strzelec/AI-ParticleSwarmOptimization-Pmap"
}
},
"version" : "1.008",
"x_Dist_Zilla" : {
"perl" : {
"version" : "5.032001"
},
"plugins" : [
{
"class" : "Dist::Zilla::Plugin::GatherDir",
- 'Åukasz Strzelecki <lukasz@strzeleccy.eu>'
build_requires: {}
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 0
generated_by: 'Dist::Zilla version 6.017, CPAN::Meta::Converter version 2.150010'
license: lgpl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: AI-ParticleSwarmOptimization-Pmap
requires:
AI::ParticleSwarmOptimization: '1.006'
List::Util: '0'
Parallel::parallel_map: '0.02'
resources:
homepage: https://github.com/d-strzelec/AI-ParticleSwarmOptimization-Pmap
repository: https://github.com/d-strzelec/AI-ParticleSwarmOptimization-Pmap.git
version: '1.008'
x_Dist_Zilla:
perl:
version: '5.032001'
plugins:
-
class: Dist::Zilla::Plugin::GatherDir
config:
Dist::Zilla::Plugin::GatherDir:
exclude_filename: []
Makefile.PL view on Meta::CPAN
use ExtUtils::MakeMaker;
my %WriteMakefileArgs = (
"ABSTRACT" => "Particle Swarm Optimization (object oriented) with support for multi-core processing",
"AUTHOR" => "\x{141}ukasz Strzelecki <lukasz\@strzeleccy.eu>",
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => 0
},
"DISTNAME" => "AI-ParticleSwarmOptimization-Pmap",
"LICENSE" => "lgpl",
"NAME" => "AI::ParticleSwarmOptimization::Pmap",
"PREREQ_PM" => {
"AI::ParticleSwarmOptimization" => "1.006",
"List::Util" => 0,
"Parallel::parallel_map" => "0.02"
},
"VERSION" => "1.008",
"test" => {
"TESTS" => "t/*.t"
}
);
my %FallbackPrereqs = (
"AI::ParticleSwarmOptimization" => "1.006",
"List::Util" => 0,
"Parallel::parallel_map" => "0.02"
);
unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) {
delete $WriteMakefileArgs{TEST_REQUIRES};
delete $WriteMakefileArgs{BUILD_REQUIRES};
$WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs;
}
delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
NAME
AI::ParticleSwarmOptimization::Pmap - Particle Swarm Optimization
(object oriented) with support for multi-core processing
SYNOPSIS
use AI::ParticleSwarmOptimization::Pmap;
my $pso = AI::ParticleSwarmOptimization::Pmap->new (
-fitFunc => \&calcFit,
-dimensions => 3,
-iterations => 10,
-numParticles => 1000,
# only for many-core version # the best if == $#cores of your system
# selecting best value if undefined
-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;
}
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.
The Particle Swarm Optimization technique uses communication of the
current best position found between a number of particles moving over a
hyper surface as a technique for locating the best location on the
surface (where 'best' is the minimum of some fitness function). For a
Wikipedia discussion of PSO see
http://en.wikipedia.org/wiki/Particle_swarm_optimization.
-fitFunc: required
-fitFunc is a reference to the fitness function used by the search.
If extra parameters need to be passed to the fitness function an
array ref may be used with the code ref as the first array element
and parameters to be passed into 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;
SEE ALSO
http://en.wikipedia.org/wiki/Particle_swarm_optimization
AUTHOR
Strzelecki Lukasz <lukasz@strzeleccy.eu>
SEE ALSO
AI::ParticleSwarmOptimization AI::ParticleSwarmOptimization::Pmap
COPYRIGHT
Copyright (c) Strzelecki Lukasz. All rights reserved. This program is
free software; you can redistribute it and/or modify it under the same
terms as Perl itself.
name = AI-ParticleSwarmOptimization-Pmap
author = Åukasz Strzelecki <lukasz@strzeleccy.eu>
license = LGPL_2_1
copyright_holder = Åukasz Strzelecki
copyright_year = 2023
version = 1.008
[GatherDir]
[PruneCruft]
[PruneFiles]
;match = ~$
[MetaJSON]
[License]
[MakeMaker]
[Manifest]
[PkgVersion]
[ReadmeFromPod]
[MetaConfig]
[Prereqs]
AI::ParticleSwarmOptimization = 1.006
Parallel::parallel_map = 0.02
List::Util = 0
[GithubMeta]
remote = d-strzelec
[Git::Commit]
changelog = Changes
commit_msg = Release v-%v%n%n%c
allow_dirty_match = .*(?:\.pm|README|\*.t)$
example/PSOTest-MultiCore.pl view on Meta::CPAN
#!/usr/bin/perl
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;
}
#=======================================================================
++$|;
#-----------------------------------------------------------------------
#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,
-iterations => 10,
-numParticles => 1000,
# only for many-core version # the best if == $#cores of your system
# selecting best value if undefined
-workers => 4,
);
my $beg = time;
$pso->init();
my $fitValue = $pso->optimize ();
my ( $best ) = $pso->getBestParticles (1);
my ( $fit, @values ) = $pso->getParticleBestPos ($best);
my $iters = $pso->getIterationCount();
printf "Fit %.4f at (%s) after %d iterations\n", $fit, join (', ', map {sprintf '%.4f', $_} @values), $iters;
warn "\nTime: ", time - $beg, "\n\n";
#=======================================================================
exit 0;
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
package AI::ParticleSwarmOptimization::Pmap;
$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;
#-----------------------------------------------------------------------
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
$_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};
my $fit = $prtcl->{currFit};
if ($self->_betterFit ($fit, $prtcl->{bestFit})) {
# Save position - best fit for this particle so far
$self->_saveBest ($prtcl, $fit, $iter);
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
my $ret;
if( defined $self->{exitFit} and $fit < $self->{exitFit} ){
$ret = $fit;
}elsif( !($self->{verbose} & AI::ParticleSwarmOptimization::kLogIterDetail) ){
$ret = undef;
}else{
printf "Part %3d fit %8.2f", $prtcl->{id}, $fit
if $self->{verbose} >= 2;
printf " (%s @ %s)",
join (', ', map {sprintf '%5.3f', $_} @{$prtcl->{velocity}}),
join (', ', map {sprintf '%5.2f', $_} @{$prtcl->{currPos}})
if $self->{verbose} & AI::ParticleSwarmOptimization::kLogDetail;
print "\n";
$ret = undef;
}
#---------------------------------------------------------------
[ $prtcl, $ret ]
} @{ $self->{ prtcls } };
@{ $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 =
$self->_randInRange (-$self->{meWeight}, $self->{meWeight});
my $themFactor =
$self->_randInRange (-$self->{themWeight}, $self->{themWeight});
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
$prtcl;
} @{ $self->{ prtcls } };
}
#=======================================================================
1;
__END__
=head1 NAME
AI::ParticleSwarmOptimization::Pmap - Particle Swarm Optimization (object oriented) with support for multi-core processing
=head1 SYNOPSIS
use AI::ParticleSwarmOptimization::Pmap;
my $pso = AI::ParticleSwarmOptimization::Pmap->new (
-fitFunc => \&calcFit,
-dimensions => 3,
-iterations => 10,
-numParticles => 1000,
# only for many-core version # the best if == $#cores of your system
# selecting best value if undefined
-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;
}
=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.
The Particle Swarm Optimization technique uses communication of the current best
position found between a number of particles moving over a hyper surface as a
technique for locating the best location on the surface (where 'best' is the
minimum of some fitness function). For a Wikipedia discussion of PSO see
http://en.wikipedia.org/wiki/Particle_swarm_optimization.
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
fitness value becomes equal or less than I<-exitFit>.
=item I<-fitFunc>: required
I<-fitFunc> is a reference to the fitness function used by the search. If extra
parameters need to be passed to the fitness function an array ref may be used
with the code ref as the first array element and parameters to be passed into
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;
lib/AI/ParticleSwarmOptimization/Pmap.pm view on Meta::CPAN
http://en.wikipedia.org/wiki/Particle_swarm_optimization
=head1 AUTHOR
Strzelecki Lukasz <lukasz@strzeleccy.eu>
=head1 SEE ALSO
L<AI::ParticleSwarmOptimization>
L<AI::ParticleSwarmOptimization::Pmap>
=head1 COPYRIGHT
Copyright (c) Strzelecki Lukasz. All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
t/00_load.t view on Meta::CPAN
#/usr/bin/!perl
use Test::More qw(no_plan);
use_ok( 'AI::ParticleSwarmOptimization' );
use_ok( 'AI::ParticleSwarmOptimization::Pmap' );
t/01_pso_multi.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use lib '../lib'; # For development testing
use AI::ParticleSwarmOptimization::Pmap;
=head1 NAME
AI::ParticleSwarmOptimization::Pmap test suite
=head1 DESCRIPTION
Test AI::ParticleSwarmOptimization::Pmap
=cut
plan (tests => 1);
# Calculation tests.
my $pso = AI::ParticleSwarmOptimization::Pmap->new (
-fitFunc => \&calcFit,
-dimensions => 10,
-iterations => 10,
-numParticles => 1000,
# only for many-core version # the best if == $#cores of your system
# selecting best value if undefined
-workers => 4,
);