Project

Maximizing Function Using Genetic Algorithm

  • Genaetic algorithm is inspired by Darvin\'s theory of natural selection. Natural selection starts with selection of the fittest individuals from initial population. From fitness function fitness values of each candidate can be found.

There are 5 steps in Genetic Algorithm.

  • Initial Poulation
  • Fitness function
  • Selection
  • Crossover
  • Mutation
  • The process begins with the set of individuals which is called as Population. Each individual is a solution to the problem you want to solve
  • The fitness function determines how fit an individual is. It gives a fitness values to each individuals. The probability that an individual will be selected for reproduction is based on a fitness score.
  • The idea of selection is to select the fittest individuals and let them pass their gene to the next generation. Pairs of individuals are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.
  • The idea behind crossover is to create new individual (children) by combining pairs of parents from current population.
  • Mutation is not done in every case. Generally mutartion is creating children by randomly changing the gene of individual parent.
function [input_values] = stalagmite(input_vector)


f1x = (sin(5.1.*pi.*input_vector(1) +0.5)).^6;
f2x = exp((-4*log(2).*(input_vector(1)-0.0667).^2)./0.64);
f1y = (sin(5.1.*pi.*input_vector(2)+0.5)).^6;
f2y = exp((-4*log(2).*(input_vector(2)-0.0667).^2)./0.64);
func = f1x.*f2x.*f1y.*f2y;

input_values = -func;


end
clear all
close all
clc

%Defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 200;

% For getting same output everytime
rng(1,\'twister\')

%Generating 2D mesh
[xx,yy] = meshgrid(x,y);

%Evaluating the stalagmite function

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

%Study 1 - Statastical behaviour
for i=1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

%Plot
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt ,yopt, fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'b\')
title(\'Unbound inputs\');
subplot(2,1,2)
plot(fopt,\'linewidth\',1.5)
xlabel(\'Iterations\')

%Study 2 - Statastical behaviour with lower and upper bound
tic
for i=1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study2_time = toc

%Plot
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt ,yopt, fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'b\')
title(\'Bounded inputs\');
subplot(2,1,2)
plot(fopt,\'linewidth\',1.5)
xlabel(\'Iterations\')

%Study 3 - Getting Optimization

options = optimoptions(\'ga\');
options = optimoptions(options,\'populationsize\',200);

tic
for i=1:num_cases
    [inputs,fopt(i),output] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study3_time = toc

%Plot
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt ,yopt, fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'b\')
title(\'Bounded inputs with modified population size\');
subplot(2,1,2)
plot(fopt,\'linewidth\',1.5)
xlabel(\'Iterations\')

\"Unbound

\"Bounded

\"Optimized

  • \'rng\' command is used to get same output everytime.
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.