Challenge

Week 4.1 - Genetic Algorithm

Genetic Algorithm

The genetic algorithm is based on the natural evolution of biological species. It uses natural selection as the main tool for solving both constrained and unconstrained problems. It selects the individual from the difined population as parents and let them produce next generation which have evolved over previous generation and hence this repeated evolution give rise to optimal solution.

There should be some selection rule defined to select the parents and how the childrens or next generation are produced. It is generally stochastic in nature.

In crossover rule two parents combines to form next generation. While random variations are applied in mutation rule over the parents for production of next generation.

SYNTAX of ga

ga function have typically 11 arguments for very complex problems to simple problem involving only 2 or 3 arguments.

A typical application is found in unconstrained problems as given below:

[x,fval]=ga(@func, no_variable) % this code minimizes the objective function "func" for x without any constrain. where fval is the value of fitness function for x and no_variable is the number of design variables.

For constrained problem such as a*x<=b then

[x,fval]=ga(@func, no_variable, a, b)

further if there are more constrain then we can go further:

[x,fval]=ga(@func, no_variable, a, b,c,d) for a*x<=b and c*x<=d

further

[x,fval]=ga(@func, no_variable, a, b,c,d,Lower_bound,Upper_bound) for a*x<=b and c*x<=d

where we want the solution in a defnite range Lower_bound<x<upper_bound

further

[x,fval]=ga(@func, no_variable, a, b,c,d,Lower_bound,Upper_bound, non_linear_constraint, Options)

non_linear_constrains for any nonlinerity in the problem and Options for defining population size, score, etc.

The following code will determine the global maxima for the given stalgmite function:

close all
clear all
clc

%% 1st Case run 

%Domain of Operation
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
iterations=50;

% 2D mesh creation
[xx, yy] = meshgrid(x,y);

%evaluation of the stalagmite function
for i = 1:length(xx)
    for j = 1:length(yy)
        Input_vect(1) = xx(i,j);
        Input_vect(2) = yy(i,j);
        f(i,j)=stalagmite_gen(Input_vect(1),Input_vect(2));
    end
end

%applying genetic algorithm for calculating the Maxima of the function
%[val,fval] = ga(@stalagmite,2)

for i=1:iterations
    [val, fopt(i)]=ga(@stalagmite,2);
    xopt(i)=val(1);
    yopt(i)=val(2);
end
Fmax=-fopt;     %GA minimizes the fun. hence -ve for max.

figure(1)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,"marker","*","markersize",4,"Color","m")
title("unbounded inputs")
xlabel("x data")
ylabel("y data")

subplot(2,1,2)
plot(Fmax)
title("Fmax vs Iterations")
xlabel("No. of Iterations")
ylabel("Fmax")


%% Second Case Run
for i=1:iterations
    [val, fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=val(1);
    yopt(i)=val(2);
end
Fmax=-fopt;     %GA minimizes the fun. hence -ve for max.

figure(1)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,"marker","*","markersize",4,"Color","m")
title("unbounded inputs")
xlabel("x data")
ylabel("y data")

subplot(2,1,2)
plot(Fmax)
title("Fmax vs Iterations")
xlabel("No. of Iterations")
ylabel("Fmax")
%}
%% Third Case Run
options = optimoptions("ga");
options = optimoptions(options,"PopulationSize",150)
for i=1:iterations
    [val, fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i)=val(1);
    yopt(i)=val(2);
end
Fmax=-fopt;     %GA minimizes the fun. hence -ve for max.

figure(1)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,"marker","*","markersize",4,"Color","m")
title("unbounded inputs")
xlabel("x data")
ylabel("y data")

subplot(2,1,2)
plot(Fmax)
title("Fmax vs Iterations")
xlabel("No. of Iterations")
ylabel("Fmax")

Stalagmite function is given as:

function [fun] = stalagmite(Val)
x=Val(1);
y=Val(2);
    f1=-(sin(5.1*pi*x+0.5)).^6;
    f2=exp(-4*log(2)*((x-0.0667).^2)/0.64);
    f3=(sin(5.1*pi*y+0.5)).^6;
    f4=exp(-4*log(2)*((y-0.0667).^2)/0.64);
    fun=f1.*f2.*f3.*f4;

end

Stalagmite generation function is given as:

function [fun] = stalagmite(x,y)
    f1=-(sin(5.1*pi*x+0.5)).^6;
    f2=exp(-4*log(2)*((x-0.0667).^2)/0.64);
    f3=(sin(5.1*pi*y+0.5)).^6;
    f4=exp(-4*log(2)*((y-0.0667).^2)/0.64);
    fun=f1.*f2.*f3.*f4;

end

Result of 1st Study:

Result of 2nd Study:

Result of 3rd Study:

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.