Challenge

Week 4.1 - Genetic Algorithm

AIM:

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

INTRODUCTION:

  • Genetic algorithm (GA) is a search based optimization tecqnique based on the principle of Genetic and Natural Selection.
  • It is frequently used to find the optimal or near optimal solution for difficult problem.
  • It is used to solve optimization problems,in research, and in machine learning.

HOW THE GENETIC ALGORITHM WORKS:

  • The algorithm begins by creating a random initial population.
  • Then it creates a sequence of new population.
  • At each step, the algorithm uses individuals in the current generation creates the next population.
  • To create a new population the algorithm performs the following steps.
  • Each member in the current population is computed by fitness value. These values are called the raw fitness scores.
  • The raw fitness scorss are converted into more usable range of values.These values are called expectional values.
  • Some of the current population that have lower fitness are chosen as elite.These elite individuals are passed to the next population.
  • Children are produced either by making random changes to a single parent,mutation,or by combinig the vector entries of a pair of parents.
  • Replaces the current population with the children to form the next generation.
  • The algorithm stops when one of the stopping criteria is complete.

OPERATORS OF GENETIC ALGORITHM:

  1. Selection Operator
  2. Crossover Operator
  3. Mutation Operator

SYNTAX FOR ga:

ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],option)

ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)

ga- To find the minimum of the function using genetic algorithm.

fun- Find a local minimum of the function

nvars- number of variables

A- linear inequality constraints,specified as real matrix

b- linear inequality constraints,specified as real vector

Aeq- linear equality constraints,specified as real matrix

beq- linear equality constraints,specified as real matrix

lb- lower bound

up- upper bound

nonlcon- nonlinear constraints,specified as function handle

option- optimization option,specified as the output

PROGRAM:

clear all
close all
clc

%defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases = 50

%creating a 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);
        input_vector=[input_vector(1),input_vector(2)];
        GA(i,j)=stalagmite(input_vector);
    end
end

%study_1 in unbounded condition

tic
for i=1:num_cases
    [input, GA_opt(i)] = ga(@stalagmite,2);
    xopt(i)=input(1);
    yopt(i)=input(2);
end
study_1_time = toc
%plot the graph
figure(1)
subplot(2,1,1)
hold on
surf(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iteration')
ylabel('Function maximum')

%study_2 upper and lower bounds

tic 
for i=1:num_cases
    [input, GA_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)=input(1);
    yopt(i)=input(2);
end

study_2_time = toc
%plot the graph
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iteration')
ylabel('Function maximum')

%study_3 increasing GA iterations

options = optimoptions('ga');
options = optimoptions(options,'populationsize',240);
tic
for i=1:num_cases
    [input, GA_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i)=input(1);
    yopt(i)=input(2);
end

study_3_time = toc
%plot the graph
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-GA)
shading interp
plot3(xopt,yopt,-GA_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-GA_opt)
xlabel('Iteration')
ylabel('Function maximum')

function[ga] = stalagmite(input_vectors)

x=input_vectors(1);
y=input_vectors(2);

term1 = pi*5.1*x;
term2 = pi*5.1*y;
term3 = ((x-0.0667)^2/0.64);
term4 = ((y-0.0667)^2/0.64);
term5 = (-4)*log(2);

xf1 = (sin(term1+0.5)).^6;
yf1 = (sin(term2+0.5)).^6;
xf2 = exp(term5*term3);
yf2 = exp(term5*term4);

ga=-(xf1*yf1*xf2*yf2);
end

OUTPUT:

EXPLANATION:

  • First we initialise the values of x and y by taking 150 values between the interval o f 0 to 0.6.
  • Then we consider the no of iterations as 50.
  • Then we take a 2d array of x and y using meshgrid and store them in the input_vector array.
  • Then another function is created for the stalagmite function.Here the output is taken as -GA since ga gives only the minimum valueof the function.So -GA automatically considered to give the maximum value.
  • Using the genetic algorithm function the maxima is calculated and the output is stored in insputs.
  • In study 1 the iteration is done in unbounded conditions
  • We use the plot3 command to make a plot of xopt,yopt and -GA_opt.
  • To get the time taken for the complete cycle we uae the command tic toc
  • In study 2 in takes place in bounded values.Here we specify the upper and lower values for x and y, and ga is initoated with random population only.
  • Then the same plotting method is done to plot the graph.
  • In study 3 let us increase our initial population which is default as 50 to 250.
  • The initial population is set my using the command called optimumopytions and get our value of populationsize to 250.
  • Now the results are stored in and the graph is plotted as in the previous study.

ERROR:

Havnt got any error while running the program.

REFERENCES:

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.