-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_BFOA_TransferFunction_Study.m
More file actions
299 lines (248 loc) · 14.6 KB
/
Copy pathrun_BFOA_TransferFunction_Study.m
File metadata and controls
299 lines (248 loc) · 14.6 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
% Binary Fossa Optimization Algorithm (BFOA) with 8 Transfer Functions
% Evaluates BFOA on 25 binary knapsack problems using 8 different transfer functions
function run_BFOA_TransferFunction_Study()
%% Setup
problems = define_problems();
transfer_funcs = define_transfer_functions();
num_runs = 30;
max_iter = 100;
pop_size = 30;
% Output table: rows = problems, columns = transfer functions
results_std = zeros(length(problems), length(transfer_funcs));
%% Main Loop
for p = 1:length(problems)
problem = problems(p);
fprintf('Running problem: %s\n', problem.id);
for t = 1:length(transfer_funcs)
tf = transfer_funcs(t);
best_vals = zeros(num_runs, 1);
for run = 1:num_runs
[~, best_fit, ~] = BFOA(problem.weights, problem.profits, problem.capacity, tf.func, max_iter, pop_size);
best_vals(run) = best_fit;
end
results_std(p, t) = std(best_vals);
fprintf(' Transfer Function %s - STD: %.2f\n', tf.name, results_std(p, t));
end
end
%% Compute Friedman Mean Ranks
ranks = zeros(size(results_std));
for i = 1:size(results_std, 1)
[~, sorted_idx] = sort(results_std(i, :));
rank = 1:length(transfer_funcs);
ranks(i, sorted_idx) = rank;
end
mean_ranks = mean(ranks, 1);
mean_ranks_row = [{'Mean Rank'}, arrayfun(@(x) sprintf('%.2f', x), mean_ranks, 'UniformOutput', false)];
%% Display Results Table in Exponential Format
disp('Standard Deviation Table (BFOA, 30 runs)');
header = ['ID', {transfer_funcs.name}];
formatted_vals = cell(size(results_std));
for i = 1:size(results_std,1)
for j = 1:size(results_std,2)
formatted_vals{i,j} = sprintf('%.2E', results_std(i,j));
end
end
table_data = [cellstr({problems.id})', formatted_vals];
table_data = [table_data; mean_ranks_row];
results_table = cell2table(table_data, 'VariableNames', header);
format shortE
disp(results_table);
end
%% BFOA core with injected transfer function
function [best_pos, best_fit, convergence] = BFOA(weights, profits, capacity, transfer_func, max_iter, pop_size)
dim = length(weights);
positions = randi([0 1], pop_size, dim);
fitness = zeros(pop_size, 1);
territories = positions;
territory_strength = ones(pop_size, 1);
for i = 1:pop_size
fitness(i) = knapsack_fitness(positions(i,:), weights, profits, capacity);
end
[best_fit, best_idx] = min(fitness);
best_pos = positions(best_idx, :);
convergence = zeros(max_iter, 1);
for iter = 1:max_iter
a = 2 - 2 * iter / max_iter;
for i = 1:pop_size
r = rand();
new_pos = positions(i,:);
influence = zeros(1, dim);
if r < 0.3
influence = 0.3 * a * (2*rand(1,dim) - 1);
elseif r < 0.7
r1 = rand(1,dim);
r2 = rand(1,dim);
influence = 2 * r1 .* (best_pos - positions(i,:)) + 1.5 * r2 .* (territories(i,:) - positions(i,:));
else
if rand < 0.5
influence = zeros(1,dim);
else
idx = randi(pop_size);
while idx == i, idx = randi(pop_size); end
influence = 0.5 * rand(1,dim) .* (positions(idx,:) - positions(i,:));
end
end
if territory_strength(i) > rand()
influence = influence + 0.2 * rand(1,dim) .* (territories(i,:) - positions(i,:));
end
if rand() < 0.1
neighbor = randi(pop_size);
while neighbor == i, neighbor = randi(pop_size); end
influence = influence + 0.1 * rand(1,dim) .* (positions(neighbor,:) - positions(i,:));
end
for d = 1:dim
if rand < transfer_func(influence(d))
new_pos(d) = 1 - positions(i,d);
end
end
new_fit = knapsack_fitness(new_pos, weights, profits, capacity);
if new_fit < fitness(i)
positions(i,:) = new_pos;
fitness(i) = new_fit;
territories(i,:) = new_pos;
territory_strength(i) = min(territory_strength(i) + 0.1, 2);
else
territory_strength(i) = max(territory_strength(i) - 0.05, 0.1);
end
if fitness(i) < best_fit
best_fit = fitness(i);
best_pos = positions(i,:);
end
end
for i = 1:pop_size
r = randi(pop_size);
while r == i, r = randi(pop_size); end
if fitness(i) < fitness(r) && rand() < 0.3
territories(i,:) = round(0.7 * territories(i,:) + 0.3 * territories(r,:));
end
end
convergence(iter) = best_fit;
end
end
%% Fitness function for binary knapsack
function f = knapsack_fitness(x, weights, profits, capacity)
total_w = sum(weights .* x);
total_p = sum(profits .* x);
if total_w > capacity
f = 1e6 + total_w;
else
f = -total_p;
end
end
%% Transfer Functions
function tf = define_transfer_functions()
tf(1).name = 'Ts1'; tf(1).func = @(x) 1 ./ (1 + exp(-x));
tf(2).name = 'Ts2'; tf(2).func = @(x) 1 ./ (1 + exp(-2*x));
tf(3).name = 'Ts3'; tf(3).func = @(x) 1 ./ (1 + exp(-x/2));
tf(4).name = 'Ts4'; tf(4).func = @(x) 1 ./ (1 + exp(-x/3));
tf(5).name = 'Tv1'; tf(5).func = @(x) abs(tanh(x));
tf(6).name = 'Tv2'; tf(6).func = @(x) abs(x ./ sqrt(1 + x.^2));
tf(7).name = 'Tv3'; tf(7).func = @(x) abs(2/pi * atan(pi/2 * x));
tf(8).name = 'Tv4'; tf(8).func = @(x) abs(erf(sqrt(pi)/2 * x));
end
%% Knapsack problem definitions (ks8a to ks24e)
function problems = define_problems()
% Define the knapsack problems (paste all ks8a to ks24e here from your dataset)
% Due to length, paste manually from your dataset
% Example for ks8a:
problems(1).id = 'ks8a';
problems(1).weights = [12630 284975 583838 575342 780934 164152 912739 412657];
problems(1).profits = [25424 604597 1272766 1174735 1707707 313906 1689410 860062];
problems(1).capacity = 1863633;
problems(2).id = 'ks8b';
problems(2).weights = [666885 346111 97003 337746 507338 836649 564424 289280];
problems(2).profits = [1452321 747077 209067 674618 1076193 1619423 1212262 538078];
problems(2).capacity = 1822718;
problems(3).id = 'ks8c';
problems(3).weights = [580593 34916 848308 528023 497440 490277 97943 141338];
problems(3).profits = [1155261 73653 1631996 1130519 1045697 1061672 193508 296438];
problems(3).capacity = 1609419;
problems(4).id = 'ks8d';
problems(4).weights = [800588 496540 364175 699421 179078 778021 206205 700556];
problems(4).profits = [1521886 919285 794937 1296002 354090 1582223 409313 1401234];
problems(4).capacity = 2112292;
problems(5).id = 'ks8e';
problems(5).weights = [862754 318418 651921 844733 438185 591527 810761 468202];
problems(5).profits = [1582289 640086 1270769 1839736 795116 1099087 1524697 951036];
problems(5).capacity = 2493250;
problems(6).id = 'ks12a';
problems(6).weights = [672437 664905 66143 670473 649045 513812 638975 262871 79332 334123 620541 437769];
problems(6).profits = [1370413 1281034 124356 1296241 1410881 993625 1209949 573180 147263 617431 1193525 861742];
problems(6).capacity = 2805213;
problems(7).id = 'ks12b';
problems(7).weights = [419614 463634 305284 918709 743181 652957 256487 790046 310107 985008 43471 629575];
problems(7).profits = [800734 843137 551965 1921987 1429742 1272555 552649 1468914 645615 1859603 89001 1190478];
problems(7).capacity = 3259036;
problems(8).id = 'ks12c';
problems(8).weights = [399907 241106 271976 105019 65202 864369 458263 27528 667143 681262 982460 215395];
problems(8).profits = [744436 446887 550596 191341 142738 1571133 868558 54288 1425628 1318834 2127868 422621];
problems(8).capacity = 2489815;
problems(9).id = 'ks12d';
problems(9).weights = [992884 417147 996822 591627 482278 651305 491683 727443 135904 152947 590330 677035];
problems(9).profits = [2157066 853212 1845571 1068849 962615 1278897 1026191 1377079 264669 299959 1080762 1263347];
problems(9).capacity = 3453702;
problems(10).id = 'ks12e';
problems(10).weights = [187536 919812 281447 290967 293933 146982 335995 76949 296586 732368 912094 566115];
problems(10).profits = [390564 1896554 518776 534038 539357 281514 679085 164965 603431 1601666 1826086 1235821];
problems(10).capacity = 2520392;
problems(11).id = 'ks16a';
problems(11).weights = [123074 517025 655582 371336 51246 296092 431720 933989 561395 376612 722734 995095 568523 168710 520215 267362];
problems(11).profits = [248913 1051063 1211866 716645 103462 544575 860033 2039565 1174920 704133 1495071 1907792 1093059 345186 1097141 487799];
problems(11).capacity = 3780355;
problems(12).id = 'ks16b';
problems(12).weights = [31957 117108 250834 775747 994544 315281 937184 258099 877986 327103 632921 518873 708189 537974 732066 838024];
problems(12).profits = [66111 215853 452976 1435157 1901253 649654 2034198 496995 1725886 678356 1329367 1088047 1507322 1041781 1607816 1634282];
problems(12).capacity = 4426945;
problems(13).id = 'ks16c';
problems(13).weights = [406270 182392 468242 749900 581384 334532 483985 342610 260722 970898 887883 657188 369265 252140 727195 971955];
problems(13).profits = [837989 370555 843247 1625049 1127636 664244 900936 747010 517890 2092844 1749694 1376143 756658 483621 1583667 1826529];
problems(13).capacity = 4323280;
problems(14).id = 'ks16d';
problems(14).weights = [169122 435204 480076 638597 906325 613953 713858 817993 18612 819650 867495 992711 509674 103140 481247 334219];
problems(14).profits = [319363 924519 1016542 1387079 1840227 1346660 1515021 1572948 37076 1702673 1623789 1968433 1004311 221324 956448 692230];
problems(14).capacity = 4450938;
problems(15).id = 'ks16e';
problems(15).weights = [615938 74618 109110 821054 458488 821360 609945 361196 633910 133731 619385 11437 232580 987645 354237 676225];
problems(15).profits = [1188681 160632 225764 1762724 840566 1501156 1265916 672380 1336652 289808 1252296 20947 472835 1828177 759603 1253380];
problems(15).capacity = 3760429;
problems(16).id = 'ks20a';
problems(16).weights = [807304 365268 71972 635962 628375 105248 912959 102532 258867 778387 351744 775629 997576 264796 438778 549088 522456 920079 775693 76582];
problems(16).profits = [1490790 691574 150435 1164050 1227890 219877 1934511 199053 514991 1522492 769943 1680173 1839759 523879 800279 1097473 1091605 1921590 1495883 139897];
problems(16).capacity = 5169647;
problems(17).id = 'ks20b';
problems(17).weights = [426375 91504 915455 60292 337974 336679 566396 425705 836379 27713 779627 207304 174254 703660 421993 450456 828590 121896 719160 931334];
problems(17).profits = [928904 170787 1689092 117966 632580 731869 1185747 923646 1617156 52263 1501384 428275 368772 1513686 918075 903139 1588130 220241 1451995 1863885];
problems(17).capacity = 4681373;
problems(18).id = 'ks20c';
problems(18).weights = [440356 79368 945895 666364 594595 358461 666020 801970 716869 431640 576028 308237 541552 879694 18964 741144 410306 24327 868689 57104];
problems(18).profits = [925328 164044 1966178 1440910 1288193 741301 1241063 1447446 1388189 916476 1240551 639466 1124003 1867028 40187 1361579 770874 44934 1671823 119080];
problems(18).capacity = 5063791;
problems(19).id = 'ks20d';
problems(19).weights = [637662 193120 776715 262749 23227 384111 768320 168361 650172 453970 873944 389217 579192 490419 485659 478971 79818 507772 134957 234926];
problems(19).profits = [1252158 350292 1479672 521323 50313 715956 1462192 314251 1319970 978978 1743412 748759 1274136 1031710 890331 1029238 174683 1062778 250178 431650];
problems(19).capacity = 4286641;
problems(20).id = 'ks20e';
problems(20).weights = [63416 838949 231705 278430 579076 542663 544786 226831 362836 192630 543297 194244 668225 858827 306890 914752 243468 983177 262040 115758];
problems(20).profits = [128319 1601667 475634 531575 1251993 1108634 1039759 468728 698158 372342 1161048 403961 1362470 1773653 580301 1947699 495773 2007127 472069 245042];
problems(20).capacity = 4476000;
problems(21).id = 'ks24a';
problems(21).weights = [382745 799601 909247 729069 467902 44328 34610 698150 823460 903959 853665 551830 610856 670702 488960 951111 323046 446298 931161 31385 496951 264724 224916 169684];
problems(21).profits = [825594 1677009 1676628 1523970 943972 97426 69666 1296457 1678693 1902996 1844992 1049289 1252836 1319836 953277 2067538 675367 853655 1826027 65731 901489 577243 466257 369261];
problems(21).capacity = 6404180;
problems(22).id = 'ks24b';
problems(22).weights = [763783 914370 831207 100284 487486 182921 369532 727094 272162 227474 170387 697344 266796 41169 937876 883615 923090 385246 839734 423575 33377 975193 69579 418849];
problems(22).profits = [1559844 1724668 1548321 186455 1012952 337633 806295 1329837 546869 446874 316848 1399126 578882 79486 1816706 1665934 1917601 770200 1732704 806240 65751 1870594 140713 859568];
problems(22).capacity = 5971071;
problems(23).id = 'ks24c';
problems(23).weights = [425855 348636 526169 332447 441002 924697 383512 696754 345706 295567 606800 502532 67352 991656 779035 244102 623815 460346 44551 774214 215619 914117 568645 227812];
problems(23).profits = [911193 699561 1049934 675119 942922 1911983 766504 1428013 665388 601640 1126415 1004011 135369 2158735 1516949 505363 1160279 1003942 81812 1654345 455207 1774312 1245431 422075];
problems(23).capacity = 5870470;
problems(24).id = 'ks24d';
problems(24).weights = [844709 36928 649129 925892 475939 108733 488034 395701 550693 509694 440355 49925 100736 79142 152798 88114 941561 820124 827804 918561 261353 573421 497161 788062];
problems(24).profits = [1844400 75605 1344522 1725990 1029572 223878 935619 725710 1003949 953278 900820 97657 215673 143729 293251 187454 1696715 1495747 1589311 1821594 495147 1250695 970416 1459955];
problems(24).capacity = 5762284;
problems(25).id = 'ks24e';
problems(25).weights = [695093 430667 233583 832832 422539 614321 898576 371233 324029 958551 381955 519075 94236 835863 352292 983000 286126 924119 679730 229377 152007 794048 566176 729710];
problems(25).profits = [1319972 804470 487111 1636416 915637 1334476 1703095 727928 588848 1974348 818432 1012735 181004 1826340 736446 2070580 597255 1765740 1374118 467036 310529 1614819 1157316 1370965];
problems(25).capacity = 6654569;
end