Challenge

Week 4 - Genetic Algorithm

ga explanation

f_min=ga(@function,nvars,A,B,Aeq,Beq,LB,UB,nonlcon,intcon,options)

f_min= local minima

@function=the function to be called

nvars=no of variables

A and B=inequalities that exist in the function

Aeq and Beq=ineualities in the function

LB=lower bound

UB=upper bound

nonlcon=gives two vectors representing inequalities and equalities

options=used to perform additional operations

step wise explanation of the code

1.the search space rquired to define the function is created using the linspace command

2.and then using the meshgrid command vectors x and y are converted into matrices xx and yy

3.using for loop the input vectors for stalagmite function are defined using matrices xx and yy

4.in the first iteration the algorithm is studied by not defining the limits

5. here the upper and lower bounds are defines to get the global maxima

6.in third iteration population size is inreased as to converge the global maxima.

Errors while programming

1.not defined marker size command after the marker command

2.incorrect syntax used for for xopt

3.not using shading interp command

steps taken to overcome errors are

1.used marker size option

2.used xopt instead of x_val

3.used shading interp command

function [F]=stalagmite(input_vector)

x=input_vector(1);
y=input_vector(2);

f1=(sin(5.1*pi*x+0.5)).^6;
f2=(sin(5.1*pi*y+0.5)).^6;
f3=exp(-4*log10(2)*(x-0.0667).^2/0.64);
f4=exp(-4*log10(2)*(y-0.0667).^2/0.64);

F=-f1*f2*f3*f4;

end





clear all
close all
clc

%Defining oue searchspace

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
[xx,yy]=meshgrid(x,y);


%Evaluating the stalagmite function
for i=1:length(xx)
    for j=1:length(yy)
    input(1)=xx(i,j);
    input(2)=yy(i,j);
    f(i,j)=stalagmite(input);
    end
end

% first iteration of statistical behavior
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2)
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
iterations_1_time=toc

%plotting

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(fopt)
xlabel('num_cases')
ylabel('function maximum')

% second iteration of statistical behavior
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
iterations_2_time=toc

%plotting

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded input')
subplot(2,1,2)
plot(fopt)
xlabel('num_cases')
ylabel('function maximum')

% third iteration of statistical behavior

options=optimoptions('ga')
options=optimoptions(options,'populationsize',200)
tic
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
iterations_3_time=toc

%plotting

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded input with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('num_cases')
ylabel('function maximum')
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.