Challenge

Week 5 - Genetic Algorithm

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Objective- To optimise the stalagmite function and find the global maxima of the function ,write a code for that. plot graphs for all three studies and for F maximum vs no. of iterations.
clear all
close all
clc

% Defining search space
x=linspace(0,0.5,250);
y=linspace(0,0.5,250);
num_cases=100;

%creating a 2 Dimensional mesh
[xx,yy] =meshgrid(x,y);
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
%Evaluating the stalagmite function
for i=1:num_cases
    [inputs,fopt(i)] =ga(@stalagmite,2);

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

 
end
figure(1)
subplot(2,1,1)
surf(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'o','markersize',4,'markerfacecolor','b')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

%With upper and lower bounds
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

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'o','markersize',4,'markerfacecolor','b')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')


%increasing GA iterations
options =optimoptions('ga');
options =optimoptions(options,'PopulationSize',200);


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

    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'o','markersize',4,'markerfacecolor','b')
title('bounded inputs with modified population size')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')






Results-

plot 1- Unbounded Inputs

Plot 2- Bounded Inputs

Plot 3- Bounded inputs with modified population size.

  • Step wise explaination of your code-
  • I started the code with % command and assigning the terms required for the code.
  • Then linspace command is used for x and y variables and compute the data.
  • For creating a two dimensional mesh ,we used meshgrid command for x and y function.
  • Linspace is used to create an evenly spaced sequence in a specified interval.
  • Meshgrid function is rectangular grid out of two given one dimensional arrays representing the cartesian indexing or matrix indexing.
  • Then for loop is used for initialize the i and j variable and input vectors 1 and 2 are created.
  • Stalagmite function is used to compute the input vectors.
  • stalagmite function generally refers to a function with which we generate values ​​on the surface of a stalagmite to find the local and global maxima and minima of that particular given function.
  • Plot command is used to plot the graph.
  • Here, iterations vs minimum function graph are plotted.
  • Then ga function is used in the above program.
  • The algorithm repeatedly modifies a population of individual solutions.
  • Conditions like bounded inputs ,unbounded inputs and bounded inputs with modified population size ,for these ,graphs are plotted.
  • Expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • Genetic algorithm theory was first proposed by "CHARLES DARWIN" and developed by "JOHN HOLLAND".
  • John Holland is also known as the father of genetic algorithms.
  • A genetic algorithm is a technique for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution.
  • The genetic algorithm constantly adjusts the population size of individual solutions.At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation.
  • The working process of ga can be given as;

1. Initialization :In this process we have to decide the population size ,i.e, the more the population size ,the more we get accurate result.

2. Selection :
In this step, we need to select the parents for the sample data points based on the high curve fit. Selection is based on fitness score. Two pairs of individuals are selected based on the fitness score. Individuals with high fitness have a better chance of being selected for reproduction.


3. Crossing:
In this step, take two parents and produce a child based on the interaction between the genes.Crossover is the most significant phase in a genetic algorithm. offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.


4. Mutation:
In this step, the genes are replaced by random integers. In this the algorithm terminates if the population has converged.Sequence of phases is repeated to produce individuals in each new generation which are better than the previous generation.


5. Fitness function:
In this step, we find out how good the solution is.

  • ga Syantax
ga(func,nvars,A,b,Aeq,Beq,lb,ub,nonlcon,lntcon,options)
fun - fitness function.
nvars- number of variables,
A,b= Linear inequality constraints
Aeq,beq=Linear equality constraints
lb- Lower bound(limit),
ub- Upper bound(limit),
nonlcon : Use for nonlinear constraints
IntCon : to get a solution in integer.
options- To set population size to minimize with non-default options
So, this was a detailed explanation of the genetic algorithm and its syntax

References-

1.https://skill-lync.com/student-projects/Stalagmite-function-67841

2.https://skill-lync.com/student-projects/Matlab-code-to-optimise-the-stalagmite-function-and-find-theglobal-maximaof-the-function-52561

3.https://www.sciencedirect.com/topics/medicine-and-dentistry/genetic-algorithm#:~:text=Genetic%20Algorithms%20(GA)%20were%20introduced,selection%20(Darwin%2C%201859)

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.