Challenge

Week 4 - Genetic Algorithm

genetic algorithm :A genetic algorithm is a search heuristic that is inspired by Charles Darwin's theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

% genetic algorithm

clear all
close all
clc

%inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

[xx yy]=meshgrid(x,y);
num_case=150;

for i=1:length(xx)
    for j=1:length(yy)
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        F(i,j)=stalagmite(input_vector);
    end
end

%study1
tic
for i=1:num_case
    [inputs,Fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study_time_1=toc
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x label');
plot3(xopt,yopt,Fopt,'marker','0','markersize','5','markerfacecolor','k');
title('bounded inputs');
subplot(2,1,2);
plot(Fplot);
xlabel('iteration');
ylabel('Function maxmimum');

%study 3
options=optimoptions('ga');
options=optimoptions(options,'populationsize',450);
tic
for i=1:num_case
    [inputs,Fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_time_3=toc

figure(3)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,Fopt,'marker','0','markersize',5,'markerfacecolor','k');
title('bounded inputs with increase population size ');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('function maximum');

max_value=Fopt

% stalagmite function

function y=stalagmite(input_vector)
    x=input_vector(1);
    y=input_vector(2);
    f1_x=(sin(5.1*pi*x+0.5))^6;
    f1_y=(sin(5.1*pi*y+0.5))^6;
    f2_x=exp(-4*log(2)*((x-0.0667)^2/0.64));
    f2_y=exp(-4*log(2)*((y-0.0667)^2/0.64));
    y=-[f1_x*f1_y*f2_x*f2_y];  
end

code explanation:

1) the code starts with defining x and y as inputs using linspace command

2) a function stalagmite known as stalagmite.m is defined as shown as shown in the 2nd stage of codes.

3) 2 loops are incorporated in the main code so that all the combinations of x and y values is taken in the stalagmite function using the meshgrid command.

4)in the 1st study GA functions runs for 50 iterations .plots of the stalagmite and maximum values generated in each iteration is shown.

5)in the 2nd study GA function is corporated with lower and upper bounds and runs for 50 iterations .plots of the stalagmites and maximum values generated in each iteration is shown.

6) in the 3rd study GA function is incorporated with lower upper bounds along with the population size.plots of the stalagmites and maximum values generated in each iteration is given

outputs :

conclusion: by using the genetic algorithm we can find the global minima and the global maxima ,which intern helps us to optimize our errors.

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.