Challenge

Week 4 - Genetic Algorithm

Writing code in MATLAB to optimize the stalagmite function and find the global maxima of the function

First, we have to create the stalagmite function for optimizing for that we know the formula so putting the values in the formula and we use negative sig to find the maximum we get the function as follows

function :

function F =stalagmite(input)

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

f1x = (sin((5.1*pi.*x)+0.5)).^6;
f2x = exp((-4*log(2).*((x-0.0667).^2))./0.64);
f1y = (sin((5.1*pi.*y) + 0.5)).^6;
f2y = exp((-4*log(2).*((y-0.0667).^2))./0.64);

F =-(f1x .* f2x .* f1y .* f2y);
end

after getting the function we let consider

- search space in which we have to optimize so for x and y consider having values 150 values between 0 to 0.6

- and number of repetition or iteration for optimization is n = 50

now for the function, we create the input by using meshgrid and use as input to function so we can get the value of the function at various values of x and y

1) Now for the first case, the maximum value of the function is found out with no restrictions, no bounds, and no limits. The genetic algorithm is free to choose the population then reproduce and mutate.

2) To get the same value each time we run the program, we need to restrict the program within specific bounds namely upper bound and lower bound.

3) Even the results from the bounded cases are not the same every time the program terminates. Hence we now need to increase GA iterations. We can raise it to any value, we have chosen it as 300. In this case, syntax for GA is different than the previous case. GA has to give this population size via an option.

code :

clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%creating a 2 diamensional mesh
[xx yy] = meshgrid(x,y);
% evaluating the 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
%study 1 - Statistical behavior
n=50; %number of iteration
for i = 1:n
 [input, fopt(i)] = ga(@stalagmite, 2);
 xopt(i)=input(1);
 yopt(i)=input(2);
end
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 input')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function maximum')
%study2 -statistical behaviour with upper and lower bounds
tic
for i = 1:n
 [input, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
 xopt(i) = input(1);
 yopt(i) = input(2);
end
study2time = toc
figure(2)
1
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded input')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('function maximum')
%study3 incresing ga iteration
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);
tic
for i = 1:n
 [input, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],
[],options);
 xopt(i) = input(1);
 yopt(i) = input(2);
end
study2time = 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')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('function maximum')

Results: figure 1 shows the maximum value of the function is found out with no restrictions

figure2 with specific bounds namely upper bound and lower bound

figure3 represents the ga with population size.

Error Faced: many challenges were faced solved by using correct notation and symbols but the main problems of not getting maxima and getting different values are solved by using technical support.

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.