-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneticAlgorithm.m
134 lines (116 loc) · 3.65 KB
/
GeneticAlgorithm.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
function [X,F,ga_BestSol,fmin,fevalCount] = GeneticAlgorithm(fhd,funcId,nPop,bnds,d,X,Tmax,fevalCount)
LB = bnds(1);
UB = bnds(2);
F=zeros(1,nPop); %costs of returning particles
%% GA Parameters
pc=0.7; % Crossover Percentage
nc=2*round(pc*nPop/2); % Number of Offsprings (also Parnets)
gamma=0.4; % Extra Range Factor for Crossover
pm=0.3; % Mutation Percentage
nm=round(pm*nPop); % Number of Mutants
mu=0.1; % Mutation Rate
TournamentSize=3; % Tournamnet Size
%% GA Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
pop=repmat(empty_individual,nPop,1);
fmin = inf;
for i=1:nPop
pop(i).Position=X(i,:);
pop(i).Cost=feval(fhd,pop(i).Position',funcId);
end
% Sort Population
costs=[pop.Cost];
[costs, SortOrder]=sort(costs);
pop=pop(SortOrder);
ga_BestSol=pop(1); % Store Best Solution
ga_BestCost=zeros(Tmax,1); % Array to Hold Best Cost Values
WorstCost=pop(end).Cost; % Store Cost
saved_sol = zeros(nPop,d); %solution saved by the GA to be passed on to PSO
saved_cost = inf*ones(1,nPop);%cost saved by the GA to be passed on to PSO
%% Genetic Algorithm Main Loop
for it=1:Tmax
%check criteria and save solution
for kk=1:nPop
if pop(kk).Cost < saved_cost(kk)
%save solutions to feed PSO
saved_sol(kk,:) = pop(kk).Position;
saved_cost(kk) = pop(kk).Cost;
end
end
% Crossover
popc=repmat(empty_individual,nc/2,2);
for k=1:nc/2
% Select Parents Indices
i1=TournamentSelection(pop,TournamentSize);
i2=TournamentSelection(pop,TournamentSize);
% Select Parents
p1=pop(i1);
p2=pop(i2);
% Apply Crossover
[popc(k,1).Position,popc(k,2).Position]=Crossover(p1.Position,p2.Position,gamma,bnds);
% Evaluate Offsprings
popc(k,1).Cost=feval(fhd,popc(k,1).Position',funcId);
popc(k,2).Cost=feval(fhd,popc(k,2).Position',funcId);
fevalCount=fevalCount+1;
end
popc=popc(:);
% Mutation
popm=repmat(empty_individual,nm,1);
for k=1:nm
% Select Parent
i=randi([1 nPop]);
p=pop(i);
popm(k).Position=Mutate(p.Position,mu,LB,UB); % Apply Mutation
popm(k).Cost=feval(fhd,popm(k).Position',funcId); % Evaluate Mutant
fevalCount=fevalCount+1;
end
% Create Merged Population
pop=[pop
popc
popm]; %#ok
% Sort Population
costs=[pop.Cost];
[costs, SortOrder]=sort(costs);
pop=pop(SortOrder);
WorstCost=max(costs); % Update Worst Cost
% Truncation
pop=pop(1:nPop);
costs=costs(1:nPop);
% Store Best Solution Ever Found
ga_BestSol=pop(1);
% Store Best Cost Ever Found
ga_BestCost(it)=ga_BestSol.Cost;
fmin=ga_BestSol.Cost;
end
for ii=1:nPop
X(ii,:) = pop(ii).Position;
F(ii) = pop(ii).Cost;
end
function y=Mutate(x,mu,VarMin,VarMax)
nVar=numel(x);
nmu=ceil(mu*nVar);
j=randsample(nVar,nmu);
j=j'; %vertical vector j causes error
sigma=0.1*(VarMax-VarMin);
y=x;
y(j)=x(j)+sigma*randn(size(j));
y=max(y,VarMin);
y=min(y,VarMax);
function i=TournamentSelection(pop,m)
nPop=numel(pop);
S=randsample(nPop,m);
spop=pop(S);
scosts=[spop.Cost];
[~, j]=min(scosts);
i=S(j);
function [y1,y2]=Crossover(x1,x2,gamma,bnds)
%x1
%x2
alpha=unifrnd(-gamma,1+gamma,size(x1));
y1=alpha.*x1+(1-alpha).*x2;
y2=alpha.*x2+(1-alpha).*x1;
y1=max(y1,bnds(1));
y1=min(y1,bnds(2));
y2=max(y2,bnds(1));
y2=min(y2,bnds(2));