Week 4.1 - Genetic Algorithm
The following submission includes code to perform optimization using Genetic Algorithm in following format)
1.Code to optimize a stalagmite function for global minima and maxima (code/output/screenshots)
2.Study the iteration output for optimizing gentic algorithm(code/output/screenshots)
3.Concept of genetic algorithm to solve minimization of stalagmite function using one variable format(code/output/screenshots)
4. Citation/references
1. OPTIMIZE STALAGMITE FUNCTION
1.1. CODE
%W3 MATLAB BASIC Stalagmite-Optimization Problem
%Skill-Lync: Bhavin Chaudhary 27 MARCH 2022
%Stalagmite function helps in solving a global optimisation problem
%The stalagmite function uses the fundamental of Genetic Algorithm
%OBJECTIVE
%1. To optimize(Minimize) given challenge using Genetic Algorithm
%2. To optimize(Maximize) given challenge using Genetic Algorithm
%The given Objective function in challenge is a function of two variable x and y
%let x(1)=x and x(2)=y are the initial population
%f(x,y)=f1x.f1y.f2x.f2y hence f(x(1),x(2))=f1.f2.f3.f4 is the population characteristics or property
%f1x=(sin(5.1*pie*x+0.5))^6 Hence f1=(sin(5.1*pie*x(1)+0.5))^6
%f1y=(sin(5.1*pie*y+0.5))^6 Hence f2=(sin(5.1*pie*x(2)+0.5))^6
%f2x=exp((-4*log(2)*(x-0.0667)^2)/0.64) Hence f3=exp((-4*log(2)*(x(1)-0.0667)^2)/0.64)
%f2y=exp((-4*log(2)*(y-0.0667)^2)/0.64) Hence f4=exp((-4*log(2)*(x(2)-0.0667)^2)/0.64)
%
%OBJECTIVE 1-TO OPTIMIZE(Minimum) USING GA
syms x %variable x
syms x1 %initial value of x
syms x2 %final value of x
syms y %variable y
syms y1 %initial value of y
syms y2 %final value of y
syms N %number of iteration
syms n %length of array element
disp('Code to optimize(minimize) the function using Genetic Algorithm')
N=input('enter the number of times GA loops the analysis(N>50)=')
% Boundary conditions of variables/ defining parent
n=input('enter the number of elements in data array=')
x1=input('enter the initial value of x=')
x2=input('enter the final value of x=')
x=linspace(x1,x2,n);
y1=input('enter the initial value of y=')
y2=input('enter the final value of y=')
y=linspace(y1,y2,n);
%creating a 2d mesh that is parent pool
[xx,yy]=meshgrid(x,y);
%evaluating stalgamite function
for i=1:length(xx)
for j=1:length(yy)
parent(1)=xx(i,j); %parent from ith row & jth row of xx square matrix
parent(2)=yy(i,j); %parent from ith row & jth row of yy square matrix
f(i,j)=w3_mb_function_stalagmite(parent);
end
end
surfc(xx,yy,f) %generates the 3D Plot
shading interp %command converts counter lines into smooth surface
xlabel('x axis-x chromozome')
ylabel('y axis-y chromosome')
zlabel('z axis-fitness')
options=optimoptions('ga');
options=optimoptions(options,'Populationsize',N);
%generate the iteration usin gentic algorithm inbuild function
[output1,fval1]=ga(@w3_mb_function_stalagmite,2,[],[],[],[],[x1;y1],[x2+10;y2+10],[],[],options);
disp('point where function is minimum')
x_minima=output1(:,1)
y_minima=output1(:,2)
disp('minimum value of function')
f_min=fval1
%OBJECTIVE 2 TO OPTIMIZE(Maximum) USING GA
%here we use negative function to define the maximum
%hence modifying function to just negative gives maximum values
x=linspace(x1,x2,n);
y=linspace(y1,y2,n);
[XX,YY]=meshgrid(x,y);
%evaluating stalgamite_reverse function for maximize
for i=1:length(XX)
for j=1:length(YY)
parent_reverse(1)=XX(i,j);
parent_reverse(2)=YY(i,j);
F(i,j)=w3_mb_function_stalagmite_reverse(parent_reverse);
end
end
options=optimoptions('ga');
options=optimoptions(options,'Populationsize',N);
[output2,fval2]=ga(@w3_mb_function_stalagmite_reverse,2,[],[],[],[],[x1;y1],[x2;y2],[],[],options);
disp('point where function is maximum')
x_max=output2(:,1)
y_max=output2(:,2)
f_max=-fval2
%Conclusion-the GA is stochastical method which runs on probability of random selection
%Hence the solution always differ and we cannot have perfect maxima value.
function [generation]=w3_mb_function_stalagmite(parent)
f1=(sin(5.1*pi*parent(1)+0.5))^6;
f2=(sin(5.1*pi*parent(2)+0.5))^6;
f3=exp((-4*log(2)*(parent(1)-0.0667)^2)/0.64);
f4=exp((-4*log(2)*(parent(2)-0.0667)^2)/0.64);
generation=(f1*f3*f2*f4);
end
function [generation_reverse]=w3_mb_function_stalagmite_reverse(parent_reverse)
f1=(sin(5.1*pi*parent_reverse(1)+0.5))^6;
f2=(sin(5.1*pi*parent_reverse(2)+0.5))^6;
f3=exp((-4*log(2)*(parent_reverse(1)-0.0667)^2)/0.64);
f4=exp((-4*log(2)*(parent_reverse(2)-0.0667)^2)/0.64);
generation_reverse=-(f1*f3*f2*f4);
end
1.2. OUTPUT
>> w3_mb_stalagmite_optimization
Code to optimize(minimize) the function using Genetic Algorithm
enter the number of times GA loops the analysis(N>50)=50
N =
50
enter the number of elements in data array=150
n =
150
enter the initial value of x=0
x1 =
0
enter the final value of x=0.6
x2 =
0.6000
enter the initial value of y=0
y1 =
0
enter the final value of y=0.6
y2 =
0.6000
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
point where function is minimum
x_minima =
9.8632
y_minima =
10.2142
minimum value of function
f_min =
0
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
point where function is maximum
x_max =
0.0668
y_max =
0.2618
f_max =
0.8472
>>
>> w3_mb_stalagmite_optimization
Code to optimize(minimize) the function using Genetic Algorithm
enter the number of times GA loops the analysis(N>50)=70
N =
70
enter the number of elements in data array=150
n =
150
enter the initial value of x=0
x1 =
0
enter the final value of x=0.6
x2 =
0.6000
enter the initial value of y=0
y1 =
0
enter the final value of y=0.6
y2 =
0.6000
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
point where function is minimum
x_minima =
10.3041
y_minima =
9.7629
minimum value of function
f_min =
0
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
point where function is maximum
x_max =
0.0668
y_max =
0.0668
f_max =
1.0000
>>

