Challenge

Week 4.1 - Genetic Algorithm

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

Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.

Make sure that your code gives the same output, even if it is made to run several times.

Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.

GOVERNING EUATIONS:

GENETIC ALGORITHM:

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.

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.

SYNTAX (GA):

Description

example

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.

example

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

example

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

example

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

example

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

example

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.

example

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon) or x = ga(fun,nvars,A,b,Aeq,beq,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 nonlinear equality constraints, only nonlinear inequality constraints.

x = ga(problem) finds the minimum for problem, a structure described in problem.

example

[x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

example

[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

example

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

BODY: function code

function [F] = stalagmite(input_vector)


x = input_vector(1);
y = input_vector(2);
term1=(sin((5.1*pi*x)+0.5))^6;
term2=exp((-4)*log(2)*((x-0.0667).^2)/0.64);
term3=(sin((5.1*pi*y)+0.5))^6;
term4=exp((-4)*log(2)*((y-0.0667).^2)/0.64);

F = -(term1*term2*term3*term4);

end

MAIN CODE:

clear all
close all
clc

x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
num_cases=50
[xx,yy]=meshgrid(x,y);

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

tic
% study 1 statistical behaviour
for i= 1: num_cases;
    [inputs,fopt(i)]=ga(@stalagmite,2);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end    
study1_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('unbounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

tic
% study 2 statistical behaviour wuth upper and lower bounds

for i= 1: num_cases;
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
    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('bounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum') 

% study 3 increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',350);

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    
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('unbounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')

RESULTS:STUDY1:

STUDY2:

STUDY3:

workspace:

References:https://in.mathworks.com/discovery/genetic-algorithm.html

https://in.mathworks.com/help/gads/ga.html

https://skill-lync.com/student-projects/week-4-genetic-algorithm-401

Conclusion:Genetic algorithm code is done to find global maxima.

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.

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.