Challenge

Week 4 - Genetic Algorithm

INTRODUCTION:

Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning.

In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.

In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.

Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well

GAs have various advantages which have made them immensely popular. These include −

  • Does not require any derivative information (which may not be available for many real-world problems).
  • Is faster and more efficient as compared to the traditional methods.
  • Has very good parallel capabilities.
  • Optimizes both continuous and discrete functions and also multi-objective problems.
  • Provides a list of “good” solutions and not just a single solution.
  • Always gets an answer to the problem, which gets better over the time.

AIM: Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

Procedure:

We need to consider random values in space for mesh grids. now need write a program for our requirements and for our functional equations like

In the function we need to write the equations.

Behaviour of genetic algorithm for 50 iterations.

Behaviour of genetic algorithm for 50 iterations with upper and lower limits.

Behaviour of genetic algorithm for 100 iterations for increase in random values up to 300.

For first case and second case the the values are not single.it will give all the max values each location. And graph also not smooth. With this result find the single peak is difficult.

In third case we are going increase the random values to 300.

Normally ‘ga’ will give the min value, but for our requirement we need write for max as negative of optimal value.

Funtion Code:

function[fx_y]=stalgamite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin((5.1*pi.*x)+0.5)).^6;
f1_y=(sin((5.1*pi.*y)+0.5)).^6;
f2_x=exp(-4*log(2).*(((x-0.0667).^2)/0.64));
f2_y=exp(-4*log(2).*(((y-0.0667).^2)/0.64));
fx_y=-(f1_x.*f1_y.*f2_x.*f2_y);
end

Program Code:

clear all
close all
clc

%Work space%
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%2d_MESH%
[xx yy]=meshgrid(x,y);
% solving Stalgamite 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)=stalgamite(input_vector);
    end
end
surfc(f)
xlabel('X axis');
ylabel('Y axis');

shading interp
[inputs,f_val]=ga(@stalgamite,2)

num_ites=50;
tic
%Stastical Behaviour%
for i=1:num_ites
    [inputs,fopt(i)]=ga(@stalgamite,2)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
case1_time=toc

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbuond inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function min')
hold off

tic
%2nd case stastic behaiour with limits%

for i=1:num_ites
    [inputs,fopt(i)]=ga(@stalgamite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
case2_time=toc

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbuond inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function min')
hold off

%case3 increase in iterations%
num_ites=100;
options=gaoptimset('ga')
options=gaoptimset(options,'populationsize',300)
tic
for i=1:num_ites
    [inputs,fopt(i)]=ga(@stalgamite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

case3_time=toc

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbuond inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function min')
hold off
f=(-(fopt));
h=max(f);
disp(h);

Results:

1st case:

2nd Case:

3rd Case:

Errors:

  1. ‘xopt’ function went wrong in ‘for’ loop issue resolved by the suggestion from the MATLAB group.
  2. Warning: You are using 'mutationgaussian' mutation function for constrained minimization.
    Solution may be infeasible; use '@mutationadaptfeasible' function for constrained minimization.

References:

https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_introduction.htm

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.