Challenge

Week 5 - Genetic Algorithm

PROGRAM CODE 


clear all 
close all
clc

% Defining our Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

% Creating 2-d Array
[xx,yy] = meshgrid(x,y)

% Calculating Fitting 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_func(input);
    end
end

% Converting Fitting Function to change the graph upwards
F = -f;

% Plotting Stalagmite Function
surface(xx,yy,F);
% shading interp;
hold on;

% Performing the Genetic Algorithm
[output , fval] = ga(@stalagmite_func,2,[],[],[],[],[0,0],[0.6,0.6])

% Plotting Global Maxima
x_max = output(:,1);
y_max = output(:,2);
f_max = -fval;
plot3(x_max,y_max,f_max,'marker','o','markersize',5,'markerfacecolor','r');
xlabel ('X-value');
ylabel ('y-value');
title('GLOBAL MAXIMA');
legend('STALAGMITE','GLOBAL MAXIMA')

FUNCTION CODE:-
function A = stalagmite_func(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log(2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.0667).^2)/0.64);
A =-(f1*f2*f3*f4);
end

Output

What are Genetic Algorithms?

Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.

GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.

Syntax for genetic algorithm-

we can give the inputs by any one of the following ways-

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.