Challenge

Week 4.1 - Genetic Algorithm

Aim: Write a code in MATLAB to optimise the stalagmite function and find the Global Maxima of the function.

Question asaked in video the meaning of ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);

answer:

GA

Find minimum of function using genetic algorithm

Sytax:

Description

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.

Note

Passing Extra Parameters explains how to pass extra parameters to the objective function and nonlinear constraint functions, if necessary.

x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').

x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').

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.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)

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.

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon) or x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) requires that the variables listed in intcon take integer values.

Note

When there are integer constraints, ga does not accept nonlinear equality constraints, only nonlinear inequality constraints.

x = ga(problem) finds the minimum for problem, a structure described in problem.

[x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

[x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

Code:

clear all
close all
clc
%Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
%Creating a dimensional mesh
[xx yy]=meshgrid(x,y);

%Evaluating stalagmite 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


tic
% Study 1 - statistical Behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc;

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','O','MarkerSize',5,'MarkerFaceColor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

tic
% Study 2 - Statistical behaviour with with upper and lower bounds
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end

study2_time=toc;

figure(2)
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')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

%Study 3 increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',170);

tic
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

study3_time=toc;

figure(3)
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')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

Stalagmite function code:

function f=stalagmite(input_vector)

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

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

Code explaination:

First we started with as always

clear all
close all
clc

Then we defining x and values and also number of cases in 50 values.

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

Then we creating 2 dimensional mesh with the help of meshgrid function.

%Creating a dimensional mesh
[xx yy]=meshgrid(x,y);

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

After that we evaluating the stalagmite function. In this the stalagmite function formula is converted into a code.

Formula

Code:

function f=stalagmite(input_vector)

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

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

Now we are making our first study-

tic
% Study 1 - statistical Behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc;

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','O','MarkerSize',5,'MarkerFaceColor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

In this we are taking number of iterations = num_cases and we are using genetic algorithm function to find the maxima.

in this we calculate the study_time by using tic toc command.

we store the inputs into xopt and yopt and fopt is the optimun value at which we get the maxima.

Now we need to plot a surface of x,y and f to get the 3D plot. In this we use shading interp function to get plain plot.

We use plot3 command to plot xopt,yopt and fopt to plot optimum maximas.

We also plot for fopt to visualise the each values at each iteration and to get a maximum value.

Now we are making second study in between bounded points.

tic
% Study 2 - Statistical behaviour with with upper and lower bounds
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end

study2_time=toc;

figure(2)
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')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

In this also we are taking number of iterations = num_cases and we are using genetic algorithm function.

in this we also calculate the study2_time.

we store the inputs into xopt and yopt and fopt is the optimun value at which we get the maxima.

Now we need to plot a surface of x,y and f to get the 3D plot. In this we use shading interp function to get plain plot.

We use plot3 command to plot xopt,yopt and fopt to plot optimum maximas.

We also plot for fopt to visualise the each values at each iteration and to get a maximum value.

Now we are making third study

%Study 3 increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',170);

tic
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

study3_time=toc;

figure(3)
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')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

In this we are increasing population size to 170 and to get the initial population we use 'optimoptions' command

In this also we are taking number of iterations = num_cases and we are using genetic algorithm function.

in this we also calculate the study3_time.

we store the inputs into xopt and yopt and fopt is the optimun value at which we get the maxima.

Now we need to plot a surface of x,y and f to get the 3D plot. In this we use shading interp function to get plain plot.

We use plot3 command to plot xopt,yopt and fopt to plot optimum maximas.

We also plot for fopt to visualise the each values at each iteration and to get a maximum value.

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.