Challenge

Week 5 - Genetic Algorithm

clear all 
close all
clc 

num_case= 100;

%difining the search space
x = linspace(0,0.5,50);
y = linspace(0,0.5,50);

%creating the 2 dimentional array

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

%evaluvating the stalagmate 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_func (input_vector);
    end
end

%surfc(xx,yy,f)

%shading interp
 

 tic
 %study1 - statistical behaviour
 for i = 1:num_case
     [input,fopt(i)] = ga(@stalagmite_func , 2);
     xopt(i) = input(1);
     yopt(i) = input(2);
 end

 study1_time = toc

 figure(1)
 subplot(2,1,1)
 
 surfc(x,y,-f)
 shading interp
 hold on
 plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',4,'MarkerFaceColor','r')
 title('unbounded inputs')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('itteration')
 ylabel('function minimum')


  tic
 %study2 - statistical behavior - with upper and lower bounds
 for i = 1:num_case
     [input,fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
     xopt(i) = input(1);
     yopt(i) = input(2);
 end
 
 study2_time = toc
 figure(2)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',4,'MarkerFaceColor','r')
 title('bounded inputs')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('itteration')
 ylabel('function minimum')

 %study3 - increasing GA iteration
 options = optimoptions('ga');
 options = optimoptions(options,'PopulationSize',500);


tic
 for i = 1:num_case
     [input,fopt(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
     xopt = input(1);
     yopt = input(2);
 end
 study3_time = toc
 figure(3)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',4,'MarkerFaceColor','r')
 title('bounded inputs')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('itteration')
 ylabel('function minimum')

Introduction:

To write a code in Matlab to optimise the stalagmite function and find the global maxima of the function.

Objective:

The main objective of this function is to make a gentic algorithm using an stalagmite func code and to find the global maxima of the given function.

Explanation:

The first three lines are starting with the basic commands that are Close all, Clear all and Clc.

The 5th line gives the num case which is 100

The 7th line gives the header for defining the search space and the 8th and 9th line gives the X and Y value which is the linspace of 0 to 0.5 with 50 intervals.

The 11th gives the header for creating the 2 dimentional array.
The 13th line gives the xx and yy value which is given as meshgrid command as x and y.
The 15th line gives the header for evaluating the stalagmite function.
The 17th line gives the for loop for evaluating the stalagmite func as i equals to the 1:length of xx, as same as the j gives the same for loop with 1:length of yy.
Then the 19th and 20th lines give the input vector for 1 and 2 for xx and yy with respect to the i,j.
The 21st line gives the f value with respect to i,j as stalagmite func which is evaluated as another script to run this code and given as input vector.
Then the 22nd and 23rd lines give the end loop for both i and j.
The 25th line gives the surfc command which gives the better animation look for the output plot for xx and yy in f.
The 27th line is a header as shading intern.
The 30th line gives the first tic header, then the 31st line gives the study1 behaviour as statistical.
The 32nd line gives the for loop where i is equal to the 1:num case and input fopt(i) equals to the ga @stalagmite func which is created as another code as 2.
The 34th line gives the x output as input 1 and the 35th line gives the y output as input 2 and the 36th line is the end of the for loop.
The 38th line gives the 1st study time as toc Where the 40th and 41st line gives the figure 1 and subplots as 2,1,1.
The 43rd line shows the surfc command for x,y,-f where the surfc command gives the animating appearance of the plot. and below the shading interp command also given and then hold on command is also given for command repeatition.
The 46th line gives the plot 3 command for plotting the x,y,-f output with marker and marker size and face color as 4 and color red with a title of unbounded inputs.
The 48th line gives the subplot with the values of 2,1,2 and again the plot command is used for plotting the -f output.
The 50th and 51st line gives the label for X and Y labels as itteration and function minimum.
The 54th line shows the tic header where the 55th line gives an another header study 2 , statistical behaviour with upper and lower bounds.
The 56th line gives the for loop opens as i = 1:num case.
The 57th line shows the input and output of f (i) equals as ga at stalagmite function in 2 with 4 empty columns and 0:0 also with 1:1.
The 58th and 59th line gives the X and Y output as input 1 and 2 with for loop end.
The lines from 62 to 73 shows the plotting command basis as same as mentioned in the first plot.
The line 75 shows the header for 3rd study as increasing genetic algorithm.
The 76th and 77th line shows the optimoptions which is ga on 76th line and options as population size as 500.
The line 80 shows the header as tic 3 and the 81st shows the for loop for study 3 as i equals to the 1:number case.
The 82nd line gives the input and output of f as i which equals genetic algorithm stalagmite function 2 with 4 empty columns and with 0:0 and 1:1 and also with 2 empty column options.
The 83rd and 84th line shows the input for 1 and 2 where the 85th line gives the end for the for loop.
The lines from 86 to 97 shows the plotting command basis as same as mentioned in the first and second plot.
Conclusion:
Thus the function to make a genetic algorithm using an stalagmite func code and to find the global maxima of the given function is determined.
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.