Week 4.1 - Genetic Algorithm
Aim:
To optimize the stalagmite function using genetic algorithm techniques using Matlab.
Theory:
Optimization
Optimization is the most common way of making something better(such as a plan, framework, or choice) as completely great, utilitarian, or compelling as could really be expected, this likewise assists us with tracking down the most ideal outcomes under a bunch of requirements. This process is executed again and again till the final result or the output is satisfactory. This incorporates boosting the item, strength, dependability, effectiveness, and usage.
Variables
Variable is a process that may vary during optimization is variables.
Constraints
During the optimization your output must not go beyond certain limits are known as constraints.
Genetic Algorithm:
The Genetic Algorithm is a strategy for settling both obliged and unconstrained improvement issues that depends on normal determination, the cycle that drives organic development. The Genetic Algorithm more than once changes a populace of individual arrangements. This is commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection.
The operators are (i) Selection, (ii) Reproduction/Crossover and (iii) Mutation. The new population obtained after selection, crossover and mutation is then used to generate another population.
Function Code:(Stalagmite Function)
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);
A =-(f1*f2*f3*f4);
end
Main code:
close all
clear all
clc
%input
x=linspace(0,1,300);
y=linspace(0,1,300);
num_spaces=50;
%forming 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);
f(i,j)=stalagmite(input_vector);
end
end
%Study 1- Unbounded
tic
for i=1:num_spaces
[results,f_opt(i)]= ga(@stalagmite,2)
x_opt(i)=results(1)
y_opt(i)=results(2)
end
study_time1=toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(x_opt,y_opt,-f_opt,'marker','o')
subplot(2,1,2)
plot(-f_opt)
xlabel('iteration')
ylabel('function maximum')
%Study 2- Bounded
tic
for i=1:num_spaces
[results,f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
x_opt(i)=results(1)
y_opt(i)=results(2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(x_opt,y_opt,-f_opt,'marker','o')
subplot(2,1,2)
plot(-f_opt)
xlabel('iteration')
ylabel('function maximum')
%Study 3- Increased Initial Population
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)
for i=1:num_spaces
[results,f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
x_opt(i)=results(1)
y_opt(i)=results(2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(x_opt,y_opt,-f_opt,'marker','o')
subplot(2,1,2)
plot(-f_opt)
xlabel('iteration')
ylabel('function maximum')
Code Explaination:
- Initially defining of x and y as inputs using linspace.
- A function known as Stalagmite_func is defined.
- 2 for loops are incorporated in the main code so that all the combinations of x and y values is taken in the stalagmite_func function.
- In the 1st study GA function runs for 50 iterations. Plots of the stalagmites and maximum values generated in each iteration is shown.
- In the 2nd study GA function is incorporated with lower and upper bounds and runs for 50 iterations. Plots of the stalagmites and maximum values generated in each iteration is shown.
- In the 3rd study GA function is incorporated with lower and upper bounds along with the population size of 300. Plots of the stalagmites and maximum values generated in each iteration is shown.
Results:

Fig 1.1 Case Study 1

Fig 1.2 Case Study 2

Fig 1.3 Case Study 3
Fig 1.1, 1.2, and 1.3 shows the different case study asked to performed where the variation can be identified easily.

Fig 1.4 Workspace
Error:
There was no error faced during this challenge.
Refrence: