-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbo_testtubeholder.m
142 lines (102 loc) · 3.41 KB
/
bbo_testtubeholder.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
%
% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPEA113
% Project Title: Biogeography-Based Optimization (BBO) in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
%
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
%
% Contact Info: [email protected], [email protected]
%
clc;
clear;
close all;
%% Problem Definition
CostFunction=@(x,y) Testtubeholder(x,y); % Cost Function
nVar=2; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=-10; % Decision Variables Lower Bound
VarMax= 10; % Decision Variables Upper Bound
%% BBO Parameters
MaxIt=1000; % Maximum Number of Iterations
nPop=50; % Number of Habitats (Population Size)
KeepRate=0.2; % Keep Rate
nKeep=round(KeepRate*nPop); % Number of Kept Habitats
nNew=nPop-nKeep; % Number of New Habitats
% Migration Rates
mu=linspace(1,0,nPop); % Emmigration Rates
lambda=1-mu; % Immigration Rates
alpha=0.9;
pMutation=0.1;
sigma=0.02*(VarMax-VarMin);
%% Initialization
% 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(1),pop(i).Position(2));
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 Main Loop
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(1),newpop(i).Position(2));
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(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
end
%% Results
figure;
%plot(BestCost,'LineWidth',2);
semilogy(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;