-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.m
251 lines (230 loc) · 10.1 KB
/
Environment.m
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
classdef Environment < handle
properties
%position limit [min,max] for x,y and z
POSITION_LIMIT = [0,100]
%Adjustable Parameter
%==============================================
%Termination Condition
MAX_ITERATION = 200
GOAL_THRESHOLD = 1
GOAL_RADIUS = 5
%swarm parameter
SWARM_SIZE = 10
CONSTANTS = [0.5 0.5 0.5 1]
%force field size
ZONE = [1 2 5]
%==============================================
withPlot = true
algoType = ''
goal = zeros(1,3)
iteration = 1
particles = []
globalBestPosition = zeros(1,3)
globalBestPositionHistory = []
collisionCounter = 0
timeTaken = 0
isGoal = false
end
methods
function position = randInitialPosition(this)
position = this.POSITION_LIMIT(1) + ...
Utility.randRange(this.POSITION_LIMIT(1),this.POSITION_LIMIT(2));
end
function isGoal = checkGoal(this)
inGoalCounter = 0;
for particle = this.particles
if Utility.calculateDistance(this.goal, particle.position) <= this.GOAL_RADIUS
inGoalCounter = inGoalCounter + 1;
end
end
if inGoalCounter >= ceil(this.SWARM_SIZE * this.GOAL_THRESHOLD)
isGoal = true;
else
isGoal = false;
end
end
function hasChange = checkChange(this)
num = length(this.particles);
prevPosition = zeros(1,num);
currPosition = zeros(1,num);
for i = 1:num
prevPosition(i) = Utility.getValueByPrecision(this.particles(i).positionHistory(end-1),1);
currPosition(i) = Utility.getValueByPrecision(this.particles(i).positionHistory(end),1);
end
if prevPosition == currPosition
hasChange = false;
else
hasChange = true;
end
end
function force = calculateForce(this,d)
d(d==0) = Utility.randomVector * 0.1;
dAbsolute = abs(d);
dRepulsion = d(dAbsolute < this.ZONE(1));
dAttraction = d(dAbsolute > this.ZONE(2) & dAbsolute < this.ZONE(3));
if strcmp(this.algoType,"PSO")
%PSO
force = 0;
elseif strcmp(this.algoType,"NPSO")
%NPSO
%AIC 1
% if ~isempty(dRepulsion)
% dAbsolute = abs(dRepulsion);
% distance = min(dAbsolute);
% index = find(dAbsolute==distance,1);
% force = dRepulsion(index);
% elseif ~isempty(dAttraction)
% dAbsolute = abs(dAttraction);
% distance = min(dAbsolute);
% index = find(dAbsolute==distance,1);
% force = -dAttraction(index);
% else
% force = 0;
% end
%===========================================================
%AIC 2
% if ~isempty(dRepulsion)
% dAbsolute = abs(dRepulsion);
% distance = min(dAbsolute);
% index = find(dAbsolute==distance,1);
% force = 0.1 / dRepulsion(index) * 0.1 * this.ZONE(1);
% elseif ~isempty(dAttraction)
% dAbsolute = abs(dAttraction);
% distance = min(dAbsolute);
% index = find(dAbsolute==distance,1);
% force = -dAttraction(index);
% else
% force = 0;
% end
%===========================================================
%AIC 3
if ~isempty(dRepulsion)
dAbsolute = abs(dRepulsion);
distance = min(dAbsolute);
index = find(dAbsolute==distance,1);
dValue = dRepulsion(index);
vector = dValue/abs(dValue);
force = (this.ZONE(1) - abs(dValue)) * vector;
elseif ~isempty(dAttraction)
dAbsolute = abs(dAttraction);
distance = min(dAbsolute);
index = find(dAbsolute==distance,1);
dValue = dAttraction(index);
vector = dValue/abs(dValue);
force = -(abs(dValue) - this.ZONE(1)) * vector;
else
force = 0;
end
else
%FFPSO
if ~isempty(dRepulsion)
dAbsolute = abs(dRepulsion);
distance = min(dAbsolute);
index = find(dAbsolute==distance,1);
dValue = dRepulsion(index);
force = dValue/abs(dValue) * (this.ZONE(1) - abs(dValue));
else
force = 0;
end
end
end
function initialise(this)
this.goal = [this.randInitialPosition this.randInitialPosition this.randInitialPosition];
for i = 1:this.SWARM_SIZE
initialPosition = [this.randInitialPosition this.randInitialPosition this.randInitialPosition];
particle = Particle(initialPosition);
this.particles = [this.particles particle];
end
end
function plot(this, xPositions, yPositions, zPositions)
clf
%Graph Plotting
scatter3(xPositions, yPositions, zPositions, 'b.');
hold on
scatter3(this.goal(1),this.goal(2), this.goal(3),'g.')
[x,y,z] = sphere;
% Scale to desire radius.
x = x * this.GOAL_RADIUS + this.goal(1);
y = y * this.GOAL_RADIUS + this.goal(2);
z = z * this.GOAL_RADIUS + this.goal(3);
surf(x,y,z,'EdgeColor', 'green','FaceColor','g','LineStyle','none', 'FaceAlpha',0.1)
grid on
axis(repmat(this.POSITION_LIMIT,1,3));
view(42,24)
title(sprintf('Iteration %d',this.iteration))
xlabel('x')
ylabel('y')
zlabel('z')
hold off
pause(0.1)
end
function beginSimulation(this)
xPositions = zeros(1,length(this.particles));
yPositions = zeros(1,length(this.particles));
zPositions = zeros(1,length(this.particles));
if this.withPlot
figure(1)
end
hasChange = true;
while this.iteration <= this.MAX_ITERATION && ~this.isGoal && hasChange
%update personal and global best position
for i = 1:length(this.particles)
particle = this.particles(i);
xPositions(i) = particle.position(1);
yPositions(i) = particle.position(2);
zPositions(i) = particle.position(3);
particle.updatePersonalBest(this.goal)
this.globalBestPosition = Utility.selectBestPos(this.globalBestPosition, particle.position, this.goal);
end
this.globalBestPositionHistory = [this.globalBestPositionHistory;this.globalBestPosition];
if this.withPlot
this.plot(xPositions, yPositions, zPositions);
end
%Calculate force
force = zeros(1,3);
for particle = this.particles
otherParticles = this.particles(this.particles ~= particle);
d_x = zeros(1,length(otherParticles));
d_y = zeros(1,length(otherParticles));
d_z = zeros(1,length(otherParticles));
for i = 1:length(otherParticles)
d_x(i) = particle.position(1) - otherParticles(i).position(1);
d_y(i) = particle.position(2) - otherParticles(i).position(2);
d_z(i) = particle.position(3) - otherParticles(i).position(3);
end
force = [this.calculateForce(d_x),this.calculateForce(d_y),this.calculateForce(d_z)];
particle.updateForce(force);
end
%update velocity and position
randoms = [rand() rand()];
for particle = this.particles
particle.updateVelocity(this.CONSTANTS, randoms, this.globalBestPosition)
particle.updatePosition(this.POSITION_LIMIT)
end
%Calculate collision that occured
for particle = this.particles
otherParticles = this.particles(this.particles ~= particle);
for otherParticle = otherParticles
if Utility.getValueByPrecision(particle.position,1) == Utility.getValueByPrecision(otherParticle.position,1)
this.collisionCounter = this.collisionCounter + 1;
end
end
end
this.isGoal = this.checkGoal;
hasChange = this.checkChange;
this.iteration = this.iteration + 1;
end
this.iteration = this.iteration - 1;
for i = 1:length(this.particles)
particle = this.particles(i);
xPositions(i) = particle.position(1);
yPositions(i) = particle.position(2);
zPositions(i) = particle.position(3);
end
if this.withPlot
this.plot(xPositions, yPositions, zPositions);
Analysis.plotFlyPath(this.particles, this.goal, this.GOAL_RADIUS, this.POSITION_LIMIT);
end
end
end
end