Challenge

Week 4.1 - Genetic Algorithm

clear all 
close all 
clc

%Defining the search space
x = linspace(0,0.6,150);    %along the x-axis
y = linspace(0,0.6,150);    %along the y-axis
num = 50;

%Creating a 2D mesh 
[xx yy] = meshgrid(x,y);

%Evaluating the stalagmite function
for i = 1:length(xx)
    for j = 1:length(yy)
         input(1) = xx(i,j);
         input(2) = yy(i,j);
         f(i,j) = z_stalagmite(input);
         
    end
end



%% Study 1 

for i = 1:num
    [result,fopt(i)] = ga(@z_stalagmite,2);
        xopt(i) = result(1);
        yopt(i) = result(2);
end

Study_Time_1=toc

figure(1)
subplot(2,1,1)
surf(x,y,f)
shading interp
hold on 
plot3(xopt,yopt,fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('no. of Itreation')
ylabel('Function maximum')
title('Study 1')

%% Study 2

tic
for i=1:num;
    [result,fopt(i)] = ga(@z_stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
    xopt(i) = result(1);
    yopt(i) = result(2);
    
    
end

Study_Time_2=toc

figure(2)
subplot(2,1,1)
surf(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('no. of Itreation')
ylabel('Function maximum')
title('Study 2')

%% Study 3

options = optimoptions('ga','populationsize',250)
tic
for i=1:num;
    [result,fopt(i)] = ga(@z_stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
    xopt(i) = result(1);
    yopt(i) = result(2);
    
    
end

Study_Time_2=toc

figure(3)
subplot(2,1,1)
surf(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('no. of Itreation')
ylabel('Function maximum')
title('Study 3')
%axis([1 50 -1.1 0])

Section 2:- Explanation

-we defined the search space along the x-axis and y-axis using the "linspace" command. and then using the "meshgrid" command we convert the array into a 2D array

-We considered the no. of iteration to be 50. and called "num" to be 50.

-Then we created a function file "z_stalagmite.m" to solve the maxima for the given problem.

-Using the "for loop" command we calculated the value for the maxima for each point on the search space and saved the values as "result" and "fopt" respectively.

Study 1

-Then we initiated the GA with a random population. and saved the result as "xopt", "yopt" and "-fopt".

-Then using the x, y, -f values we plotted a 3D graph using the "surf" command and using xopt, yopt, -fopt values we plotted a 3D graph using the "plot3" command and plotted the graph to represent the value of maxima on each iteration using the plot command.

Result:-

This case study took 5.2582e+03s of time to complete. Using this process to calculate global maxima was a failure as most of the values fall away from the stalagmite surface.

Study 2

-Due to the errors of the last study we specified the upper and lower bounds for the stalagmite surface.

-Then we initiated the GA with a random population. and saved the result as "xopt", "yopt" and "-fopt".

-Then using the x, y, -f values we plotted a 3D graph using the "surf" command and using xopt, yopt, -fopt values we plotted a 3D graph using the "plot3" command and plotted the graph to represent the value of maxima on each iteration using the plot command.

Result:-

This case study took 1.9290s of time to complete. even though the optimum values are within the bound still the value isn't accurate enough to plot the exact maxima values. we can confirm that point by looking at the iteration chart as it's not stable at any point on the stalagmite surface.

Study 3

-Due to the above inaccuracy in the solution this time we increased the initial population from 50 to 250 to achieve more accuracy.

-Then we initiated the GA with a random population. and saved the result as "xopt", "yopt" and "-fopt".

-Then using the x, y, -f values we plotted a 3D graph using the "surf" command and using xopt, yopt, -fopt values we plotted a 3D graph using the "plot3" command and plotted the graph to represent the value of maxima on each iteration using the plot command.

Result:-

This case study took 6.7757s of time to complete. as we can see due to increasing the population the value has been accurate till 1* -10^7th value but execution time increased.

Section 3:- EXPLAINING THE GENETIC ALGORITHM

GA is a process of finding the best optimum solution for the given situation based on natural selection. Just like Biological evolution in which the population tried to modify the individual solution. During each step of the genetic algorithm, it selects randomly Selects fit individuals from the population to mate and to produce better offspring. Over successive generations, the population evolves towards the optimum solution. each individual is assigned a fitness value depending on the Fitness value an individual is expected to produce a fitter individual. The more fit the individual is higher the chance it has to mate and produce the fitter individual.

There are 5 phases in a GA.

1) Initial Population:- The set of individuals involved in the process is called population. Each individual from the population is considered to be the solution to the problem that we are solving.

2)Fitness Function:- It evaluates how close the individual is to the optimum solution for the problem.

3)Selection:- It is the process of selecting the fittest individual from the group so that it can produce the fittest offspring.

4)cross Over:- It is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.

5)Mutation:- It occurs to maintain diversity with the population and prevent premature convergence.

Section 4:- SYNTAX OF GENETIC ALGORITHM.

x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.