UPDATE November 17, 2014: The full source code can now be downloaded from github: https://github.com/therealmanalu/pso-example-java
UPDATE May 03, 2012: The MegaUpload was long gone. If you still need the code, please contact me directly on my email at gandhi.mtm [at] gmail [dot] com. Thank you.
UPDATE April 23, 2011: The sample code can be downloaded from here. I hope the code will be useful for you to understand the topic more clearly.
—
Yes, I’m still coding and I’m proud of it 🙂
This post assume that the reader has already known about Particle Swarm Optimization (PSO) method, and hence I wouldn’t spare a space to explain about it. But if you’d like to ask the method’s component on the code I provide below then I’ll be gladly explain it in greater length. [This post is written by Gandhi Manalu – gandhim.wordpress.com]
In this post, I’ll describe and provide a sample code of PSO method for solving a very simple function optimization. Let say that the function to be minimized is as follow:
With the following constraints:
In order to solve this problem using PSO, we’ll need these classes:
- Position: to represent Position part of the particle
- Velocity: to represent Velocity part of the particle
- Particle: the particle itself [This post is written by Gandhi Manalu – gandhim.wordpress.com]
- SimplePSO: the main control of the program
- PSOConstants: an interface to define parameters used in the PSO
Since we’re going to solve two-variable function optimization, we’ll need to provide two-dimensional position and velocity.
For the Position we have:
package org.gandhim.pso; /** * * @author gandhim */ public class Position { private double x; private double y; public Position(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } }
For the Velocity we have:
package org.gandhim.pso; /** * * @author gandhim */ public class Velocity { private double x; private double y; public Velocity(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } }
And the Particle is as follow:[This post is written by Gandhi Manalu – gandhim.wordpress.com]
package org.gandhim.pso; /** * * @author gandhim */ public class Particle { private Position location; private Velocity velocity; private double fitness; public double getFitness() { calculateFitness(); return fitness; } public void calculateFitness() { double x = this.location.getX(); double y = this.location.getY(); fitness = Math.pow((2.8125 - x + x * Math.pow(y, 4)), 2) + Math.pow((2.25 - x + x * Math.pow(y, 2)), 2) + Math.pow((1.5 - x + x * y), 2); } public Position getLocation() { return location; } public void setLocation(Position location) { this.location = location; } public Velocity getVelocity() { return velocity; } public void setVelocity(Velocity velocity) { this.velocity = velocity; } }
Pay attention to the calculateFitness() method, it is where we put the function evaluation.
Now, we’re ready for the main process of the PSO. In this class, we’ll need several methods:
- initializeSwarm() – to initialize the swarm used in the method
- execute() – the main part of the process[This post is written by Gandhi Manalu – gandhim.wordpress.com]
Here’s the code (partial code):
private void initializeSwarm() { Particle p; Random generator = new Random(); for (int i = 0; i < SWARM_SIZE; i++) { p = new Particle(); double posX = generator.nextDouble() * 3.0 + 1.0; double posY = generator.nextDouble() * 2.0 - 1.0; p.setLocation(new Position(posX, posY)); double velX = generator.nextDouble() * 2.0 - 1.0; double velY = generator.nextDouble() * 2.0 - 1.0; p.setVelocity(new Velocity(velX, velY)); swarm.add(p); } } public void execute() { Random generator = new Random(); initializeSwarm(); evolutionaryStateEstimation(); int t = 0; double w; while (t < MAX_ITERATION) { // calculate corresponding f(i,t) corresponding to location x(i,t) calculateAllFitness(); // update pBest if (t == 0) { for (int i = 0; i < SWARM_SIZE; i++) { pBest[i] = fitnessList[i]; pBestLoc.add(swarm.get(i).getLocation()); } } else { for (int i = 0; i < SWARM_SIZE; i++) { if (fitnessList[i] < pBest[i]) { pBest[i] = fitnessList[i]; pBestLoc.set(i, swarm.get(i).getLocation()); } } } int bestIndex = getBestParticle(); // update gBest if (t == 0 || fitnessList[bestIndex] < gBest) { gBest = fitnessList[bestIndex]; gBestLoc = swarm.get(bestIndex).getLocation(); } w = W_UP - (((double) t) / MAX_ITERATION) * (W_UP - W_LO); for (int i = 0; i < SWARM_SIZE; i++) { // update particle Velocity double r1 = generator.nextDouble(); double r2 = generator.nextDouble(); double lx = swarm.get(i).getLocation().getX(); double ly = swarm.get(i).getLocation().getY(); double vx = swarm.get(i).getVelocity().getX(); double vy = swarm.get(i).getVelocity().getY(); double pBestX = pBestLoc.get(i).getX(); double pBestY = pBestLoc.get(i).getY(); double gBestX = gBestLoc.getX(); double gBestY = gBestLoc.getY(); double newVelX = (w * vx) + (r1 * C1) * (pBestX - lx) + (r2 * C2) * (gBestX - lx); double newVelY = (w * vy) + (r1 * C1) * (pBestY - ly) + (r2 * C2) * (gBestY - ly); swarm.get(i).setVelocity(new Velocity(newVelX, newVelY)); // update particle Location double newPosX = lx + newVelX; double newPosY = ly + newVelY; swarm.get(i).setLocation(new Position(newPosX, newPosY)); } t++; } }
And the last is the interface for storing the constants:
package org.gandhim.pso; /** * * @author gandhim */ public interface PSOConstants { int SWARM_SIZE = 30; int DIMENSION = 2; int MAX_ITERATION = 300; double C1 = 2.0; double C2 = 2.0; double W_UP = 1.0; double W_LO = 0.0; }
I made the interface just for the sake of flexibility.
A sample of running the program is as follow:
As we can see from the result, the program found the solution of the problem for (x=3.0 and y=0.5). You might have noticed that the program is not optimized yet, for example it could have been stopped when it already found the solution.[This post is written by Gandhi Manalu – gandhim.wordpress.com]
Actually, there are other PSO’s components that have not been implemented in this program, for example constraints handling. But since the problem we solved here is a very simple one it doesn’t really need the constraint handling.
I hope that this post is useful for you.
hai gandhi
I m naveena lakshmi from Malaysia.I m doing my Ph.d on pso for software testing.
I m new for this PSO so i want to know how to find best solution for my problem .
If u send ur mail id for my mail id i can send my problem through attach files.
anapa.naveena@gmail.com this is my id.
Expecting ur positive reply sooon
Thank u
it is a good example to study, could you send your entire code to me ?
my email is wzj008@gmail.com. many thanks to you.
hi! it’s possible to have all source code?
fabio.
Hello,could you send me a copy of code by e-mail because the program are short of some code. My e-mail is menghua523@163.com . Thank you very much!
Hello!
Would you mind sending me the entire code please? I’m developing an article and it would be very helpfull to me. Thanks in advance!
my e-mail is: guilhermejf1987@gmail.com
hi….
i need to calculate optimize cost…..
can u plz help me
Hi this coding is very interesting, May i have full coding please. pprabhu70@gmail.com
HI GANDHI,
REALLY A INTERESTING POST.
I WANTED TO KNOW CAN THIS BE USED FOR THE OPTIMIZATION OF NEURAL NETWORK…CAN U HELP PLEASE.
MY EMAILID PRASHANT.TALKIN@GMAIL.COM
MANY THANKS
PRASHANT
Hi Gandhi,I Found your work to be really great and appreciable.I myself is working on PSO right now.SO,if you send the entire code,it would be of great help to me.
Thanks in Advance!!!!!!
Hello!
I’m working on a project with pso in python. pls would u mind sending me entire code? so i can use it as a guide. i really would be grateful. Thanks!
My email is: johnnysblinkin@yahoo.com
hi,
i am doing final year project in PSO for job scheduling. i want this full coding. plz send this code to my mail id. my mail id is raviabirami23@gmail.com.
Hey,
Your code is truly Fantastic. Could you please send it to my email id archana.shiva@gmail.com. I am sure it would be of real help to my studies.
Thank you very much….
Dear Gandhi:
would you mind sending me a copy of the code?
I am developing a controller in a cloud environment.
my email is : mina_sedaghatir@yahoo.com
thanks so much
mina
Can you please update the link to your code.
Thanks for your help!
Hello~~
The download link has been loss!
It is a good example to study, could you send your entire code to me ?
my email is coman192001@gmail.com. many thanks to you.
Would you mind sending me the entire code please? it would be very useful for my thesis about PSO on Nanobot positioning… can u help me please!!! Thanks
my email: valentina.mannara@gmail.com
Hello!
Would you mind sending me the entire code please? Thanks in advance!
my e-mail is: h_rabie2004@yahoo.com
hi …!
i’am happy if you can send me the entire code !I think this code would be helpfull to me ! Thanks a lot !
My e-mail :ngongocquang90@gmail.com
Hello!
Great post my friend, I’ve just begun learning about PSO and it has been great help for me. Unfortunately the link you gave for source code doesn’t work for me. Would you be so kind and send it to me?
My email is: eldae25@gmail.com
Thanks in advance 🙂
eldae
No need anymore, I figured it out myself 😉
hello!
Good day! Can you send me the copy of the whole source code of the PSO -sample. Through this email Sandy_1663@yahoo.com. This will greatly help our thesis. Thank you so much. And God Bless
Hi
Thanks for the nice write up
Could you please update the link for downloading the code.
Hi Gandhi..I am a student, actually I doing a benchmark beetween differents no linear functions, but I am mathematical, so the development is something new for me, but is very interesting know how we can levarage this type of knowledge and use this computational approach inside of my school. Therefore I would like to have all the code that you did to work whit this in my project. If you can send me it, I am aprecciate. jorge.valderrama.romero@alumnos.upm.es
Dear all, I’m sorry for this very late reply. If you need the program, you could just contact me on my email at gandhi.mtm [at] gmail [dot] com. Thank you.
Dear Gandhi:
Would you mind sending me a copy of the code?
Thanks for your help!
My email i:dlnx38@hotmail.com
hi,
i am doing final year project in PSO for image retrieval. i want this full coding. plz send this code to my mail id. my mail id is arifa.tehseen@gmail.com.
Hi just send to my email written on top of this article.
I find the article interesting aand would be happy if Gandhi sends me the full code. Thanks.
Pretty much all of the response you have is people asking for the full code. Why not just post it here or make a link to dropbox?
Please can you send to me the whole code of PSO… mi=y mail id is
etude.informatique@yahoo.fr
hi Manalu, nice article and very detailed explanation. Would you mind sending full code to landoulsi_marwen@hotmail.com.
Thank in advance
Good day,
Link to the source code doesn’t work. I would ask if you could please email me the code @ onlinelogin@webmail.co.za but if you fixed the link it would probably save yourself a lot of time and effort.
Thanks for the great post! It has helped me understand the layout and structure of a PSO much better.
can you please send me the code,…. i will be greatful to you
at rabiasultan91@gmail.com
Hai , I need to find the maximized optimal solution. Could you send me the code. to vikiraju@gmail.com
hi, i need to solve the problem with PSO.
send me please the sample code to inceptionslap@gmail.com
Regards,
Thanks 🙂
can you send me the code?,…. thanks
akram.saadat.91@gmail.com
Can you send me a full source? cayocruz@gmail.com. Thank you!
Hi,
Your code is very helpful! Would you be able to e-mail me the whole code? It would be very helpful. Thank you! kaylamelonie@yahoo.com
Hello,could you please send me a copy of code by e-mail because the program are short of some code. My e-mail is moaath_msh@hotmail.com . Thank you very much!
hello
could you please send a full code of standard pso to my email : moaath_msh@hotmail.com
Can you please send me the code, please? I will be greatful to you, my e-mail is: andyrociogtz@gmail.com
very good your code, Thanks, Could you send me whole of your code for me please. my email is ghonji.majid@yahoo.com
Hi,
Your code is very helpful! Would you be able to e-mail me the whole code? It would be very helpful. Thank you! mauroxamperi@espiv.net.
Can you please, send your code for me, please? e-mail: matiasromariop2@gmail.com
this article helps me a lot. can you send me the code to mike_x_@hotmail.com? thanks a lot!
Your code seems really helpful. Could you please send me the full source code? I would appreciate it very much. My email is karanikasstelios@gmail.com . Thank you
Hi,
Can you please send me the code?
my e-mail is: touilmonia@gmail.com
hai this is kumar i am doing my m.tech project on pso for the project your code is very helpful to me so can u plz mail me your hole code to me. my mai id is kkumarbabu07@gmail.com
Can you please send me the code, please? I will be greatful to you, my e-mail is: protinozec@gmail.com
Thank you.
Hello Mr. Gandhi, Can you send me all the source code related to PSO as I have to to my M.Tech Research Work on that
My Mail Id is er.akhil.goyal@gmail.com
Can you please send me the code?
my e-mail boncelsaputra@gmail.com
Could you send me the code. Thank you.
e-mail: teraztak@wp.pl
Hello!
This article is really helpful.Can you please send me the entire code?
my-email is : both9731@gmail.com
Thank you.
Hello,could you please send me a copy of code by e-mail because the program are short of some code. My e-mail is zaid436@gmail.com . Thank you very much!
can we perform clustering using PSO
can you please mail your code @ roshanforyou111@gmail.com
can u send source code of pso to mail id :akhil.edara85@gmail.com
Hi Gandhi,
First of all many thanks for your comment, it´s very helpful and clear, but just wanted to remind you something you probably know without acritude; it´s a bad practice to have constants in an interface, it´s better to have them in the class itself
Take care buddy, and thanks again
Buddy, could you please send me the full code at stackman444@gmail.com ? I´d really appreciate that, thanks again my friend, very useful post
Hi,
Can you please send me the code of PSO ?
my e-mail is: boufar_nassima@yahoo.fr
Hi,
Can you please send me the code?
my e-mail is: touilmonia@gmail.com
Hi,
Can you please send me the code?
my e-mail is: zazaboza80@gmail.com
an you please send me the code, please? I will be greatful to you, my e-mail is: mwonkoye91@gmail.com
Thank you.
hello can you please send the full code of PSO to my email plz…my boss alloted this topic to me and force me to attend for conferrence on this topic…i have no time ….thanqq in adavance..:)
hi, the file called SimplePSO is missing. could you provide it? thanks
Hi,
the file called SimplePSO is missing, could you provide it?
thanks
Can you please send me the code to my e-mail is: nehapud.np@gmail.com
Thank you.
Hi, I am doing a project on PSO optimization, so can I have the full code for better understanding. I will be very grateful to you, my email id is: er.vishu97@gmail.com
Thank you
Hi I am doing a project on PSO, so if you could send me the complete code for better understanding. It will be very grateful of you. My email id is er.vishu97@gmail.com
Thank you
thanx a million times, it is so helpful. is it possible to post sample code for ant colony optimization? i will appreciate a lot
hello please can i get a full copy of this code for a research work ? my email is abolajiadetonaodukoya@gmail.com
Hi Mr. Gandhi,
I really appreciate the work you have done to illustrate the whole problem and developing a code for helping hundreds of people. As I am seeing most people expecting you the help too. I am also one of them. I really appreciate your help if you can take a look or send me mail. skumar.srcem@gmail.com
Hello sir ,
Can you tell me how to implement pso for weight optimization of gear pair.I cant understand how to handle constraints in pso.