For maximization a surface reflection is made by negative of given objective function.
Hence, minimum of reflection is maximum of actual.

Conclusion: for given objective function on iterating more than 65 we get same fmax=1 and f min=0 values.
1.3. SCREENSHOTS




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2.STUDY ITERATION OUTPUT
2.1. CODE
%W3 MATLAB BASIC GENETIC ALGORITHM-Study the GA iteration outputs
%Skill lync- Bhavin Chaudhary- 27 March 2022
%OBJECTIVE
%1. To study the GA iteration in unbounded condition
%2. To study the GA iteration in bounded condition
%3. To study the GA iteration output by increasing the number of iteration in bounded condition
%4. To study the GA iteration by increasing population size in bounded condition
clear all
close all
clc
%OBJECTIVE 1
disp('STUDY 1 Program to plot ga iteration output in unbounded condition')
syms x %variable x
syms y %varaible y
syms p_size %population size and also the variable length
syms num_iteration %number of iteration
l=input('enter the length of variables=')
x1=input('enter the minimum value of x=')
x2=input('enter the maximum value of x=')
x=linspace(x1,x2,l);
y1=input('enter the minimum value of y=')
y2=input('enter the maximum value of y=')
y=linspace(y1,y2,l);
[xx yy]=meshgrid(x,y);
num_iteration=input('enter the number of iteration(50 or greater)=')
%here we will use tic and toc command to understand the time it took to iterate the code
tic
% we use for command to perform the ga equals to number of iteration
for k=1:num_iteration
for i=1:length(xx)
for j=1:length(yy)
parent_reverse(1)=xx(i,j);
parent_reverse(2)=yy(i,j);
f(i,j)=w3_mb_function_stalagmite_reverse(parent_reverse);
end
end
%here inputs is the 2 column matrix of having value of x and y such that function value is optimium
[inputs, foptimum(k)]=ga(@w3_mb_function_stalagmite_reverse,2);
xoptimum(k)=inputs(1);
yoptimum(k)=inputs(2);
end
study1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
xlabel('x axis-x chromozome')
ylabel('y axis-y chromosome')
zlabel('z axis-fitness')
shading interp
plot3(xoptimum,yoptimum,foptimum,'marker','o','markersize',5,'markerfacecolor','r')
title('skill lync-GA STUDY 1- unbounded iteration inputs')
subplot(2,1,2)
plot(foptimum)
xlabel('iteration')
ylabel('function minimum value')
pause(10)
%----------------------------------------------------------------------------------------------------%
%OBJECTIVE 2
disp('STUDY 2 Program to plot ga iteration output in bounded condition')
syms x %variable x
syms y %varaible y
syms p_size %population size and also the variable length
syms num_iteration %number of iteration
l=input('enter the length of variables=')
x1=input('enter the minimum value of x=')
x2=input('enter the maximum value of x=')
x=linspace(x1,x2,l);
y1=input('enter the minimum value of y=')
y2=input('enter the maximum value of y=')
y=linspace(y1,y2,l);
[xx yy]=meshgrid(x,y);
num_iteration=input('enter the number of iteration(50 or greater)=')
%here we will use tic and toc command to understand the time it took to iterate the code
tic
% we use for command to perform the ga equals to number of iteration
for k=1:num_iteration
for i=1:length(xx)
for j=1:length(yy)
parent_reverse(1)=xx(i,j);
parent_reverse(2)=yy(i,j);
f(i,j)=w3_mb_function_stalagmite_reverse(parent_reverse);
end
end
%here inputs is the 2 column matrix of having value of x and y such that function value is optimium
[inputs, foptimum(k)]=ga(@w3_mb_function_stalagmite_reverse,2,[],[],[],[],[0;0],[1;1]);
xoptimum(k)=inputs(1);
yoptimum(k)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('x axis-x chromozome')
ylabel('y axis-y chromosome')
zlabel('z axis-fitness')
plot3(xoptimum,yoptimum,foptimum,'marker','o','markersize',5,'markerfacecolor','r')
title('skill lync-GA STUDY 2- bounded iteration inputs')
subplot(2,1,2)
plot(foptimum)
xlabel('iteration')
ylabel('function minimum value')
pause(10)
%---------------------------------------------------------------------------------------------------%
%OBJECTIVE 3
disp('STUDY 3 Program to plot ga iteration output with increase iteration and in bounded condition')
syms x %variable x
syms y %varaible y
syms p_size %population size and also the variable length
syms num_iteration %number of iteration
syms l %length of variables
l=input('enter the length of variables=')
x1=input('enter the minimum value of x=')
x2=input('enter the maximum value of x=')
x=linspace(x1,x2,l);
y1=input('enter the minimum value of y=')
y2=input('enter the maximum value of y=')
y=linspace(y1,y2,l);
[xx yy]=meshgrid(x,y);
num_iteration=input('enter the number of iteration(100 or greater)=')
%here we will use tic and toc command to understand the time it took to iterate the code
tic
% we use for command to perform the ga equals to number of iteration
for k=1:num_iteration
for i=1:length(xx)
for j=1:length(yy)
parent_reverse(1)=xx(i,j);
parent_reverse(2)=yy(i,j);
f(i,j)=w3_mb_function_stalagmite_reverse(parent_reverse);
end
end
%here inputs is the 2 column matrix of having value of x and y such that function value is optimium
[inputs, foptimum(k)]=ga(@w3_mb_function_stalagmite_reverse,2,[],[],[],[],[0;0],[1;1]);
xoptimum(k)=inputs(1);
yoptimum(k)=inputs(2);
end
study3_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('x axis-x chromozome')
ylabel('y axis-y chromosome')
zlabel('z axis-fitness')
plot3(xoptimum,yoptimum,foptimum,'marker','o','markersize',5,'markerfacecolor','r')
title('skill lync-GA STUDY 3-increase iteration & bounded iteration inputs')
subplot(2,1,2)
plot(foptimum)
xlabel('iteration')
ylabel('function minimum value')
pause(10)
%-----------------------------------------------------------------------------------------------------%
%OBJECTIVE 4
disp('STUDY 4 Program to plot ga iteration output with increase population, increase iteration and in bounded condition')
syms x %variable x
syms y %varaible y
syms p_size %population size and also the variable length
syms num_iteration %number of iteration
syms l %length of variables
p_size=input('enter the population size=')
l=input('enter the length of variables=')
x1=input('enter the minimum value of x=')
x2=input('enter the maximum value of x=')
x=linspace(x1,x2,l);
y1=input('enter the minimum value of y=')
y2=input('enter the maximum value of y=')
y=linspace(y1,y2,l);
[xx yy]=meshgrid(x,y);
num_iteration=input('enter the number of iteration(100 or greater)=')
%here we will add and option variable to increase the ga population size
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',p_size);
%here we will use tic and toc command to understand the time it took to iterate the code
tic
% we use for command to perform the ga equals to number of iteration
for k=1:num_iteration
for i=1:length(xx)
for j=1:length(yy)
parent_reverse(1)=xx(i,j);
parent_reverse(2)=yy(i,j);
f(i,j)=w3_mb_function_stalagmite_reverse(parent_reverse);
end
end
%here inputs is the 2 column matrix of having value of x and y such that function value is optimium
[inputs, foptimum(k)]=ga(@w3_mb_function_stalagmite_reverse,2,[],[],[],[],[0;0],[1;1],[],[],options);
xoptimum(k)=inputs(1);
yoptimum(k)=inputs(2);
end
study4_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('x axis-x chromozome')
ylabel('y axis-y chromosome')
zlabel('z axis-fitness')
plot3(xoptimum,yoptimum,foptimum,'marker','o','markersize',5,'markerfacecolor','r')
title('skill lync-GA STUDY 4-increase population and increase bounded iterations')
subplot(2,1,2)
plot(foptimum)
xlabel('iteration')
ylabel('function minimum value')
%------------------------------------------------------------------------------------------------------%
%OBJECTIVE 5
disp('STUDY 5 Program to plot ga iteration output with increase population, increase iteration and in bounded condition')
syms x %variable x
syms y %varaible y
syms p_size %population size and also the variable length
syms num_iteration %number of iteration
syms l %length of variables
p_size=input('enter the population size(1000 or greater)=')
l=input('enter the length of variables=')
x1=input('enter the minimum value of x=')
x2=input('enter the maximum value of x=')
x=linspace(x1,x2,l);
y1=input('enter the minimum value of y=')
y2=input('enter the maximum value of y=')
y=linspace(y1,y2,l);
[xx yy]=meshgrid(x,y);
num_iteration=input('enter the number of iteration(1000 or greater)=')
%here we will add and option variable to increase the ga population size
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',p_size);
%here we will use tic and toc command to understand the time it took to iterate the code
tic
% we use for command to perform the ga equals to number of iteration
for k=1:num_iteration
for i=1:length(xx)
for j=1:length(yy)
parent_reverse(1)=xx(i,j);
parent_reverse(2)=yy(i,j);
f(i,j)=w3_mb_function_stalagmite_reverse(parent_reverse);
end
end
%here inputs is the 2 column matrix of having value of x and y such that function value is optimium
[inputs, foptimum(k)]=ga(@w3_mb_function_stalagmite_reverse,2,[],[],[],[],[0;0],[1;1],[],[],options);
xoptimum(k)=inputs(1);
yoptimum(k)=inputs(2);
end
study5_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('x axis-x chromozome')
ylabel('y axis-y chromosome')
zlabel('z axis-fitness')
plot3(xoptimum,yoptimum,foptimum,'marker','o','markersize',5,'markerfacecolor','r')
title('skill lync-GA STUDY 4-increase population and increase bounded iterations')
subplot(2,1,2)
plot(foptimum)
xlabel('iteration')
ylabel('function minimum value')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [generation_reverse]=w3_mb_function_stalagmite_reverse(parent_reverse)
f1=(sin(5.1*pi*parent_reverse(1)+0.5))^6;
f2=(sin(5.1*pi*parent_reverse(2)+0.5))^6;
f3=exp((-4*log(2)*(parent_reverse(1)-0.0667)^2)/0.64);
f4=exp((-4*log(2)*(parent_reverse(2)-0.0667)^2)/0.64);
generation_reverse=-(f1*f3*f2*f4);
end
2.2. OUTPUT
STUDY 1 Program to plot ga iteration output in unbounded condition
enter the length of variables=150
l =
150
enter the minimum value of x=0
x1 =
0
enter the maximum value of x=0.6
x2 =
0.6000
enter the minimum value of y=0
y1 =
0
enter the maximum value of y=0.6
y2 =
0.6000
enter the number of iteration(50 or greater)=50
num_iteration =
50
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: maximum number of generations exceeded.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
study1_time =
2.4489

