Challenge

Week 4.1 - Genetic Algorithm

AIM:

1. To write a MATLAB code to optimize the stalagamite function and to find the globa maxima of the function.

2. Explain the syntax of ga.

3. Plot graphs for all 3 studies of F.maximum vs number of iterations.

DESCRIPTION:

Genetic Algorithm:

The genetic algorithm works on a population using a set of operators that are applied to the population. A population is a set of points in a design space. The initial population is generated randomly by default. The algorithm computes the next generation of the population using the fitness of the individuals in the current generation.

Step by step working of the algorithm

1. The algorithm begins by creating a random initial population.

2. The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:

a,Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores

b,Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.

c,Selects members, called parents, based on their expectation

d,Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population

e,Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover

f,Replaces the current population with the children to form the next generation

3.The algorithm stops when one of the stopping criteria is met.

ga syntax

x = ga(fun,nvars);

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

STALAGMITE FUNCTION CODE:

%stalagmite

function [f] = stalagmite(input)

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

f1x = [sin((5.1*pi.*x) + 0.5)].^0.6;
f1y = [sin((5.1*pi.*y) + 0.5)].^0.6;
a = -4*log(2)*((x-0.0667).^2)/0.64;
b = -4*log(2)*((y-0.0667).^2)/0.64;
f2x = exp(a);
f2y = exp(b);

f = -f1x.*f1y.*f2x.*f2y;

end


MATLAB CODE FOR GA:

clear all
close all
clc

% GENETIC ALGORITHM OF A STALAGAMITE FUNCTION

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

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

for i = 1:length(xx)
    for j = 1:length(yy)
        input(1) = xx(i,j);
        input(2) = yy(i,j);
        f(i,j) = stalagmite(input);
    end
end
tic
study_time = toc
figure(1)
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
shading interp

%study - 1(unbounded points

tic
for i = 1:50
    [inputs fopt(i)] = ga(@stalagmite,2)
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
%plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','b')
title('unbounded points')
subplot(2,1,2)
plot(fopt)
study_time1 = toc

%study2 - bounded points
tic
for i = 1:50;
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
%plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','b')
title('bounded points')
subplot(2,1,2)
plot(fopt)
study_time2 = toc

%study-3 bounded points with higher population
options = optimoptions('ga')
options = optimoptions(options,'populationsize',180)
tic
for i = 1:iterations
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    xopt = inputs(1);
    yopt = inputs(2);
end

%plotting
figure(4)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','b')
title('boundend inputs with higher population')
subplot(2,1,2)
plot(fopt)
study_time3 = toc

OUTPUT:

Proper results were not obtained as i was frequenctly getting this error:

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.