Week 4.1 - Genetic Algorithm
%program to optimise stalagmite function and find the global maxima of the function
clear all
close all
clc
%input variable space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
iteration=50;
%creating 2D array
[xx,yy]=meshgrid(x,y);
for i=1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
%function calling
f(i,j)=stalagmite(input_vector);
end
end
%study-1 (unbounded)
tic
for i=1:iteration
%to find optimum value of inputs
[inputs,fopt(i)]=ga(@stalagmite,2)
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
study_time1=toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-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 maximum')
%study-2 (bounded)
tic
for i=1:iteration
%to find optimum value of inputs
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
study_time2=toc
figure(2)
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)
xlabel('iterations')
ylabel('function maximum')
%study-3 (increased population)
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',200)
for i=1:iteration
%to find optimum value of inputs
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
study_time3=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')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%stalagmite function definition
function [A] = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log(2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.0667).^2)/0.64);
%negative sign for maximizing the function
A=-(f1*f2*f3*f4);
end






GENETIC ALGORITHM
Concept of Genetic Algorithm
Genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population evolves toward an optimal solution.
Syntax for ga in MATLAB
- 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.) ga evaluates 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 C eq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and C eq(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, Aeq, beq, lb, ub, nonlcon, intcon) or x = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, intcon, options) requires that the variables listed in intcon take integer values.
Step-Wise Explanation of Code
- First of all, I have given the input variables search spaces and set the total number of iterations.
- Then by using ‘meshgrid’ command, I have created 2D arrays and used two ‘for’ loops to set the input variable values, and then the ‘stalagmite’ function is created and calculated the function equation.
- The ‘stalagmite’ function is then called and stored, and the ‘for’ loops are ended.
- The first study is the unbounded case, here by using genetic algorithm the optimum values of inputs and function are found out without bound.
- Timer is used to calculate the time required to solve the problem and the surface is plotted.
- By using ‘plot3’ command, optimum values of variables and functions are plotted and labelling is done.
- The second study is the bounded case, here by using genetic algorithm the optimum values of inputs and function are found out with bound and the remaining steps is similar as unbounded study
- The third study is the increased population case, here by using genetic algorithm the optimum values of inputs and function are found out with bound and increased population. Then the remaining steps is similar as bounded study.
- Finally, I have obtained three plots for three studies and the program is ended.
Errors Faced While Programming
I have faced an error while defining the ‘stalagmite’ function
Steps Taken to Overcome the Errors
The Matlab have showed the error while defining the ‘stalagmite’ function and I resolved it by correctly following the syntax for defining the function.
REFERENCES
- https://skill-lync.com/knowledgebase/genetic-algorithm-explained
- https://in.mathworks.com/help/gads/genetic-algorithm.html