STUDY 2 Program to plot ga iteration output in bounded condition
enter the length of variables=150
l =
150
enter the minimum value of x=0
x1 =
0
enter the maximum value of x=0.6
x2 =
0.6000
enter the minimum value of y=0
y1 =
0
enter the maximum value of y=0.6
y2 =
0.6000
enter the number of iteration(50 or greater)=50
num_iteration =
50
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
study1_time =
3.4162

STUDY 3 Program to plot ga iteration output with increase iteration and in bounded condition
enter the length of variables=150
l =
150
enter the minimum value of x=0
x1 =
0
enter the maximum value of x=0.6
x2 =
0.6000
enter the minimum value of y=0
y1 =
0
enter the maximum value of y=0.6
y2 =
0.6000
enter the number of iteration(100 or greater)=100
num_iteration =
100
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
study1_time =
5.8739

STUDY 4 Program to plot ga iteration output with increase population, increase iteration and in bounded condition
enter the population size=200
p_size =
200
enter the length of variables=150
l =
150
enter the minimum value of x=0
x1 =
0
enter the maximum value of x=0.6
x2 =
0.6000
enter the minimum value of y=0
y1 =
0
enter the maximum value of y=0.6
y2 =
0.6000
enter the number of iteration(100 or greater)=50
num_iteration =
50
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
study1_time =
8.5788

