Week 4.1 - Genetic Algorithm
Aim:
To write a progarm for Stalagmite Mathemitical Model & Genetic Algorithm.
%Stalagmite Mathematical model
clear all
close all
clc
%Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%Creating a 2Dimensional mesh
[xx yy]=meshgrid(x,y);
%Evaluating the 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)=functionofstalagmite(input_vector);
end
end
surfc(xx,yy,-f);
shading interp
xlabel('xvalue');
ylabel('yvalue');
numspaces=50;
%Study1-statistical behavior
for i=1:numspaces
[inputs,fopt(i)]=ga(@functionofstalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
studytime=tic;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbound inputs')
subplot(2,1,2)
plot(-fopt)
shading interp
xlabel('Iteration')
ylabel('function minimum')
tic
%Study2-statistical behavior with upper & lower limits
for i=1:numspaces
[inputs,fopt(i)]=ga(@functionofstalagmite,2,[],[],[],[],[0;0],[1:1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
%Study3-increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'populationsize',500);
tic
for i=1:numspaces
[inputs,fopt(i)]=ga(@functionofstalagmite,2,[],[],[],[],[0;0],[1:1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
%function of stalagmite
function f=functionofstalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1x=[sin(5.1*pi*x+0.5)]^6;
f1y=[sin(5.1*pi*y+0.5)]^6;
f2x=exp(-4*log(2)* (x-0.0667)^2/0.64);
f2y=exp(-4*log(2) *(y-0.667)^2/0.64);
f=-f1x*f1y*f2x*f2y;
end
Objective:
To plot graphs for Stalagmite Mathemitical Model & Genetic Algorithm.
Theory of GA:
A genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms . Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems. In a genetic algorithm, a population of candidate solutions to an optimization problem is evolved toward better solutions The evolution usually starts from a population of randomly generated individuals, and is an iterative process, with the population in each iteration called a generation. The algorithm terminates when either a maximum number of generations. A GA proceeds to initialize a population of solutions and then to improve it through repetitive application of the mutation, crossover, inversion and selection operators.
Syntax:
2.x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
3.x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)
Description of Syntax:
It finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun. In this x is the input,ga is genetic algorithm , fun is the function & nvars is the dimension.
2.x = ga(fun,nvars,A,b,Aeq,beq,lb,ub):
This Syntax defines that a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.) . In this syntax x is the input,ga is genetic algorithm, A & B are the Linear Inequality constraints, Aeq & beq are Linear equality constraints, lb & ub are the lower bounds & upper bounds respectively.
3.x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options):
This requires that the variables listed in intcon take integer values. (Set nonlcon=[] if no nonlinear constraints exist.) .In this syntax x is the input,ga is genetic algorithm, nvars is the dimension, A & B are the Linear Inequality constraints, Aeq & beq are Linear equality constraints, lb & ub are the lower bounds & upper bounds respectively.nonlcon is Non linear constraints, intcon is integer constraints, & options is optimization
Explanation of Output:
1.The first output or graph,plots the x & y values of Stalagmite Mathematical Model.
2.The second output or graph is the Study 1 of GA. In this, the plot mentions the unbounded outputs X, Y and Z values are plotted in the output
3.Whereas in the third output is th study 2 of GA. In this, the plot shows us the bounded outputs X, Y and Z values are plotted in the output
4.Both the 2nd and 3rd plots are ununiform, its not in a uniform way.
5.The fourth output or plot depends on the No. of population, as per population size the plotting has been done & this is also a bounded plot. In this also X,Y & Z values are plotted, comparing to 2nd and 3rd plot is those both plots ununiform , whereas the 4 plot is in a uniform way.