Challenge

Week 4 - Genetic Algorithm

AIM:

To write a code to optimize the stalagmite function and find the global maxima of the function

Theory behind GA :

Five phases are considered in a genetic algorithm :

1.Initial population.

2.Fitness function.

3.Selection.

4.Crossover.

5.Mutation.

Genetic Algorithms are a method that work based on Darwins theory of natural selection. 'The fittest survives,and has a greater chance of breeding and passing forward its genetic information'. Lets say we have an equation unsolvable by analytical methods,or an optimisation problem that takes too long to solve. First the program randomly generates a population of solutions.then these solutions are evaluated by a function adequate to the problem,determining how well they fit. The ones with the best fit will have a greater chance of being selected to breed and generate the next population.Finally the selected solutions are recombined amongst themselves creating a new population.Random shifts just like mutationsalso happen in order to introduce more variability whereas the first generation was completely random second is based towards solutions with better results.After several iterations they tend to converge to the optimum value.

Syntax for defining GA:

The GA syntax in matlab is used to find the minimum of function using genetic algorithm. There are several ways to define the GA function depending on the problems being solved.The syntax used for the study is shown below:

[x,fval] = ga(fun,nvars)

where

x =optimal solution/variables;

fun = Objective function;

nvars = Number of variables;

[x,fval] = ga(fun,nvars,A,B,Aeq,Beq,lb,ub)

where

A,B = Linear Inequality constraints

Aeq,Beq = Linear Equality constraints

If there are no linear equalities and inequalities then A,B,Aeq,Beq are set to []

[x,fval] = ga9fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,Intcon,options)

where

nonlcon = Nonlinear constraints

Intcon = Integer values

options = Optimization options

If there are no nonlinear constraints and integer values, the nonlcon and Intcon are set to []

The stalagmite function is as follows ;

Program :

%Define stalagmite function
function[F] = 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);
[F] = -(f1_x.*f1_y.*f2_x.*f2_y);
end

We have added negative sign in the stalagmite function to get the maxima from this function.

Genetic Algorithm case 1:

Step 1 - Clear workspace,command window and closing all plots

Step 2 - Define our search space by using linspace command

Step 3 - Calculate a 2D mesh using meshgrid command

Step 4 - Evaluating th stalagmite function for each value of x and y

Step 5 - Using GA command with unbounded inputs means optimum value maybe come from outside of search space, Result shown below : Figure (1)

clear all
close all
clc

%Define our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%creating a 2D Mesh
[xx,yy] = meshgrid(x,y);

%Evaluating the stalgmite 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

%study one - statical behaviour
num_cases = 50;
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,30);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end

F = -f;
figure(1)
subplot(2,1,1)
hold on
surf(x,y,F)
shading interp
plot3(xopt,yopt,-fopt,'marker','O','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
xlabel('x')
ylabel('y')
zlabel('f')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')


Genetic Algorith Case 2:

Using GA command with bounded inputs means optimum value will come from search space,however there will be a huge variation in optimum value.Result shown below Figure(2)

clear all
close all
clc

%Define our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%creating a 2D Mesh
[xx,yy] = meshgrid(x,y);

%Evaluating the stalgmite 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

%statical behaviour with upper and lower bounds
options = optimoptions('ga','populationsize',50);
tic
num_cases = 50;
for i =1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end

sudy2_time = toc
figure(2)
F = -f ;
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,-fopt,'marker','O','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
xlabel('x')
ylabel('y')
zlabel('f')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maxima')





Genetic Algorithm case 3:

Using GA command with bounded inputs means optimum value will come from inside the search space.However this time to avoid huge variations in optimum value we have increased initial population size to 200.(If we do not specify initial population in program matlab take 50 as initial population)

clear all
close all
clc

%Define our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%creating a 2D Mesh
[xx,yy] = meshgrid(x,y);

%Evaluating the stalgmite 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

%increasing GA iterations
options = optimoptions('ga','populationsize',200);
tic
num_cases= 50;
for i = 1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

sudy3_time = toc
figure(3)
F = -f ;
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,-fopt,'marker','O','markersize',5,'markerfacecolor','r')
title('bounded inputs with modified population size')
xlabel('x')
ylabel('y')
zlabel('f')
subplot(2,1,2)
plot(-fopt)
axis([0 50 0,1.5]);
xlabel('Iterations')
ylabel('Function maxima')
Fmax = -min(fopt);
X = mean(xopt);
y = mean(yopt);










Results:

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.