AI-ParticleSwarmOptimization-Pmap

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

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

README  view on Meta::CPAN


      -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
	  below the stall threshold.

	  * kLogIter

	  Shows the current iteration number.

	  * kLogDetail

	  Shows additional details for some of the other logging options.

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


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

    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;

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

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

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

            $prtcl->{velocity}[$d] =
                $prtcl->{velocity}[$d] * $self->{inertia} +
                $meFactor * $meDelta +
                $themFactor * $themDelta;
            $velSq += $prtcl->{velocity}[$d]**2;
        }

        my $vel = sqrt ($velSq);
        if (!$vel or $self->{stallSpeed} and $vel <= $self->{stallSpeed}) {
            $self->_initParticle ($prtcl);
            printf "#%05d: Particle $prtcl->{id} stalled (%6f)\n", $iter, $vel
                if $self->{verbose} & AI::ParticleSwarmOptimization::kLogStall;
        }

        $self->_calcNextPos ($prtcl);
		#---------------------------------------------------------------
		$prtcl;
	} @{ $self->{ prtcls } };
}
#=======================================================================
1;

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

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

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
threshold.

=item * kLogIter

Shows the current iteration number.

=item * kLogDetail

Shows additional details for some of the other logging options.



( run in 1.018 second using v1.01-cache-2.11-cpan-de7293f3b23 )