Repository navigation
Expand file tree
/
Copy pathfuture_search_algorithm.m
More file actions
284 lines (194 loc) · 9.03 KB
/
Copy pathfuture_search_algorithm.m
File metadata and controls
284 lines (194 loc) · 9.03 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
function position = future_search_algorithm(obj, problem)
opt = struct('max_iter', 10,...
'pop_size' , 20,...
'r_time',10,...
'disp',1,...
'problem',problem);
fsa = struct('name','FSA');
fsa = opt;
fsa = start(fsa);
if fsa.disp
disp(['FSA ' 'started ...']);
disp('Initializing population.');
end
fsa = initialize(fsa);
% Iterations (Main Loop)
for it = 1:fsa.max_iter
% Set Iteration Counter
fsa.iter = it;
% Iteration Started
fsa = iterate(fsa);
% Update Histories
fsa.position_history(it).position = fsa.best_sol.position;
fsa.nfe_history(it) = fsa.nfe;
fsa.best_obj_value_history(it) = fsa.best_sol.obj_value;
% Display Iteration Information
if fsa.disp
disp(['Iteration ' num2str(fsa.iter) ...
': Best de. Value = ' num2str(fsa.best_sol.obj_value) ...
', position = ' num2str(fsa.best_sol.position)]);
end
position =fsa.best_sol.position;
end
end
% Initialization
function this = initialize(this)
% Create Initial Population (Not Sorted)
sorted = false;
this = init_pop(this,sorted);
for ii = 1: this.pop_size
this.Lbe(ii).obj_value = this.pop(ii).obj_value
this.Lbep(ii).position = this.pop(ii).position
end
% Initial Value of Mutation Coefficient
%this.params.alpha = this.alpha;
end
% Iterations
function this = iterate(this)
% Decision Vector Size
var_size = [1 this.problem.dim];
% Create New Population
newpop = repmat(this.empty_individual, this.pop_size, 1);
for i = 1:this.pop_size
% Initialize to Worst Objective Value
newpop(i).obj_value = this.problem.worst_value;
% Calculate objective function for each search agent
this.pop(i).position=this.pop(i).position+(-this.pop(i).position+this.best_sol.position)*rand+(-this.pop(i).position+this.Lbep(i).position)*rand;
for k=1:this.problem.dim
if this.pop(i).position(k)>this.problem.upbound(k), this.pop(i).position(k)=this.problem.upbound(k); end
if this.pop(i).position(k)<this.problem.lobound(k), this.pop(i).position(k)=this.problem.lobound(k); end
end
this.pop(i).obj_value=this.problem.func(this.pop(i).position);
% Update the loacal best solution
if (this.pop(i).obj_value<=this.Lbe(i).obj_value)
this.Lbep(i).position=this.pop(i).position;
this.Lbe(i).obj_value=this.pop(i).obj_value;
end
if this.pop(i).obj_value<this.best_sol.obj_value % Change this to > for maximization problem
this.best_sol.obj_value=this.pop(i).obj_value; % Update alpha
this.best_sol.position=this.pop(i).position;
end
end
% loop of the initial update
for i=1:this.pop_size,
Si(i,:) = this.best_sol.position + (this.best_sol.position - this.Lbep(i).position).*rand;
for k=1:this.problem.dim
if Si(i,k)>this.problem.upbound(k), Si(i,k)=this.problem.upbound(k); end
if Si(i,k)<this.problem.lobound(k), Si(i,k)=this.problem.lobound(k); end
end
this.pop(i).obj_value=this.problem.func(Si(i,:));
% Update the loacal best solution
if ( this.pop(i).obj_value<=this.pop(i).obj_value)
this.pop(i).position=Si(i,:);
this.Lbep(i).position=Si(i);
end
end
end
% Reset the Algorithm
function this = start(this)
this.empty_individual.position = [];
this.empty_individual.obj_value = [];
this.empty_individual.solution = [];
this.pop= [];
this.best_sol= [];
this.iter= 0;
this.nfe= 0;
this.run_time= 0;
this.avg_eval_time= 0;
this.nfe_history= [];
this.best_obj_value_history= [];
this.must_stop= 0;
% reset
this.iter = 0;
this.nfe = 0;
this.nfe_history = nan(1, this.max_iter);
this.best_obj_value_history = nan(1, this.max_iter);
this.run_time = 0;
this.avg_eval_time = 0;
this.must_stop = false;
end
% Decode and Evaluate a Coded Solution
function [z, sol] = decode_and_eval(this, xhat)
% Increment NFE
this.nfe = this.nfe + 1;
% Decode and Evaluate
[z, sol] = this.problem.func(xhat);
end
% Evaluate a Single Solution or Population
function pop = eval(this, pop)
for i = 1:numel(pop)
pop(i).position = ypea_clip(pop(i).position, 0, 1);
[pop(i).obj_value, pop(i).solution] = this.decode_and_eval(pop(i).position);
end
end
% Create a New Individual
function ind = new_individual(this, x)
if ~exist('x', 'var') || isempty(x)
x = rand([1 this.problem.dim]);
end
ind = this.empty_individual;
ind.position = ypea_clip(x, 0, 1);
[ind.obj_value, ind.solution] = decode_and_eval(this,x);
end
% Initialize Population
function this = init_pop(this, sorted)
% Check for Sorted flag (Default is false, not sorted)
if ~exist('sorted', 'var') || isempty(sorted)
sorted = false;
end
% Initialize Population Array
this.pop = repmat(this.empty_individual, this.pop_size, 1);
% Initialize Best Solution to the Worst Possible Solution
this.best_sol = this.empty_individual;
this.best_sol.obj_value = this.problem.worst_value;
% Generate New Individuals
for i = 1:this.pop_size
% Generate New Solution
this.pop(i) = new_individual(this);
% Compare to the Best Solution Ever Found
if ~sorted && is_better(this,this.pop(i), this.best_sol)
this.best_sol = this.pop(i);
end
end
% Sort the Population if it is needed
if sorted
this.pop = sort_population(this,this.pop);
this.best_sol = this.pop(1);
end
end
% Sort Population
function [pop, sort_order, obj_values] = sort_population(this, pop)
% Sort the Objective Values Vector
[obj_values, sort_order] = sort([pop.obj_value] );
% Sort (Re-order) Population
pop = pop(sort_order);
end
% Sort and Select the Population
function pop = sort_and_select(this, pop)
% Sort Population
pop = sort_population(this,pop);
% Set the Population Size Limit
n = min(this.pop_size, numel(pop));
pop = pop(1:n);
end
% Check if a solution is better than other
function b = is_better(this, x1, x2)
b = x1.obj_value<x2.obj_value;
end
% Get Best Memebr of Population
function pop_best = get_population_best(this, pop)
pop_best = pop(1);
for i = 2:numel(pop)
if this.is_better(pop(i), pop_best)
pop_best = pop(i);
end
end
end
% Get Positions Matrix of Population
function pos = get_positions(this, pop)
pos = reshape([pop.position], this.problem.dim, [])';
end
% Get Objective Values of Population
function v = get_objective_values(~, pop)
v = [pop.obj_value];
end