Project

Matlab program to calculate the global maxima of a stalagmite function

A genetic algorithm is a method of solving optimization problems by the concept of theory of evolution proposed by Charles Darwin. This algorithm is used to calculate the global minimum or global maximum of a function.

The working of a genetic algorithm is given below

1. The algorithm randomly selects members from the current population which become the parents for the next iteration.

2. The parents members of the function undergo mutations and crossovers to generate the child members of the functions.

3. These child members now become the new population of the function and new parent members are selected from this new population.

The process is repeated over and over again until the required value from the function is obtained or the number of generations become very high.

Matlab has an inbuilt function called 'ga' to calculate the global minimum from the function.

The code for genetic algorithm in matlab is given below:

Genetic algorithm

clear all
close all
clc

%Defining our search space
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);

%number of iterations
num_iterations = 100;

%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);
        f(i,j) = stalagmite(input_vector);
        
    end 
end

%Assigning the population size for the function
options = optimoptions('ga');
options = optimoptions (options , 'populationSize' , 500);

tic 
for i=1:num_iterations 
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
total_time = toc

%plotting 
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
axis([0 100 0.4 0.6])
xlabel('Iterations')
ylabel('function maximum')

Genetic algorithm calculates only the global minimum of a function, to calculate the global maximum of a function

let us consider the output as f,

global maximum = 1/(f+1)

using the above formula we can calculate the global maximum of a function.

The code for the stalagmite function is given below

function [f] = stalagmite(input_vector)
    f1x = (sin(5.1*pi*input_vector(1) + 0.5)).^6;  
    f1y = (sin(5.1*pi*input_vector(2) + 0.5)).^6;   
    f2x = exp((-4*log(2))*((input_vector(1) - 0.0667).^2)/0.64);   
    f2y = exp((-4*log(2))*((input_vector(2) - 0.0667).^2)/0.64);   
    
    f = (f1x*f1y*f2x*f2y); 
    %To calculate the global maximum
    f = 1/(f+1);
end

The results otained from the above code is given below

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.