Week 4.1 - Genetic Algorithm
Aim:- Try to understand the concept of genetic algorithm.
- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
- Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
- Make sure that your code gives the same output, even if it is made to run several times.
- Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
Genetic Algorithm:-
A genetic algorithm makes uses of tecniques inspired from evolutionary biology such as selection, mutation, inheritance and recombination to solve a problem. The most commonly employed method in gentic algorithms is to create a group of individuals randomly from from a given population. The individuals thus formed are evaluated with the help of the evaluation function provided by the programmer. Individuals are then provided with a score which indirectly highlights the fitness to the given situation. The best two individuals are then used to create one or more offspring, after which random mutations are done on the offspring. Depending on the needs of the application, the procedure continues until an acceptable solution is derived or until a certain number of generation have passed.
A genetic algorithm differs from a classical, derivative-based optimation algorithm in two ways:
- A genetic algorithm generates a population of points in each iteration, whereas a classical algorithm generates a single point at each iteration.
- A genetic algorithm selects the next population by compution using random using number generatios, whereas a classical algorithm selects the next point by deterministic computation.
Compared to traditional artificial intelligence, a genetic algorithm provides many advatages. It is more robust and is susceptible to breakdowns due to slight changes in inputs or due to the presence of noise. With respect to other optimazation methods like praxis, linear programmin, heuristic, first or breadth-first, a genetic algorithm can provide better and more significant results while searching large multi-modal state spaces, large state or n-dimensional surfaces.
Genetic algorithms are widely used in many fields such as robotics, automotive design, optimized telecommunications routing, engineering design and computer-aided molecular.
SYNTAX FOR GA:-
X=ga(fun,nvars)X=ga(fun,nvars,A,b)X=ga(fun,nvars,A,b,Aeg,beg)X=ga(fun,nvars,A,b,Aeg,beg,lb,ub)
X=ga(fun,nvars,A,b,Aeg,beg,lb,ub,nonlcon)X=ga(fun,nvars,A,b,Aeg,beg,lb,ub,nonlcon,options)X=ga(fun,nvars,A,b,Aeg,beg,lb,ub,nonlcon,intcon)X=ga(fun,nvars,A,b,Aeg,beg,lb,ub,nonlcon,intcon,options)x=ga(problem)[x,fval] = ga( )[x,fval,exitflag,output] = ga( )[x,fval,exitflag,output,population,scores] = ga( )%main program
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 60;
%Creating a 2D dimensional mesh
[xx,yy] = meshgrid(x,y);
%Evaluating stalagmite function:
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
surf(x,y,f)
shading interp
%Study case 1 Statistical behaviour without bounds:
tic
for i = 1:num_cases
[opt_inputs, fval_opti(i)] = ga(@stalagmite,2);
x_opti(i) = opt_inputs(1);
y_opti(i) = opt_inputs(2);
end
studytime1 = toc
figure(1)
subplot(2, 1, 1);
hold on
surfc(x, y, f);
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
shading interp
plot3(x_opti, y_opti, fval_opti, 'marker', 'o','markersize',5,'markerfacecolor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(fval_opti)
xlabel('No. of iterations');
ylabel('F maximum');
%Study case 2 statistical behaviour with bounds:
tic
for i = 1:num_cases
[optimum_inputs,fval_opti(i)] = ga(@stalagmite, 2, [],[],[],[],[0;0],[0.6;0.6]);
x_opti(i) = optimum_inputs(1);
y_opti(i) = optimum_inputs(2);
end
studytime2 = toc
figure(2)
subplot(2, 1, 1);
hold on
surfc(xx, yy,f);
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
shading interp
plot3(x_opti, y_opti, fval_opti, 'marker', 'o','markersize',5,'markerfacecolor','g');
title('bounded inputs')
subplot(2,1,2)
plot(fval_opti)
xlabel('No. of iterations');
ylabel('F maximum');
%study case 3 statistical behaviour with bounds and increased population:
ct= 3;
for population_size = [150:50:250];
options = optimoptions('ga');
options = optimoptions(options,'populationSize',population_size);
tic
for i =1:num_cases
[optimum_inputs,fval_opti(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
x_opti(i) = optimum_inputs(1);
y_opti(i) = optimum_inputs(2);
end
study_time(ct) = toc
figure(ct)
subplot(2, 1, 1);
hold on
surfc(x, y, f);
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
shading interp
plot3(x_opti, y_opti,fval_opti, 'marker', 'o','markersize',5,'markerfacecolor','g');
title('bounded inputs with increase in population size')
subplot(2,1,2)
plot(fval_opti)
xlabel('No. of iterations');
ylabel('F maximum');
ct=ct+1;
end
Explaination of code:-
First area of plot is selected, by linspace command.
Meshgrid is generated by meshgrid command.
Then for function to implement, we are using two for loops. One for considering x values and other for y values.
Inside foe loops, input vectors are created for x and y. Then function stalagmite is plotted giving input vectors as input to function. Shading
interp is used for shading the plot. The genetic algorithm which is inbuilt function gives the optimum value of the function.
Here in this case, as we have to find out the maximum value of the function, we are mutiplying the function by (-1).
Because ga always fins the maximum value.
About Genetic algorithm :
This term is related to natural selection and genetics. They are used to find out optimal or near solution to difficult problems which might take very to solve. In GA, we have all possible solutions to the problem and then these solutions have to undergo recombination or mutation to see optimal solution and process is repeated for no. of iterations and combinations.
Now, each solution is rated on fitness value which depends upon the objective function. Fittest will survive which with Darwinian theory of ''survival of fittest''.

FUNCTION CODE FOR STALAGMITE GA:-
function T = stalagmite(x)
term1 = (sin(5.1*pi*x(1)+0.5)).^6;
term2 = (sin(5.1*pi*x(2)+0.5)).^6;
term3 = exp(((-4)*log(2)*((x(1)-0.0667).^2))/0.64);
term4 = exp(((-4)*log(2)*((x(2)-0.0667).^2))/0.64);
T = (-1)*(term1.*term2.*term3.*term4);
end
RESULTS AND DISCUSSION:-









Workspace output:-

Errors while coding:-
- Written function code in main code while coding
- Typing error.
- Run time error due to syntax error.
Steps taken to over errors:-
- written syntax correctly.
- minimised typing errors.