├── README.md └── src └── org └── gandhim └── pso ├── Location.java ├── PSOConstants.java ├── PSODriver.java ├── PSOProcess.java ├── PSOUtility.java ├── Particle.java ├── ProblemSet.java └── Velocity.java /README.md: -------------------------------------------------------------------------------- 1 | pso-example-java 2 | ================ 3 | 4 | Particle Swarm Optimization (PSO) Sample Code using Java 5 | 6 | This project is a complete version of Particle Swarm Optimization with Java sample code described in http://gandhim.wordpress.com/2010/04/04/particle-swarm-optimization-pso-sample-code-using-java. 7 | -------------------------------------------------------------------------------- /src/org/gandhim/pso/Location.java: -------------------------------------------------------------------------------- 1 | package org.gandhim.pso; 2 | 3 | /* author: gandhi - gandhi.mtm [at] gmail [dot] com - Depok, Indonesia */ 4 | 5 | // bean class to represent location 6 | 7 | public class Location { 8 | // store the Location in an array to accommodate multi-dimensional problem space 9 | private double[] loc; 10 | 11 | public Location(double[] loc) { 12 | super(); 13 | this.loc = loc; 14 | } 15 | 16 | public double[] getLoc() { 17 | return loc; 18 | } 19 | 20 | public void setLoc(double[] loc) { 21 | this.loc = loc; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/org/gandhim/pso/PSOConstants.java: -------------------------------------------------------------------------------- 1 | package org.gandhim.pso; 2 | 3 | /* author: gandhi - gandhi.mtm [at] gmail [dot] com - Depok, Indonesia */ 4 | 5 | // this is an interface to keep the configuration for the PSO 6 | // you can modify the value depends on your needs 7 | 8 | public interface PSOConstants { 9 | int SWARM_SIZE = 30; 10 | int MAX_ITERATION = 100; 11 | int PROBLEM_DIMENSION = 2; 12 | double C1 = 2.0; 13 | double C2 = 2.0; 14 | double W_UPPERBOUND = 1.0; 15 | double W_LOWERBOUND = 0.0; 16 | } 17 | -------------------------------------------------------------------------------- /src/org/gandhim/pso/PSODriver.java: -------------------------------------------------------------------------------- 1 | package org.gandhim.pso; 2 | 3 | /* author: gandhi - gandhi.mtm [at] gmail [dot] com - Depok, Indonesia */ 4 | 5 | // this is a driver class to execute the PSO process 6 | 7 | public class PSODriver { 8 | public static void main(String args[]) { 9 | new PSOProcess().execute(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/org/gandhim/pso/PSOProcess.java: -------------------------------------------------------------------------------- 1 | package org.gandhim.pso; 2 | 3 | /* author: gandhi - gandhi.mtm [at] gmail [dot] com - Depok, Indonesia */ 4 | 5 | // this is the heart of the PSO program 6 | // the code is for 2-dimensional space problem 7 | // but you can easily modify it to solve higher dimensional space problem 8 | 9 | import java.util.Random; 10 | import java.util.Vector; 11 | 12 | public class PSOProcess implements PSOConstants { 13 | private Vector swarm = new Vector(); 14 | private double[] pBest = new double[SWARM_SIZE]; 15 | private Vector pBestLocation = new Vector(); 16 | private double gBest; 17 | private Location gBestLocation; 18 | private double[] fitnessValueList = new double[SWARM_SIZE]; 19 | 20 | Random generator = new Random(); 21 | 22 | public void execute() { 23 | initializeSwarm(); 24 | updateFitnessList(); 25 | 26 | for(int i=0; i ProblemSet.ERR_TOLERANCE) { 36 | // step 1 - update pBest 37 | for(int i=0; i