Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created March 16, 2009 13:32
Show Gist options
  • Save btbytes/79877 to your computer and use it in GitHub Desktop.
Save btbytes/79877 to your computer and use it in GitHub Desktop.

Revisions

  1. btbytes revised this gist Mar 24, 2009. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pso.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    Pradeep Gowda 2009-03-16
    '''

    from numpy import *
    from numpy import array
    from random import random
    from math import sin, sqrt

    @@ -24,6 +24,7 @@ class Particle:


    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])))) * \
  2. btbytes revised this gist Mar 18, 2009. 2 changed files with 5 additions and 5 deletions.
    4 changes: 2 additions & 2 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    *~
    *.pyc
    *~
    *.pyc
    6 changes: 3 additions & 3 deletions README → README.textile
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    h1. A Particle Swarm Implementation in Python


    h1. A Particle Swarm Implementation in Python
  3. btbytes revised this gist Mar 18, 2009. 2 changed files with 5 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    *~
    *.pyc
    3 changes: 3 additions & 0 deletions README
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    h1. A Particle Swarm Implementation in Python


  4. btbytes revised this gist Mar 16, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pso.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    '''
    pso.py
    A Naive version of the Particle Swarm Optimisation Algorithm.
    A simple implementation of the Particle Swarm Optimisation Algorithm.
    Uses Numpy for matrix operations.
    Pradeep Gowda 2009-03-16
    '''
  5. btbytes revised this gist Mar 16, 2009. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions pso.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    A Naive version of the Particle Swarm Optimisation Algorithm.
    Uses Numpy for matrix operations.
    Pradeep Gowda 2009-03-16
    '''

    from numpy import *
  6. btbytes revised this gist Mar 16, 2009. 1 changed file with 76 additions and 13 deletions.
    89 changes: 76 additions & 13 deletions pso.py
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,84 @@
    #/usr/bin/env python
    #!/usr/bin/env python

    '''
    A naive particle swarm implementation
    pso.py
    A Naive version of the Particle Swarm Optimisation Algorithm.
    Uses Numpy for matrix operations.
    '''

    class Swarm:
    def __init__(self, particles):
    self.particles = particles
    from numpy import *
    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:
    def __init__(self, pos, vel):
    self.pos = pos
    self.vel = vel
    pass


    def f6(param):
    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)

    def main():
    pass
    if __name__ == '__main__':
    main()
    # 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 '\nParticle Swarm Optimisation\n'
    print 'PARAMETERS\n','-'*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\n', '-'*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)
  7. btbytes created this gist Mar 16, 2009.
    21 changes: 21 additions & 0 deletions pso.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #/usr/bin/env python

    '''
    A naive particle swarm implementation
    '''

    class Swarm:
    def __init__(self, particles):
    self.particles = particles


    class Particle:
    def __init__(self, pos, vel):
    self.pos = pos
    self.vel = vel


    def main():
    pass
    if __name__ == '__main__':
    main()