Week 4.1 - Genetic Algorithm



Main program
clear all
close all
clc
%define search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%cerate 2dimensional mesh
[xx, yy] = meshgrid(x, y);
%evaluate 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) = stalagmite(input_vector);
end
end
%surf(xx,yy,F)
%shading interp
%[x, fval] = ga(@stalagmite, 2);
%study one
for i = 1:num_cases
[inputs, Fopt(i)] = ga(@stalagmite, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
hold on
surf(xx,yy,-F)
shading interp
plot3(xopt,yopt,-Fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('iterations')
ylabel('function minimum')
%study 2 with upper boun and lower bound
for i = 1:num_cases
[inputs, Fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],[]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-F)
shading interp
plot3(xopt,yopt,-Fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('iterations')
ylabel('function minimum')
%study 3 increase iteration
options = optimoptions('ga');
options = optimoptions(options, 'populationsize',230);
for i = 1:num_cases
[inputs, Fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,-F)
shading interp
plot3(xopt,yopt,-Fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with population')
subplot(2,1,2)
plot(-Fopt)
xlabel('iterations')
ylabel('function minimum')
stalagmite function
function [F] = stalagmite(input_vector)
fx1 = (sin((5.1*3.14).*input_vector(1) +0.5)).^6;
fy1 = (sin((5.1*3.14).*input_vector(2) +0.5)).^6;
fx2 = exp(((input_vector(1)-0.0667).^2/0.64).*(-4*log(2)));
fy2 = exp(((input_vector(2)-0.0667).^2/0.64).*(-4*log(2)));
F = -(fx1*fx2*fy1*fy2);
end
Genetic Algorithm:-
This is a method for solving constrained and unconstrained optimaization(liner optimaization) problems based on a natural selection process that mmics the biological evolution,for using this concept in matlab to find gobal maxima of the function.
syntex:-
x = ga(fun,no of inputs)
find unconstrained minimum x,from the function of fun with respective inputs given.
Stopping Criteria:-
1. The number of generators reaches the algorithm stops
2. the algorithm stops when the current population less than or equal to fitness limit
Overall explanation of code and result:-
1. the array of x y values are created and create 2D dimensional array xx and yy
2. using for loop to nested the arrays as input_vector this to be the input for stalagmite derivation
3. after that derivate the stalagmite function in seperate script.

4. In that result mention the (-sign) for get gobal maxima because inbult of ga to find global minima.
5. in first case study about unbounded inputs, plot were ploted with loop conditon, in surf command mentiond f as -f to get upward function.
6. in second case used some constrains and fix the constrain limits to get bounded outputs
7. in third case population is incerase with respect to ga and get gobal maxima plot
8. the x y values of function gets maximum values and displayed done
x = 0.066866
y = 0.066867