Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>pso-example-java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.ltk.core.refactoring.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pso-example-java
Particle Swarm Optimization with UI
================

Particle Swarm Optimization (PSO) Sample Code using Java
Swing library is used here for UI programming.

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.
Thanks to https://github.com/therealmanalu for PSO implementation on Java.
15 changes: 15 additions & 0 deletions src/org/gandhim/pso/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gandhim.pso;

import javax.swing.JFrame;

public class Main {

public static void main(String[] args)
{
MainFrame frame1 = new MainFrame();
frame1.setSize(600, 400);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
155 changes: 155 additions & 0 deletions src/org/gandhim/pso/MainFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.gandhim.pso;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Hashtable;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicArrowButton;

public class MainFrame extends JFrame implements PSOConstants
{
private PaintPanel panel1;
private JPanel panel2;
private Hashtable labelTable1;
private JPanel container1;
private JButton button1, button2;
private JCheckBox checkbox1;
private JSlider slider1;
private static JTextArea textarea1;
private JScrollPane scroll1;

public MainFrame()
{
super("Particle Swarm Optimization");

container1 = new JPanel(new GridBagLayout());
add(container1, BorderLayout.CENTER);

panel2 = new JPanel();
add(panel2, BorderLayout.SOUTH);

button1 = new JButton("Back to the begin");
panel2.add(button1, BorderLayout.WEST);


button2 = new JButton("Forward");
panel2.add(button2, BorderLayout.WEST);

checkbox1 = new JCheckBox("Iterate automatically");
panel2.add(checkbox1, BorderLayout.CENTER);

labelTable1 = new Hashtable();
labelTable1.put(new Integer(MIN_DELAY), new JLabel(MIN_DELAY/1000f + "it/sec"));
labelTable1.put(new Integer((MIN_DELAY + MAX_DELAY)/2), new JLabel((MIN_DELAY + MAX_DELAY)/2000f + "it/sec"));
labelTable1.put(new Integer(MAX_DELAY), new JLabel(MAX_DELAY/1000f + "it/sec"));

slider1 = new JSlider(JSlider.VERTICAL, MIN_DELAY, MAX_DELAY, MIN_DELAY);
slider1.setLabelTable(labelTable1);
slider1.setPaintLabels(true);
add(slider1, BorderLayout.WEST);

textarea1 = new JTextArea(20, 22);
textarea1.setEditable(false);
textarea1.setLineWrap(true);
scroll1 = new JScrollPane(textarea1);
scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

add(scroll1, BorderLayout.EAST);

panel1 = new PaintPanel();
container1.add(panel1);

container1.addComponentListener(new ComponentListenerOne());
button1.addActionListener(new ActionListenerOne(false));
button2.addActionListener(new ActionListenerOne(true));
checkbox1.addItemListener(new ItemListenerOne());
slider1.addChangeListener(new ChangeListenerOne());
}

public static void appendText(String text)
{
textarea1.append(text + "\n");
textarea1.setCaretPosition(textarea1.getDocument().getLength());
}

class ComponentListenerOne implements ComponentListener
{

@Override
public void componentResized(ComponentEvent e) {

int w = container1.getWidth();
int h = container1.getHeight();
int size = Math.min(w, h);
panel1.setPreferredSize(new Dimension(size, size));
container1.revalidate();
}

@Override
public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub

}

@Override
public void componentShown(ComponentEvent e) {
// TODO Auto-generated method stub

}

@Override
public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub

}
}

class ActionListenerOne implements ActionListener
{
private boolean forward;

public ActionListenerOne(boolean forward)
{
super();
this.forward = forward;
}

public void actionPerformed(ActionEvent e)
{
panel1.executeProcess(this.forward);
}

}

class ItemListenerOne implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
panel1.setAutoIterate(checkbox1.isSelected());
}
}

