Project

Optimizing stalagmite function to find global maxima using genetic algorith

OBJECTIVE : The main objective of this project is to find global maxima in stalagmite function using solver \'Genetic Algorithm\'

Genetic Algorithm : Genetic algorithm solves smooth or nonsmooth optimization problems with any types of constraints, including integer constraints. It is a stochastic, population-based algorithm that searches randomly by mutation and crossover among population members. It\'s 5 phases are

1. Initial population

2. Fitness function

3. Selection

4. Crossover

5. Mutation

\"\"

This project is divide in three studies :

STUDY 1 :

Finding local unconstrained maximum without defining bounds or population

Syntax : x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.

STUDY 2 :

Finding the solution by defining upper and lower bounds thereby reducing inaccuracy.

Syntax : x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)

STUDY 3 :

Finding the solution by defining upper, lower bounds and population size thereby reducing inaccuracy.

Syntax : x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

MATLAB CODE:

Following is the main code which finds solution in stalgmite function using GA in 3 cases.

% Input variables
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

num_case = 50; % No of iteration for the ga function


% Creating An Array
[xx, yy] = meshgrid(x,y);

% For loop for passing the stalagmite function
for i=1:length(xx)
    for j=1:length(yy)
       input(1) = xx(i,j);
       input(2) = yy(i,j);
        f(i,j) = stalagmite_func(input);
    end
end
surfc(x,y,-f)
colorbar
xlabel(\'x Values\')
ylabel(\'y Values\')
zlabel(\'Function Values\')
title(\'Stalagmite Function\')
shading interp

[x_output,fval] = ga(@stalagmite_func,2);

% CASE STUDY 1

tic
for i = 1:num_case
    [inputs, f_optima(i)] = ga(@stalagmite_func,2);
    x_optima(i) = inputs(1);
    y_optima(i) = inputs(2);
end

case1 = toc;

figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
xlabel(\'x values\')
ylabel(\'y values\')
zlabel(\'Function Values\')
hold on 
 
plot3(x_optima,y_optima,-f_optima,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded Input\')
subplot(2,1,2)
plot(-f_optima);
xlabel(\'No Of Iterations\')
ylabel(\'Maximum Value Of The Function\')

%CASE STUDY 2

tic
for i = 1:num_case
    [inputs, f_optima(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.5;0.5]);
    x_optima(i) = inputs(1);
    y_optima(i) = inputs(2);
end
case2 = toc;

figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
xlabel(\'x Values\')
ylabel(\'y Values\')
zlabel(\'Function Values\')
hold on 

plot3(x_optima,y_optima,-f_optima,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded Input\')
subplot(2,1,2)
plot(-f_optima);
xlabel(\'No Of Iterations\')
ylabel(\'Maximum Value Of The Function\')

% CASE STUDY 3

options = optimoptions(\'ga\');   % Default values for the ga function
options = optimoptions(options, \'PopulationSize\', 200); % Setting the population size to 200

tic
for i=1:num_case
    [inputs,f_optima(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
    x_optima(i) = inputs(1);
    y_optima(i) = inputs(2);
end

case3 = toc;
figure(4)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
xlabel(\'x Values\')
ylabel(\'y Values\')
zlabel(\'Function Values\')
hold on 

plot3(x_optima,y_optima,-f_optima,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'Unbounded Input\')
subplot(2,1,2)
plot(-f_optima);
xlabel(\'No Of Iterations\')
ylabel(\'Maximum Value Of The Function\')

Following is the equation to plot stalagmite function:

\"\"

Following is the Matlab code for stalagmite function which incorporates above function:

function [f] = stalagmite_func(input)
X=input(1);
Y=input(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);  % -ve sign for finding global maximum
end

RESULTS :

This is plot of stalagmite function in which global maximum is to be found out.

\"\"

STUDY 1:

In this case we can see the solution is random in nature since no bounds or population is defined.

\"\"

STUDY 2 :

In this case we have defined upper and lower bounds and so the solution is lot closer to global maxima.

\"\"

STUDY 3 :

In this case we have defined bounds as well as population size so the solver has accurately found global maximum. which is 1.00 and location is [x,y]=[0.0668,0.0668]

\"\"

RESULT :

With this project we have found optimum solution in stalagmite function with the help of GA solver and were able to study statistical behaviour in various cases.

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.