Week 4.1 - Genetic Algorithm
- First of all defining Stalagmite function. Save function with same name then create function.
function [f] = stalagmite(input_variable) a = input_variable(1); b = input_variable(2); c = 5.1*pi*a; d = 5.1*pi*b; e = -4*log(2); f = (a-0.0667)^2/0.64; g = (b-0.0667)^2/0.64; f1x = (sin(c+0.5))^6; f2x = exp(e*f); f1y = (sin(d+0.5))^6; f2y = exp(e*g); f = -(f1x*f2x*f1y*f2y); end - After defining Stalagmite equation generate common function to get graph or surface. In this tore value of function with its inputs. In this use meshgrid function to convert column matrix into equal rank of row and column matrix. Use for loop to store output values in function array. In this also save programm with same name of function.
function f = get_ga(x,y) [xx yy] = meshgrid(x,y); for i = 1:length(xx) for j = 1:length(yy) input_variable(1) = xx(i,j); input_variable(2) = yy(i,j); f(i,j) = stalagmite(input_variable); end end - Now, create new file and define input variables. And execute get_ga function to get stalagmite function outputs in array which will use further to get surface plot. And also start time counting of function execution time by defineing tic function.
clear all close all clc x = linspace(0,0.6,150); y = linspace(0,0.6,150); num_case = 50; f = get_ga(x,y); tic; - After all this,
- Study 1 :- TO run GA function without any upper and lower bound. And with basic population size of 50.
- For that get output of optimum maximum value and store it in matrix. For that run for loop for 50 times to get 50 iteration. In this ga function can evaluate maximum output everytime. ga function is not terminate till new value of output is weaker in fitness with compare to previous one. After Executing ga function to get maximun output its evaluating function again and again on input and give us optimum maximum output.
- As syntax of ga function, for this program we have to just call function at 1st place then define no of inputs which is 2. Also store inputs variable in xopt and yopt as array and also stop time counting by using toc function. During execution of ga function get output values stored in inputs and fopt by defining output variable at LHS side.
- ga function is work on like this. First it evaluate ouput which i call population. Then it is comparing its fittness of population and get strongest one. Then it will changeove and mutate population value to get better fitness of function. This ga function is vary till it willgive better fittness after crossover and mutation. So ga function is terminated.
-
% Basic execution of ga function without boundry for i = 1: num_case [inputs , fopt(i)] = ga(@stalagmite,2); xopt(i) = inputs(1); yopt(i) = inputs(2); end study_time_1 = toc - THen after getting all values as stored in array, there is need to combine two plot in single one. For that use subplot command and define its number of row and column and its current grid position value.
- For first grid axis we have to combine two graph together which are stalagmite function and graph of function output according to input. So ue hold on and define all those plots for which use plot3 command to plot 3 value graph and surf command to plot stalagmite function output. Also define title, label and legends. And to off shading lines on stalagmite piles use shading iterp function. And to get three different figure in single program output use figure function. In plot function as usual use marker related option to get better plot.
figure(1) subplot(2,1,1) hold on surf(x, y, -f) shading interp plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'Color','r') legend('Stalagmite output','Function Maximum Output'); title('Unbounded Inputs') hold off- Then, to plot graph in same figure define grid axis value to 2 in subplot function and define second plot with labels.
subplot(2,1,2) plot(-fopt) xlabel('Iteration') ylabel('Function Maximum')
- Then, to plot graph in same figure define grid axis value to 2 in subplot function and define second plot with labels.
- For study 2,
- Define, upper and lower bound for better function evaluaion. For this problem we have no fixing variable for inputs which are A and B, also not having equalities Aeqa, Beqa. So, define [] for empty variable inputs in ga syntax respectively. Then define lb and ub respectively to bound our output. Then rest process is same as study1.
% Execution of ga function with up and lb and without equalities for i = 1: num_case [inputs , fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); xopt(i) = inputs(1); yopt(i) = inputs(2); end study_time_2 = toc figure(2) subplot(2,1,1) hold on surf(x, y, -f) shading interp plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'Color','r') legend('Stalagmite output','Function Maximum Output'); title('Bounded Points') hold off subplot(2,1,2) plot(-fopt) xlabel('Iteration') ylabel('Funciton Maximum')
- For study 3,
- Define polulation size in option in ga function syntax. For that first define function of options then define population size in it. Then call that option in above study 2 ga syntax after two blank variables as [] which are nonIcon and intcon which is not applicable for this and then define option function and rest process is same as other program.
% Incease population size by using option function options = optimoptions ('ga'); options = optimoptions (options, 'PopulationSize',170) for i = 1: num_case [inputs , fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); xopt(i) = inputs(1); yopt(i) = inputs(2); end study_time_3 = toc figure(3) subplot(2,1,1) hold on surf(x, y, -f) shading interp plot3(xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'Color','r') legend('Stalagmite output','Function Maximum Output'); title('Incease Population Size') hold off subplot(2,1,2) plot(-fopt) xlabel('Iteration') ylabel('Funciton Maximum') -
By executing whole program it will give three different plots at a time.