Project

Genetic Algorithm

Genetic Algorithm

AIM: Try to understand the concept of genetic algorithm.

PROGRAM EXPLANATION:

Firstly we will create our own function to calculate the value of objective function at various values of x and y
we use linspace command for creating various x and y values
Now, we create a matrix using the meshgrid function
Now the values in the different matrix created can be put as an input into the function that we have created, by calling it. By this we can get the value of the function at various values of x and y
Now since we have got the value of the function at various values, we now need to calculate the maximum value, which can be calculated using the ga, which is an in-built function

So, for our first study we will use the for loop and iterate it 50 times
We will use the following syntax

1. Creating 1D array.

2. Getting output from the stalagmite function and storing it.

3. plotting unbound iteration case.

4. Makes the surface look more smoother.

5. plotting bound iteration case.

6. study 3 increasing the population size, by doing so we are increasing the probability of the function, to converge at global maximum.

7. Increasing the population size, creates a surface 3D.

8. plotting -f vs number of iterations.

PROGRAM: Genetic algorithm

%Genetic algorithm
clear all
close all
clc
% Creating 1D array
xi=linspace(0,0.6,150);
yi=linspace(0,0.6,150);

% Creating 2D array
[xx,yy]=meshgrid(xi,yi);

% Getting output from the stalagmite function and storing it
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

%study 1
iterations = 100;
tic
for i=1:iterations
[input1,f_val1(i)]=ga(@stalagmite,2);
x1(i)=input1(1);
y1(i)=input1(2);
end
time1 = toc;

% plotting unbound iteration case
figure(1);
subplot(2,1,1);
surfc(xx,yy,-f);
shading interp; %makes the surface look more smoother
hold on
plot3(x1,y1,-f_val1,'marker','*');
title('Study1: unbound inputs values');
xlabel('x_input');
ylabel('y_input');
zlabel('Z Value');
subplot(2,1,2);
plot(-f_val1);
axis([0,100,0,1]);
xlabel('iterations');
ylabel('z_max_1');


%study 2
tic
for i=1:iterations
[input2,f_val2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
x2(i)=input2(1);
y2(i)=input2(2);
end
time2 = toc;

% plotting bound iteration case
figure(2);
subplot(2,1,1);
surfc(xx,yy,-f);
shading interp; %makes the surface look more smoother
hold on;
plot3(x2,y2,-f_val2,'marker','*');
title('Study2: unbound inputs values');
xlabel('x_input');
ylabel('y_input');
zlabel('Z Value');
subplot(2,1,2);
plot(-f_val2);
axis([0,100,0,1]);
xlabel('iterations');
ylabel('z_max_2');


% study 3 increasing the population size
% by doing so we are increasing the probability of the function
% to converge at global maximum
opti=optimoptions('ga');
opti=optimoptions(opti,'PopulationSize',350); %increasing the popolation size
tic;
for i=1:iterations
[input3,f_val3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],opti);
x3(i)=input3(1);
y3(i)=input3(2);
end
time3=toc; %gives the time taken for study
time1;
time2;
time3;
x_optimum = x3(1);
y_optimum = y3(1);
z_optimum = -f_val3(1);
figure(3);
subplot(2,1,1);
surfc(xx,yy,-f); %creates a surface to visualise 3D
shading interp;
hold on;
plot3(x3,y3,-f_val3,'marker','*');
title('Study3: Bounded inputs with increased population size');
xlabel('x_inputs');
ylabel('y_inputs');
zlabel('Z Value');
subplot(2,1,2); plot(-f_val3) %
plot(-f_val3) %plotting -f vs number of iterations
xlabel('iterations')
ylabel('Z_max_3');

FUNCTION: stalagmite

function z =stalagmite(input)

x=input(1);
y=input(2);

fx_1 = (sin((5.1*pi.*x)+0.5)).^6;
fx_2 = exp((-4*log(2).*((x-0.0667).^2))./0.64);
fy_1 = (sin((5.1*pi.*y) + 0.5)).^6;
fy_2 = exp((-4*log(2).*((y-0.0667).^2))./0.64);

z =(-fx_1 .* fx_2 .* fy_1 .* fy_2);
end

OUTPUT:


ERROR: Im facing here some text errors, i tried to sole these errors my self.

CONCLUSION: Its big challange, i have learned lots of things in this in this challenge about genetic algorithm.



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.