0% found this document useful (0 votes)
13 views

10/25/16 11:22 Am E:/Phd Material (Ch... /main - Adaptive - Ga.M 1 of 3

This document contains MATLAB code for an adaptive genetic algorithm. It loads initial population data and target values, then enters a while loop that iterates the genetic algorithm. Each iteration sorts the population by fitness, selects the best solutions, performs adaptive crossover and mutation with rates that change based on population statistics, and updates the population. Performance metrics are saved to files at intervals.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

10/25/16 11:22 Am E:/Phd Material (Ch... /main - Adaptive - Ga.M 1 of 3

This document contains MATLAB code for an adaptive genetic algorithm. It loads initial population data and target values, then enters a while loop that iterates the genetic algorithm. Each iteration sorts the population by fitness, selects the best solutions, performs adaptive crossover and mutation with rates that change based on population statistics, and updates the population. Performance metrics are saved to files at intervals.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

10/25/16 11:22 AM E:\PhD Material(Ch...\Main_Adaptive_GA.

m 1 of 3

clc
clear all
close all
load E %locations in chromosome that do not undergo genetic operations.

Min = 0.0446;
Max = 5000.0000;

L = 2000; %lenght of chromosomes


% Tar = randi([floor(Min) floor(Max)],[1 L])+rand([1 L]);
%
% save Tar Tar
load Tar
Sol_Len = 40; %size of population
%==== Loading Initial Ensemble=======
% S = InitialSolution(Sol_Len,L,Min,Max);
load S
Err = 5000;
iter = 1;inc = 1;
while Err > 1000

for i = 1 : size(S,1)
C = S(i,:);
Fit(i,1) = sum(abs(C-Tar)); % Calculate Fitness Value
end

if iter == 174
save S S
break;
end

[val ind] = sort(Fit);

for i = 1 : floor(length(ind)/2)
Best_Sol(i,:) = S(ind(i),:);
end

if iter == 1
Best = [Best_Sol(1,:) val(1)];
V(iter,1) = Best(end);
Err = val(1)
else
if Best(end) > val(1)
Best = [Best_Sol(1,:) val(1)];
V(iter,1) = Best(end);
Err = val(1)
else
Best = Best;
V(iter,1) = Best(end);
end
end
10/25/16 11:22 AM E:\PhD Material(Ch...\Main_Adaptive_GA.m 2 of 3

if iter == 1
mr = 0.001;
cr = 0.5;
else
[vl id] = sort(Fit);
fmin = Fit(id(1));
fmax = Fit(id(end));
favg = mean(Fit);
Pm = mr;
B = 0.02;
nc = 0.5;
si = ((fmax - fmin)/favg).^nc;
nmr = Pm*(1+B*(((fmax-fmin).^nc)-(favg.^nc))/(si*((fmax-fmin).^nc)-(favg.^nc)));
mr = nmr;

%============ Crossover rate =============


Pc = cr;nc = 0.5;A =0.02;
ncr = Pc *(1+A*((favg^nc)/(((fmax-fmin)^nc)+(favg^nc))));
cr = ncr;
end
%==========================
% Save Values
if mod(iter,50)==0
save(['.\All\S-' num2str(iter) '.mat'],'S')
save(['.\All\F-' num2str(iter) '.mat'],'Fit')

% Fit_V = Best(end);
% Solution = Best(1:end-1);
% save(['.\All\Fit_' num2str(iter) '.mat'],'Fit_V') ;
% save(['.\All\Solution_' num2str(iter) '.mat'],'Solution') ;
save(['.\All\Pm_' num2str(iter) '.mat'],'Pm') ;
save(['.\All\Pc_' num2str(iter) '.mat'],'Pc') ;
end
%=============================
Cross_rate = floor(size(S,2)*cr);
if Cross_rate > size(S,2)
Cross_rate = size(S,2);
end
%For SGA operation; uncomment line 92 and comment line 94
% cross = Uniform_Crossover(Best_Sol,Cross_rate,E);

cross = Adaptive_Crossover(Best_Sol,Cross_rate,E);
Mut = Mutation(cross,mr,Min,Max,E);

S = [];

S = [Best_Sol ; Mut];
iter = iter +1;
end
10/25/16 11:22 AM E:\PhD Material(Ch...\Main_Adaptive_GA.m 3 of 3

figure,plot(V)

You might also like