Project

Maximizing Stalagmite function using Genetic Algorithm

A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents 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 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 following outline summarizes how the genetic algorithm works:

  1. The algorithm begins by creating a random initial population.
  2. The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:
    1. Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
    2. Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
    3. Selects members, called parents, based on their expectation.
    4. Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.
    5. Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.
    6. Replaces the current population with the children to form the next generation.
  3. The algorithm stops when one of the stopping criteria is met.

Since the GA takes random values, the solution (maxima in this case) might not be same always. one or two iterative solution may vary. In this case as shown in the plot the maximum value remain same for the entire 50 iterations except for one iteration and we have used population size 250.

This can be further smoothened by increasing the number of iteration and population size

NOTE:-

  • The GA is used for minimizing the function.
  • The negative value of the function is taken to maximize the function

Ref:-1)- https://in.mathworks.com/discovery/genetic-algorithm.html

close all
clear all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%defining number of iteration

num_cases=50;

[xx yy]=meshgrid(x,y)
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

options=optimoptions(\'ga\');

options=optimoptions(options,\'populationsize\',250);

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

end

%plotting
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',8,\'markerfacecolor\',\'r\')
xlabel(\'x value\')
ylabel(\'y value\')
zlabel(\'Function Values\')

subplot(2,1,2)

plot(-1*fopt)
axis([0,50,0,1.5]);
xlabel(\'Number Of Iteration\')
ylabel(\'Function Maxima\')

  • stalagmite function

function [fxy]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f_1x=(sin(5.1*pi.*x+0.5)).^6;
f_1y=(sin(5.1*pi.*y+0.5)).^6;
f_2x=exp(-4*log(2).*((x-0.0667).^2/0.64));
f_2y=exp(-4*log(2).*((y-0.0667).^2/0.64));
[fxy]=-f_1x.*f_1y.*f_2x.*f_2y;
end

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.