Week 4.1 - Genetic Algorithm
AIM :- Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Introduction to genetic algorithms:
Genetic algorithms are a type of optimisation algorithm, meaning they are used to find the maximum or minimum of a function. In this paper, we introduce, illustrate, and discuss genetic algorithms for beginning users. We show what components make up genetic algorithms and how to write them. Using MATLAB, we program several examples, including a genetic algorithm that solves the classic Traveling Salesman Problem. We also discuss the history of genetic algorithms, current applications, and future developments. Genetic algorithms are a type of optimisation algorithm.
Introduction to optimisation:
In the optimisation of a design, the design objective could be simply to minimise the cost of production or to maximise the efficiency of production. An optimisation algorithm is a procedure that is executed iteratively by comparing various solutions till an optimum or satisfactory solution is found.
With the advent of computers, optimisation has become a part of computer-aided design activities. There are two distinct types of optimisation algorithms widely used today.
The genetic algorithm solves both constrained and unconstrained optimisation problems based on natural selection, which 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 optimisation problems that are not well suited for standard optimisation algorithms, including problems in which the objective function is discontinuous, non differentiable, stochastic or highly nonlinear.
The genetic algorithm can address mixed integer programming problems, where some components are restricted to integerThe 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, who contribute to the next generation's population.
• 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, optimisation algorithm in two main ways, as below Classical Algorithm.
• Generates a single point at each iteration. The sequence of points approaches an optimal solution.
• Selects the next point in the sequence by a deterministic computation.
Genetic Algorithm:
• 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.
• The following outline summarizes how the genetic algorithm works:
• The algorithm begins by creating a random initial population.
• 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 a 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 expectations.
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.
• The algorithm stops when one of the stopping criteria is met.
• The algorithm takes modified steps for linear and integer constraints.
• The algorithm is further modified for nonlinear constraints.
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, ub)
X = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon)
X = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options)
X = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, intcon)
X = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, intcon, options)
X = ga (problem)
[x, fval] = ga(__)
[x, fval] = ga(__)
[x, fval, exitflag, output] = ga(__)
[x, fval, exitflag, output, population, scores] =ga(__)
Description-
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.
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.
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 minimisation 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 minimises the fun such that c(x) ≤e 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 non1con=[] if no nonlinear constraints exist.) Create options using optimizations.
X = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, intcon)
X = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, intcon, options) requires that the variables
listed in intcon take integer values.
writing of a Function code.
Code explaination:
- at first we have to use the word function to start the function command.
- and then we have to create a template and this is a code template and name for the template.
- we are going to name the code template stalagmite.
- we are going to use this formulae to solve the equation.
GENETIC ALGORITHM MATLAB CODE:
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
% Creating a 2Dimensional mesh
[xx,yy] = meshgrid(x,y)
% Logic
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_1(input_vector)
end
end
tic
% Study 1 - Satistical behaviour
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_1, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, -f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbonded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function minimum')
tic
% Study 2 - Stastical behaviour - with upper & lower bounds
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_1,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x, y, -f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bonded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function minimum')
% Study 3 increaing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize', 170);
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_1,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x, y, -f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bonded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function minimum')
FUNCTION CODE:
function f = stalagmite_1(input_vector)
x1 = input_vector(1);
y1 = input_vector(2);
f1 = (sin((5.1*pi*x1)+0.5)).^6;
f2 = (sin((5.1*pi*y1)+0.5)).^6;
f3 = exp((-4*(log(2))*(x1-0.0667).^2)/0.64);
f4 = exp((-4*(log(2))*(y1-0.0667).^2)/0.64);
f = -(f1.*f2.*f3.*f4)
end
OUTPUT SCREENSHOTS:


