Week 4.1 - Genetic Algorithm
%% Find minimum function by using gentetic algoritham %%
clear all
close all
clc
%defining our search space
x=linspace(0,0.5,100);
y=linspace(0,0.5,100);
% creating a 2Dimensional mesh
[xx yy]=meshgrid(x,y);
%evaluating the stalagamite 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)=stalagamite_f(input_vector);
end
end
num_cases=50;
tic
%study_1
for i=1:num_cases
[inputs , fval(i)]=ga(@stalagamite_f,2);
xval(i)=inputs(1);
yval(i)=inputs(2);
end
study_1time=toc;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f);
shading interp
plot3(xval,yval,fval,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fval)
title('unbounded')
xlabel('iteration')
ylabel('function maximum')
%study_2
for i=1:num_cases
[inputs,fval(i)]=ga(@stalagamite_f,2,[],[],[],[],[0,0],[0.6,0.6]);
fval(i)=inputs(1);
fval(i)=inputs(2);
end
stydy_2time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f);
shading interp
plot3(xval,yval,fval,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fval)
title('bounded')
xlabel('iteration')
ylabel('function maximum')
%study_3
for i=1:num_cases
[inputs,fval(i)]=ga(@stalagamite_f,2,[],[],[],[],[0,0],[0.6,0.6],[],[]);
fval(i)=inputs(1);
fval(i)=inputs(2);
end
study_3time=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f);
shading interp
plot3(xval,yval,fval,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fval)
title('bounded inputs with modified population size')
xlabel('iteration')
ylabel('function maximum')
%%stalagamite function%%
function [f] = stalagamite_f(input_vector)
x=input_vector(1);
y=input_vector(2);
fx1=(sin((5.1*pi*x+0.5)).^6);
fy1=(sin((5.1*pi*y+0.5)).^6);
fx2=exp(-4*log(2)*(((x-0.6667).^2)/0.64));
fy2=exp(-4*log(2)*(((y-0.6667).^2)/0.64));
f=-(fx1.*fy1.*fx2.*fy2);
end
Explanation:
Gentic algoritham is used to find a minimum function .A genetic algorithm is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions
A genetic algorithm is a search-based algorithm used for solving optimization problems in machine learning. This algorithm is important because it solves difficult problems that would take a long time to solve
=>Population: This is a subset of all the probable solutions that can solve the given problem.
=>Chromosomes: A chromosome is one of the solutions in the population.
=>Gene: This is an element in a chromosome.
=>Allele: This is the value given to a gene in a specific chromosome.
=>Fitness function: This is a function that uses a specific input to produce an improved output. The solution is used as the input while the output is in the form of solution suitability.
=>Genetic operators: In genetic algorithms, the best individuals mate to reproduce an offspring that is better than the parents. Genetic operators are used for changing the genetic composition of this next generation.
syntax:
x = ga(fun,nvars) in this fun means stalagmitefunction and nvars means no.of variables of functionused
1:
first we take arrays by using linsapce command it gives 100 values between 0 to 0.5 with same intervals
x=linspace(0,0.5,100);
y=linspace(0,0.5,100);
2: by using meshgrid to create 2d arrays
[xx yy]=meshgrid(x,y)
3: using nest loop we assign values automatically from this array into array named input_vector.input vector is input of 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)=stalagamite_f(input_vector);
4: using this syntax x = ga(fun,nvars) to find local unconstrained minimumof function and variables of function and using tic-toc commands to find the running time of the function.using different syntax of ga to find minimum function for bounded and unbounded inputs,linear inequalities ,non linear ,unconstrained values.
tic
for i=1:num_cases
[inputs , fval(i)]=ga(@stalagamite_f,2);
[inputs,fval(i)]=ga(@stalagamite_f,2,[],[],[],[],[0,0],[0.6,0.6]);
[inputs,fval(i)]=ga(@stalagamite_f,2,[],[],[],[],[0,0],[0.6,0.6],[],[]);
xval(i)=inputs(1);
yval(i)=inputs(2);
end
study_1time=toc
5.stalagmite function is separate by using formula we give input vector is input of stalagmite function we assume x= input_vector(1)
and y=input_vector(2)
and dot operation is used in this because elementwise operatoin is carried out.minus(-) sign is given to output of function because ga process normally gives minimum values then it gives global maximum
6:subplot : -creates axis in tiled positions
surfc :-contour plot under surfaceplot
shading interp:- using this command to plot smoother and hide grid lines
marker command used for marks the cotact points with sphere symbols
ERRORS:
i forget some parenthesis and dot operations


