Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified for Multi-Objective #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
40 changes: 24 additions & 16 deletions PSO/Function.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package PSO;

class Function {

/**
Expand All @@ -9,8 +7,8 @@ class Function {
* @param x the x component
* @return the y component
*/
static double functionA (double x) {
return Math.pow(x, 4) - 2 * Math.pow(x, 3);
static double functionA (double[] values) {
return Math.pow(values[0], 4) - 2 * Math.pow(values[0], 3);
}

/**
Expand All @@ -21,9 +19,9 @@ static double functionA (double x) {
* @param y the y component
* @return the z component
*/
static double ackleysFunction (double x, double y) {
double p1 = -20*Math.exp(-0.2*Math.sqrt(0.5*((x*x)+(y*y))));
double p2 = Math.exp(0.5*(Math.cos(2*Math.PI*x)+Math.cos(2*Math.PI*y)));
static double ackleysFunction (double[] values) {
double p1 = -20*Math.exp(-0.2*Math.sqrt(0.5*((values[0]*values[0])+(values[1]*values[1]))));
double p2 = Math.exp(0.5*(Math.cos(2*Math.PI*values[0])+Math.cos(2*Math.PI*values[1])));
return p1 - p2 + Math.E + 20;
}

Expand All @@ -35,9 +33,9 @@ static double ackleysFunction (double x, double y) {
* @param y the y component
* @return the z component
*/
static double boothsFunction (double x, double y) {
double p1 = Math.pow(x + 2*y - 7, 2);
double p2 = Math.pow(2*x + y - 5, 2);
static double boothsFunction (double[] values) {
double p1 = Math.pow(values[0] + 2*values[1] - 7, 2);
double p2 = Math.pow(2*values[0] + values[1] - 5, 2);
return p1 + p2;
}

Expand All @@ -47,11 +45,21 @@ static double boothsFunction (double x, double y) {
* @param y the y component
* @return the z component
*/
static double threeHumpCamelFunction (double x, double y) {
double p1 = 2*x*x;
double p2 = 1.05*Math.pow(x, 4);
double p3 = Math.pow(x, 6) / 6;
return p1 - p2 + p3 + x*y + y*y;
static double threeHumpCamelFunction (double[] values) {
double p1 = 2*values[0]*values[0];
double p2 = 1.05*Math.pow(values[0], 4);
double p3 = Math.pow(values[0], 6) / 6;
return p1 - p2 + p3 + values[0]*values[1] + values[1]*values[1];
}

static double sphereFunction (double[] values) {
double sum = 0;

for (double value : values) {
sum += value * value;
}

return sum;
}

}
}
12 changes: 6 additions & 6 deletions PSO/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package PSO;

import java.util.Scanner;

public class Main {
Expand All @@ -21,21 +19,21 @@ public static void main (String[] args) {
private static void menu (boolean flag) {
Swarm swarm;
Particle.FunctionType function;
int particles, epochs;
int particles, epochs, variables;
double inertia, cognitive, social;

function = getFunction();
particles = getUserInt("Particles: ");
epochs = getUserInt("Epochs: ");
variables = getUserInt("Variables: ");

if (flag) {
inertia = getUserDouble("Inertia: ");
cognitive = getUserDouble("Cognitive: ");
social = getUserDouble("Social: ");
swarm = new Swarm(function, particles, epochs, inertia, cognitive, social);
swarm = new Swarm(function, particles, epochs, variables, inertia, cognitive, social);
} else {
swarm = new Swarm(function, particles, epochs);

swarm = new Swarm(function, particles, epochs, variables);
}

swarm.run();
Expand Down Expand Up @@ -108,6 +106,7 @@ private static void printMenu () {
System.out.println("2. Ackley's Function");
System.out.println("3. Booth's Function");
System.out.println("4. Three Hump Camel Function");
System.out.println("5. Sphere Function");
System.out.print("Function: ");
}

Expand All @@ -116,6 +115,7 @@ private static Particle.FunctionType getFunction (int input) {
else if (input == 2) return Particle.FunctionType.Ackleys;
else if (input == 3) return Particle.FunctionType.Booths;
else if (input == 4) return Particle.FunctionType.ThreeHumpCamel;
else if (input == 5) return Particle.FunctionType.Sphere;
System.out.println("Invalid Input.");
return null;
}
Expand Down
28 changes: 14 additions & 14 deletions PSO/Particle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package PSO;

import java.util.Random;

/**
Expand All @@ -18,13 +16,13 @@ class Particle {
* @param beginRange the minimum xyz values of the position (inclusive)
* @param endRange the maximum xyz values of the position (exclusive)
*/
Particle (FunctionType function, int beginRange, int endRange) {
Particle (FunctionType function, int beginRange, int endRange, int variables) {
if (beginRange >= endRange) {
throw new IllegalArgumentException("Begin range must be less than end range.");
}
this.function = function;
position = new Vector();
velocity = new Vector();
position = new Vector(variables, 0);
velocity = new Vector(variables, 0);
setRandomPosition(beginRange, endRange);
bestPosition = velocity.clone();
bestEval = eval();
Expand All @@ -36,21 +34,22 @@ class Particle {
*/
private double eval () {
if (function == FunctionType.FunctionA) {
return Function.functionA(position.getX());
return Function.functionA(position.get());
} else if (function == FunctionType.Ackleys) {
return Function.ackleysFunction(position.getX(), position.getY());
return Function.ackleysFunction(position.get());
} else if (function == FunctionType.Booths) {
return Function.boothsFunction(position.getX(), position.getY());
return Function.boothsFunction(position.get());
} else if (function == FunctionType.Sphere) {
return Function.sphereFunction(position.get());
} else {
return Function.threeHumpCamelFunction(position.getX(), position.getY());
return Function.threeHumpCamelFunction(position.get());
}
}

private void setRandomPosition (int beginRange, int endRange) {
int x = rand(beginRange, endRange);
int y = rand(beginRange, endRange);
int z = rand(beginRange, endRange);
position.set(x, y, z);
for(int i = 0; i < position.values.length; i++){
position.values[i] = rand(beginRange, endRange);
}
}

/**
Expand Down Expand Up @@ -126,7 +125,8 @@ public enum FunctionType {
FunctionA,
Ackleys,
Booths,
ThreeHumpCamel
ThreeHumpCamel,
Sphere,
}

}
30 changes: 12 additions & 18 deletions PSO/Swarm.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package PSO;

import java.util.Random;

import PSO.Particle.FunctionType;

/**
* Represents a swarm of particles from the Particle Swarm Optimization algorithm.
*/
Expand All @@ -13,10 +9,10 @@ public class Swarm {
private double inertia, cognitiveComponent, socialComponent;
private Vector bestPosition;
private double bestEval;
private FunctionType function; // The function to search.
public static final double DEFAULT_INERTIA = 0.729844;
public static final double DEFAULT_COGNITIVE = 1.496180; // Cognitive component.
public static final double DEFAULT_SOCIAL = 1.496180; // Social component.
private Particle.FunctionType function; // The function to search.
static final double DEFAULT_INERTIA = 0.729844;
static final double DEFAULT_COGNITIVE = 1.496180; // Cognitive component.
static final double DEFAULT_SOCIAL = 1.496180; // Social component.

/**
* When Particles are created they are given a random position.
Expand All @@ -32,28 +28,30 @@ public class Swarm {
* Construct the Swarm with default values.
* @param particles the number of particles to create
* @param epochs the number of generations
* @param variables the number of variables
*/
public Swarm (FunctionType function, int particles, int epochs) {
this(function, particles, epochs, DEFAULT_INERTIA, DEFAULT_COGNITIVE, DEFAULT_SOCIAL);
public Swarm (Particle.FunctionType function, int particles, int epochs, int variables) {
this(function, particles, epochs, variables, DEFAULT_INERTIA, DEFAULT_COGNITIVE, DEFAULT_SOCIAL);
}

/**
* Construct the Swarm with custom values.
* @param particles the number of particles to create
* @param epochs the number of generations
* @param variables the number of variables
* @param inertia the particles resistance to change
* @param cognitive the cognitive component or introversion of the particle
* @param social the social component or extroversion of the particle
*/
public Swarm (FunctionType function, int particles, int epochs, double inertia, double cognitive, double social) {
public Swarm (Particle.FunctionType function, int particles, int epochs, int variables, double inertia, double cognitive, double social) {
this.numOfParticles = particles;
this.epochs = epochs;
this.inertia = inertia;
this.cognitiveComponent = cognitive;
this.socialComponent = social;
this.function = function;
double infinity = Double.POSITIVE_INFINITY;
bestPosition = new Vector(infinity, infinity, infinity);
bestPosition = new Vector(variables, infinity);
bestEval = Double.POSITIVE_INFINITY;
beginRange = DEFAULT_BEGIN_RANGE;
endRange = DEFAULT_END_RANGE;
Expand Down Expand Up @@ -88,13 +86,9 @@ public void run () {
}

System.out.println("---------------------------RESULT---------------------------");
System.out.println("x = " + bestPosition.getX());
if (function != FunctionType.FunctionA) {
System.out.println("y = " + bestPosition.getY());
}
System.out.println("values = " + bestPosition.toString());
System.out.println("Final Best Evaluation: " + bestEval);
System.out.println("---------------------------COMPLETE-------------------------");

}

/**
Expand All @@ -104,7 +98,7 @@ public void run () {
private Particle[] initialize () {
Particle[] particles = new Particle[numOfParticles];
for (int i = 0; i < numOfParticles; i++) {
Particle particle = new Particle(function, beginRange, endRange);
Particle particle = new Particle(function, beginRange, endRange, bestPosition.values.length);
particles[i] = particle;
updateGlobalBest(particle);
}
Expand Down
Loading