Challenge

Week 4.1 - Genetic Algorithm

Aim:to find global maxima of the function and concept of the genetic alogrithm.

what is optimization?

Optimization is the process of making something better In any process, we have a set of inputs and a set of outputs as shown in the following figure.

Optimization refers to finding the values of inputs in such a way that we get the best output values. The definition of best varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.

The set of all possible solutions or values which the inputs can take make up the search space. In this search space, lies a point or a set of points which gives the optimal solution. The aim of optimization is to find that point or set of points in the search space.

WHAT IS GA

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.

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.

Advantages of GAs

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.

  • Useful when the search space is very large and there are a large number of parameters involved.

Limitations of GAs

Like any technique, GAs also suffer from a few limitations. These include −

  • GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.

  • Fitness value is calculated repeatedly which might be computationally expensive for some problems.

  • Being stochastic, there are no guarantees on the optimality or the quality of the solution.

  • If not implemented properly, the GA may not converge to the optimal solution.

1) TO FIND GLOBAL MAXIMA

% depending on the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%convert to 2d array
[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_function(input_vector);
end
end
%ploting
figure(1)
surfc(xx,yy,f)
shading interp
xlabel('xvalue')
ylabel('yvalue')

[inputs , fval] = ga(@stalagmite,2)

2)FUNCTION TO FIND GLOBAL MAXIMA

function [f]= stalagmite_function(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*log(2)*(x-0.0667)^2/0.64);
f4 =  exp(-4*log(2)*(y-0.0667)^2/0.64);
f = (f1*f2*f3*f4);
end

EXPLANATION:

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

by using linspace we taken the value from 0 to 0.6 in 150 parts by using linspace command.

[xx, yy] = meshgrid(x,y); by using meshgrid command we meshed both x&y vectors

for i = 1:length(xx)
for j = 1:length(yy) then i used for command to define the length of 2d array
input_vector(1) = xx(i,j);
input_vector(2) = yy(i,j);and takes the i value in the 2d array which is define as inputs.

f(i,j) = stalagmite_function(input_vector); then i define function by providing inputs in them
surfc(xx,yy,f) by using surfc i got a stalagmite countour

[inputs , fval] = ga(@stalagmite,2 )by use ga command i got the optim solution.

3) 1ST ITERATION FOR GA CONCPET UNBOUNDED INPUTS

clear all
close all
clc
%depending on the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases=50;
%convert into 2d array
[xx , yy] = meshgrid(x,y);

 %evaluating the 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_function_minimum(input_vector);
     end 
 end
%step1 
tic

for i = 1:num_cases
    [inputs, fopt(i)] = ga(@ stalagmite_function_minimum,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1_time = toc
%plotting
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
title('unbounded inputs')
xlabel('iterations')
ylabel('minimum function')

4) 2ND ITERATIONS USING BOUNDED INPUTS

%step2

    tic
    num_cases = 50;
    for i = 1:num_cases
%taking upper bond and lower bond
        [inputs fopt(i)] = ga(@ stalagmite_function_minimum,2,[],[],[],[],[0,0],[0.6,0.6]);
        xopt(i) = inputs(1);
        yopt(i) = inputs(2);
    end
    study2_time = toc
    
    %plotting
    figure(2)
    subplot(2,1,1)
    hold on
    surfc(xx,yy,f)
    shading interp
    plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
    subplot(2,1,2)
    plot(fopt)
    title('bounded inputs')

    xlabel('iterations')
    ylabel('minimum function')

5) 3RD ITERATION BY INCREASING POPULATION SIZE

 %step3
    options = optimoptions('ga');
    options = optimoptions(options,'populationsize',200);
    tic
    for i = 1:num_cases
        %by increasing population size
        [inputs fopt(i)] = ga(@ stalagmite_function_minimum,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
        xopt(i) = inputs(1);
        yopt(i) = inputs(2);
    end
    study3_time = toc
    %plotting
    figure(3)
    subplot(2,1,1)
    hold on
    surfc(xx,yy,f)
    shading interp
    plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
    subplot(2,1,2)
    plot(fopt)
    xlabel('iterations')
    ylabel('minimum function')
    title('optimization')

6) FUNCTION FOR GA CONCEPT

function [f] = stalagmite_function_minimum(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*log(2)*(x-0.0667).^2)/(0.64));
f4 = exp((-4*log(2)*(y-0.0667).^2)/(0.64));
f = -(f1.*f2.*f3.*f4);
end

EXPLANATION:

1)for 1st iteration

num_cases = 50; for providing iterations to run the program

for i = 1:num_cases; by using for loop to define the iterations the program will run.
[inputs, fopt(i)] = ga(@ stalagmite_function_minimum,2);

xopt(i) = inputs(1);
yopt(i) = inputs(2);

by using ga command we got the optimum solution at x and y point.

surfc(xx,yy,f) to get countour at xx yy and f

plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r') to plot xopt yopt and fopt by using plot 3

plot(fopt)

2) for second iterations


num_cases = 50;for providing iterations to run the program.
for i = 1:num_cases,by using for loop to define the iterations the program will run.
%taking upper bond and lower bond by taking upper and lower limit by assigning 0 and 0.6
[inputs fopt(i)] = ga(@ stalagmite_function_minimum,2,[],[],[],[],[0,0],[0.6,0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);

by using ga command we got the optimum solution at x and y point.

surfc(xx,yy,f) to get countour at xx yy and f




plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r'),to plot xopt yopt and fopt by using plot 3 command

plot(fopt) to plot function.

3) for 3rd iterations

options = optimoptions('ga');
options = optimoptions(options,'populationsize',200); by use of option command we used to increase the population .

for i = 1:num_cases,for providing iterations to run the program.
%by increasing population size
[inputs fopt(i)] = ga(@ stalagmite_function_minimum,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
by using ga command we got the optimum solution at x and y point.
surfc(xx,yy,f)
to get countour at xx yy and f
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
plot xopt yopt and fopt by using plot 3 command
plot(fopt) to plot function.

7) graph gor local maxima

8) figure (1) for unbounded inputs for ga

2) for bounded inputs

3) increasing population size

conslusion: all the graphs excuted in right path.

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.