Project

Optimization and FInding the Global maxima of a stalagmite function

CALCULATING THE GLOBAL MAXIMA OF A STALAGMITE FUNCTION:-

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population 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. Over successive generations, the population \"evolves\" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

  • Selection rules select the individuals, called parents, that contribute to the population at the next generation.

  • Crossover rules combine two parents to form children for the next generation.

  • Mutation rules apply random changes to individual parents to form children.

Syntax for Genetic Algorithm:-

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

Code according to the given challenge:-

%code to optimize the stalagmite function and finding the global maxima of
%the function
clear all
close all
clc

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

%2D meshgrid
[xx yy]= meshgrid(x,y);

num_case=150;

%looping

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

%case 1:: Unbounded values
tic
for i=1:num_case
    
    [inputs,Fopt1(i)]= ga(@stalagmite,2);
  xopt(i)= inputs(1);
  yopt(j)= inputs(2);
end

study_time_1=toc;

%plotting
 figure(1)
 subplot(2,1,1);
 hold on
 surfc(xx,yy,F);
 shading interp;
 ylabel(\'Y data\');
 xlabel(\'X data\');
 plot3(xopt,yopt,Fopt1,\'marker\',\'o\',\'markersize\',5);
 title(\'unbounded inputs\')
 subplot(2,1,2)
 plot(Fopt1)
 xlabel(\'Iterations\')
 ylabel(\'Function Maximum\')
 
%case 2 Bounding the values
tic
for i=1:num_case
    
   [inputs,Fopt2(i)]= ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);  
  xopt(i)= inputs(1);
  yopt(j)= inputs(2);
end

study_time_2=toc;

%plotting
 figure(2)
 subplot(2,1,1);
 hold on
 surfc(xx,yy,F);
 shading interp;
 ylabel(\'Y data\');
 xlabel(\'X data\');
 plot3(xopt,yopt,Fopt2,\'marker\',\'o\',\'markersize\',5);
 title(\'bounded inputs\')
 subplot(2,1,2)
 plot(Fopt2)
 xlabel(\'Iterations\')
 ylabel(\'Function Maximum\')
 
 %case 3:: bounding the values and increasing population size by 500
 
 %for creating optimization options
 options= optimoptions(\'ga\');
 options= optimoptions(options,\'populationsize\',500);
 
  tic   %time variable
for i=1:num_case
    
   [inputs,Fopt3(i)]= ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options);  
  xopt(i)= inputs(1);
  yopt(j)= inputs(2);
end

study_time_3=toc;

%plotting
 figure(3)
 subplot(2,1,1);
 hold on
 surfc(xx,yy,F);
 shading interp;
 ylabel(\'Y data\');
 xlabel(\'X data\');
 plot3(xopt,yopt,Fopt3,\'marker\',\'o\',\'markersize\',5);
 title(\'bounded inputs with increasing population size\')
 subplot(2,1,2)
 plot(Fopt3)
 xlabel(\'Iterations\')
 ylabel(\'Function Maximum\')
 
 max_value=(Fopt3)
 

%code for stalagmite function:::

function [F]= stalagmite(input_vector)

x=input_vector(1);
y=input_vector(2);
f1=sin(5.1*pi*x + 0.5).^6;
f3=exp(-4*log(2)*(x-0.0667)^2)/0.64;
f2=sin(5.1*pi*y + 0.5).^6;
f4=exp(-4*log(2)*(y-0.0667)^2)/0.64;
F=(f1*f2*f3*f4);
F=1/(F+1);
end

Outputs:-

\"\"

\"\"

\"\"

time for the 3 cases:- 30.1794,33.9548 and 158.7010.

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.