Week 4.1 - Genetic Algorithm
function A = stagalmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1 = (sin(501*pi*x+0.5)).^6;
f2 = exp((-4*log(2)*((y-0.0667).^2))/0.64);
f3 = (sin(501*pi*y+0.5)).^6;
f4 = exp((-4*log(2))*((y-0.667).^2)/0.64);
A = -(f1*f2*f3*f4);
end
clear all
close all
clc
%inputs
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_spaces=50;
%crating a 2D array
[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);
f(i,j)=stagalmite(input_vector);
end
end
%study 1-unbounded
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,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
plot3(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(@stagalmite,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',250)
for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time3=toc
figure(3)
subpolt(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')
AIM:
To write a code in matlab tooptimise the stalagmite function and find the global maxima of the function.
GENETIC ALGORITHM (GA):
Genetic Algorithm(GA) is a search based optimization technique based on the principles of Genetic and Natural Selection. It is frequently used to find optimal or near_optimal solutions of difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research and in machine learning.
Natural has always been a great source of inspiration to all mankind.Genetic Algorithms are search based Algorithms based on the concepts of natural selection and genetics. GA are a subset of a much largerbranch of computation known as EVOLUTIONARY COMPUTATION.
SYNTAX FOR GA:
We can give the inputs by any one of the following ways:
x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga (fun,nvars,A,b,Aeq,beq,lb,ub)......and ect.
FUNCTION:
First to be creat the function of to do the program of stalagmite function to inputs to the
needed values of this case.
PROGRAM:
To given inputs of the variables to the num_spaces and to creat the mesh grid of arrays
using for loops.
To be write the code of the study 1 is unbounded values.
Then to be write the study 2 comments is the bounded values of this function.
finally the write the code of study 3 is a increased initial population.
OUTPUTS:



ERRORS:
I put it in the name of the function is input_vectors so i run the comment the error will be the unrecognized function of the stalagmite function so it will comes the error.
I whould like to chane the name of the input_vectors to stalagmite it overcomes the errors.
then run the code i got an outputs.

CONCLUTION:
To created the global maxima of the stalagmite function to get the output plots.