Challenge

Week 4.1 - Genetic Algorithm

function script

function [f]=stalagmite(v)
x=v(1);
y=v(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*f1y*f2x*f2y);

end

Main script

clear
close
clc

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

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

%creating for loop to use stalagmite 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

%case 1 study unbounded
%use ga to find minimum of function
tic
for i=1:num_case
    [x1,dopt(i)]=ga(@stalagmite,2);
    xopt(i)=x1(1);
    yopt(i)=x1(2);
end
time1=toc;

%plotting the surface and graph for case 1 
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-dopt,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-dopt)
xlabel('iteration')
ylabel('function minimum')

%case study 2 bounded
tic
for i=1:num_case
    [x2,dopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4]);
    xopt2(i)=x2(1);
    yopt2(i)=x2(2);
end
time2=toc;
%plotting the surface and graph for case 2
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt2,yopt2,-dopt2,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-dopt2)
xlabel('iteration')
ylabel('function minimum')

%case study 3
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',400);
tic
for i=1:num_case
    [x3,dopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);
    xopt3(i)=x3(1);
    yopt3(i)=x3(2);
end
time3=toc;
%plotting the surface and graph for case 3
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt3,yopt3,-dopt3,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('case 3')
subplot(2,1,2)
plot(-dopt3)
xlabel('iteration')
ylabel('function minimum')

Step for of Code genetic algorithm

  1. First we create the array using linspace command and then defining it to the x and y
  2. Then we use meshgrid to get 2d data.
  3. In next step we create a function in which we make devide the equation into 2 parts and then naming it f1 f2 and later on using the f1 and f2 make it into one equation which makes our work easy and then naming it as stalagmite we are going to save it to same location where main script is saved.
  4. Now its time to use our function so we need our function run the number of times so we create a nested for loop in which we use our xx and yy which we get from meshgrid command to get the value of f and lets make the loop end
  5. Our next step is to create another for loop for our case 1 which is unbounding case but this we gonna use one new command before and after which is tic-toc to start the timer and stop the timer respectively
  6. In for loop first we define indexing i which is equal to 1:number of times (which we take 50) and output we required is x1,dopt,xopt,yopt to plot the unbounding case surface and graph but require value of index in single digit and remaining 3 output in multiple values so we make here the use of i like dopt(i) ,xopt(i), yopt(i) after this lets end the for loop and stop it with time1=toc as we started it with tic.
  7. Now its time to plot the graph and surface so here we use the bunch of commands because we require both plot and surface in same figure so first we use figure(1) as its our first case. Next command is subplot(2,1,1) giving the values of number of grid rows no of grid column and grid position for axes
  8. Our next command in bunch is hold on to hold the above data of subplot and plot next data which is surf(xx,yy,f) as we requires surface in 3d so we require data in 3 axis so we take xx as x, yy as y,and f as z so we get the 3d plot.
  9. Now its time to plot the unbounded points so for that we take dopt ,xopt, yopt as inputs and use marker command to create the dot points. After that we plot the dopt single input graph and then normal commands like xlabel and ylabel Now lets go to second case in second case its our bounded in this case we didn’t require much changes from above for loop we just need to change one command line which is given below
  10. [x2,dopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4])
  11. In above line we see output as x2 which is single digit and dopt(i) which we requires to be multiple numbers the new change here is input line which uses that ga command which is used for finding of minimum in function using genetic algorithm and next is our function and hence we don’t want bound so we use [] in place of aeq, beq,ib and defines remaining values. Rest of the part for plotting is same as case one as so as for case 3 just variable name changes.
  12. in case 3 whole part is same we just ad 2 new commands before the for loop which is given below

options=optimoptions('ga');

options=optimoptions(options,'PopulationSize',400);

this optimoption command is use to optimise the solver we take ga as solver and in next command taking option as solver and naming it population size and define its value which could be change. the next change in case 3 can be seen in equation below

[x3,dopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);

As we see the difference between 2nd and 3rd case equation we have to apply here the option which optimise the value but we don’t have the remaining values so we kept that another 2 square bracket empty before option in equation rest of the command are same as in case 2 like plot, subplot and all

1.theory on ‘ga’ and its syntax-

‘ga’ is the command use to find the minimum of the function using genetic algorithm. In ga command syntax ,we can fill the data we have or rest data we can left with empty square bracket ga syntax we used is given below [x3,dopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);

In this syntax like we see at left hand side there is output and at Rhs inputs in inputs we see first we use the ga of the function and then number of variables we don’t have some inputs so we left the square bracket empty there and only fill the values which we have our last inputs is option which we defines as optimise option

  1. stoping criteria: there are few stoping criteria’s like max generation, max time, fitness limit, max stall generation, max stall time, function tolerance

3.significance of running multiple iteration and multiple cases : we run 3 cases in which we see the results goes in more detail and more the values minimises and this is our need to minimise the values and get more detailed result like in case 1 we see the boundary is not defined hence the point created are more and in case 2 the boundary is defined here so we can see the points are stoped in boundary and there are limited points mainly 2 or 3.and in case we get more detailed result as we have only one point there. So for more detailed and minimised points we need number of cases and iteration

4.concept of genetic algorithm : natural selection where fuittest individualare selected for reproduction in order to produce the next generation

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.