Pages

Particle Swarm Optimization (PSO) running on CellPhones (Symbian + 5800Xpress)

Tuesday, September 29, 2009


Hi all,

Have you ever imagined running optimization algorithms like genetic algorithms or particle swarm optimization on portable devices like cellphones ?

So, let me say that it's possible! First of all, one excellent work on running genetic algorithms on devices like Sony PSP and Symbian Phones was done by Christian Perone, author of the PyEvolve Framework (Python Genetic Algorithms Framework) written all in Python!

His work inspired me to port my old undergraduate project, the particle swarm optimization algorithm implementation in Java to Python! I decided to develop it from scratch and now it's almost complete for its first official release: The PyPSO Toolbox. You can take a look at the project and the source at its official home page repository.

Soon, it will be official available! Until there, i am doing some tests with the framework, correcting some bugs, documenting the source code, etc. But i was wondering here if it's possible to run also the PyPSO framework in the cellphone, like the Symbian Nokia 5800 XpressMusic.

Using the new version of the PyS60, the release 1.9.7, which comes with the new 2.5.1 Python core, I've executed the PyPSO toolbox without problems, and i was very amazed by the good performance of the PSO on the Nokia 5800, the problem that i ran was the minimization of one of the De Jogn's test suite functions, the Sphere function. The function is very simple as you can see its plot below at 3-D chart:

I've used particles with 5 dimmensions each with real values between the interval of [-5.12, 5.12], the Constricted Factor and the Global Topology of the PyPSO framework. Also i've set a population size of 30 particles.

After 36 iterations (about 12 seconds), the PSO execution ended with the best fitness of 0.0, representing the optimal solution (the minimum) of the De Jong's Sphere function.

Follow some screenshots of the simulation (click on the pictures to enlarge):





How to install PyPSO on the PyS60 ?

1) First, you need to install the PyS60 runtime for your Symbian platform. You can see more information about how to install it here.

2) Then, you must create a directory on your Memory Card or Phone Memory (Depends on where you installed the Python Script Shell), inside the "Python" directory, named "lib" , and inside this directory, you copy the "pypso" folder. The absolute folder structure will be like this:

C:\Python\lib\pypso or E:\Python\lib\pypso

Some features of the framework will not work, like some report Adapters, however, the PSO core is working really great. I've used the PyPSO subversion release 0.11. Since it's not finished yet, you can download from the subversion repository from here.

Here is the source code I've used to minimize the De Jong's Sphere function (Special thanks to Christian Perone who inspired me to do this work) :


"""
PyPsoS60Demo.py
Author: Marcel Pinheiro Caraciolo
caraciol@gmail.com
License: GPL3
"""
BLUE = 0x0000ff
RED = 0xff0000
BLACK = 0x000000
WHITE = 0xffffff


import e32, graphics, appuifw
print "Loading PyPSO modules... ",
e32.ao_yield()

from pypso import Particle1D
from pypso import GlobalTopology
from pypso import Pso
from pypso import Consts

img = None
coords = []
graphMode = False
canvas = None
w = h = 0
print " done !"
e32.ao_yield()

def handle_redraw(rect):
if img is not None:
canvas.blit(img)


def sphere(particle):
total = 0.0
for value in particle.position:
total += (value ** 2.0)

return total

def pso_callback(pso_engine):
it = pso_engine.getCurrentStep()
best = pso_engine.bestParticle()
if graphMode:
img.clear(BLACK)
img.text((5, 15), u"Iteration %d - Best Fitness: %.2f" % (it,best.ownBestFitness), WHITE, font=('normal', 14, appuifw.STYLE_BOLD))
img.line((int(w/2),0,int(w/2),h),WHITE)
img.line((0,int(h/2),w,int(h/2)),WHITE)

cx = int(w/2) / int(best.getParam("rangePosmax"))
cy = int(-h/2) / int(best.getParam("rangePosmax"))

for particle in pso_engine.topology:
img.point([int(cx*particle.position[0]+ (w/2)),int(cy*particle.position[1]+(h/2)),5,5],BLUE,width=4)
img.point([int(cx*best.ownBestPosition[0]+ (w/2)),int(cy*best.ownBestPosition[1]+(h/2)),5,5],RED,width=4)
handle_redraw(())
else:
print "Iteration %d - Best Fitness: %.2f" % (it, best.ownBestFitness)
e32.ao_yield()

return False


def showBestParticle(best):
global graphMode

if graphMode:
img.clear(BLACK)
img.text((5, 15), u"Best particle fitness: %.2f" % best.ownBestFitness, WHITE, font=('normal', 14, appuifw.STYLE_BOLD))
img.line((int(w/2),0,int(w/2),h),WHITE)
img.line((0,int(h/2),w,int(h/2)),WHITE)
cx = int(w/2) / int(best.getParam("rangePosmax"))
cy = int(-h/2) / int(best.getParam("rangePosmax"))
img.point([int(cx*best.ownBestPosition[0]+ (w/2)),int(cy*best.ownBestPosition[1]+(h/2))],RED,width=7)
handle_redraw(())
e32.ao_sleep(4)
else:
print "\nBest particle fitness: %.2f" % (best.ownBestFitness,)


if __name__ == "__main__":
global graphMode, canvas, w, h

data = appuifw.query(u"Do you want to run on graphics mode ?", "query")

graphMode = data or False

if graphMode:
canvas = appuifw.Canvas(redraw_callback=handle_redraw)
appuifw.app.body = canvas
appuifw.app.screen = 'full'
appuifw.app.orientation = 'landscape'
w,h = canvas.size
img = graphics.Image.new((w,h))
img.clear(BLACK)
handle_redraw(())
e32.ao_yield()

#Parameters
dimmensions = 5
swarm_size = 30
timeSteps = 100

particleRep = Particle1D.Particle1D(dimmensions)
particleRep.setParams(rangePosmin=-5.12, rangePosmax=5.13, rangeVelmin=-5.12, rangeVelmax=5.13, bestFitness= 0.0 ,roundDecimal=2)
particleRep.evaluator.set(sphere)

topology = GlobalTopology.GlobalTopology(particleRep)
pso = Pso.SimplePSO(topology)
pso.setTimeSteps(timeSteps)
pso.setPsoType(Consts.psoType["CONSTRICTED"])
pso.terminationCriteria.set(Pso.FitnessScoreCriteria)
pso.stepCallback.set(pso_callback)
pso.setSwarmSize(swarm_size)

pso.execute()

best = pso.bestParticle()
showBestParticle(best)

To make things more interesting, i've decided to put a simple graphic mode, when the user can choose to see the simulation in graphics mode or console mode. So as soon as you start the PyPsoS60Demo.py script, a dialog will show up to ask which mode the user prefer to see the simulation. You can see the video of the graphics mode running below.




Each point is a particle and they're updating their position through the simulation and since the best optimal solution of the problem is the point (0,0), in the end of the simulation, the particles will be close to the best solution. You can see that the red point is the best particle of all swarm.

You can note at the source code the use of the module "e32" of the PyS60, this is used to process pending events, so we can follow the statistics of the current iteration while it evolves.

I hope you enjoyed this work, the next step is to port the Travelling Salesman Problem to cellphone!

You can download the script above here.

Marcel Pinheiro Caraciolo

5 comments:

  1. Hi,Amazing post!
    I see that you are interested about Particle swarm optimization.
    I found one great book about it here: http://www.intechopen.com/books/show/title/particle_swarm_optimization
    It is free to download!!
    Some chapters are: Swarm Intelligence Applications in Electric Machines, Search Performance Improvement for PSO in High Dimensional Space, Swarm Intelligence in Portfolio Selection etc.
    Hope you will enjoy it!

    ReplyDelete
  2. WIZTECH Automation, Anna Nagar, Chennai, has earned reputation offering the best automation training in Chennai in the field of industrial automation. Flexible timings, hands-on-experience, 100% practical. The candidates are given enhanced job oriented practical training in all major brands of PLCs (AB, Keyence, ABB, GE-FANUC, OMRON, DELTA, SIEMENS, MITSUBISHI, SCHNEIDER, and MESSUNG)

    PLC training in chennai
    Automation training in chennai
    Best plc training in chennai
    PLC SCADA training in chennai
    Process automation training in chennai
    Final year eee projects in chennai
    VLSI training in chennai

    ReplyDelete
  3. Embedded system training: Wiztech Automation Provides Excellent training in embedded system training in Chennai - IEEE Projects - Mechanical projects in Chennai Wiztech provide 100% practical training, Individual focus, Free Accommodation, Placement for top companies. The study also includes standard microcontrollers such as Intel 8051, PIC, AVR, ARM, ARMCotex, Arduino etc.

    Embedded system training in chennai
    Embedded Course training in chennai
    Matlab training in chennai
    Android training in chennai
    LabVIEW training in chennai
    Arduino training in chennai
    Robotics training in chennai
    Oracle training in chennai
    Final year projects in chennai
    Mechanical projects in chennai
    ece projects in chennai

    ReplyDelete
  4. Nice Blog , This is what I exactly Looking for , Keep sharing more blog .


    Spark Training Academy Chennai

    ReplyDelete