Challenge

Optimising a function using genetic algorithm (ga)

Objective

-Write a program to optimise the stalagmite function and find global maxima value.

-Explain the command ga in matlab.

Program

close all

clear all

clc

%created two inputs for the stalagmite

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

%converting input array in 2d array
[xx, yy]=meshgrid(x,y);

%setting number of iterations
n=50;

%calculating stalagmite function and storing it in the f
for i=1:length(x)
for j=1:length(y)
ip(1)=xx(i,j);
ip(2)=yy(i,j);
f(i,j)=stalagmite(ip);
end
end

%study of 3 different cases with 3 different conditions for similar inputs

%study 1-Unbounded
tic; %setting the timer
for k=1:n
[res, fo(k)]=ga(@stalagmite, 2); %using ga getting two values
xo(k)=res(1);
yo(k)=res(2);
end
study_time1=toc; %timer Ended

%plotting the two graphs in first figure
figure(1);
subplot(2,1,1); %creating subplot
hold on
surfc(xx,yy,-f); %first plot is of the values stored in 2d array and f variable in surface format
shading interp
plot3(xo,yo,-fo,'marker','o','markersize',5,'markerfacecolor','r'); %in same subplot plotting the values of the study1
subplot(2,1,2); %creating second subplot

plot(-fo); %plotting only optimised function fo from the case 1

%setting timer for the case 2-Bounded with upper and lower limit


tic; %setting the timer
for k=1:n
[res, fo(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); %performed similary as case 1 with two constraints

xo(k)=res(1);
yo(k)=res(2);
end
study_time2=toc; %timer end

%plotting for the case 2 is similar as that of 1 with two subplots
figure(2);
subplot(2,1,1);
hold on
surfc(xx,yy,-f);
shading interp
plot3(xo,yo,-fo,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2);
plot(-fo);

%study 3 -bounded with three constraints


options=optimoptions('ga'); %using optimoptions command we will get the optimum value for the ga and storing it in the options variable
options=optimoptions(options,'populationsize',300); %now again using the optimoptions we are optimising the options variable with the 300 iterations.and again storing in options variable
tic; %setting a timer
for k=1:n
[res, fo(k)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); %calculating optimum values with more contraints
xo(k)=res(1);
yo(k)=res(2);
end
study_time3=toc; %timer ending

%Plotting the graphs as similar to the previous study
figure(3);
subplot(2,1,1);
hold on
surfc(xx,yy,-f);
shading interp
plot3(xo,yo,-fo,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2);
plot(-fo);

Stalagmite Function-

%This function is created from the standard stalagmite function.

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

Explanation-

What is stalagmite?

It is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmites are typically composed of calcium carbonate, but may consist of lava, mud, peat, pitch, sand, sinter and amberat.

Actual Stalagmite

What is stalagmite Function?

It is the standard stalagmite function.

-In above program we optimised the stalagmite function using 'ga' algorithm to get the global maxima.

-We created the input variables x and y and then converted them into 2d array xx and yy then we stored then in ip variable and called the stalagmite function that we previously created.

-After that we studied 3 cases with 'ga' command

1. Unbounded

2.Bounded with upper and lower limit

3.Bounded with upper and lower limt and options constraint calculated from the optimoptions

So, What is the ga(genetic algorithm)?

It is inbuilt optimization function in matlab. It is used to get the optimised value of the given result. Here we are going to get global maxima by 'ga' function.

ga command constraints are as follows

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)

fun=represents function which is to be optimised

nvar=Number of variables

A,b = Linear inequalities if any present ...none in our case

Aeq,beq = Linear equalities if any present ...none in our case

lb,ub = upper limits and lower limits of the variables which we used in second and third case.

nonlcon = Not present in our case

Options = It minimizes with the default optimization parameters replaced by values in option used in our third case.

-In three cases we used [a,b]=ga(x) this type of calling. In this type of calling 'ga' returns the a= minimum value of function

b= value of fitness function at 'a'.

-In all three cases we are plotting this value 'b' which we have stored in 'fo' and we also plotted all the values stored in xx,yy, and f in surface format and we also plotted the xo,yo,and fo in similar format.

-For the FIRST CASE - Unbounded

for 50 iterations we got this result which is random plot and the optimum global maximum lies in 0,1 which is not the optimum result

For SECOND CASE - Bounded with upper limit and lower limit

For 50 iterations we got 4 different values lying between 0.7-1 which is optimized very good and error is less

For THIRD CASE- Bounded with three constraints upper and lower limit and options

In this case we further optimised the function with options ,upper limit and lower limit, we got single point as global maxima and it varies in limit between values 0.99999980 to 0.99999986 which is very close to accurate.

-We plotted all the graphs with '-ve' sign and that's why we got plot in reverse direction if we plot with '+ve' sign we will get reverse plot.

-Hence we were able to optimize function get the global maxima value of the function

Result and Conclusion-

- By using our program and function we were able to study three different cases and also were able to get close to accurate ans by that

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.