-
Notifications
You must be signed in to change notification settings - Fork 2
/
test1c.m
189 lines (131 loc) · 4.07 KB
/
test1c.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
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
% test1c stands for PSO_VCO_3stage for Temprature , 'V3.sp' , with
% GA
clc;
tic;
clear;
close all;
%% Problem Definition
global NFE;
NFE=0;
CostFunction=@test2c; % Cost Function
nVar=1; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=1;
VarMax=50; % Upper Bound of Variables
%% GA Parameters
MaxIt=10; % Maximum Number of Iterations
nPop=4;
%nPop=10; % Population Size
pc=0.5; % Crossover Percentage
nc=2*round(pc*nPop/2); % Number of Offsprings (Parnets)
pm=0.5; % Mutation Percentage
nm=round(pm*nPop); % Number of Mutants
gamma=0.05;
mu=0.02; % Mutation Rate
ANSWER=questdlg('Choose selection method:','Genetic Algorith',...
'Roulette Wheel','Tournament','Random','Roulette Wheel');
UseRouletteWheelSelection=strcmp(ANSWER,'Roulette Wheel');
UseTournamentSelection=strcmp(ANSWER,'Tournament');
UseRandomSelection=strcmp(ANSWER,'Random');
if UseRouletteWheelSelection
beta=8; % Selection Pressure
end
if UseTournamentSelection
TournamentSize=3; % Tournamnet Size
end
pause(0.1);
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
% Initialize Position
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
% Evaluation
pop(i).Cost=CostFunction(pop(i).Position);
end
% Sort Population
Costs=[pop.Cost];
[Costs, SortOrder]=sort(Costs);
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;
% Array to Hold Number of Function Evaluations
nfe=zeros(MaxIt,1);
%% Main Loop
for it=1:MaxIt
% Calculate Selection Probabilities
P=exp(-beta*Costs/WorstCost);
P=P/sum(P);
% 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 UseTournamentSelection
i1=TournamentSelection(pop,TournamentSize);
i2=TournamentSelection(pop,TournamentSize);
end
if UseRandomSelection
i1=randi([1 nPop]);
i2=randi([1 nPop]);
end
% Select Parents
p1=pop(i1);
p2=pop(i2);
% Apply Crossover
[popc(k,1).Position popc(k,2).Position]=...
Crossover(p1.Position,p2.Position,gamma,VarMin,VarMax);
% 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);
% Apply Mutation
popm(k).Position=Mutate(p.Position,mu,VarMin,VarMax);
% Evaluate Mutant
popm(k).Cost=CostFunction(popm(k).Position);
end
% Create Merged Population
pop=[pop
popc
popm];
% 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;
% Store NFE
nfe(it)=NFE;
% Show Iteration Information
disp(['Iteration ' num2str(it) ': NFE = ' num2str(nfe(it)) ', Best Cost = ' num2str(BestCost(it))]);
end
%% Results
figure;
%hold on
plot(BestCost,'r','LineWidth',2);
% semilogy(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Fitness = Best Dynamic Average Power');
toc;