Week 4.1 - Genetic Algorithm
ntroduction: coding in MATLAB to optimise the stalagmite funtion & find the global maxima of the function using genetic algorithm.
1) Main code:
% coding to optimise the stalagmite function and find the global maxima of the function
clear all
close all
clc
% defining input space
x= linspace(0,0.6,100);
y= linspace(0,0.6,100);
% mesh by mesh by grid
[xx,yy]= meshgrid(x,y);
% for loop intructions to solve function using stalagmite function script template
for i= 1: length(x)
for j= 1: length(y)
input(1)=xx(i,j)
input(2)=yy(i,j)
f(i,j)= stalagmite(input);
end
end
surf(xx,yy,f)
shading interp
% defing inputs for GA solver syntax & optimisation of output function with for loop instructions
options= optimoptions('ga')
options= optimoptions(options,'PopulationSize',100);
for z= 1: 100
[inputs,fopt(z)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(z)= inputs(1);
yopt(z)=inputs(2);
end
2) Function script:
% stalagmite function script
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(-4*log(2)*(((x-0.0667)^2)/0.64));
f2y=exp(-4*log(2)*(((y-0.0667)^2)/0.64));
f=(f1x*f1y*f2x*f2y);
end
Errors screen shots:



OUTPUT:



Explanation:
- firstly ,defined input spcace such as input variables (x & y) assigned with requred range of data value (0 to 0.6) & number of data points/population size(100)in the space using linspace commands.
- in addition, obtained array of mesh grid points in as [xx,yy] using the meshgrid command on two input variables x & y resly in the space.
- moreover developed stalagmite function script of mathematical expressions/equations for solving individual output functions in terms wise , in such a way obtained main objective function(f).
- such that followed nested for loop instructions with proper indexing & called stalagmite function template to solve objective function(f) for each meshgrid point in the xy plane as the output function(f) relative to the two dimensional inputs such as x & y variables respectively.
- furtherly, using surf command generating surface plots such as for three varibles (x,y,f) plotting with respect to meshgrid ponts of varibles.
- eventually genetic algorithm solver was adopted using optimoptions feature command such that defined desired population size & assigned to the string/character options.
- as well as GA syntax in which defined clearly such as specifying the function(stalagmite) ,no.of input variables , lower & upper bounds & optimization options respectively.(however contraints not considered based on the problem)
- hence GA syntax with for loop intructions w.r.t to the length of population size in the space, therefore GA solver performs the optimisation for the stalgmite output functions such that achieves/obtain the global maxima(optimum solution) of the output functions. (stored an array of optimized output functions(z) w.r.t to array of x&y variable inputs as assigned accordingly).
Errors:
- faced few errors while coding for GA syntax, such that first one, the for loop instruction/command not given priorly to the GA solver syntax.
- remaining errors as the closing brackets usage in GA syntax & end command usage after the loop instructions.
inference:
- hence observed that from the coding/results, the meshgrid command which is useful to transform the variables from one dimensional array to two dimensional arrays. for the evaluation of objective functions.
- studied that the stalagmite function(script) which produces the output functions results w.r.t input variables.
- analysed from the results that the GA solver which optimizes the output functions w.r.t input variables accordingly the population size , such that produces the global maxima of output functions as represented/visualized in the surface plots of output results.
- as well as observed the default properties of GA solver as which controls the GA syntax in the optimzation performance accordingly.( displayed in the command window).