AI-ParticleSwarmOptimization-MCE
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
-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
#-----------------------------------------------------------------------
__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 } = [ ];
#-------------------------------------------------------------------
view all matches for this distributionview release on metacpan - search on metacpan
( run in 1.120 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )