maximizing stalagmite function
Theory
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[1]
Working of Genetic Algorithm in matlab:
- The algorithm begins by creating a random initial population(if not mentioned it will be of size 50).
- The algorithm then creates a sequence of new populations by using individuals in the current generation at every step. To create the new population, the algorithm performs the following steps:
- Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.
- Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.
- Selects members, called parents, based on their expectation.
- 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.
- 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.
- Replaces the current population with the children to form the next generation.[2]
- The algorithm stops when one of the stopping criteria is met. Stopping criteria is as follows
- Generations
- Time limit
- Fitness limit
- Stall generations
- Stall time limit
- Stall test
- Function tolerance
- Constraint tolerance
code:
close all
clear all
clc
num_cases = 200;
%defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2Dimensional mesh
[xx,yy] = meshgrid(x,y);
%evaluating 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_func(input_vector);
end
end
figure(1);
hold on;
surfc(xx, yy, f);
shading interp;
%setting population size
options=optimoptions(\'ga\');
options=optimoptions(options,\'populationSize\',370);
%finding out minimum value for iterations=numcases
tic
for j=1:num_cases
[inputs,fopt(j)]=ga(@stalagmite_func,2,[],[],[],[],[0;0],[1,1],[],[],options);
x_opt(j)= inputs(1);
y_opt(j)=inputs(2);
end
study_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f);
shading interp;
xlabel(\'x\');
ylabel(\'y\');
plot3(x_opt,y_opt,fopt,\'marker\',\'o\',\'markersize\',5)
subplot(2,1,2)
plot(fopt)
xlabel(\'iteration\')
ylabel(\'fmin\')
maxvalue=1
% from figure as minimum value of -f is -1 so max value of f is 1
function code
function [f] = stalagmite_func(input_vector)
x = input_vector(1);
y = input_vector(2);
term_1=(sin(16.02*x + 0.5))^6;
term_2=(sin(16.02*y + 0.5))^6;
term_3 = exp((-4*log(2)*(x-0.0667)^2)/(0.64));
term_4 = exp((-4*log(2)*(y-0.0667)^2)/(0.64));
f = term_1*term_2*term_3*term_4;
end
Results:
Study caried out to minimise -f for 100 iterations and 470 population size.
Study caried out to minimise -f for 200 iterations and 370 population size
Study caried out to minimise -f for 200 iterations and 270 population size