Week 4.1 - Genetic Algorithm
Genetic Algorithm
Genetic Algorithm:
- A genetic algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems.
- A genetic algorithm (GA) is a heuristic search algorithm used to solve search and optimization problems. This algorithm is a subset of evolutionary algorithms, which are used in the computation. Genetic algorithms employ the concept of genetics and natural selection to provide solutions to problems.
working:
A genetic algorithm works by building a population of chromosomes which is a set of possible solutions to the optimization problem. Within a generation of a population, the chromosomes are randomly altered in hopes of creating new chromosomes that have better evaluation scores.
Basic structure:

Optimization:
Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs.
Optimization refers to finding the values of inputs in such a way that we get the best output values.
The definition of best varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.

Procedure:
- Dechen fined stalagmite function separately. this function called in GA program
- define the x,y inputs using linspace command
- then these input vectors are transferred as array [xx, yy]
- Now these values create different matrixes as inputs
- calling the stalagmite function and we can get various values of x and y
- We need to calculate the maximal value, which can be calculated using the GA command which is inbuilt function IN MATLAB
- Defined num of cases to needs run GA with help of the loop made to run each iteration
- GA generally gives minimum value but we need maxima hence we can just inverse the stalagmite function by introducing Negative sign.
- The total study time calculates with using tic and tock commands
- the results are plotted using surfc and plot commands
- GA is made to run 3 itirations with each iteration having bounded inputs and unbounded inputs and increasing the population size
code:
clear all
close all
clc
x = linspace(0,1,150);
y =x;
[xx,yy] = meshgrid(x,y);
n = 50;
%study-1 stastical model
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(input_vector);
end
end
tic
for i = 1:n
[inputs, fopt1(i)] = ga(@stalagmite, 2);
xopt1(i) = inputs(1);
yopt1(i) = inputs(2);
end
figure(1)
study1_time = toc;
subplot(2,1,1)
surfc(x,y,f);
shading interp
hold on
plot3(xopt1, yopt1, fopt1, 'b')
subplot(2,1,2)
plot(fopt1)
xlabel('optimal value')
ylabel('no. of itirations')
% study 2 - uuper and lower bounds
tic
for i =1:n
[inputs2, fopt2(i)] = ga(@stalagmite, 2, [], [], [], [], [0;0], [1,1]);
xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
surfc(x,y,f);
shading interp
hold on
plot3(xopt2, yopt2, fopt2, 'b')
subplot(2,1,2)
plot(fopt2)
xlabel('no. of itirations')
ylabel('optimum function')
% study-3 setting population size
options = optimoptions('ga', 'PopulationSize', 500);
tic
for i =1:n
[inputs3, fopt3(i)] = ga(@stalagmite, 2, [], [], [], [], [0;0], [1,1],[],[], options);
xopt3(i) = inputs3(1);
yopt3(i) = inputs3(2);
end
study2_time = toc;
figure(3)
subplot(2,1,1)
surfc(x,y,f);
shading interp
hold on
plot3(xopt3, yopt3, fopt3, 'b')
subplot(2,1,2)
plot(fopt3)
xlabel('no. of itirations')
ylabel('optimum function')
Writing genetic algorithm format in MATLAB is as follows:
x = ga(fun, nvars, A,b,Aeq,beq,Ib,ub,nonicon,options)
- x - is local minima
- fun - calling function
- nvars - no of variables
- A,b - inequalities exist in function
- Aeq,beq - inequalities
- in - lower bound
- ub - upper bound
- nonicon - represent equalities and inequalities
- options - used for additional operations
STALANGMITE FUNCTION fallows:

%code for stalagmite function:
function[out] = stalagmite(input_vector)
% defining input conditions
x =input_vector(1);
y = input_vector(2);
%solving the equations to obtain the values of function
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.0667)^2)/0.64));
f = f1x*f1y*f2x*f2y;
% output is set to negative in order to measure the global maxima instead of global minima
%in the genetic algorithm
out = -f;
end
RESULTS:
The fallowing results obtained on running GA.
each itiration = (50 itirations); Study time 5 sec.

- Study 1 plot results all over the place. The size of the population is small no limits have been defined for the plot
- From the plots we can see how dta is over the plce and algoritham defined global maxima many points
- run the simulation 5 sec vlues changed every secound.

- Study 2 runs with lower and upper limits and higher values suggesting the population is superiour
- however the values are not best so we can conduct third study

- increasing the population time upto 500.
- Run the simulation it take time and we can get minimam value between -0.99999985 to -0.99999975
- In study 3 algoritham has converged