Week 4.1 - Genetic Algorithm
Explanation for genetic algorithm
Genetic algorithm(GA) is search-based optimization technique based on the concepts of natural selction and genetics.it is used to find the optimum solution for complecated problems,which takes much longer time to solve. available solutions are combined/modified to get better and better solutions.
How GA works ?
initialy we import feature data its called initialization. the imported data is evaluated then parent candidate's solutions are selected for mating.Crossover and mutations are applied to generate new candidate solution.the new results are replaced with existing solution candidates for better solutions.
similar steps are repeated till optimum solution.
Genetic algorithm syntax in matlab
ga(@Stfunc,2)
above syntax is to determine minimum value of a function using MATLAB. in this syntax 'ga' represents genetic algorithm, '@Stfunc' is the function name & '2' represents no. of variables.
in general it is in form { x=ga(function, no. variables}
{x=ga(fun,no.variables,A,b,Aeq,beq,lb,ub)}
this syntax defines or specifies upper and lower bounds of a variable x so that solution will be such that x lies between upper(ub) & lower (lb) bounds.
MATLAB code
Main code
clear all
close all
clc
% Search space
x = linspace(0,0.6,150); %creating the search space of x and y
y = linspace(0,0.6,150);
n= 50; % no of iterations to be performed
% creating a 2D mesh x and y values through mesh grid command
[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) = Stfunc(input_vector);
end
end
%study_1
tic %clockfunction for calculating time
for i= 1:n % the funtion will be repeted for n no. of times
[inputs,Fopt(i)]=ga(@Stfunc,2); %(taking inputs and Optimum values after performing ga)
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_1=toc %clock function end
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
xlabel('x data');
ylabel('y data');
plot3(xopt,yopt,Fopt,'marker','o','MarkerSize',4);
title('un-inputs');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('function_maximum');
%study 2
tic
for i=1:n
[inputs,Fopt(i)]=ga(@Stfunc,2,[],[],[],[],[0:0],[1:1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_2 = toc
figure(2)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
xlabel('x data');
ylabel('y data');
plot3(xopt,yopt,Fopt,'marker','o','MarkerSize',4);
title('bounded inputs');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('function maximum');
%study3
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',450);
tic
for i=1:n
[inputs,Fopt(i)]=ga(@Stfunc,2,[],[],[],[],[0:0],[1:1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_3 = toc
figure(3)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
xlabel('x data');
ylabel('y data');
plot3(xopt,yopt,Fopt,'marker','o','MarkerSize',5);
title('bounded inputs with population');
subplot(2,1,2);
plot(Fopt);
xlabel('Iterations');
ylabel('Function maximum');
max_value=Fopt;
Code for stalagmite function
function[v]=Stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1_x1=(sin((5.1*3.14*x)+0.5))^6;
f1_y1=(sin((5.1*3.14*y)+0.5))^6;
f2_x2=exp(-4*log(2)*(x-0.0667)^2/0.64);
f2_y2=exp(-4*log(2)*(y-0.0667)^2/0.64);
[v]=-(f1_x1*f1_y1*f2_x2*f2_y2);
end
Similar steps are carried for studies so only study 1 is explained in the above function.