Genetic Algorithm Using MATLAB

OBJECTIVE - Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

PROCEDURE-

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 rulesselect the individuals, called parents, that contribute to the population at the next generation.
  • Crossover rulescombine two parents to form children for the next generation.
  • Mutation rulesapply random changes to individual parents to form children.

The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.

SYNTAX FOR GENETIC ALGORITHM-

finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.

 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,[],[],lb,ub,nonlcon,IntCon,options)–

 

Requires that the variables listed in IntCon take integer values.

 

Where ,

 

x - local minimum

fun - objective function

nvars – number of variables

A – linear inequality constraints

B – linear inequality constraints

Aeq – linear equality constraints

Beq – linear equality constraints

lb – lower bounds

ub – upper bounds

nonlcon – non-linear constraints

IntCon – integer values

Options – Optimazation options

The three cases considered for Fmaximum vs no. of iterations ,

  1. Unbounded inputs-No input restrictions.
  2. Bounded inputs -Inputs are restricted with upper and lower bounds.
  3. Bounded inputs with options-Increasing no of iterations by giving populations size.

 

MATLAB PROGRAM AND EXPLANATION-

  • The search space of the stalagmite fiction is defined using the ‘linspace’
  • 2-dimensional array is created using ‘meshgrid’
  • Then to plot the stalagmite function for loop is used and separate function is coded using the equations given below,

  • Then the stalagmite function is plotted usingthe ‘surfc’ command and the output graph is obtained.
  • Then the first study with unbounded inputs are given using the genetic algorithm which is inbuilt matlab function.
  • The total time of evaluation is calculated using tic toc command.
  • Then the same procedure is repeated the next two studies by giving the lower and upper bounds for study 2 and by increasing the number of iterations and giving the population size for study 3.

 MAIN CODE -

close all
clear all
clc

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

 %creating a 2-d array
 [xx yy] = meshgrid(x,y);
 num_cases = 50;
 
 %evaluating the 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(input_vector);
    end
end
surfc(xx,yy,-F)
shading interp
xlabel('x value')
ylabel('y value')
title('Stalagmite Function')

%study 1
tic
for i =  1:num_cases
    [inputs,Fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_1 = 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('Unbounded Inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('iterations')
ylabel('function maximum')


%study 2
tic
for i =  1:num_cases
    [inputs,Fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study_2 = 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('Optimal solution')
subplot(2,1,2)
plot(-Fopt)
xlabel('iterations')
ylabel('function maximum')


%study 3
options = optimoptions(@ga)
options = optimoptions(options,'populationsize',400);

tic
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
study_3 = 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('Bounded Inputs')
subplot(2,1,2)
plot(-Fopt)
xlabel('iterations')
ylabel('function maximum')

Global_Maxima = -Fopt

FUNCTION CODE-

function [F] = stalagmite(input_vector)
a = input_vector(1);
b = input_vector(2);

f1 = (sin((5.1*pi*a)+0.5))^6;
f2 = (sin((5.1*pi*b)+0.5))^6;
f3 = exp((-4*log(2)*((a-0.0667)^2))/(0.64));
f4 = exp((-4*log(2)*((b-0.0667)^2))/(0.64));

F = -(f1*f2*f3*f4);

OUTPUT OF THE PROGRAM-

Surface plot of Stalagmite Function

Plot of study 1 and time for evaluation

Plot of study 2 and time of evaluation

 

Plot of study 3 and output 

CONCLUSION –

Thus a code was written in MATLAB to optimise the stalagmite function and find the global maxima of the function.

 

 

 

 

 

 


Projects by Habishek Umapathy

AIM – To run a pipe flow simulation with an inlet Reynolds number of 100, 1000 and 10000. For each of these cases do the following Place the line probes at 95%, 90%, 85%of the pipe length. Compare the normalized velocity profile at each of these locations. N Read more

OBJECTIVE – We have to write the script which should take column numbers as the input and plot the respective columns as separate images. The plot labels should be extracted from the file. If there is a request for a plot between column 1(crank angle) and column 8 Read more

  OBJECTIVE – To write a program to perform curve fitting.   Curve Fitting is the process of constructing a curve or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically constr Read more

The Newton Raphson method is an iterative technique that is used to find out the roots of a real valued function. It uses the idea that a continuous and differentiable function can be approximated by a straight-line tangent to it. In this case Newton Raphson method is Read more

OBJECTIVE – To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of its motion.   PROCEDURE – In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum Read more

OBJECTIVE - To write a code for P_V diagram of an Otto cycle and calculate thermal efficiency

 

PROCEDURE –

ASSUMPTIONS

  • Initially state variables p1,t1,t3 and gamma a Read more

OBJECTIVE - 1. Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the data file. Calculate the molecular weight of each species and display it in the command window. Plot the Cp, Ent Read more

OBJECTIVE – To write code to fit a linear and cubic polynomial for the Cp data and plot the linear and cubic fit curves along with the raw data points.   PROCEDURE – Curve Fitting is the process of constructing a curve or mathematical functions, whic Read more


Loading...

The End