Challenge

Week 4.1 - Genetic Algorithm

Aim: Write a code in Matlab to optimize the stalagmite function and find the global maxima of the function.

1. Clearly explain the concept of genetic alogrithm in your own words and also explain the syntax for ga in Matlab in your report.

2. Plot graph for all 3 studies and for F maximum vs no of iteration.Title and axes label are a must,legend could be shown if necessary.

Theory:

1. What is optimization in Matlab?

Practically, function optimization describes a class of problems for finding the input to a given function that results in the minimum or maximum output from the function. The objective depends on certain characteristics of the system, called variables or unknowns.

2. What is stalagmite functon?

A stalagmite function generally refers to the function with which we are generating values over a stalagmite kind of surface for finding out the local and global maxima and minima of that particular given function. For this in Matlab we are using a method called 'Optimization'.

3. What is Genetic algorithm (GA) how it works?

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 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.

This flow chart outlines the main algorithmic steps. For details, see

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. The selection is generally stochastic, and can depend on the individuals' scores.

  • 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 summarized in the following table:

Classical Algorithm Genetic Algorithm

Generates a single point at each iteration. The sequence of points approaches an optimal solution.

Generates a population of points at each iteration. The best point in the population approaches an optimal solution.

Selects the next point in the sequence by a deterministic computation.

Selects the next population by computation which uses random number generators.

Typically converges quickly to a local solution.

Typically takes many function evaluations to converge. May or may not converge to a local or global minimum.

Application of Genetic Algorithm:

1. Manufacturing System:

One of the major applications of genetic optimization is to minimize a cost function using the optimized set of parameters. In manufacturing we can see various examples of cost function and finding an optimal set of parameters for this function can be performed by following the genetic optimization. In many cases, we can find the application of genetic optimization in product manufacturing (variation of production parameters or comparison of equipment layout). The main motive behind applying genetic optimization is to achieve an optimum production plan by taking into consideration dynamic conditions like inventories, capacity, or material quality.

2. Mechanical Engineering Design:

In many designing procedures of mechanical components, we can also find the application of genetic optimization. We can take aircraft wing design as an example where we are required to improve the ratio of lift to drag for a complex wing. This kind of designing problem can be considered as a multidisciplinary problem, the fitness function in genetic optimization can be altered by considering some specific requirement of the design.

Advantages of Genetic Algorithm:

1. The concept is easy to understand.

2. GA search from a population of points not a single point.

3. GA use payoff (objective function) information,not derivatives.

4. GA support multi-objective optimization.

5. GA is robust w.r.t to lacal minima/maxima.

Disadvantages of Genetic Algorithm:

1. GA implementation is still an art.

2. GA requires less information about the problem,but designing an objective function and getting the representation and operator can be difficult.

3. GA is computationally expensive i:e time consuming.

Syntax for GA:

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,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)
Governing Equation:

f(x,y) = f1*f2*f3*f4

f1 = [sin(5.1pi*x+0.5)]^6

f2 = [sin(5.1pi*y+0.5)]^6

t1 = ((x-0.0667)^2)/0.64

f3 = exp(-4*log(2)*t1)

t2 = ((y-0.0667)^2)/0.64

f4 = exp(-4*log(2)*t2)

Body of the content:

Write a code to define stalagmite function:

function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);

f1 = (sin(5.1*pi*x+0.5)).^6;
f2 = (sin(5.1*pi*y+0.5)).^6;

t1 = ((x-0.0667).^2)/0.64;
f3 = exp(-4.*log(2)*t1);

t2 = ((y-0.0667).^2)/0.64;
f4 = exp(-4.*log(2)*t2);

f = -(f1.*f2.*f3.*f4);
end








Write for code for GA :

clear all
close all
clc

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

%  Creating a 2 Dimensional mesh
[xx yy] = meshgrid(x,y);

% 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

% GA runs for 50 iterations
num_cases = 50;
% study 1-Statistical behavior
tic % Study time count start
for i = 1:num_cases
   
    [inputs,f_opt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    opt_f(i) = -f_opt(i);
end

study1_time = toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(f_opt)
xlabel('Iteration')
ylabel('Function Max')


% GA runs for 100 iterations
num_cases = 100;
% study 2-Statistical behavior
 tic % Study time count start
for i = 1:num_cases
    
    [inputs,f_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    opt_f(i) = -f_opt(i);
end

study2_time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(f_opt)
xlabel('Iteration')
ylabel('Function Max')



% GA runs for 150 iterations
num_cases = 150;
options = optimoptions('ga')
options = optimoptions(options,'PopulationSize',400);
% study 2-Statistical behavior
 tic % Study time count start
for i = 1:num_cases
   
    [inputs,f_opt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    opt_f(i) = -f_opt(i);
end

study3_time = toc

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,f_opt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with increased population size')
subplot(2,1,2)
plot(f_opt)
xlabel('Iteration')
ylabel('Function Max')







Plot for Case 1: Unbounded inputs

Plot for Case 2: Bounded inputs

Plot for case 3: Bounded inputs with increased population size

Explanation of the code:

1. First step of our programming is to define the search spaces using linspace command, the linspace command generates lineraly spaced vector.

2. After that for creating a 2 dimensional mesh using meshgrid command,Meshgrid return 2-D grid co-ordinates based on the co-ordinates contained in vector x and y.

3. Evaluating the stalagmite function for that use loop command to define x and y.

4. After that create a stalagmite function for that select new script type all the formulas require in stalagmite function and run the stalagmite function in command window

5. i) for Case-1 Genetic Algorithm (GA) runs for 50 iteration for Unbounded inputs.

ii) for Case-2 Genetic Algorithm (GA) runs for 100 iteration for Bounded inputs.

iii) for Case-3 Genetic Algorithm (GA) runs for 150 iteration for Bounded inputs with increased populatio size.

6. Study the statistical behaviour and use tic command to study time count start.

7. After that use for loop command then define the require inputs and use end.

8. Then use study_time = toc command, Mention figure no and assign co-ordinate to subplot.

9. Use hold on command.

10. Then use surfc(x,y,f) command and used shading interp command.

11. Use markersize command for sphere and use markerfacecolor command to define the color of sphere.

12. Plot(xopt,ypot,f_opt).

13. Mention the title and again assign the co-ordiates for subplot.

14. Plot(f_opt).

15. lastly label the plot xlabel(Iteration) and ylabel(Function Max).

References:

https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html

https://analyticsindiamag.com/10-real-life-applications-of-genetic-optimization/ 

https://electricalvoice.com/genetic-algorithm-advantages-disadvantages/

https://in.mathworks.com/help/gads/ga.html
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.