Week 4.1 - Genetic Algorithm
Genetic Algorithm (MATLAB)
Aim-
Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for ga.
Introduction-
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.
Process of natural selection
- The process of natural selection starts with the selection of the fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.
- This notion can be applied to a search problem. We consider a set od solutions for a problem and select the set of best ones out of them.
- Five phases are considered in a genetic algorithm.
1. Initial population
2. Fitness function
3. Selection
4. Crossover
5. Mutation
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.
The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways as follows-
1) Classical Algorithm-
- It generates a single point at each iteration. The sequence of points approaches an optimal solution.
- It selects the next point in the sequence by a deterministic computation.
2) Genetic Algorithm-
- It generates a population of points at each iteration. The best point in the population approaches an optimal solution.
- Selects the next population by computation which uses random number generators.
About Stalagmite function-
- It is a function that contains the input parameters that are to be given a plot like a stalagmite structure. Calling the function stalagmite that contains the code for evaluates the value of the function using the array input. For the study of the statistical behavior of the Genetic Algorithm, primarily a stopwatch is started.
It contains the following formulas to generate the data that needs to be plot,
F = (F x * F y * F x * F y)
F y = (sin (5.1*π*y1+0.05))
F x = exp(-4*log(2)*((x1-0.0667) )/0.64)
F y = exp(-4*log(2)*((y1-0.0667) /0.64)
ga is function of MATLAB
Syntax
x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,
x = ga(fun,nvars,A,b,Aeq,beq,lb,
x = ga(fun,nvars,A,b,Aeq,beq,lb,
x = ga(fun,nvars,A,b,[],[],lb,ub,
x = ga(fun,nvars,A,b,[],[],lb,ub,
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,
Explaination-
- 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.
Note
Passing Extra Parameters explains how to pass extra parameters to the objective function and nonlinear constraint functions, if necessary. - x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
- x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
- 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.) - x = ga(fun,nvars,A,b,Aeq,beq,lb,
ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns
vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=
[] if no bounds exist.) - 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. - x = ga(fun,nvars,A,b,[],[],lb,ub,
nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub, nonlcon,IntCon,options) requires that the variables listed in IntCon take integer
values.
Note- When there are integer constraints, ga does not accept linear or nonlinear equality constraints, only inequality constraints.
- x = ga(problem) finds the minimum for problem, a structure described in problem.
- [x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.
- [x fval exitflag output] = ga( ) also returns exitflag an integer identifying the reason the algorithm terminated and output a structure that contains output
- [x,fval,exitflag,output,
population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the
final population.
Stalagmite function-
function [f] = stalagmite_function(input_vector);
x1 = input_vector(1);
y1 = input_vector(2);
f1x = (sin(5.1*pi*x1+0.5))^6;
f1y = (sin(5.1*pi*y1+0.5))^6;
f2x = exp(-4*log(2)*((x1-0.0667).^2)/0.64);
f2y = exp(-4*log(2)*((y1-0.0667).^2)/0.64);
f = -(f1x*f1y*f2x*f2y);
end
Main Program-
clear all
close all
clc
% Search space defining
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%2D mesh creating
[xx, yy]=meshgrid(x,y);
%Evaluating the stalagmite functiontion
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_function(input_vector);
end
end
figure(1)
surfc(xx,yy,-f)
shading interp
xlabel('x Value')
ylabel('y Value')
title('Stalagmite')
%study 1
n_iteration = 50;
tic
for i = 1:n_iteration
[inputs,fopt(i)] = ga(@stalagmite_function,2);
xopt1(i) = inputs(1);
yopt1(i) = inputs(2);
end
study_time1 = toc;
%plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
title('Unbounded Input')
xlabel('x Value')
ylabel('y Value')
shading interp
hold on
plot3(xopt1,yopt1,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
tic
%study2 - statistical behaviour with upper and lower bounds
for i=1:n_iteration
[inputs,fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time=toc
%plotting
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded Inputs')
xlabel('x Value')
ylabel('y Value')
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
%study3 increasing the population size
options = optimoptions(@ga);
options = optimoptions(@ga,'populationsize',500)
tic
for i=1:n_iteration
[inputs,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc;
figure(4)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded Inputs With Increased Population Size')
xlabel('x Value')
ylabel('y Value')
hold on
plot3(xopt1,yopt1,-fopt,'marker','x','markersize',4,'markerfacecolor','k')
subplot(2,1,2)
plot(-fopt)
xlabel('Iteration')
ylabel('Function minimum')
OUTPUTS:
1. Stalagmite Function-

2. Unbounded Inputs-

3. Bounded Inputs-

4. Bounded Inputs with Increase Population-
