Code covered by the BSD License  

Highlights from
Genetic Algorithm to Optimise Schaffer's F6 Function

image thumbnail

Genetic Algorithm to Optimise Schaffer's F6 Function

by

 

11 Apr 2013 (Updated )

A basic GA with a real-time plotting of evaluation funtion inputs and outputs

f6.m
%Main Control Program
clear;
f6Surfaces;
tic

%GA INPUTS
method='bin'; %'gray' or 'bin' - Gray or binary coding (Gray is slow due to the extra functions required)
popSize=100; %Default 100(Not optimal) - make sure it's an even number
genSize=100; %Default 40(Larger is better but more time consuming)
mutProb=0.01; %default 0.008(not necessary optimal)
crossProb=0.8; %default 0.65(not necessary optimal)

[chromoChild]=f6Seed(popSize,method);
bestMasses=zeros(genSize,1);
avMasses=zeros(genSize,1);

%LOOP OVER GENERATIONS
for genNo=1:genSize
    %Convert coded array into parameters for evaluation function
    [x,y] = f6Bin2Dec(popSize,chromoChild,method);
    %F6 Evaluation Function
    f6EvalFun=0.5-((sin(sqrt(x.^2+y.^2))).^2-0.5)./((1+0.001.*(x.^2+y.^2)).^2);
    %Fitness and Selection
    [fitness,sel]=f6FitSel(f6EvalFun,popSize);
    %Reorder population according to selection order
    chromoParent=zeros(popSize,36);
    for popNo=1:popSize
        chromoParent(popNo,:)=chromoChild(sel(popNo,1),:);
    end
    %Single-Point Crossover
    crossPoints=randi([1 35],popSize/2,1);
    for k=2:2:popSize
        if crossProb>rand
            isolate=chromoParent(k-1:k,crossPoints(k/2,1)+1:end);
            isolate([1 2],:)=isolate([2 1],:);
            chromoParent(k-1:k,crossPoints(k/2,1)+1:end)=isolate;
        end
    end
    %Mutation
    for popNo=1:popSize
        for k=1:36
            if mutProb>rand
                if 0.5>rand
                    chromoParent(popNo,k)=abs(chromoParent(popNo,k)-1);
                end
            end
        end
    end
    %Next Generation
    chromoChild=chromoParent;
    %Find average and best solutions per generation
    duration=toc;
    bestSol=find(fitness==max(fitness)==1,1);
    bestMasses(genNo,:)=f6EvalFun(bestSol);
    avMasses(genNo,:)=mean(f6EvalFun);
    
    %figure(1)
    subplot(2,3,1)
    hold all
    fig1=scatter3(x,y,f6EvalFun,'black','filled');
    
    %figure(2)
    subplot(2,3,2)
    hold all
    fig2=scatter(x,f6EvalFun,'black','filled');
    
    %figure(3)
    subplot(2,3,3)
    hold all
    fig3=scatter(y,f6EvalFun,'black','filled');
    
    %figure(4)
    subplot(2,3,4)
    hold all
    fig4=scatter(duration,bestMasses(genNo),'blue','.');
    title('Real-Time Largest Evaluation Function Value');
    xlabel('Time(s)');
    ylabel('Evaluation Function Value');
    pause(0.1)
    
    %figure(5)
    subplot(2,3,5)
    hold all
    fig5=scatter(duration,avMasses(genNo),'red','.');
    title('Real-Time Average Evaluation Function Value');
    xlabel('Time(s)');
    ylabel('Evaluation Function Value');
    
    %figure(6)
    subplot(2,3,6)
    hold all
    fig6a=scatter(genNo,bestMasses(genNo),'blue','.');
    fig6b=scatter(genNo,avMasses(genNo),'red','.');
    title('Largest and Average Evaluation Function Value');
    xlabel('Generation Number');
    ylabel('Evaluation Function Value');
    legend('Maximum','Mean','Location','SouthEast');

    if genNo~=genSize
        delete(fig1)
        delete(fig2)
        delete(fig3)
    end
end

% figure(7)
% hold all
% plot(1:genSize,avMasses,'r-')
% plot(1:genSize,bestMasses,'b-')
% title('Largest and Average Evaluation Function Value');
% xlabel('Generation Number');
% ylabel('Evaluation Function Value');
% legend('Average','Largest');

toc

Contact us