Challenge

Week 4.1 - Genetic Algorithm

concept of genetic algorithm

genetic algorithm is carried out by the process of natural selection , used for solving constrained and unconstrained optimization problems , where the capable indiduals are selected for reproduction and for production of offspring of next generation . it keeps on modifying the population to created a sequuence of new population , over successive generations , the population evolves to an optimum solution .

explanation for the syntax

[x,fval] = ga(@fun,nvars)

x contains the two output values and the fval contains single output ,

ga is the solver name , and the function is the obejective function which we have to solve ,

nvars is the number of variables

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

a is the A matrix for linear inequality constraints

b is the b vector for linear inequality constraint

aeq is the Aeq matrix for linear equality constraints

beq is the beq vector for linear equality constraints

lb is the lower bound

ub is the upper bound

nonlcon is the Nonlinear constraint function

intcon is the Indices of integer variables

options is the Options created using optimoptions or an options structure

stopping criteria

stopping criteria determines what causes the algorithm to terminate , generations specifies the maximun bnumber of iterations the genetic algorithm performs

time limits specifies the time limit the genetic algorithm runs before stopping

if the best fitness valuee is less than or equal to the value of fitness limit , it stops

if the average change in the fitness function value over stall generation is less than the function tolerance , algorithm stops

errors faced

while typing the formula for the function i had used Ln instead of using the log , mat lab throws an error , after discussed with my mentod , i came to know that log must be used .

in tthe ga solver syntax , for setting the lower boundary i have used o instead of using 0 , this throws me an error

and i faced a 'n' number of errors , those were not even supposed to be taken in account , but i had made it , im trying a lot to overcome these kind of lathogic mistakes

significance of running multiple iterations

this has a most important significant that while running a multiple iterations , the solver gives us a constant values of output

at the end of reproduction ,

clear all
close all
clc

%inputs
x=linspace(0,1.2,130);
y=linspace(0,1.2,130);

%2 dimensional mesh creation

[X Y]=meshgrid(x,y);

%evaluating the stalagnite function

for i =1:length(X);
  for j =1:length(Y);
    
    inp_vector(1)=X(i,j);
    inp_vector(2)=Y(i,j);
    f(i,j)=stalagmite(inp_vector);
end
end

   %creating a 3d surface(X,Y,f)
   
   
   surfc(X,Y,-f)
   
   shading interp
   
  [x,fval] = ga(@stalagmite,2)
   
  xlabel('xaxis')
  ylabel('yaxis')
  zlabel('zaxis')
  title('geometric algorithm')
  tic
    %study 1 
  
num_cases=50
for i = 1:(num_cases);
[inputs,fopt(i)] =ga(@stalagmite ,2);
    
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study1_time=toc

figure(1)
pos1 = [0.1 0.3 0.3 0.3];
subplot('Position',pos1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt)
title('unbounded inputs')
pos2 = [0.5 0.15 0.4 0.7];
subplot('Position',pos2)
plot(-fopt,'-p','color','red','linewidth',2)
xlabel('iterations')
ylabel('function max')

%study2
%statistical behaviour with upper and lower bounds
tic
num_cases=50
for i = 1:(num_cases);
[inputs,fopt(i)] =ga(@stalagmite ,2,[],[],[],[],[0;0],[1;1]);
    
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study2_time=toc

figure(2)

subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','d')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt,'-p','color','red','linewidth',2)
xlabel('iterations')
ylabel('function max')


%study 3
tic

options=optimoptions('ga');
options=optimoptions(options,'populationsize',250);


for i=1:num_cases;

[inputs,fopt(i)] =ga(@stalagmite ,2,[],[],[],[],[0;0],[1;1],[],[],options);
    
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
    
end
study3_time=toc

figure(3)

subplot(2,1,1)
hold on
surfc(X,Y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','d')
title('bounded inputs with modified population size')
subplot(2,1,2) 
plot(-fopt)
xlabel('iterations')
ylabel('function max')




stalagmite function :



function[f]=stalagmite(inp_vector)
x=inp_vector(1);
y=inp_vector(2);
f1_x=(sin(5.1*pi*x+0.5))^6;
f2_x=exp(-4*log(2)*((x-0.0667)^2/0.64));
f1_y=[sin(5.1*pi*y+0.5)]^6;
f2_y=exp(-4*log(2)*((y-0.0667)^2/0.64));

f= -(f1_x*f2_x*f1_y*f2_y);
end
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.