STUDY 5 Program to plot ga iteration output with increase population, increase iteration and in bounded condition
enter the population size=300
p_size =
300
enter the length of variables=150
l =
150
enter the minimum value of x=0
x1 =
0
enter the maximum value of x=0.6
x2 =
0.6000
enter the minimum value of y=0
y1 =
0
enter the maximum value of y=0.6
y2 =
0.6000
enter the number of iteration(100 or greater)=60
num_iteration =
60
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
study1_time =
14.2919

2.3.SCREENSHOTS
1 unbounded

2 bounded

3 bounded increase iteration

4 bounded increase population and increase iteration


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3. CONCEPT OF BINARY GENETIC ALGORITHM USING SINGLE ROW/COLUMN ARRAY
3.1. CODE
%objective function in one variable
function z=w3_mb_function_population_objective(x)
f1=(sin(5.1*pi.*x+0.5)).^6;
f2=(sin(5.1*pi.*x+0.5)).^6;
f3=exp((-4*log(2)*(x-0.0667).^2)/0.64);
f4=exp((-4*log(2)*(x-0.0667).^2)/0.64);
f=(f1.*f3.*f2.*f4);
z=min(f);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CODE CANVAS
%W3 MATLAB Basic Genetic Algorithm
%skill-lync-Bhavin Chaudhary 27 March 2022
%This code details the concept of working behind genetic algorith
%OBJECTIVE
%1. To learn the basics of Genetic algorithm
%2. To create code or functions which performs GA
%3. To perform optimisation(minimum using GA)
%CITATION
%Basics of GA
%1. Genetic Algorithm ,MATLAB code and example-you tube video for reference- https://www.youtube.com/watch?v=MLPHLm56inQ&t=316s
%2. Gentic Algorithm with solved example (Selection, cross over,mutation) youtube video reference- https://www.youtube.com/watch?v=InVJWW_NzFY&t=144s
%3. Genetic Algorithm of function with multiple variable using MATLAB- youtube video reference -https://www.youtube.com/watch?v=vhSn3N_jqFU
%Coding of GA
%1. Introduction to Practical Genetic Algorithm in Python and MATALB youtube video- https://www.youtube.com/watch?v=Fdk7ZKJHFcI&list=PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9&index=1
%2. Binary Genetic Algorithm part A-Practical GA series youtube video- https://www.youtube.com/watch?v=ICzcG0ORv6I&list=PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9&index=2
%3. Binary Genetic Algorithm part B-Practical GA series youtube video- https://www.youtube.com/watch?v=pW39nKyYlN4&list=PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9&index=3
%Introduction
%GA first introduced by Holland(1975)is a stochastic search algorithm(random probability distribution)
%GA is based on mechanism of gentics and natural selection by Goldberg(1989)
%Components of GA
%1. POPULATION-Initial set of random solution
%2. CHROMOSOMES-Each individual in population representing a solution to problem at hand
%3. GENERATION-The successive iteration through which chromosomes evolve
%The process of GA
%During each generation, the chromosomes are evaluated using some measures of fitness (Fitness evaluation)
%1. OFFSPRINGS-These are new chromosomes of NEXT generation formed by (process)
%1.1. merging two chromosomes from CURRENT generation using a cross-over operator
%1.2. modifying a chromosome using a mutation operator
%2. OFFSPRING Formation-The new(NEXT) Generation is formed by (sorting and filtering)
%2.1. selecting according to the fitness values, some of parents and offspring
%2.2. rejecting others so as to keep population size constant
%Fitter chromosomes have higher probability of being selected
%After several generation the algorithm converges to best chromosomes, which hopefully represent the optimal or suboptimal solution to problem(Gen and Cheng 1997)
% Code Methodology of GA
%0. Let output variable z=f(x,y) is function of x input variable and y input variable
%0.1. the length of variable x and y (both pair to form one Z)
%0.2. enter the value of x & y (note the array vector must be of same length)
%0.3. the value of z defines the property/characteristic of population,m how it is mathematically distributed
%1. Define Population function
%1.1. define the size/length of output variable Z which is equal to size of pair of x & y
%1.2. the z is itself a initial population characteristics on entering the initial array value of x & y
%1.3. Hence, the first set of x & y is the population
%2. Define selection function/Selection Operator (to select best x & y values and elememinate the least fit x & y values)
%2.1. here we sort the data to have parents and select the best chromosomes
%2.2. We use selection operator before proceeding cross over
%2.3. The selection method is based on roulette wheel selection, Boltzmann selection, tournament selection and rank selection
%2.4. The elements in first population are evaluated for their fitness, as per above method, by mathematically evaluating the population characteristics or z value
%2.5. The parents (x & y values) selected are the one with highest fitness or chances of seletion
%2.6. The parents with lowest fitness are eliminated by replacing them with highest fit parents (x & y values)
% It means that the higher fit parents gets more chance to reproduce.
%3. Define cross over function/operator
%3.1. The cross over function defines the function to generate the new offspring of selected parents based on gene break point method
%3.2. The corss over function also defines the cross over point in the array of chromosomes of each parent
%3.3. The cross over will result in some changes in x and y values that is new chromosomes
%3.4. The cross over is performed either by single point, double point or uniform crossover
%3.5. The new x & y values are again checked for their fitness.
%4. Define Mutation function/operator
%4.1. The mutation is completely based on randomly selecting string(x & y value) and randomly mutating, further changing x & y values
%4.2. The mutation is randomly operated function but does not select the highest fit values of x & y which were in cross over
%4.3. The element are mutated and new x & y values are formed randomly
%4.4. This new elements are new population
%5. Define new population
%5.1. The new value of x and y after the cross over and mutation is the best offspring
%5.2. Check the fitness
%6. The more number of times the above process is repeated the best(optimium) value will generate in progeny
%7. In term of mathematics best fit x & y value gives
%7.1. either the highest z=f(x,y) value, if GA is to optimize for maximum
%7.2. or the lowest z=f(x,y) value, if GA is to optimize for minimum
%CODE TO PERFORM OPTIMIZATION USING GENETIC ALGORITHM MANUALLY
disp('DETAILED GENETIC ALGORITH TO OPTIMIZE FUNCTION AS POPULATION OBJECTIVE')
p=input('enter the number of parts or fragmentation of initial and final value, THIS DEFINES THE SIZE OF POPULATION=')
x1=input('enter the minimum value of x=')
x2=input('enter the maximum value of x=')
x=linspace(x1,x2,p);
%%PROBLEM DEFINITION
%defining objective by using structure syntax
problem.optimisation_function=@(x) w3_mb_function_population_objective(x);
%define search space by numbers of variable (thesse is the number of binary genes in one chromosome)
%Hence in binary system each nuber(chromosome) is defined by 0 or 1 upto 8 variables
problem.nvariable=10;
%%GA PARAMETER
%defining parameters using structure syntax
%define number of iteration
parameter.maxiteration=100;
%define population size
parameter.npopulation=p;
%define the fraction of population crossover for reproduction
crossover_fraction_input=input('enter the fraction of crossover between 0 to 1 of the total population=')
crossoverfraction=crossover_fraction_input;
parameter.crossover_fraction=crossoverfraction;
%define mutation parameter
murate=input('enter the value between 0 & 1, which defines the rate of mutation in selected population=')
parameter.mutation_rate=murate;
%%RUN GA TO SOLVE
Output=w3_mb_function_RunGA(problem,parameter);
%%RESULTS
figure;
plot(Output.bestproperty);
title('optimized value (variation) of function w.r.t iteration in GA')
xlabel('iterations')
ylabel('best property')
grid on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%GA Function
function [Output]=w3_mb_function_RunGA(problem ,parameter)
%importing problem data
optimisation_function=problem.optimisation_function;
nvariable=problem.nvariable;
%importing parameter data
maxiteration=parameter.maxiteration;
npopulation=parameter.npopulation;
crossover_fraction=parameter.crossover_fraction;
%we define the number of crossover performed and it is alwas even hence we divide it by 2
crossover_number=round(crossover_fraction*npopulation/2)*2;
%we define mutation parameter
mutation_rate=parameter.mutation_rate;
%defining tempelate for individuals
individual.position= [];
individual.property= [];
%defining worst solution using structure equals infinity so that it can be used to eliminate worst output
best_solution.property=inf;
%Defining initial population algorithm with repeat matrix command
population=repmat(individual, npopulation,1);
for i=1:npopulation %for the length or size of population
%we will generate random position for individual within the given length of population
population(i).position=randi([0,1], 1, nvariable);
%we will anlyse the random individual position for property w.r.t optimisation function
population(i).property=optimisation_function(population(i).position);
%here we will compare and determine the best solution and worst solution using if else
if population(i).property < best_solution.property
best_solution=population(i);
end
end
%Best property iteration
%to store the number of iterations we need a empty matrix with elemnts equal to numbers of iteration
%to generate such empty matrix we can use initializers like zeros, nan(not a number)
best_property=nan(maxiteration,1); %this generate matrix with number of rows equal to number of iteration and 1 column
%MAIN LOOP
%Here we will perform cross over, mutation, merge, sort & select/reject for each number of iteration
for it=1:maxiteration
%initial offspring population
population_crossover=repmat(individual, crossover_number/2,2); %here we generate initial offspring matrix with number of rows=crossover_number/2 and 2 column
%here we will perform cross over by selecting the parent
for k=1:crossover_number/2
%select parent by random permutation of population size
selected_parent=randperm(npopulation);
parent1=population(selected_parent(1));
parent2=population(selected_parent(2));
%Perform cross over and define offspring
[population_crossover(k,1).position, population_crossover(k,2).position]=w3_mb_function_select_crossover(parent1.position, parent2.position);
end
%we will convert the population cross over matrix into single colum matrix
population_crossover=population_crossover(:);
%here we will perform mutation by selecting the crossover offspring
for l=1:crossover_number
%here original value of population crossover is passed through mutation and we get new population after mutation
population_crossover(l).position=w3_mb_function_mutation(population_crossover(l).position , mutation_rate);
%now we will evaluate the solution property or check the fitness
population_crossover(l).property=optimisation_function(population_crossover(l).position);
%here we will compare and determine the best solution and worst
%solution using if else after mutation (we check best fit after mutation)
if population_crossover(l).property < best_solution.property
best_solution=population_crossover(l);
end
end
best_property(it)=best_solution.property;
%merge the offsprings to define new population
merged_population=[population; population_crossover];
%sort the best one and worst one in a decending order of property value
sorted_population=w3_mb_function_population_sorting(merged_population);
%removing extra individuals from the population who are least fit or have poor property
%we finally get new population
population=sorted_population(1:npopulation);
%display the iteration sattus
disp(['Iteration' num2str(it) ' : best property= ' num2str(best_property(it))]);
end
%Results
Output.population=population;
Output.bestsolution=best_solution;
Output.bestproperty=best_property;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [y1, y2]=w3_mb_function_select_crossover(parent1, parent2)
m=randi([1, 3]);
switch m
case 1
[y1, y2]=w3_mb_function_single_point_crossover(parent1, parent2);
case 2
[y1, y2]=w3_mb_function_double_point_crossover(parent1, parent2);
otherwise
[y1, y2]=w3_mb_function_uniform_crossover(parent1, parent2);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [y1, y2]=w3_mb_function_single_point_crossover(parent1, parent2)
nvariable=numel(parent1);
j=randi([1, nvariable-1]);
y1=([parent1(1:j) parent2(j+1:end)]);
y2=([parent2(1:j) parent1(j+1:end)]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [y1, y2]=w3_mb_function_double_point_crossover(parent1, parent2)
nvariable=numel(parent1);
q=randperm(nvariable);
j1=min(q(1), q(2));
j2=max(q(1), q(2));
j=randi([1, nvariable-1]);
y1=([parent1(1:j1) parent2(j+1:j2) parent1(j2+1:end)]);
y2=([parent2(1:j1) parent1(j+1:j2) parent2(j2+1:end)]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [y1, y2]=w3_mb_function_uniform_crossover(parent1, parent2)
alpha=randi([0, 1], size(parent1));
y1=(alpha.*parent1) + ((1-alpha).*parent2);
y2=(alpha.*parent2) + ((1-alpha).*parent1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mutation_output=w3_mb_function_mutation(x, mutation_rate)
% we will define a parameter (radiation) which is the probalbility of randomly selected
% x (crossover generation) whose value is less than the mutation fraction,
% means it falls in the category of mutation
radiation= (rand(size(x)) < mutation_rate);
%we will perform logical indexing such that we can arrive a mutation output after radiation
mutation_output=x;
mutation_output(radiation)=1-x(radiation);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sorted_population=w3_mb_function_population_sorting(population)
[~,sort_order]=sort([population.property]);
sorted_population=population(sort_order);
end
3.2. OUTPUT
>> w3_mb_genetic_algorithm
DETAILED GENETIC ALGORITH TO OPTIMIZE FUNCTION AS POPULATION OBJECTIVE
enter the number of parts or fragmentation of initial and final value, THIS DEFINES THE SIZE OF POPULATION=150
p =
150
enter the minimum value of x=0
x1 =
0
enter the maximum value of x=0.6
x2 =
0.6000
enter the fraction of crossover between 0 to 1 of the total population=1
crossover_fraction_input =
1
enter the value between 0 & 1, which defines the rate of mutation in selected population=0.1
murate =
0.1000
Iteration1 : best property= 1.153e-05
Iteration2 : best property= 1.153e-05
Iteration3 : best property= 1.153e-05
Iteration4 : best property= 1.153e-05
Iteration5 : best property= 1.153e-05
Iteration6 : best property= 1.153e-05
Iteration7 : best property= 1.153e-05
Iteration8 : best property= 1.153e-05
Iteration9 : best property= 1.153e-05
Iteration10 : best property= 1.153e-05
Iteration11 : best property= 1.153e-05
Iteration12 : best property= 1.153e-05
Iteration13 : best property= 1.153e-05
Iteration14 : best property= 1.153e-05
Iteration15 : best property= 1.153e-05
Iteration16 : best property= 1.153e-05
Iteration17 : best property= 1.153e-05
Iteration18 : best property= 1.153e-05
Iteration19 : best property= 1.153e-05
Iteration20 : best property= 1.153e-05
Iteration21 : best property= 1.153e-05
Iteration22 : best property= 1.153e-05
Iteration23 : best property= 1.153e-05
Iteration24 : best property= 1.153e-05
Iteration25 : best property= 1.153e-05
Iteration26 : best property= 1.153e-05
Iteration27 : best property= 1.153e-05
Iteration28 : best property= 1.153e-05
Iteration29 : best property= 1.153e-05
Iteration30 : best property= 1.153e-05
Iteration31 : best property= 1.153e-05
Iteration32 : best property= 1.153e-05
Iteration33 : best property= 1.153e-05
Iteration34 : best property= 1.153e-05
Iteration35 : best property= 1.153e-05
Iteration36 : best property= 1.153e-05
Iteration37 : best property= 1.153e-05
Iteration38 : best property= 1.153e-05
Iteration39 : best property= 1.153e-05
Iteration40 : best property= 1.153e-05
Iteration41 : best property= 1.153e-05
Iteration42 : best property= 1.153e-05
Iteration43 : best property= 1.153e-05
Iteration44 : best property= 1.153e-05
Iteration45 : best property= 1.153e-05
Iteration46 : best property= 1.153e-05
Iteration47 : best property= 1.153e-05
Iteration48 : best property= 1.153e-05
Iteration49 : best property= 1.153e-05
Iteration50 : best property= 1.153e-05
Iteration51 : best property= 1.153e-05
Iteration52 : best property= 1.153e-05
Iteration53 : best property= 1.153e-05
Iteration54 : best property= 1.153e-05
Iteration55 : best property= 1.153e-05
Iteration56 : best property= 1.153e-05
Iteration57 : best property= 1.153e-05
Iteration58 : best property= 1.153e-05
Iteration59 : best property= 1.153e-05
Iteration60 : best property= 1.153e-05
Iteration61 : best property= 1.153e-05
Iteration62 : best property= 1.153e-05
Iteration63 : best property= 1.153e-05
Iteration64 : best property= 1.153e-05
Iteration65 : best property= 1.153e-05
Iteration66 : best property= 1.153e-05
Iteration67 : best property= 1.153e-05
Iteration68 : best property= 1.153e-05
Iteration69 : best property= 1.153e-05
Iteration70 : best property= 1.153e-05
Iteration71 : best property= 1.153e-05
Iteration72 : best property= 1.153e-05
Iteration73 : best property= 1.153e-05
Iteration74 : best property= 1.153e-05
Iteration75 : best property= 1.153e-05
Iteration76 : best property= 1.153e-05
Iteration77 : best property= 1.153e-05
Iteration78 : best property= 1.153e-05
Iteration79 : best property= 1.153e-05
Iteration80 : best property= 1.153e-05
Iteration81 : best property= 1.153e-05
Iteration82 : best property= 1.153e-05
Iteration83 : best property= 1.153e-05
Iteration84 : best property= 1.153e-05
Iteration85 : best property= 1.153e-05
Iteration86 : best property= 1.153e-05
Iteration87 : best property= 1.153e-05
Iteration88 : best property= 1.153e-05
Iteration89 : best property= 1.153e-05
Iteration90 : best property= 1.153e-05
Iteration91 : best property= 1.153e-05
Iteration92 : best property= 1.153e-05
Iteration93 : best property= 1.153e-05
Iteration94 : best property= 1.153e-05
Iteration95 : best property= 1.153e-05
Iteration96 : best property= 1.153e-05
Iteration97 : best property= 1.153e-05
Iteration98 : best property= 1.153e-05
Iteration99 : best property= 1.153e-05
Iteration100 : best property= 1.153e-05
conclusion: the output of iteration is almost near to zero which is the minimum value after optimization
Hence iteration is constant and no deviation for given objective function.
This is due to selection of uniform crossover.

CONCLUSION :The variation of output is constant with respect to each iteration
3.3. SCREENSHOTS



4. CITATION/REFERENCE
%Basics of GA
%1. Genetic Algorithm ,MATLAB code and example-you tube video for reference-
https://www.youtube.com/watch?v=MLPHLm56inQ&t=316s
%2. Gentic Algorithm with solved example (Selection, cross over,mutation) youtube video reference- https://www.youtube.com/watch?v=InVJWW_NzFY&t=144s
%3. Genetic Algorithm of function with multiple variable using MATLAB- youtube video reference -
https://www.youtube.com/watch?v=vhSn3N_jqFU
%Coding of GA
%1. Introduction to Practical Genetic Algorithm in Python and MATALB youtube video-
https://www.youtube.com/watch?v=Fdk7ZKJHFcI&list=PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9&index=1
%2. Binary Genetic Algorithm part A-Practical GA series youtube video-
https://www.youtube.com/watch?v=ICzcG0ORv6I&list=PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9&index=2
%3. Binary Genetic Algorithm part B-Practical GA series youtube video-
https://www.youtube.com/watch?v=pW39nKyYlN4&list=PLgH3sgdvgO4TQagjfi1FmPcdBjIlmk5s9&index=3
%4. Binary Genetic Algorithm part C-Practical GA series youtube video-