Challenge

Week 4.1 - Genetic Algorithm

clear all 
close all 
clc

% Difining Our search place 
x = linspace(0,0.6,150);         % 1 D array
y = linspace(0,0.6,150);         % 1 D array
num_cases = 50
% Creating mesh grid 
[xx yy] = meshgrid(x,y)
% Evaluating the stalagmite function
for i = 1:length(xx)
    for j =  1:length(yy)
        input_vector(1) = xx(i,j);
        input_vector(2) = yy(i,j);
        input_vector =[input_vector(1),input_vector(2)]; 
        f(i,j) = stalagmite(input_vector);

    end
end
 % Study_1 Statical behaviour
 for i = 1:num_cases
        [inputs, fopt(i)] = ga(@stalagmite,2);
        xopt(i) = inputs(1);
        yopt(i) = inputs(2);
   
 end
 figure(1)
 subplot(2,1,1)
 hold on 
 surfc(x,y,f)
 shading interp
 plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5)
 title('Unbounded inputs ')
 subplot(2,1,2)
 plot(fopt)
 xlabel('iteration')
 ylabel('Function minimum')

 % Study_2 Statical behaviour with upper and lower limit 
 for i = 1:num_cases
        [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
        xopt(i) = inputs(1);
        yopt(i) = inputs(2);
   
 end
 figure(1)
 subplot(2,1,1)
 hold on 
 surfc(x,y,f)
 shading interp
 plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5)
 title('Unbounded inputs ')
 subplot(2,1,2)
 plot(fopt)
 xlabel('iteration')
 ylabel('Function minimum')

 % Study_3 Increasing GA iteration
 options = optimoptions('ga')
 options = optimoptions(options,PopulationSize=170)
 for i = 1:num_cases
        [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
        xopt(i) = inputs(1);
        yopt(i) = inputs(2);
   
 end
 figure(1)
 subplot(2,1,1)
 hold on 
 surfc(x,y,f)
 shading interp
 plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5)
 title('Unbounded inputs ')
 subplot(2,1,2)
 plot(fopt)
 xlabel('iteration')
 ylabel('Function minimum')

Expalanation:

A genetic algorithm (GA) is great for finding solutions to complex search problems. They are often used in fields such as

engineering to create incredibly high quality products thanks to their ability to search a through a huge combination of

parameters to find the best match. For example, they can search through different combinations of materials and designs to

find the perfect combination of both which could result in a stronger, lighter and overall, better final product.

Genetic algorithms are based on the process of evolution by natural selection which has been observed in nature. They

essentially replicate the way in which life uses evolution to find solutions to real world problems. Surprisingly although

genetic algorithms can be used to find solutions to incredibly complicated problems, they are themselves pretty simple to use

and understand.

The genetic algorithm creates three types of children for the next generation:

  • Eliteare the individuals in the current generation with the best fitness values. These individuals automatically survive to
    the next generation.

  • Crossover are created by combining the vectors of a pair of parents.

  • Mutation children are created by introducing random changes, or mutations, to a single parent.

1) Evaluating the stalagmite function

In this step we used for loop for the function installation name stalagmite.

2) In stalagmite function we used below equation to solve 2 variable

f1 = (sin(5.1*pi*x+0.5)).^6
f3 = (sin(5.1*pi*y+0.5)).^6
f2 = exp((-4*log(2).*((x-0.0667).^2))/0.64);
f4 = exp((-4*log(2).*((y-0.0667).^2))/0.64);
A = -(f1.*f2.*f3.*f4)

3) Study_1 Statical behaviour


for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);

end
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5)
title('Unbounded inputs ')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('Function minimum')

For this step we have showed the stastical behaviour of population by using function stalagmite and ga command which is inbuilt in toolkit of matlab .

x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.

xopt(i) = inputs(1);
yopt(i) = inputs(2); and in this step input is divided into 2 coordinates.

4) Study_2 Statical behaviour with upper and lower limit


for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);

end
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5)
title('Unbounded inputs ')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('Function minimum')

In this step we have used upper and lower limit as a constraint.

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)

Rest steps are simillar to study 1

5) Study_3 Increasing GA iteration


options = optimoptions('ga')
options = optimoptions(options,PopulationSize=170)
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);

end
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5)
title('Unbounded inputs ')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('Function minimum')

In this step x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

we have increased the no of population to 170 in order to To increase more iteration .

ERRORS :

f1 = (sin(5.1*pi*x+0.5)).^6
f3 = (sin(5.1*pi*y+0.5)).^6
f2 = exp((-4*log(2).*((x-0.0667).^2))/0.64);
f4 = exp((-4*log(2).*((y-0.0667).^2))/0.64);
A = (f1.*f2.*f3.*f4)

To overcome

f1 = (sin(5.1*pi*x+0.5)).^6
f3 = (sin(5.1*pi*y+0.5)).^6
f2 = exp((-4*log(2).*((x-0.0667).^2))/0.64);
f4 = exp((-4*log(2).*((y-0.0667).^2))/0.64);
A = -(f1.*f2.*f3.*f4)

Conclusion :

We can use GA to find different combinations of materials and designs to find the perfect combination of both which could

result in a stronger, lighter and overall, better final product.

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.