Week 4 - Genetic Algorithm
Here is the program to solve Genetic Algorithm for stalagmite equation.
This program is helpful to understand the function of Genetic Algorithm(GA).
Concept of GA:
The syntax of GA is as follows:
- x= ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
- x= ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
- x= ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) gaevaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
- x= ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines 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.)
- x= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)
- x= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.
- x= ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntContake integer values.
- x= ga(problem) finds the minimum for problem, where problem is a structure.
- [x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.
- [x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.
- [x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.
Stalagmite mathematical model is used in this program which creates local maxima and minima so GA is used to find the global maxima of the equation.
Function:
function f = stalagmite_function(input_vector)
x = input_vector(1);
y = input_vector(2);
term1 = [sin(5.1*pi.*x+0.5)].^6;
term2 = exp(-4*log(2).*(x-0.0667).^2/0.64);
term3 = [sin(5.1*pi.*y+0.5)].^6;
term4 = exp(-4*log(2).*(y-0.0667).^2/0.64);
f = -(term1.*term2.*term3.*term4);
end
The function of GA can be improved by using three methods.
- Using Unbounded inputs.
- Using Bounded inputs.
- By specifying population size for iterations.
Each case is studied in the program by observing and improving the function of GA. Time taken for each study is shown in the output as study_time.
Case 1 (Unbounded inputs):
Here, there is no boundary to the inputs is given hence GA shows global maxima all over the graph as shown in fig(1). This method is least accurate as compared to the other two methods as the precision and accuracy for calculating global maxima is minimum in this case. Time taken to calculate the global maxima is shown in seconds as study_time1.
Case 2 (Bounded inputs):
In this case, Bounded inputs are provided to increase the accuracy and functioning of GA. By use of this method, many points outside the inputs gets eliminated as the inputs are specified. By doing so, GA calculates the global maxima point inside the specified bounded inputs. But again, this method is not that effective and accurate. Here many points are shown as the global maxima as shown in the fig(2). Time recorded for this case is shown as study_time2.
Case 3 (Specifying population size for iterations):
This method is most effective and accurate among all three methods used. In this method, along with the bonded inputs, population size is specified which increases the accuracy of the GA function to calculate global maxima. The time recorded for this study is shown as study_time3. By using this method, global maxima can be found accurately. The graph for this case is shown in fig(3).
Code:
clear all
close all
clc
%writtng a program to find out global maxima for stalagmite function
%generating 1D array
x = linspace(0,0.7,150);
y = linspace(0,0.7,150);
%defining no cases for ga
num_cases = 50;
% creating 2D array from 1D array
[xx yy] = meshgrid(x,y);
%command used to record time
tic
%creating for loop
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_function(input_vector); %calling function
end
end
%plotting 3D graph
surfc(xx,yy,f)
shading interp %used to shade the surface of graph
[x ,fval] = ga(@stalagmite_function, 2); %use of GA
%labelling axes
xlabel('X values')
ylabel('Y values')
zlabel('Z values')
%Study 1 (Unbounded inputs)
tic
for i = 1:num_cases
[input,fopt(i)] = ga(@stalagmite_function,2);
xopt(i) = input(1);
yopt(i) = input(2);
end
Study_time1 = toc
% crating new figure window
figure;
hold on
subplot(2,1,1); %subplot command is used to plot graphs in same figure window
hold on
surfc(xx,yy,f);
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',4,'markerfacecolor','r');
xlabel('X values')
ylabel('Y values')
zlabel('F values')
title('Unbounded inputs') %title of graph
subplot(2,1,2)
plot(fopt);
xlabel('Number of cases')
ylabel('Function values')
%Study 2 (Bounded inputs)
tic
for i = 1:num_cases
[input,fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.7;0.7]);
xopt(i) = input(1);
yopt(i) = input(2);
end
Study_time2 = toc
figure;
subplot(2,1,1);
surfc(xx,yy,f);
shading interp;
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',4,'markerfacecolor','r');
xlabel('X values')
ylabel('Y values')
zlabel('F values')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt);
xlabel('Number of cases')
ylabel('Function values')
%Study 3 (polpulation size)
%creating optimisation option
option = optimoptions('ga')
option = optimoptions(option,'populationsize',170)
tic
for i = 1:num_cases
[input,fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.7;0.7],[],[],option);
xopt(i)= input(1);
yopt(i) = input(2);
end
Study_time3 = toc
figure;
subplot(2,1,1);
surfc(xx,yy,f);
shading interp;
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',4,'markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
zlabel('F values')
title('Population size')
subplot(2,1,2)
plot(fopt);
xlabel('Number of cases')
ylabel('Function values')
%end of program
Outputs:
Figure 1
Figure 2
Figure 3
Figure 4
Errors face: No errors are faced during writing and execution of code.
Reference: https://in.mathworks.com/help/gads/ga.html