AI-ParticleSwarmOptimization-MCE
view release on metacpan or search on metacpan
copy of the Library. To do this, you must alter all the notices
that refer to this License, so that they refer to the ordinary
GNU General Public License, version 2, instead of to this
License. (If a newer version than version 2 of the ordinary
GNU General Public License has appeared, then you can
specify that version instead if you wish.) Do not make any
other change in these notices.
Once this change is made in a given copy, it is irreversible
for that copy, so the ordinary GNU General Public License
applies to all subsequent copies and derivative works made
from that copy.
This option is useful when you wish to copy part of the code
of the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable
form under the terms of Sections 1 and 2 above provided that
you accompany it with the complete corresponding
machine-readable source code, which must be distributed
uncombined with any other library facilities.
This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined
library of the fact that part of it is a work based
on the Library, and explaining where to find the
accompanying uncombined form of the same
work.
8. You may not copy, modify, sublicense, link with, or
distribute the Library except as expressly provided under this
License. Any attempt otherwise to copy, modify, sublicense,
link with, or distribute the Library is void, and will
automatically terminate your rights under this License.
However, parties who have received copies, or rights, from
you under this License will not have their licenses terminated
so long as such parties remain in full compliance.
9. You are not required to accept this License, since you
have not signed it. However, nothing else grants you
permission to modify or distribute the Library or its derivative
works. These actions are prohibited by law if you do not
accept this License. Therefore, by modifying or distributing
the Library (or any work based on the Library), you indicate
your acceptance of this License to do so, and all its terms
and conditions for copying, distributing or modifying the
Library or works based on it.
10. Each time you redistribute the Library (or any work
based on the Library), the recipient automatically receives a
license from the original licensor to copy, distribute, link with
or modify the Library subject to these terms and conditions.
You may not impose any further restrictions on the
recipients' exercise of the rights granted herein. You are not
responsible for enforcing compliance by third parties with this
License.
11. If, as a consequence of a court judgment or allegation of
patent infringement or for any other reason (not limited to
patent issues), conditions are imposed on you (whether by
court order, agreement or otherwise) that contradict the
conditions of this License, they do not excuse you from the
-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::MCE->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
Reinitialize the optimization. init () will be called during the
first call to optimize () if it hasn't already been called.
optimize ()
Runs the minimization optimization. Returns the fit value of the best
fit found. The best possible fit is negative infinity.
optimize () may be called repeatedly to continue the fitting process.
The fit processing on each subsequent call will continue from where
the last call left off.
getParticleState ()
Returns the vector of position
getBestParticles ([$n])
Takes an optional count.
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/MCE.pm view on Meta::CPAN
#-----------------------------------------------------------------------
__PACKAGE__->mk_accessors( qw(
_pop
_tpl
_wrk
));
#-----------------------------------------------------------------------
$Storable::Deparse = 1;
$Storable::Eval = 1;
#=======================================================================
sub new {
my ($class, %params) = @_;
#-------------------------------------------------------------------
my $self = bless {}, $class;
$self->SUPER::setParams( %params );
#-------------------------------------------------------------------
$self->_init_mce( \%params );
$self->_init_pop( \%params );
$self->_init_tpl( \%params );
#-------------------------------------------------------------------
return $self;
}
#=======================================================================
sub _init_tpl {
my ( $self, $params ) = @_;
my $cln = clone( $params );
delete $cln->{ $_ } for qw(
-iterCount
-iterations
-numParticles
-workers
_pop
_tpl
_wrk
);
$self->_tpl( $cln );
return;
}
#=======================================================================
sub _init_pop {
my ( $self, $params ) = @_;
my $pop = int( $self->{ numParticles } / $self->_wrk );
my $rst = $self->{ numParticles } % $self->_wrk;
my @pop = ( $pop ) x $self->_wrk;
$pop[ 0 ] += $rst;
$self->_pop( \@pop );
}
#=======================================================================
sub _init_mce {
my ( $self, $params ) = @_;
#-------------------------------------------------------------------
$self->_wrk( $params->{ '-workers' } || MCE::Util::get_ncpu() );
#-------------------------------------------------------------------
MCE::Map->init(
chunk_size => 1, # Thanks Roy :-)
#chunk_size => q[auto], # The old one. Currently it should be the same...
max_workers => $self->_wrk,
posix_exit => 1, # Thanks Roy :-)
);
#-------------------------------------------------------------------
return;
}
#=======================================================================
sub setParams {
my ( $self, %params ) = @_;
my $fles = __PACKAGE__->new( %params );
$self->{ $_ } = $fles->{ $_ } for keys %$fles;
return 1;
}
#=======================================================================
sub init {
my ( $self ) = @_;
#-------------------------------------------------------------------
my $pop = $self->{ numParticles };
$self->{ numParticles } = 1;
$self->SUPER::init();
$self->{ numParticles } = $pop;
$self->{ prtcls } = [ ];
#-------------------------------------------------------------------
lib/AI/ParticleSwarmOptimization/MCE.pm view on Meta::CPAN
$swm->init;
@{ $swm->{ prtcls } };
} @{ $self->_pop };
#-------------------------------------------------------------------
return 1;
}
#=======================================================================
sub _chunks {
my ( $self ) = @_;
#-------------------------------------------------------------------
@{ $self->{ prtcls } } = shuffle @{ $self->{ prtcls } };
#-------------------------------------------------------------------
my @chk;
for my $idx ( 0 .. $#{ $self->_pop } ){
#my $cnt = 0;
#my @tmp = map {
lib/AI/ParticleSwarmOptimization/MCE.pm view on Meta::CPAN
my @tmp = splice @{ $self->{ prtcls } }, 0, $self->_pop->[ $idx ];
$_->{ id } = $cnt++ for @tmp;
push @chk, \@tmp;
}
#-------------------------------------------------------------------
return \@chk;
}
#=======================================================================
sub _updateVelocities {
my ( $self, $iter ) = @_;
#-------------------------------------------------------------------
print "Iter $iter\n" if $self->{verbose} & AI::ParticleSwarmOptimization::kLogIter;
my $tpl = $self->_tpl;
my @lst = mce_map {
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my $ary = $_;
lib/AI/ParticleSwarmOptimization/MCE.pm view on Meta::CPAN
@{ $self->{ prtcls } } = map { @{ $_->[ 0 ] } } @lst;
$_->{ id } = $cnt++ for @{ $self->{ prtcls } };
#-------------------------------------------------------------------
$self->{ bestBest } = min grep { defined $_ } map { $_->[ 1 ] } @lst;
#-------------------------------------------------------------------
return;
}
#=======================================================================
sub _moveParticles {
my ( $self, $iter ) = @_;
#-------------------------------------------------------------------
print "Iter $iter\n" if $self->{verbose} & AI::ParticleSwarmOptimization::kLogIter;
my $tpl = $self->_tpl;
my @lst = mce_map {
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my $ary = $_;
lib/AI/ParticleSwarmOptimization/MCE.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/MCE.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::MCE->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
lib/AI/ParticleSwarmOptimization/MCE.pm view on Meta::CPAN
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 ()>
Runs the minimization optimization. Returns the fit value of the best fit
found. The best possible fit is negative infinity.
B<optimize ()> may be called repeatedly to continue the fitting process. The fit
processing on each subsequent call will continue from where the last call left
off.
=item B<getParticleState ()>
Returns the vector of position
=item B<getBestParticles ([$n])>
Takes an optional count.
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 > 100, 'Computations');
sub calcFit {
my @values = @_;
my $offset = int (-@values / 2);
my $sum;
$sum += ($_ - $offset++)**2 for @values;
return $sum;
}
( run in 1.019 second using v1.01-cache-2.11-cpan-88abd93f124 )