-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgab1.m
162 lines (109 loc) · 3.45 KB
/
gab1.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
clc;
clear;
close all;
%% Problem Definition
CostFunction=@(x) MinOne(x); % Fitness Function
nVar=30; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
%% GA Parameters
MaxIt=100; % Maximum Number of Iterations
nPop=40; % Population Size
pc=0.6; % Crossover Percentage
nc=2*round(pc*nPop/2); % Number of Offsprings (also Parnets)
pm=0.04; % Mutation Percentage
nm=round(pm*nPop); % Number of Mutants
mu=0.02; % Mutation Rate
ANSWER=questdlg('Choose selection method:','Genetic Algorith',...
'Roulette Wheel','Random','Roulette Wheel');
UseRouletteWheelSelection=strcmp(ANSWER,'Roulette Wheel');
UseRandomSelection=strcmp(ANSWER,'Random');
if UseRouletteWheelSelection
beta=80; % Selection Pressure
end
pause(0.01); % Needed due to a bug in older versions of MATLAB
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
% Initialize Position
pop(i).Position=randi([0 1],VarSize);
% Evaluation
pop(i).Cost=CostFunction(pop(i).Position);
end
% Sort Population
Costs=[pop.Cost];
[Costs, SortOrder]=sort(Costs,'descend');
pop=pop(SortOrder);
% Store Best Solution
BestSol=pop(1);
% Array to Hold Best Cost Values
BestCost=zeros(MaxIt,1);
% Store Cost
WorstCost=pop(end).Cost;
%% Main Loop
for it=1:MaxIt
% Calculate Selection Probabilities
if UseRouletteWheelSelection
P=exp(-beta*Costs/WorstCost);
P=P/sum(P);
end
% Crossover
popc=repmat(empty_individual,nc/2,2);
for k=1:nc/2
% Select Parents Indices
if UseRouletteWheelSelection
i1=RouletteWheelSelection(P);
i2=RouletteWheelSelection(P);
end
if UseRandomSelection
i1=randi([1 nPop]);
i2=randi([1 nPop]);
end
% Select Parents
p1=pop(i1);
p2=pop(i2);
% Perform Crossover
[popc(k,1).Position, popc(k,2).Position]=Crossover(p1.Position,p2.Position);
% Evaluate Offsprings
popc(k,1).Cost=CostFunction(popc(k,1).Position);
popc(k,2).Cost=CostFunction(popc(k,2).Position);
end
popc=popc(:);
% Mutation
popm=repmat(empty_individual,nm,1);
for k=1:nm
% Select Parent
i=randi([1 nPop]);
p=pop(i);
% Perform Mutation
popm(k).Position=Mutate(p.Position,mu);
% Evaluate Mutant
popm(k).Cost=CostFunction(popm(k).Position);
end
% Create Merged Population
pop=[pop
popc
popm]; %#ok
% Sort Population
Costs=[pop.Cost];
[Costs, SortOrder]=sort(Costs);
pop=pop(SortOrder);
% Update Worst Cost
WorstCost=max(WorstCost,pop(end).Cost);
% Truncation
pop=pop(1:nPop);
Costs=Costs(1:nPop);
% Store Best Solution Ever Found
BestSol=pop(1);
% Store Best Cost Ever Found
BestCost(it)=BestSol.Cost;
% Show Iteration Information
disp(['Iteration ' num2str(it) ': Best Min = ' num2str(BestCost(it))]);
end
%% Results
figure;
plot(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Minimun');
grid on;