class ChangeListenerOne implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
if(!source.getValueIsAdjusting())
{
int delay = (int)source.getValue();
panel1.setIterateTimerDelay(delay);
}
}
}
}
10 changes: 6 additions & 4 deletions src/org/gandhim/pso/PSOConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ public interface PSOConstants {
int SWARM_SIZE = 30;
int MAX_ITERATION = 100;
int PROBLEM_DIMENSION = 2;
double C1 = 2.0;
double C2 = 2.0;
double W_UPPERBOUND = 1.0;
double W_LOWERBOUND = 0.0;
double C1 = 1;
double C2 = 1;
double W_UPPERBOUND = 0.9;
double W_LOWERBOUND = 0.4;
int MIN_DELAY = 100;
int MAX_DELAY = 1000;
}
11 changes: 0 additions & 11 deletions src/org/gandhim/pso/PSODriver.java

This file was deleted.

76 changes: 56 additions & 20 deletions src/org/gandhim/pso/PSOProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,44 @@ public class PSOProcess implements PSOConstants {
private double gBest;
private Location gBestLocation;
private double[] fitnessValueList = new double[SWARM_SIZE];
private int t = 0;
private double w;
private double err = 9999;

public int getT() {
return t;
}

Random generator = new Random();

public void execute() {
initializeSwarm();
updateFitnessList();
public void execute(boolean forward) {

for(int i=0; i<SWARM_SIZE; i++) {
pBest[i] = fitnessValueList[i];
pBestLocation.add(swarm.get(i).getLocation());
if(forward && t < MAX_ITERATION)
{
t++;
}
else if(!forward && t > 0)
{
t = 0;
}
else if(t >= MAX_ITERATION)
return;

int t = 0;
double w;
double err = 9999;
if(t == 0)
{
initializeSwarm();
System.out.println("LAL");
updateFitnessList();

for(int i=0; i<SWARM_SIZE; i++) {
pBest[i] = fitnessValueList[i];
pBestLocation.add(swarm.get(i).getLocation());
}
err = 9999;
}

while(t < MAX_ITERATION && err > ProblemSet.ERR_TOLERANCE) {
if(t < MAX_ITERATION && err > ProblemSet.ERR_TOLERANCE)
{
// step 1 - update pBest
for(int i=0; i<SWARM_SIZE; i++) {
if(fitnessValueList[i] < pBest[i]) {
Expand Down Expand Up @@ -77,23 +98,26 @@ public void execute() {

err = ProblemSet.evaluate(gBestLocation) - 0; // minimizing the functions means it's getting closer to 0


MainFrame.appendText("ITERATION " + t + ": ");
MainFrame.appendText(" Best X: " + gBestLocation.getLoc()[0]);
MainFrame.appendText(" Best Y: " + gBestLocation.getLoc()[1]);
MainFrame.appendText(" Value: " + ProblemSet.evaluate(gBestLocation));

System.out.println("ITERATION " + t + ": ");
System.out.println(" Best X: " + gBestLocation.getLoc()[0]);
System.out.println(" Best Y: " + gBestLocation.getLoc()[1]);
System.out.println(" Value: " + ProblemSet.evaluate(gBestLocation));

t++;
updateFitnessList();
}

System.out.println("\nSolution found at iteration " + (t - 1) + ", the solutions is:");
System.out.println(" Best X: " + gBestLocation.getLoc()[0]);
System.out.println(" Best Y: " + gBestLocation.getLoc()[1]);
else
{
MainFrame.appendText("\nSolution found at iteration " + (t - 1) + ", the solutions is:");
MainFrame.appendText(" Best X: " + gBestLocation.getLoc()[0]);
MainFrame.appendText(" Best Y: " + gBestLocation.getLoc()[1]);
}
}

public void initializeSwarm() {
Particle p;

swarm.clear();
for(int i=0; i<SWARM_SIZE; i++) {
p = new Particle();

Expand All @@ -120,4 +144,16 @@ public void updateFitnessList() {
fitnessValueList[i] = swarm.get(i).getFitnessValue();
}
}

public Vector<Particle> getSwarm() {
return swarm;
}

public Vector<Location> getpBestLocation() {
return pBestLocation;
}

public Location getgBestLocation() {
return gBestLocation;
}
}
Loading