-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvanillaPSO.jl
More file actions
102 lines (80 loc) · 3.79 KB
/
vanillaPSO.jl
File metadata and controls
102 lines (80 loc) · 3.79 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
## Vanilla Partile Swarm Optimization Algorithm
## Author: Quazi Irfan
using Plots,Random, Statistics, Colors, Distributions
Random.seed!(123)
# setup
# function f(x::AbstractFloat)::AbstractFloat
# return x^2
# #return (2-x)^3 + x^2 # https://www.desmos.com/calculator/k5pqd1ydhb
# end
## User function
# f(x)::Float64 = x^2;
f(x)::Float64 = x^4 -8x^2 + 2x;
# f(x)::Float64 = cos(x);
x_min = -4;
x_max = 4;
numOfParticles = 10;
numOfIteration = 25;
## Initilization
w = 1.5;
scale1 = 1;
scale2 = 2;
# Draw the user function
x_interval = (x_max - x_min)/100;
x = [x_min:x_interval:x_max;];
plot(x, f.(x),title = "Iteration 0", label="User function")
# generate particles
particlePosOffset = (x_max - x_min) * .05; # so particle do not appear at the edge, only for visualization
particlePos = collect(range(x_min+particlePosOffset, x_max-particlePosOffset, length=numOfParticles)) #.+ rand(-1:0.01:1, numOfParticles);
particleVel = zeros(Float64, numOfParticles);
#particleVel = rand(-1:0.001:1, numOfParticles); #set random velocity
# draw the initial particles
# Source: http://juliagraphics.github.io/Colors.jl/stable/colormapsandcolorscales/#Color-interpolation
# color_names link: https://github.com/JuliaGraphics/Colors.jl/blob/master/src/names_data.jl
tempColor = mc=range(HSL(colorant"red"), stop=HSL(colorant"green"), length=numOfParticles);
scatter!(particlePos, f.(particlePos), mc=tempColor, label="")
plot!([particlePos particlePos.+particleVel]', [f.(particlePos) f.(particlePos)]', color="black", label="")
savefig("C:\\Users\\iamcr\\Desktop\\GSoC\\2021\\Julia\\Plots\\plot0.png")
# if pos < 0 then add c*2pi, if pos > 2pi then subtract c*2pi
function periodicClamp(x, x_min, x_max)
periodLength = x_max - x_min
while x < x_min
x += periodLength
end
while x > x_max
x -= periodLength
end
return x
end;
## Optimization Step
# at the beginning best position of a particle is its current position
particleBestPos = particlePos;
# initialize an empty array to save the cost change
absAvgCostDiffHistory = zeros(numOfIteration);
for i = 1:numOfIteration
swarmBestPos = particlePos[argmin(f.(particlePos))]; #find the particle pos with lowest cost
m = particleVel;
stochastic1 = rand(Uniform(), 1);
stochastic2 = rand(Uniform(), 1);
p = (stochastic1 * scale1) .* (particleBestPos - particlePos);
s = (stochastic2 * scale2) .* (swarmBestPos .- particlePos);
particleVel = m .+ p .+ s;
particleVel = clamp.(particleVel, -.25, .25)
particlePos = particlePos .+ particleVel.*1; # considering unit time
particlePos = periodicClamp.(particlePos, x_min, x_max);
# redraw the new plots with updated particle position
plot(x, f.(x), title = string("Iteration ", i), label="User function")
# plot(x, f.(x), label="User function")
scatter!(particlePos, f.(particlePos), color=tempColor, label="")
plot!([particlePos particlePos.+particleVel]', [f.(particlePos) f.(particlePos)]', color="black", label="")
savefig("C:\\Users\\iamcr\\Desktop\\GSoC\\2021\\Julia\\Plots\\plot" * string(i) * ".png")
plot!([particlePos particlePos.+particleVel * 2]', [f.(particlePos) f.(particlePos)]', color="black", label="")
# calculate the improvement before updating candidate solution
absAvgCostDiffHistory[i] = abs(mean(particleBestPos - particlePos));
# Before going to a new iteration check if the new position are better than the older ones
mask = f.(particlePos) .<= f.(particleBestPos);
particleBestPos = (.!mask .* particleBestPos) .+ (mask .* particlePos);
end
plot(absAvgCostDiffHistory, title="Cost history", xlabel="Num of Iteration", ylabel = "Average change in position", label="")
savefig("C:\\Users\\iamcr\\Desktop\\GSoC\\2021\\Julia\\Plots\\CostHistory.png")
#vanillaPSO(x -> x^2, -10.0, 10.0)