Challenge

Week 4 - Genetic Algorithm

#Aim:To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

#Concept of Generic Algorithm:

1.We have defined domain for a problem.So GA begins by creating random initial population ,it can be outside the domain too, as GA use negative sign also for individuals to find minima of function.

2.For all the regions inside mesh,one point will be selected based on the fitness value.

3.Minimum value of fitness function evaluated.

4.If the solution obtained is not optimum,then next generations population is created.

5.On the basis of fitness value of individuals,parents are chosen.

6.In selection process,parents are selected from current generation on the basis of fitness value.

7.Then in Elite phase ,individual having best fitness value passed to next generation straightforwardly.

8.These are considered as parents.Next childrens are created by Crossover and Mutation.

9.Crossover children by selecting vector entries, or genes, from a pair of individuals in the current generation and combines them to form a child

Mutation children by applying random changes to a single individual in the current generation to create a child

10..These childrens created replaces current generation individual with best fitness value to form next generation.

11.The algorithm stops when one of the stopping criteria is met.Few of them are:

  • MaxGenerations — The algorithm stops when the number of generations reaches MaxGenerations

  • MaxTime — The algorithm stops after running for an amount of time in seconds equal to MaxTime .

  • FitnessLimit — The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to FitnessLimit.

------------------------------------------****************************----------------------------------------

#Explain the syntax for ga in MATLAB in your report:

Complete form for GA :

[X fval] = ga(FITNESSFCN,NVARS,A,b,[],[],lb,ub,NONLCON,INTCON,options)

X:Points at which Fitness function has global minima

fval:Global minimum value of the function.

fitnessfcn: Fitness function
nvars: Number of design variables
Aineq: A matrix for inequality constraints
bineq: b vector for inequality constraints
Aeq: Aeq matrix for equality constraints
beq: beq vector for equality constraints
lb: Lower bound on X
ub: Upper bound on X
nonlcon: Nonlinear constraint function
intcon: Index vector for integer variables
options: Options created with optimoptions('ga',...)

----------------------------------------------*****************************-----------------------------------

#Program for this challenge:

clear all 
close all
clc

%Genetic algorithm program
%Main program
%Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);



%Evaluating stalagmite function
for i =1: length(x);
    for j =1: length(y);
        input(1)=x(i);
        input(2)=y(j);
        f(i,j)=stalagmite_fun(input);
    end
end



%Study-1 statistical behaviour
tic;
for k=1:50
[result fopt(k)]=ga(@stalagmite_fun,2);
xopt(k)=result(1);
yopt(k)=result(2);
end
study1_time=toc

figure(1)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
xlabel('x value')
ylabel('y value')
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbound Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')



%Study-2-Statistical behaviour-with Lower and Upper bounds
tic;
Lower_bound=[0 0];
Upper_bound=[0.6 0.6];
for k=1:50
[result fopt(k)]=ga(@stalagmite_fun,2,[],[],[],[],Lower_bound,Upper_bound);
xopt(k)=result(1);
yopt(k)=result(2);
end
study2_time=toc

figure(2);
subplot(2,1,1);
hold on;
surf(x,y,f);
shading interp;
xlabel('x value');
ylabel('y value');
title('Results with bounded inputs');
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2);
plot(fopt);
xlabel('Iterations');
ylabel('Function Maximum');
axis([0 50 -1.2 0.8]);


%STudy 3 Statistical behaviour with lower and upper bounds and increased
%population size

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


tic;
Lower_bound=[0 0];
Upper_bound=[0.6 0.6];
for k=1:50
[result fopt(k)]=ga(@stalagmite_fun,2,[],[],[],[],Lower_bound,Upper_bound,[],[],options);
xopt(k)=result(1);
yopt(k)=result(2);
end
study3_time=toc

figure(3);
subplot(2,1,1);
hold on;
surf(x,y,f);
shading interp;
xlabel('x value');
ylabel('y value');
title('Results with bounded inputs and Population size = 170 ');
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r');
subplot(2,1,2);
plot(fopt);
xlabel('Iterations');
ylabel('Function Maximum');
axis([0 50 -1.2 0.8]);

fprintf('<strong>Global Maxima of the Fitness function for study-3 is %.5f</strong>',fopt(end))
function [out]=stalagmite_fun(input)
%Fitness function defined
out1=(sin(5.1*pi*input(1)+0.5))^6;
out2=(sin(5.1*pi*input(2)+0.5))^6;
out3=exp(-4*log(2)*(input(1)-0.0667)^2/0.64);
out4=exp(-4*log(2)*(input(2)-0.0667)^2/0.64);
out=-out1*out2*out3*out4;
end

#Plot for 1-Study:

First plot shows,the results of GA for a given fitness function is not dependent on domain of values to be given.It is giving results even for the region where our domain is not defined in negative x and y values.

For every iteration the result also varying which is completely incorrect.

------------------------------------------------***********************-----------------------------------------

#Plot for 2-Study:

Second study has incorporated lower and upper bounds,regions where GA will find optimum solution.

------------------------------------------------***********************-----------------------------------------

#Plot for 3-Study:

Third study adds population size values of 170 ,so the probabilty of getting optimised solution is more from a particular mesh region.

It definitely slow down GA process.

Time of Study 1-8.6608 sec

Time of Study 2-8.8692 sec

Time of Study 3-19.1895 sec

----------------------------------------********************************-------------------------------------

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.