Week 4.1 - Genetic Algorithm
clear all
close all
clc
%Defining our search space
x =linspace(0,.6,50);
y =linspace(0,.6,50);
num_case=150;
%creating a 2Dimentional mesh
[xx,yy]= meshgrid(x,y);
%evaluating the basic stalagmite function
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 k = 1:num_case;
[inp,fval(k)] = ga(@stalagmite,2);
x_1(k) = inp(1);
y_1(k) = inp(2);
end
study1_time=toc
tic
for m = 1:num_case;
[input,fval(m)]= ga(@stalagmite,2,[],[],[],[],[0,0],[.6,.6]);
x_2(m)= input(1);
y_2(m)= input(2);
end
study2_time = toc
% third case study increasing ga iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',150);
tic
for m = 1:num_case;
[input,fval(m)]= ga(@stalagmite,2,[],[],[],[],[0,0],[.6,.6],[],[],options);
x_3(m)= input(1);
y_3(m)= input(2);
end
study3_time = toc
figure (1)
hold on
grid on
surfc(xx,yy,-f)
shading interp
title('stalagmite plot')
xlabel ('X')
ylabel('Y')
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_1,y_1,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
% second case study statistical behavior
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_2,y_2,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
% third case study increasing ga iterations
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x_3,y_3,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function minimum')
%function code
function [f]=stalagmite(input)
x=input(1);
y=input(2);
f1x = (sin(5.1*pi*x + 0.5))^6;
f1y = (sin(5.1*pi*y + 0.5))^6;
f2x = exp((-2.776)*(x-(0.0667))^2/0.64);
f2y = exp((-2.776)*(y-(0.0667))^2/0.64);
f=-(f1x*f2x*f1y*f2y);
end
Genetic algorithm (GA) is the process of selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimisation and search problems by relying on biologically inspired operators such as mutation,crossover and selection n a genetic algorithm, a population of candidate solutions (called individuals,) to an optimization problem is evolved toward better solutions. Each candidate solution has a set of properties (its chromosomes or genotype) which can be mutated and altered; traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible.The evolution usually starts from a population of randomly generated individuals, and is an iterative process,with the population in each iteration called a generation. In each generation, the fitness of every individual in the population is evaluated; the fitness is usually the value of the objective function in the optimization problem being solved. The more fit individuals are sochastically selected from the current population, and each individual's genome is modified (recombined and possibly randomly mutated) to form a new generation.

The new generation of candidate solutions is then used in the next iteration of the algorithm. Commonly, the algorithm terminates when either a maximum number of generations has been produced, or a satisfactory fitness level has been reached for the population.he population size depends on the nature of the problem, but typically contains several hundreds or thousands of possible solutions. Often, the initial population is generated randomly, allowing the entire range of possible solutions (the search space). Occasionally, the solutions may be "seeded" in areas where optimal solutions are likely to be found. he fitness function is defined over the genetic representation and measures the quality of the represented solution. The fitness function is always problem dependent.so we are doing the program in matlab software,we have to know the syntax of the ga function,
The GA syntax in Matlab is given by :
x = ga(fun,nvars,A,B.Aeq.Beq,LB,
Where,
fun = objective function
nvars = number of variables
A and B = subject to linear inequalities of the form A*x <=B
Aeq and Beq = subject to linear equalities of the form A*x = B
LB = lower bound
UB = upper bound
nonlcon = non linear inequalities
intcon = integer constrained
options = helps to add contraint tolerance by increasing the population size
They usually perform better than traditional feature selection techniques. Genetic algorithms can manage data sets with many features and don't need specific knowledge about the problem under study. These algorithms can be easily parallelized in computer clusters.The algorithm begins by creating a random initial population.The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population.To create the new population, the algorithm performs the various steps,first Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.Selects members, called parents, based on their expectation. Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.Produces children from the parents. Children are produced either by making random changes to a single parent—mutation or by combining the vector entries of a pair of parents—crossover. Replaces the current population with the children to form the next generation.The algorithm stops when one of the stopping criteria is met.the fitness function used in the program is given below.this is the stalagmite function

The code starts with defining x and y as inputs using linspace.A function known as Stalagmite is defined as down in the 2nd stage of codes.2 for loops are incorporated in the main code so that all the combinations of x and y values is taken in the stalagmite 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 150. Plots of the stalagmites and maximum values generated in each iteration is shown.
RESULTS-

As shown in figure, there are more upward-growing peaks, which are basically stalagmite structure. However, because the function tends to return a negative value, which corresponds to local minima, it is necessary to make the function positive in order to obtain local and global maxima. (Going up peaks) The highest peak is known as a global maxima, while others are known as local maximas.
The results for case studys are below
for case study(1)

Because the solutions in case study (I) are not bounded, the GA will calculate all feasible maxima in the 'x' and 'y' coordinates. and it took 7.3724 seconds to calculate the probable solution.
for case study(2)

As shown, the reading of global maxima takes 9.7369 seconds in result of second study. We're utilising the genetic algorithm tool to limit the lower and upper values because if we limit the upper and lower values, the ga tool will only search inside that range. For the x and y-direction, the lower and higher bounds are (0 and.6), respectively.
for last case study(3rd)

The key fact for this global maxima is the resultant line from high peak to low peak, and the time it takes is 24.7568, which is approximately 3 times longer than the case study one. Because the population is large enough, 50 iterations iscompleted simultaneously. to get most optimal solution this can be used.
Conclusion-
Optimised stalagmite function and global maxima of the function is found using matlab.
reference-
https://in.mathworks.com/help/gads/ga.html
https://en.wikipedia.org/wiki/Genetic_algorithm