Week 4.1 - Genetic Algorithm
clear all
close all
clc
%stalagmite function
%inputs
%in linspace command 1st value represents starting point of value 2nd point
% refers ending point of value and last point refers no of values in between
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%num of iteration to run GA i.e 30
num_cases=30;
%meshgrid transforms vectors into 2d array
[xx,yy]=meshgrid(x,y);
%evaluating the stalagmite function using the for loop
%the loop staring from 1 to maxmimum value of xx
for i=1:length(xx)
%the loop starting from 1 to maxmimum value of yy
for j=1:length(yy)
%input_vector (1) is assigned to xx
input_vector(1)=xx(i,j);
%input_vector (2) is assigned to yy
input_vector(2)=yy(i,j);
%calling the function input_vector is the input value for stalagmite function
f(i,j)=stalagmite_func(input_vector);
end
end
%study1-statistical behaviour(unbounded) of GA
%tic and toc command is used to calculate the study time
tic
for i=1:num_cases
%calculating the optimized value of the function
[inputs, fval(i)]=ga(@stalagmite_func,2);
%inputs1 and 2 are assigned to x_out and y_out
x_out(i) = inputs(1);
y_out(i) = inputs(2);
end
% tic toc command to display the timetaken to study
study1_time=toc;
%plotting the graph
figure(1)
%subplot divides the current figure into rectangular panes
subplot(2,1,1)
% surfc generating the 3D surface plot with contour plot under it
surfc(xx,yy,-f)
%xlable is used to define xaxis
xlabel('x')
%ylabel is used to define yaxis
ylabel('y')
%title is used for title of plot
title('case(1)unbounded inputs');
%shading interp varies the color in each line segment
shading interp
%hold on is used to add plot in existing plot
hold on
%plot is used to create 2D line data
plot3(x_out,y_out,-fval,'marker','o','markerFaceColor','r')
%subplot divides the current figure into rectangular panes
%plot is used to create 2D plots
subplot(2,1,2)
plot(-fval)
%xlable is used to define xaxis
xlabel('no of iterations');
%ylabel is used to define yaxis
ylabel('function maximum');
% figure1 -from the results as shown its clear that the results are not
% much accurate and giving a lot of variation in optimizing function
%thus further optimization is required .
%STUDY2 - statistical behaviour with boundation of GA with upper and lower bounds
%the difference over here is providing the upper and the lower limit in
%the GA so that the search space is reduced with the upper and lower boundary
%tic toc command is used calculate the study time
tic
for i=1:num_cases
%calculating the optimized value of the function
%[0;0] is upper and lower limit of x
%[1;1] is upper and lower limit of y
[inputs,fval(i)]=ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
x_out(i)=inputs(1);
y_out(i)=inputs(2);
end
study2_time=toc;
%plotting the bounded inputs
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
xlabel('x')
ylabel('y')
title('bounded inputs');
shading interp
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerFaceColor','r');
subplot(2,1,2)
plot(-fval)
xlabel('no of iterations')
ylabel('function maximum')
%FIGURE2-after providing the boundary limits the variation in value of
%function has reduced comparing study 1
%study3 -statistical study with increasing in populations size(GA)
%optimoptions command is used to increase the size of population
%it will be working on the large set of numbers and which will led to
%increase the possibilities of finding the better fitness of the function
%increasing the size to 170
options=optimoptions('ga');
options=optimoptions(options,'populationsize',170);
%tic toc command is used to calculate the study time
tic
%[0;0] is upper and lower limit of x
%[0.5;0.5] is upper and lower limit of y
for i=1:num_cases
[inputs,fval(i)]=ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
x_out(i)=inputs(1);
y_out(i)=inputs(2);
end
study3_time=toc;
%plotting bounded inputs increased population size
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x');
ylabel('y');
title('bounded inputs with increased population size');
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerFaceColor','r')
subplot(2,1,2)
plot(-fval)
xlabel('no of iterations');
ylabel('function maximum');
%FIGURE 3 -now we getting the most accurate result which is between 0.999997 to 0.999998.
%stalagmite function
function[f] = stalagmite_func(input_vector)
f1_x=(sin((5.1*pi).*input_vector(1)+0.5)).^6;
f1_y=(sin((5.1*pi).*input_vector(2)+0.5)).^6;
f2_x=exp((((input_vector(1)-0.0667).^2)/0.64).*(-4*log(2)));
f2_y=exp((((input_vector(2)-0.0667).^2)/0.64).*(-4*log(2)));
f1=f1_x*f1_y*f2_x*f2_y;
f=-f1;
end
%end of program
outputs
inputs =0.0668 ,0.0668
studytime1=2.0927
studytime2=2.0532
studytime3=4.4675
REFERENCES
STALAGMITE
stalagmite structures are formed from the cave that point upward .
stalagmite and stalactite functions are mathematical functions which
represents shapes similar to that of the stalagmite functions.
the stalagmite function is defined below
f(x,y)=f1,xf2,xf1,yf2,y
where,
f1,x=[sin(5.1*pi)*x+0.5)]^6
f1,y=[sin(5.1*pi)*y+0.5)]^6
f2,x=exp[-4ln(2)*(x-0.0667^2)/0.664]
f2,y=exp[-4ln(2)*(y-0.0667^2)/0.664]
in the challenge -f is used because for getting the maxmimum value
GENETIC ALGORITHM(GA)
genetic algorithm is a method for solving constrained and unconstrained
optimization problems that baased on natural selection and the process
that drives biological evolution.genrally GA repeatedly modifies
population of individual solutions
GA is only used for the minimum function to maximize the function we inversed the function(-)
SYNTAX used in this challenge
x=ga(fun,nvars)finds a local unconstrained minimum x to the objective function
nvars is the number of design variables of function(fun)
x=ga(fun,nvars.A,b,Aeq,beq,lb,ub,nonlcon,options)
x=variable to store output of genetic algorithm
nvars is the number of design variables of function
fun is the function
A and b are the linear inequalities of the function,if any otherwise they are set as empty[]
Aeq and beq are used to represent the equality constrains
lb and ub are the lower and upper bounds of the variables of the function
nonlcon is the fuction that is specified using a function handle .
set options for genetic algorithms by using optimoptions.
STOPPING CRITERIA
1.the algorithm stops when the number of generation reaches the value of generations.
2.the algorithm stops after running fo an amount of time equal to the time limit.
3.the algorithm stops when the value of the fitness function for the best
point in the current population is less than or equal to fitness limit.