Challenge

Week 4.1 - Genetic Algorithm

Aim:

To code a matlab program for the globel maxima and plot a 3 set of function.

Program:

Main Program:

clear all
close all
clc

%defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cause = 50;

%craeting a 2D array

[xx yy] = meshgrid(x,y);

%evaluating the stagmite function

for 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(input_vector);
    end
end

tic
for i =1:num_cause
[inputs,fopt(i)] = ga(@stalagmite,2)
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time_1 = toc

subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','x','MarkerSize',10,'MarkerFaceColor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iteraction')
ylabel('minimum function')

%study_time_2
tic
for i =1:num_cause
[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

subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','*','MarkerSize',5,'MarkerFaceColor','auto');
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iteraction')
ylabel('minimum function')

%study_time_3 

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',150);

tic
for i =1:num_cause
[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

subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',10,'MarkerFaceColor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iteraction')

Function Program:

function f = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(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*f2x*f1y*f2y);

end

Explanaction:

  • clear all, close all, clc commend used to helps clear all the things stored in workspace,opened dialog box in related to matlab.clc commend clear all things done in commend window.
  • x and y are the input variables and the values are give 0 to 0.6 with the data count of 150 values.
  • meshgrid commend used to create a 2d array.
  • for loop is used to evalate the stalagmite function for the each x value. to evaluate y value for loop created inside the for loop.
  • input vector(1)=xx(i,j) given the 2D vector value to input_vector(1).
  • input vector(2)=yy(i,j) given the 2D vector value to input_vector(2).
  • f(i,j) is the output variable for the stalagmite function.in the calling function we just used the input _vector . while runing the program the function automaticaly call both input vectors as it is assigned.

Function Program enplained:

  • the function is called as Stalagmite.
  • here the input vector values are assigned to n x and y.
  • the given respective formula for populaction has coded.
  • to get the maximum value (-) sign has inputed.

Study Program Explanaction:

Study 1 :

  • tic toc commend is used to calculate the running time for the study 1.
  • num_cause is the number of iteraction used to run the program . Initialy set to 50.
  • an for loop is create to run the program fr the length of num_cause.
  • [inputs,fopt(i)] = ga(@stalagmite,2) in these sentence . inputs are the output value and it contain two outputs. and the fopt gives the optimisaction value.

  • ga is a inbuilt commend in matlab to call genetic algorithm and @ is a sign used to call stalagmite function and the 2 denotes the number of input variables that given
  • the firsr sdtudy is given the unbounded results for the inputs.
  • subplot commend is used plot a graph inside a plot.
  • the number of iteraction is ploted to the minimum values of the function.
  • study 1 takes the run time of 2.2337 sec.
  • surf (x,y,f) used to plot the surface of the 3D model plot
  • subplot(2,1,1) is seting the ploting limits for the inside a plot so the plots wont get mix and give a complex look.
  • in the iteraction plot the graph goes differently respect to data, because the inputs are without limits.
  • for each time running the program the local maxima value change. its is represented in iteraction plot.

Study 2 :

  • Stin the study 2 the inputs are set to inside the limits.
  • [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); in these syntex after calling the function the limits for the evaluation was set between 0 to 0.6.
  • thesese is calles as bounded input function.
  • the prediction of the values are set to sertine limits. so the values predected are inside the limits due to these the evealuated output become 70% accurate.
  • in the ga syntex [] represent the empty values. therefore it has no data. But it is important to include in the syntex because the syntex is a standered formate.
  • the study 2 runn time was 3.9834 sec.
  • in the secound study the iteraction peacks are reduced to four to five. which is indicated in iteraction plot.

Study 3:

  • in the study 3 the populaction size was increased to 150 so the prediction value become minimized to two value and the accuracy of the study become 98%.
  • [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); these syntex is also a standard formate.

  • here opction is inbuilt key to helps to increase the populaction size inside the limite which is 0 to 0.6.
  • since the inputs are high the running time become high compare to other two studyes.
  • the running time for study 3 is 6.9106 sec. in these stud only in two iteraction minimum values are seen.
  • the plot outputs are attached below.
  • in the study 3 the global minima\maxima value are reduced in 2 peacks.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.