AI-ParticleSwarmOptimization-Pmap

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


        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

README  view on Meta::CPAN


	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.

	By default -stallSpeed is undefined but particles with a speed of 0
	will be repositioned.

      -themWeight: number, optional

	Coefficient determining the influence of the neighbourhod best
	position on the next iterations velocity. Defaults to 0.5.

	See also -inertia and -meWeight.

      -exitPlateau: boolean, optional

	Set true to have the optimization check for plateaus (regions where
	the fit hasn't improved much for a while) during the search. The
	optimization ends when a suitable plateau is detected following the
	burn in period.

	Defaults to undefined (option disabled).

      -exitPlateauDP: number, optional

	Specify the number of decimal places to compare between the current
	fitness function value and the mean of the previous
	-exitPlateauWindow values.

	Defaults to 10.

      -exitPlateauWindow: number, optional

example/PSOTest-MultiCore.pl  view on Meta::CPAN

#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);

lib/AI/ParticleSwarmOptimization/Pmap.pm  view on Meta::CPAN


        if ($self->_betterFit ($fit, $prtcl->{bestFit})) {
            # Save position - best fit for this particle so far
            $self->_saveBest ($prtcl, $fit, $iter);
        }

		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;

lib/AI/ParticleSwarmOptimization/Pmap.pm  view on Meta::CPAN


    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

lib/AI/ParticleSwarmOptimization/Pmap.pm  view on Meta::CPAN


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.

By default I<-stallSpeed> is undefined but particles with a speed of 0 will be
repositioned.

=item I<-themWeight>: number, optional

Coefficient determining the influence of the neighbourhod best position on the
next iterations velocity. Defaults to 0.5.

See also I<-inertia> and I<-meWeight>.

=item I<-exitPlateau>: boolean, optional

Set true to have the optimization check for plateaus (regions where the fit
hasn't improved much for a while) during the search. The optimization ends when
a suitable plateau is detected following the burn in period.

Defaults to undefined (option disabled).

=item I<-exitPlateauDP>: number, optional

Specify the number of decimal places to compare between the current fitness
function value and the mean of the previous I<-exitPlateauWindow> values.

Defaults to 10.

=item I<-exitPlateauWindow>: number, optional

t/01_pso_multi.t  view on Meta::CPAN

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,
);

$pso->init();

my $fitValue         = $pso->optimize ();
my ( $best )         = $pso->getBestParticles (1);
my ( $fit, @values ) = $pso->getParticleBestPos ($best);
my $iters            = $pso->getIterationCount();



( run in 1.081 second using v1.01-cache-2.11-cpan-d80b1682f3f )