Genetic Algorithm
AIM: To optimise the solution given a random number of samples
PROGRAM
close all
clear all
clc
num_cases=150;
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx yy]=meshgrid(x,y);
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
tic
for i=1:num_cases
[inputs,fopt(i)]= ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
time_study1=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')
%study2
tic
for i=1:num_cases
[inputs2,fopt2(i)]= ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
xopt2(i)=inputs2(1);
yopt2(i)=inputs2(2);
end
fopt2=-fopt2;
time_study2=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')
%figure(2)
subplot(2,1,2)
plot(fopt2)
xlabel('Iteration')
ylabel('Function minimum')
%Case_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],[1,1],[],[],options);
xopt3(i)=inputs3(1);
yopt3(i)=inputs3(2);
end
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('Iteration')
ylabel('Function minimum')
time_study3=toc
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.0667)^2)/0.64)));
fyy=exp(-4*log(2)*(((input_vector(2)-0.0667)^2)/0.64));
f=-(fx*fy*fxx*fyy);
end
FUNCTION
function [dtheta_dt]=ode_func1(t,theta,m,c,w,k)
theta1=theta(1);
theta2=theta(2);
dx1_dt=theta2;
dx2_dt=-c*theta2-k*theta(1)+2*cosd(w*t)
dtheta_dt=[dx1_dt;dx2_dt]
end
PRINCIPLE
- To find the maximum and minimum of a function we make use of optimisation tools.
- Genetic algorithm is one such optimisation tools for finding the global minima
- In the current problem it is asked to find the global maximum of the given function named stalagmate function
- In the current problem three cases are done
- Unbounded region-The region of interest is not specified for finding the global values. These will result in random optimised results over different time span
- Bounded region- The region is bounded to a definite space of interest(say 0-6 in both x and y direction). This result will given more accurate result but an solution is never achieved.
- Bounded region with increased population- The increased population is obtained by using an optimoption command. This is the most accurate result to obtain the optimised solution
RESULTS
1. UNBOUNDED INPUTS

2. BOUNDED INPUTS

3. BOUNDED INPUTS WITH INCRESED POPULATION

Thus the optimised solution of the stalagmite function is obtained to be 1.