Week 4.1 - Genetic Algorithm
program:
clear all
close all
clc
%defining our search space
x=linspace(0,0.5,150);
y=linspace(0,0.5,150);
% creating a 2Dimensional mesh
[xx yy]=meshgrid(x,y);
num_cases=50;
%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
tic
%study_1(unbound)
for i=1:num_cases
[inputs , fval(i)]=ga(@stalagamite_f,2);
xval(i)=inputs(1);
yval(i)=inputs(2);
end
study1_time=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(bounded)
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
study2_time=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
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)
for i=1:num_cases
[inputs,fval(i)]=ga(@stalagamite_f,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
fval(i)=inputs(1);
fval(i)=inputs(2);
end
study3_time=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')
explanation:
genetic algorithm:A genetic algorithm is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that generates population of points at each iteration The best point in the population approaches an optimal solution.
You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous
x= ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension of function.
finds a local minimum x = ga(fun,nvars,A,B)x to function, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
finds a local minimum x = ga(fun,nvars,A,B,Aeq,beq)x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
code explanation:
clear all
close all
clc
x=linspace(0,0.6,150)
y=linspace(0,0.6,150)
% creating a 2Dimensional mesh
[xx yy]=meshgrid(x,y)
num_cases = 50
first we take the valves of x and y taking 150 valves between interval 0 and 0.6 and no of iterations is 50
%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)
we take 2d array of x and y using meshgrid command and store in input_vector array to call the stalagmite function
we create another function file for stalagmite function here 'ga' is inbuilt command solves minimum function and to solve maximum function we have taken output f as -f
stalagmite 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)
study1:
we take no of iterations into num_cases we use ga command to solve function maximum we store the outputs into inputs which contain maximas and fval
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')
we store the results into xopt,yopt now we need to plot surface of x,y,-f to get 3d plot we use plot3 command for plotting in 3d and fval plot to show the no if iterations
tic and toc commands to record the time
in this case we failed to reach maxima
study_time1=2.215sec
study2(bounded):
we take lower and upper bounds of x and y ga is initiated with random population only
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_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')
inthe same way of study1 we plot x,y,-fval and also plot -fval to get 3d plot
here optimum valves are some what better than study1 but could not give the exact maxima
study_time2 =5.417
study3(bounded with modified population size):
we increase initial population from 50 to 250
tic
options = optimoptions('ga')
options = optimoptions(options,'populationsize',250)
for i=1:num_cases
[inputs,fval(i)]=ga(@stalagamite_f,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options)
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')
here optimoptions command is used to set initial population and we plot the valves x,y,-fval
time execution is high in this case and we got the accuracy to find out correct maxima using stalagmite function
study_time3 =14.05
stalagmite 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)
reference link: