Week 4.1 - Genetic Algorithm
Title: Matlab funciton for optimiazation for generic algorithm and writing a program for the same.
Code Explanation:
- The code begins with the basic command as clear all close all and clc.
- Further the x and y values are assinged in the linspace command which has the margin of 0-0.5 out of which 100 pairs are beentaken.
- x and y variables are assignedin the meshgrid command which mesh the row and columncordinates of x and y with xx and yy.
- The num cases are used to pick uo the number of file cases.
- further the program is coded in for loop which defines that for the length of xx changes by one for the each value of i and the value of yy changes for each number of j.
- The input 1 &2 are assigned for the definate value of i and j respectively.
- F variable fpr i and j is a stalahnite input function which is coded in for loop which genrates the values over the stalagmite surface to get the local and global maxima and minima for the given function i.e the inputs and for loop is closed.
- further the plotting is done with respect to x,y and f. The shading interp is used to smoothen the shades overlap.
- A desired plot for x and fval is called after the inputs of generic algorithm of stalagmite function.
- Generic algorithm is the function used to solve the costrained and unconstrained functions of optimization problem based of total natural selection.it is used to find peak and minima of the given function on optimazation basis.
- GA generated the population on points at each iteration as desired in the question. in short GA represents the objective the decision maker seeks to gain.
- For x and fval the ga function is used in stalagmite form which are feed with the data inputs as stalagmite ,2(variables),f1x,f1y,f2x,f2y,0,0.5.
- f1x,f1y,f2x,f2y are the varible which are coded for the ga function in other script. 0 and 0.5 defines the length or limit of the function.
- Hence the plot between x and fval is recalled and the subplot is created for the input vector and f value in the same.
clear all close all clc x = linspace(0,0.5,100); y = linspace(0,0.5,100); [X,Y] = meshgrid(x,y); num_cases = 50 for i = 1:length(X) for j = 1:length(Y) input(1) = X(i,j); input(2) = Y(i,j); f(i,j) = stalagmite(input); end end surface(x,y,f) shading interp [x,fval] = ga(@stalegmite,2,[f1x],[f1y],[f2x],[f2y],[0;0],[0.5;0.5]) plot(x,fval) figure(1) subplot(2,1,1) hold on plot(input_vector,f)
function[x,y] = stalegmite(x,y);
f1x = (sin(5.1*3.14*+0.5))^6;
f1y = (sin(5.1*3.14*+0.5))^6;
f2x = exp(-4*log10(2)*(-0.0667)^2/0.64);
f2y = exp(-4*log10(2)*(-0.0667)^2/0.64);
end
Error faced while writing the function:
- There were maximum numbers of errors were faced while coding for the program out of which many were minors.
- while programming the errors were determined but i failed to overcome those error.

- the code for f was unable to stalagmite.

- These are the some kind of errors which lead to the failure of program.