Challenge

Week 4.1 - Genetic Algorithm

                                                  PROGRAM IN MATLAB TO WRITE A CODE TO OPTIMISE THE STALAGMITE FUNCTION

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

THEORY: Genetic algorithm is a search based technique to based on principles of genetic and natural selection. It is used to find the best optimal solution or near optimal solutions for the given data set functions

Formulae required:

 In order to get the best fit for the given below function the below formulae are required.

The given stalagmite functions has inputs of x and y

`f(x,y) = f1*f2*f3*f4`

`f1=[sin(5.1pix+0.5)]^6`

`f2=[sin(5.1piy+0.5)]^6`

`f3=exp[-4log(2)*(x-0.0667)^2/0.64]`

`f4=exp[-4log(2)*(y-0.0667)^2/0.64]`

MATLAB CODE FOR THE GIVEN STALAGMITE FUNCTION:

clear all
close all
clc

%creating the x and y values and num of cases
x=linspace(0,0.6,100);
y=linspace(0,0.6,100);
num_cases=50

%Creating mesh for plot
[xx yy] = meshgrid(x,y);

%Determining the length using FOR loop
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 
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

%Plotting of figure 1 for many solutions
figure(1)
subplot(2,1,1)
surface(xx,yy,f)
hold on
xlabel('X value')
ylabel('Y value')
shading interp
[inputs,fval]=ga(@stalagmite,2)
plot3(xopt,yopt,fopt,'marker','o','markersize',3,'markerfacecolor','g')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function local max and min')
legend('No of solutions')


%study 2
for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

%Plotting for solution and population size
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
legend('Iterations')

%Study 3
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);

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

%Plot for optimised result for local maxima
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
legend('No of solutions')

MATLAB CODE FOR FINDING THE GLOBAL MAXIMA FOR GIVEN FUNCTION:

function [f] = stalagmite(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

CONCEPT OF GENETIC ALGORITHM:

Genetic algorithm is a process that starts with an optimisation initialization, when the population is the number of sample data points taken to perform the genetic algorithm operation. These sample data points undergo the following three steps.

Selection: In this step the parental data are selected from the sample data points, the main objective of the selection process is to select the highly fit individuals. The selected parent data will crossover the process. Therefore fitter individuals have a higher chance of mating and propagating their features to the next generation.

Crossover: It is the process of taking two parent solutions and producing the crossover solution from them. There are many ways to do cross over operation. Mutation is one of the methods carried out to maintain genetic diversity from one generation to another population. If mutation is not performed the fresh generation of the population will become similar to each other which results in local minima. Similar to crossover there are many ways to perform the mutation process.

FITNESS FUNCTION: fitness function takes the solution as input and produces the suitability of the solution as the output. This evaluates how the close solution is to the optimal solution of the desired problem.

Optimisation: Optimisation is a decision making process, it helps in find the best results under a set of prioritized criteria or constraints. It is a procedure which is executed iteratively by comparing various solutions till an optimum or satisfactory solution is found.

RESULTS:

Plot for many solutions

Plot for solution with iterated solutions and population size

Plot for Maxima solution for population size and minimum solutions

CONCLUSION:

Thus the plots for iterated and optimum solution found for given data set using an GA algorithm.

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.