Академический Документы
Профессиональный Документы
Культура Документы
In order to solve this problem using PSO, well need these classes:
Since were going to solve two-variable function optimization, well need to provide two-dimensional position
and velocity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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, were ready for the main process of the PSO. In this class, well need several methods:
execute() the main part of the process[This post is written by Gandhi Manalu
gandhim.wordpress.com]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
}
}
}
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++;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
}
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 PSOs 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 doesnt really need the
constraint handling.