Learning about swarm intelligence

Swarm intelligence is inspired by the collective behavior of social animal colonies such as ants, birds, wasps, and honey bees. These animals work together to achieve a common goal.

Swarm intelligence phenomena can be found in our environment. You can see swarm intelligence in deep-sea animals, shown in the following image of a school of fish in formation that was captured by a photographer in Cabo Pulmo:

Using information from swarm intelligence studies, swarm intelligence is applied to coordinate among autonomous robots. Each robot can be described as a self-organization system. Each one negotiates with the others on how to achieve the goal.

There are various algorithms to implement swarm intelligence. I recommend you read textbooks about it. Some math and statistics are applied for implementing swarm intelligence. The following is a list of swarm intelligence types that researchers and developers apply to their problems:

  • Particle swarm optimization
  • Ant system
  • Ant colony system
  • Bees algorithm
  • Bacterial foraging optimization algorithm

The Particle Swarm Optimization (PSO) algorithm is inspired by the social foraging behavior of some animals such as the flocking behavior of birds and the schooling behavior of fish. A sample of PSO algorithm in Python can be found at https://gist.github.com/btbytes/79877. This program needs the numpy library. numpy (Numerical Python) is a package for scientific computing with Python. You computer should has installed Python. If not, you can download and install on this site, https://www.python.org. If your computer does not have numpy , you can install it by typing this command in the terminal (Linux and Mac platforms):

    $ pip install numpy  

For Windows platform, please install numpy refer to this https://www.scipy.org/install.html.

You can copy the following code into your editor. Save it as ch07_pso.py and then run it on your computer using terminal:

from numpy import array 
from random import random 
from math import sin, sqrt 
 
iter_max = 10000 
pop_size = 100 
dimensions = 2 
c1 = 2 
c2 = 2 
err_crit = 0.00001 
 
class Particle: 
    pass 
         
 
def f6(param): 
    '''Schaffer's F6 function''' 
    para = param*10 
    para = param[0:2] 
    num = (sin(sqrt((para[0] * para[0]) + (para[1] * para[1])))) *  
        (sin(sqrt((para[0] * para[0]) + (para[1] * para[1])))) - 0.5 
    denom = (1.0 + 0.001 * ((para[0] * para[0]) + (para[1] * para[1]))) *  
            (1.0 + 0.001 * ((para[0] * para[0]) + (para[1] * para[1]))) 
    f6 =  0.5 - (num/denom) 
    errorf6 = 1 - f6 
    return f6, errorf6; 
  
  
#initialize the particles 
particles = [] 
for i in range(pop_size): 
    p = Particle() 
    p.params = array([random() for i in range(dimensions)]) 
    p.fitness = 0.0 
    p.v = 0.0 
    particles.append(p) 
 
# let the first particle be the global best 
gbest = particles[0] 
err = 999999999 
while i < iter_max : 
    for p in particles: 
        fitness,err = f6(p.params) 
        if fitness > p.fitness: 
            p.fitness = fitness 
            p.best = p.params 
 
        if fitness > gbest.fitness: 
            gbest = p 
        v = p.v + c1 * random() * (p.best - p.params)  
                + c2 * random() * (gbest.params - p.params) 
        p.params = p.params + v 
           
    i  += 1 
    if err < err_crit: 
        break 
    #progress bar. '.' = 10% 
    if i % (iter_max/10) == 0: 
        print '.' 
 
print '
Particle Swarm Optimisation
'
print 'PARAMETERS ','-'*9
print 'Population size : ', pop_size
print 'Dimensions : ', dimensions
print 'Error Criterion : ', err_crit
print 'c1 : ', c1 print 'c2 : ', c2
print 'function : f6' print 'RESULTS ', '-'*7 print 'gbest fitness : ', gbest.fitness print 'gbest params : ', gbest.params print 'iterations : ', i+1 ## Uncomment to print particles for p in particles: print 'params: %s, fitness: %s, best: %s' % (p.params, p.fitness, p.best)

You can run this program by typing this command:

    $ python ch07_pso.py  

You can see a sample of my program output here:

This program will generate PSO output parameters based on input. You can see PARAMETERS value on program output.  At the end of codes, we can print all PSO particle parameter while iteration process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.66