Challenge

Week 4 - Genetic Algorithm

Aim- Write code in MATLAB to optimize the stalagmite function and find the global maxima of the function and explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.

Introduction about Genetic algorithm

Genetic Algorithm is a method of solving both Constrained and Unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies the population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves"towards an optimal solution.

Genetic algorithm is used to solve optimization problems across many industries like engineering, logistics and transportation. There are many optimization tools but the genetic algorithm (ga) is widely used because of its simplicity and easy application to a wide range of optimization problems.

Genetic Algorithm Generates a population of points at each iteration. The best point in the population approaches an optimal solution and also Selects the next population by computation which uses random number generators.

Syntax

matlab code-
clear all 
close all 
clc

%def search space
x=linspace(0,1,200);
y=linspace(0,1,200);

%creating 2-d

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

%evaluating the staligmate function

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
%assinging population size

tic
for i=1:150
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1,1],[],[])
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
total_time=toc
%plotting

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp

plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
axis([0 100 0.4 0.6])
xlabel('iterations')
ylabel('function maxima')

% bounded outputs with upper and lower limts
tic;
for k=1:150;
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study_time2 = toc
figure(2);
subplot(2,1,1) ;
surfc(xx,yy,-f);
hold on;
shading interp;
plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'Markerfacecolor','r');
subplot(2,1,2);
plot(-fopt);
xlabel('No of iterations');
ylabel('Function maximum');
title('bounding Condition');
% incresing ga iterations
tic
options = optimoptions('ga');
options = optimoptions(options,'populationsize',200);

for k=1:150
[inputs,fopt(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end
study_time3 = toc
figure(3);
subplot(2,1,1);
surfc(xx,yy,-f);
hold on;
shading interp;
plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'Markerfacecolor','r');
subplot(2,1,2);
plot(-fopt);
xlabel('No of iterations');
ylabel('Function maximum');
title('Increasing GA Iterations');
​
Staligmite function code-
function [fx_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);

fx_y = -(f1_x.*f1_y.*f2_x.*f2_y);
%(-ve sign)to calculate maxima

end​
the stalagmite function is created from the equations and formulaes provided and later called upon in the main code. we have used -ve sign here as ga always returns the minima of the function but here we needed the maxima. so the -ve sign will return the opposite of minima i.e maxima of the function.

Results-

Study-1: Unbouded Condition

time taken =11.89sec.

Study-2: upper and lower bound condition

time taken 10 sec.

Study 3:Increasing the ga iterations

time taken=36.28sec.

Output-

Conclusion- The ga in Matlab gives Minimum value of a function we need to convert it to maximum value by -ve sign.

We can also see the difference in time by various conditions given for the 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.