Challenge

Week 4.1 - Genetic Algorithm

The aim is to write a code to optimise the stalagmite function and find the global maxima of the function,

the initial values is taken between 0 - 0.6 as 100 values and the number of iteration value is 50,

using the mesh grid command we are plotting in it,

and we are creating a function called stalagmite which is mentioned below,

now the 1st study is takes place using the stalagmite and no of iteration = no of sums of spaces at which the global maxima can attain

the study 2 and 3 are as follows with random numbers,

finally the optimum value are given as global maxima values.

GENETIC ALGORITHM : It is a method for solving both constrained and unconstrained optimization problems that is based on random selection. The genetic algorithm repeatedly modifies a population of individual solutions from the given value in the code.

x=linspace(0,0.6,100);
y=linspace(0,0.6,100);
num_spaces=50; 
[xx,yy]=meshgrid(x,y); 
for i=1:length(x) 
    for j=1: length(y)
         input_vector(1)=xx(i,j);
         input_vector(2)=yy(i,j);
    end
end
%study 1-unbounded 
tic;
for i=1: num_spaces;
    [results, fopt(i)]=ga(@stalagmite, 2);
    xopt(i)=results(1);
    yopt(i)=results(2);
end 
study_time1=toc;
figure(1);
subplot(2,1,1); 
surf(x,y,-f);
shading interp;
hold on;
plot 3(xopt, yopt, - fopt, 'marker','o');
subplot(2,1,2);
plot(-fopt);
xlabel('iteration');
ylabel('function maximum');
%study 2- bounded
tic; 
for i=1:num_spaces;
     [results,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0:0],[0.6;0.6]);
     xopt(i) = results(1);
     yopt(i) = results(2);
end
%study time2-toc
figure(2);
subplot(2,1,1); 
surf(x,y,-F);
shading interp; 
hold on;
plot3(xopt, yopt, -fopt, 'marker','o');
subplot(2,1,2);
plot(-fopt);
xlabel('iteration');
ylabel('function maximum');
%study 3- increased initial population 
tic; 
options-optimoptions('ga'); 
options=optimoptions(options,'populationsize',200);
for i=1: num_spaces
    [results, fopt(i)] = ga(@stagalmite,2,[],[],[],[],[0;2],[0.6;2.6],[],[], options);
    xopt(i)=results(1);
    yopt(i)=results(2);
end
study_time3 = toc;
figure(3);
subplot(2,1,1);
surf(x,y,-f);
shading interp;
hold on;
plot 3(xopt, yopt,-fopt,'marker','o'); 
subplot(2,1,2); 
plot(-fopt);
xlabel('iteration');
ylabel('function maximum');

stalagmite function,

%stalagmite function
function A = stalagmite(input_vector) 
x_input_vector(1);
y_input_vector(2);
fl=(sin(5.1 pi*x+0.5)).^6;
f2=exp((-4*log(2)*((X-0.6667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2)*((y-0.0667).^2)/0.64);
A =-(fi*f2*f3*f4);
end

outputs :

study 1

study 2

study 3

errors faced

adding (;) on the error shown places, error on stalagmite, instead of typo error stagalmite, are the error faced.

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.