AI-ParticleSwarmOptimization-MCE

 view release on metacpan or  search on metacpan

README  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;
        }

Description

    This module is enhancement of on original AI::ParticleSwarmOptimization
    to support multi-core processing with use of MCE. 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.

README  view on Meta::CPAN

	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.

README  view on Meta::CPAN

	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

README  view on Meta::CPAN

	  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::Pmap->new(		# Multi-core	
my $pso = AI::ParticleSwarmOptimization::MCE->new(		# Multi-core	
#my $pso = AI::ParticleSwarmOptimization->new(			# Single-core
    -fitFunc    	=> \&calcFit,
    -dimensions 	=> 10,

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

));
#-----------------------------------------------------------------------	
$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;
}
#=======================================================================

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

		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 {

lib/AI/ParticleSwarmOptimization/MCE.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 MCE. 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/MCE.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/MCE.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/MCE.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 > 100, 'Computations');


sub calcFit {
    my @values = @_;
    my $offset = int (-@values / 2);
    my $sum;

    $sum += ($_ - $offset++)**2 for @values;
    return $sum;
}



( run in 0.673 second using v1.01-cache-2.11-cpan-49f99fa48dc )