AI-ParticleSwarmOptimization-Pmap

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
     
        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

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    This pure Perl module is an implementation of the Particle Swarm
    Optimization technique for finding minima of hyper surfaces. It
    presents an object oriented interface that facilitates easy
    configuration of the optimization parameters and (in principle) allows
    the creation of derived classes to reimplement all aspects of the
    optimization engine (a future version will describe the replaceable
    engine components).
 
    This implementation allows communication of a local best point between
    a selected number of neighbours. It does not support a single global
    best position that is known to all particles in the swarm.
 
Methods
 
    AI::ParticleSwarmOptimization provides the following public methods.
    The parameter lists shown for the methods denote optional parameters by
    showing them in [].
 
    new (%parameters)

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

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#use AI::ParticleSwarmOptimization;
#use AI::ParticleSwarmOptimization::MCE;
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

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
 
    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

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
 
This pure Perl module is an implementation of the Particle Swarm Optimization
technique for finding minima of hyper surfaces. It presents an object oriented
interface that facilitates easy configuration of the optimization parameters and
(in principle) allows the creation of derived classes to reimplement all aspects
of the optimization engine (a future version will describe the replaceable
engine components).
 
This implementation allows communication of a local best point between a
selected number of neighbours. It does not support a single global best position
that is known to all particles in the swarm.
 
=head1 Methods
 
AI::ParticleSwarmOptimization provides the following public methods. The parameter lists shown
for the methods denote optional parameters by showing them in [].
 
=over 4
 
=item new (%parameters)

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

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 0.456 second using v1.01-cache-2.11-cpan-95122f20152 )