Project

Genetic Algorithm

Working of Genetic Algorithm:

  1. Random initial population is created first.
  2. A sequence of new populations is created by the algorithm. The individuals in current population are used to create new population.
  3. The algorith stops when the stopping criteria is met.

Code to optimize stalagmite function :

clear all
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

[xx yy] = meshgrid(x,y)

num_cases = 100

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

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

for i = 1:num_cases
[inputs,fval(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end

subplot(2,1,1)
hold on 
surfc(x,y,f)
shading interp
xlabel(\'x values\')
ylabel(\'y values\')
plot3(xopt,yopt,fval,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Bounded inputs\')
subplot(2,1,2)
plot(fval)
xlabel(\'Iterations\')
ylabel(\'Function minimum\')

% function for stalagmite

function[fxy] = stalagmite(input_vector)

f1x = (sin((5.1*pi*input_vector(1))+0.5))^6;
f1y = (sin((5.1*pi*input_vector(2))+0.5))^6;

f2x = exp(-4*log(2)*((input_vector(1)-0.0667)^2)/0.64);
f2y = exp(-4*log(2)*((input_vector(2)-0.0667)^2)/0.64);

fxy = -f1x*f1y*f2x*f2y;

inputs =

0.0668 0.0668

fval =

-1.0000

So the maximum value of the function is 1 and lies on (0.0668,0.0668).

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.