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.

PROGRAM :

clear all 
close all
clc
num_cases=150
% Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% creating a 2dimensional mesh
[xx,yy]=meshgrid(x,y);
%Evaluating tha 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
surfc(xx,yy,f)
xlabel('xvalue');
ylabel('yvalue');
shading interp;
[inputs,fval]=ga(@stalagmite,2)

% study 1 unbound
tic
for i=1:num_cases
    % genetic algorithm for maximization
    [input,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=input(1);
    yopt(i)=input(2);
end
study_time1=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('Unbound inputs')
%figure(2)
subplot(2,1,2)
plot(fopt)
xlabel('Iteration')
ylabel('Function minimum')

% study 2 statistical behaviour witiin upper & lower bounds
tic
for i=1:num_cases
    [inputs2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
    xopt2(i)=inputs2(1);
    yopt2(i)=inputs2(2);
end
fopt2=fopt2;
study_time2=toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt2)
xlabel('Iterations')
ylabel('Functions maximum')

%study 3 Maximising the population size to optimise the result

options = optimoptions('ga');
options = optimoptions( options,'Populationsize',250);

tic
for i=1:num_cases
[inputs3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
xopt3(i)=inputs3(1);
yopt3(i)=inputs3(2);
end
study_time3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with optioptims function')
subplot(2,1,2)
plot(-fopt3)
xlabel('Iterarion')
ylabel('Function minimum')
rng('default');

FUNCTION :

function[f]=stalagmite(input_vector)
fx=(sin(5.1*pi*input_vector(1)+0.5))^6;
fy=(sin(5.1*pi*input_vector(2)+0.5))^6;
fxx=exp((-4*log(2)*(((input_vector(1)-0.667)^2)/0.64)));
fyy=exp((-4*log(2)*(((input_vector(2)-0.667)^2)/0.64)));
f=-(fx*fy*fxx*fyy);
end

EXPLANATION:

1) In our code we use linspace command for 1 dimensional & meshgrid command for 2 dimentional. we using for loop & we get input vector & function value. for function value we write a code for function & we calling function for using stalagmite function. we use here surfc & shading interp command we got figure stalagmite function.

2) we study Genetic Algorithm(GA). GA is a search e based optimization technique based on the principal of genetic & natural selection. It is used to find optimal solution.

3) Here we take 3 study

a) we take conditions for unbounde inputs. here we use for loop & we get [input,fopt(i)]=ga(@stalagmite,2).

we use tic & toc command. we got 2 plot in our figure 1.

b) our 2 nd study for upper & lower bound.

here took lower bound [0,0] & upper bound [0.6,0.6]. & use syntax [inputs2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]. we got result in our figure 2.

c) In our 3rd study we increase the population size to optimise the result. we took population size is 250.

[inputs3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options)

in our syntax option is used for max population. we got our result in figire 3.

RESULT:

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.