Week 4 - Genetic Algorithm
1. AIM :-
write the stalagmite function to find global maxima of the function.
clearly expain the concept of GA in your own word s and also explain the syntax for GA.
plot all the 3 graph and show necessary details.
2. THEORY :-
. Genetic Algorithm is an optimization method used to find minimum solution. it is more suited to solve non smooth functions,since there are other solver which takes less time than GA to solve smooth or relatively less complex problems. It is based on the darwins theory of bio lo gical evolution.
it starts by generationg populatin samples with a random number generated. It is very flexible as the numbers can be explicitly entered by the user also then it calculates function value at the points and select a number of points with lowest vvalue. It ten makes further population from these parent points through mutation and crossover.
the fration of the population can be set manuallay also. then GA finds the function value at the points. this process is repeated until said condition is achived.
2. CODE :-
clear all
close all
clc
x = linspace(0,0.6,100); % x range
y = linspace(0,0.6,100); % y range
[xx,yy] = meshgrid(x,y); % creating a meshgrid with x and y values
f = zeros(100); % pre initialising f matrix to make the code faster
for i = 1:length(xx)
for j = 1:length(yy)
c(1) = xx (i,j);
c(2) = yy(i,j);
f(i,j) = func1(c); % calling func1 in our program
end
end
options=optimoptions('ga');
%changing population size from default 50 to 200
options=optimoptions(options,'Populationsize',200);
%changing function creation function to use <= equalities, more about this
%at the end of the code
%options=optimoptions(options,'CreationFcn', @gacreationlinearfeasible);
%changing function tolernce value to terminate optimization
options = optimoptions(options,'FunctionTolerance',1e-4);
iter=50; %number of iterations
coor=zeros(iter,2); %preinitialising coordinate matrix to make the code faster
fmax=zeros(iter,1); %preinitialising fmax matrix to make the code faster
for i=1:iter
%calling ga function with linear inequalities. more about this at the
%at the end of the code
[coor(i,:),fmax(i)]=ga(@func1,2,[1,-1],0,[],[],[0;0],[0.6;0.6],[],[],options);
%calling ga function with just lower and upper bounds
[coor(i,:),fmax(i)]=ga(@func1,2);
end
subplot(2,1,1) %1st plot
hold on
surfc(xx,yy,-f) %plotting the contours of func1
shading interp
plot3(coor(:,1),coor(:,2),-fmax)
xlabel('X Axis');
ylabel('Y Axis');
subplot(2,1,2) %2nd plot
plot(-fmax)
axis([0 iter 0 1.2])
xlabel('Iteration number');
ylabel('fmax');
coor(25,:)
FUNCTION:-
function y= func1(x)
f1=(sin((5.1*pi*x(:,1)+0.5))).^6;
f2=(sin((5.1*pi*x(:,2)+0.5))).^6;
f3=exp(-4*log(2)*((x(:,1)-0.0667).^2)/0.64);
f4=exp(-4*log(2)*((x(:,2)-0.0667).^2)/0.64);
y=-f1.*f2.*f3.*f4;
end
SYNTAX:-
[coor(i,:),fmax(i)]=ga(@func1,2,[1,-1],0,[],[],[0;0],[0.6;0.6],[],[],options)
> @func1 - is assigning our function.
> 2 - number of variables.
> [1,-1] - linear inequalities A
> [] - linear inequalities B
>[],[] - linear equalities
>[0.6;0.6] - upper ound and lower bound resp.
>[],[] - non linear constrains and integer variable indices resp.
EXPLATIONS -
1) first we creat a governing function on which optimaization is to be performed.
2) then we creat x range and y range using command 'linspace'
3) we the n creat a meshgrid with x and y values.
4)then using for loop we substitute values of xx and yy in our function.
5) Now we use 'ga ' optimization method.
6) As we have to find the maximum and not the minimum in this case we just use -ve sign for our function.
7)we now provide lower bound and upper bound to the program so as to restrict ga in that area only.
8) after that we use surfc command to plot the surface. and also plot the graph.
RESULTS-
bounded point
bounded with higher popin