Genetic Algorithm
Aim- Code in MATLAB to optimise the stalagmite function and find the global maxima of the function and Clearly expalin the concept of genetic algorithm .
Theory-
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution . algorithms are randomized search algorithms that have been developed in an effort to imitate the mechanics of natural selection and natural genetics. Genetic algorithms operate on string structures, like biological structures, which are evolving in time according to the rule of survival of the fittest by using a randomized yet structured information exchange. Thus, in every generation, a new set of strings is created, using parts of the fittest members of the old set
- he genetic algorithm works with a coding of the parameter set, not the parameters themselves.
- The genetic algorithm initiates its search from a population of points, not a single point.
- The genetic algorithm uses payoff information, not derivatives.
- The genetic algorithm uses probabilistic transition rules, not deterministic ones.
t first, the coding to be used must be defined. Then using a random process, an initial population of strings is created. Next, a set of operators is used to take this initial population to generate successive populations, which hopefully improve with time. The main operators of the genetic algorithms are reproduction, crossover, and mutation. The implementation of GAs to the problem of optimization of operation and site-scale energy use in production plants is envisaged to be carried along the following lines.First, using the pinch design method, the problem requirements and constraints will be defined. Next, the region of the pinch will be identified, and the essential matches will be made. Then, the problem will be coded by generating the appropriate strings. These strings will contain the general features and the parameters, which affect the problem. Each string will represent a possible network configuration. For each one of the strings, the objective function will be calculated, that is, the value of energy and utility usage and the number of units need to be used in the represented network configuration, that is, the total capital and operating cost. Initially, a starting population of strings will be created using a random procedure. The three main operators of the genetic algorithms will be performed to improve the value of the objective function, namely, to create network configurations with minimal total capital and operating cost. To the final population of strings/possible networks, advanced techniques will be applied for further improvement.

Procedure -
- the code is divided into two ways that is code for genetic algarathim and the calling function (stalagmite).
- we start the code by giving the comments with the help of % and the comment is defining our search space by giving this command x = linspace(0,0.6,150); , y = linspace(0,0.6,150); .
- after that we should create a 2d mesh with this command [xx yy] = meshgrid(x,y); .
- after that we have to write the code to evaluate the stalagmite function the code can be given as 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 - there is a sub function in the above code .
after ending the evaluation of the stalagmite function we need to code for getting the values on the graph . all the code is given below .
code -
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% creating 2dimensional mesh
[xx yy] = meshgrid(x,y);
% evaluating thw 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
[x,fval] = ga(@stalagmite,2)
surfc(xx,yy,-f)
shading interp
hold on
plot3(x(1),x(2),-fval,'*','markersize',5)
Code For Calling Function -
function [f]= stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
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*f2x*f1y*f2y;]
end
Plots-
Errors While Doing The Code -

