Week 4.1 - Genetic Algorithm
Genetic Algorithm theory :
Genetic algorithm is a techniquec to find out the optimal value of the next generation.to perform for find out best solution to next and focuse on the optimization.it is based on darvan theory
they have to convert phonotype to ganotype with the help of encode.
Phenotype means collection of row data.
Geniotype means to convert intezers decimal real number etc.
STEPS:
- firstly we use linespace comand to create150 value from 0 to 0.6
- we use the mashgraid comand to evalvate the funtion of two variable and these dimantional plots
- we use the stalgamite funtin for difrant iteration
- we use ga function to deline the stalagamite function for variouse iteration
- we mwssure the time for each of 50 iteration and the optimum value is obtained
stalgamite equation
f(x y) = f1,xf2,xf1,yf2,y
f1_x =(sin(5.1*pi*x + 0.5) ).^6;
f1_y = (sin(5.1*pi*y + 0.5)).^6;
f2_x = exp(-4*log(2).* (((x-0.0667).^2)/0.64));
f2_y = exp(-4*log(2).* (((y-0.0667).^2)/0.64));
PROGRAM ON FIND OUT THE STALAGAMITE FUNCTION
%program to cal;culate goloble maximma of stalagamite functionclose allclear allclc %difining our surch spacex = linspace(0,0.6,150);y = linspace (0,0.6,150);%f = stalagmite_func(input_vector)%creating a 2dimensional mesh[xx,yy] = meshgrid (x,y);num_cases = 50; % number of itteration to run ga i.e ga%for loop for Evalvating or calling the stalagmite functionfor 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_func(input_vector);endend%study 1 = statical behavior%study_time1 = toc;ticfor i = num_cases[inputs , fopt(i)] = ga(@stalagmite_func, 2);xopt(i) = inputs(1);yopt(i) = inputs(2);endstudy_time1 = toc%polttingfigure(1)subplot(2,1,1)hold onsurfc(x,y ,-f )hold onshading interpplot3 (xopt ,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')title ('Unbounded input')subplot(2,1,2)plot(-fopt)xlabel('Itertions')ylabel('Function maximum')%legend('Itertion','finction minima')tic%study 2 = statical behavior with upper and lower bondfor i = num_cases[inputs , fopt(i)] = ga(@stalagmite_func, 2,[],[],[],[],[0:0],[1:1]);xopt(i) = inputs(1);yopt(i) = inputs(2);endstudy_time2 = toc;figure(2);subplot(2,1,1);hold onsurfc(x,y ,f );hold onshading interpplot3 (xopt ,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r');title ('Bounded input');subplot(2,1,2);plot(fopt);xlabel('Itertions');ylabel('Function maximum');%study 3 study GA ittrationoption = optimoptions('ga');option = optimoptions(option,'populationsize',150);ticstudy_time3 = toc;for i = num_cases[inputs , fopt(i)] = ga(@stalagmite_func, 2,[],[],[],[],[0:0],1:1,[],[]);xopt(i) = inputs(1);yopt(i) = inputs(2);endfigure(3)subplot(2,1,1)hold onsurfc(x,y ,f )hold onshading interpplot3 (xopt ,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')title ('bounded input with modified population size')subplot(2,1,2)plot(fopt)xlabel('Itertions')ylabel('Function maximma')STALAGAMITE FUNCTION CODE:
function [f] = stalagmite_func(input_vector) x = input_vector(1);y = input_vector(2); %f(input) = f1,xf2,xf1,yf2,yf1_x =(sin(5.1*pi*x + 0.5) ).^6;f1_y = (sin(5.1*pi*y + 0.5)).^6;f2_x = exp(-4*log(2).* (((x-0.0667).^2)/0.64));f2_y = exp(-4*log(2).* (((y-0.0667).^2)/0.64));f =-f1_x.* f1_y.* f2_x.*f2_y;end