-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBBOFCN.m
More file actions
157 lines (148 loc) · 4.53 KB
/
BBOFCN.m
File metadata and controls
157 lines (148 loc) · 4.53 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
function bestfis=BBOFCN(fis,data)
% Variables
p0=GettingFuzzyParameters(fis);
Problem.CostFunction=@(x) FuzzyCost(x,fis,data);
Problem.nVar=numel(p0);
alpha=1;
VarMin = -10; % Decision Variables Lower Bound
VarMax = 10; % Decision Variables Upper Bound
Problem.VarMin=-(10^alpha);
Problem.VarMax=10^alpha;
%
% BBO Algorithm Parameters
Params.MaxIt = 10; % Maximum Number of Iterations
Params.nPop = 7; % Number of Habitats (Population Size)
Params.KeepRate = 0.4; % Keep Rate
Params.nKeep = round(Params.KeepRate*Params.nPop); % Number of Kept Habitats
Params.nNew = Params.nPop-Params.nKeep; % Number of New Habitats
% Migration Rates
Params.mu = linspace(1, 0, Params.nPop); % Emmigration Rates
Params.lambda = 1-Params.mu; % Immigration Rates
Params.alpha = 0.9;
Params.pMutation = 0.1;
Params.sigma = 0.02*(VarMax-VarMin);
%
% Starting BBO Algorithm
results=RunbboFCN(Problem,Params);
% Getting the Results
p=results.BestSol.Position.*p0;
bestfis=FuzzyParameters(fis,p);
end
%%----------------------------------------------
function results=RunbboFCN(Problem,Params)
disp('Starting BBO-FireFly Algorithm Training');
%------------------------------------------------
% Cost Function
CostFunction=Problem.CostFunction;
% Number of Decision Variables
nVar=Problem.nVar;
% Size of Decision Variables Matrixv
VarSize=[1 nVar];
% Lower Bound of Variables
VarMin=Problem.VarMin;
% Upper Bound of Variables
VarMax=Problem.VarMax;
% Some Change
if isscalar(VarMin) && isscalar(VarMax)
dmax = (VarMax-VarMin)*sqrt(nVar);
else
dmax = norm(VarMax-VarMin);
end
%--------------------------------------------
%% BBO Algorithm Parameters
MaxIt = Params.MaxIt; % Maximum Number of Iterations
nPop = Params.nPop; % Number of Habitats (Population Size)
KeepRate =Params.KeepRate; % Keep Rate
nKeep = Params.nKeep; % Number of Kept Habitats
nNew = Params.nNew; % Number of New Habitats
% Migration Rates
mu = Params.mu; % Emmigration Rates
lambda = Params.lambda; % Immigration Rates
alpha = Params.alpha;
pMutation = Params.pMutation;
sigma = Params.sigma;
%------------------------------------------------------
%% Second Stage
% Empty Habitat
habitat.Position = [];
habitat.Cost = [];
% Create Habitats Array
pop = repmat(habitat, nPop, 1);
% Initialize Habitats
for i = 1:nPop
pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
pop(i).Cost = CostFunction(pop(i).Position);
end
% Sort Population
[~, SortOrder] = sort([pop.Cost]);
pop = pop(SortOrder);
% Best Solution Ever Found
BestSol = pop(1);
% Array to Hold Best Costs
BestCost = zeros(MaxIt, 1);
%
%% BBO Algorithm Main Body
for it = 1:MaxIt
newpop = pop;
for i = 1:nPop
for k = 1:nVar
% Migration
if rand <= lambda(i)
% Emmigration Probabilities
EP = mu;
EP(i) = 0;
EP = EP/sum(EP);
% Select Source Habitat
j = RouletteWheelSelection(EP);
% Migration
newpop(i).Position(k) = pop(i).Position(k) ...
+alpha*(pop(j).Position(k)-pop(i).Position(k));
end
% Mutation
if rand <= pMutation
newpop(i).Position(k) = newpop(i).Position(k)+sigma*randn;
end
end
% Apply Lower and Upper Bound Limits
newpop(i).Position = max(newpop(i).Position, VarMin);
newpop(i).Position = min(newpop(i).Position, VarMax);
% Evaluation
newpop(i).Cost = CostFunction(newpop(i).Position);
end
% Sort New Population
[~, SortOrder] = sort([newpop.Cost]);
newpop = newpop(SortOrder);
% Select Next Iteration Population
pop = [pop(1:nKeep)
newpop(1:nNew)];
% Sort Population
[~, SortOrder] = sort([pop.Cost]);
pop = pop(SortOrder);
% Update Best Solution Ever Found
BestSol = pop(1);
% Store Best Cost Ever Found
BestCost(it) = BestSol.Cost;
% Show Iteration Information
disp(['In Iteration ' num2str(it) ' : BBO-FireFly Fittest Cost Is = ' num2str(BestCost(it))]);
end
%------------------------------------------------
% disp('BBO Algorithm Came To End');
% Store Result
results.BestSol=BestSol;
results.BestCost=BestCost;
% Plot BBO Algorithm Training Stages
figure;
set(gcf, 'Position', [600, 300, 500, 300])
plot(BestCost,'-',...
'LineWidth',2,...
'MarkerSize',3,...
'MarkerEdgeColor','k',...
'Color',[0.1,0.1,0.1]);
title('BBO Algorithm Training','FontSize',10,...
'FontWeight','bold','Color','b');
xlabel('BBO Iteration Number','FontSize',10,...
'FontWeight','bold','Color','k');
ylabel('BBO Best Cost Result','FontSize',10,...
'FontWeight','bold','Color','k');
legend({'BBO Algorithm Train'});
end