-
Notifications
You must be signed in to change notification settings - Fork 4
/
ga.m
148 lines (95 loc) · 3.03 KB
/
ga.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
clc;
clear;
close all;
%% Problem Definition
data=LoadData();
CostFunction=@(s) FeatureSelectionCost(s,data); % Cost Function
nVar=data.nx; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
%% GA Parameters
MaxIt=50; % Maximum Number of Iterations
nPop=20; % Population Size
pc=0.7; % Crossover Percentage
nc=2*round(pc*nPop/2); % Number of Offsprings (Parnets)
pm=0.3; % Mutation Percentage
nm=round(pm*nPop); % Number of Mutants
mu=0.1; % Mutation Rate
beta=8; % Selection Pressure
%% Initialization
disp('Initialization ...');
empty_individual.Position=[];
empty_individual.Cost=[];
empty_individual.Out=[];
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
% Initialize Position
pop(i).Position=randi([0 1],VarSize);
% Evaluation
[pop(i).Cost, pop(i).Out]=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;
%% Main Loop
for it=1:MaxIt
disp(['Starting Iteration ' num2str(it) ' ...']);
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
i1=RouletteWheelSelection(P);
i2=RouletteWheelSelection(P);
% Select Parents
p1=pop(i1);
p2=pop(i2);
% Apply Crossover
[popc(k,1).Position, popc(k,2).Position]=Crossover(p1.Position,p2.Position);
% Evaluate Offsprings
[popc(k,1).Cost, popc(k,1).Out]=CostFunction(popc(k,1).Position);
[popc(k,2).Cost, popc(k,2).Out]=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);
% Evaluate Mutant
[popm(k).Cost, popm(k).Out]=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 Cost = ' num2str(BestCost(it))]);
end
%% Results
figure;
plot(BestCost,'LineWidth',2);
ylabel('Cost');