Challenge

Week 4.1 - Genetic Algorithm

AIM- write a code to optimize the stalagmite function and to calculate the global maxima of the functtion and explain the syntax of genetic algorithm (GA) in MATLAB

OBJECTIVE

  • To write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
  • To explain the concept of genetic algorithm and syntax of GA used in programs.
  • To plot the graphs of all stastical behaviour of GA and also of global maxima.

CONCEPT OF GA

GA is a way to find out the optimiation in which we study how to maximizeand minimize the partcular function and GA is a inbuilt function and it refers to basiacally minima of the particular function.

SYNTAX OF 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,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)

EXPLANATION OF SYNTAX

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

MATHEMATICAL EQUATION

here x and y are the two variables.

PROGRAM :

for plotting a global maxima:

%program on optimise the global maxima 
clear all
close all
clc

%defining our search spaces
% creating 1D array of x and y
x = linspace(0,0.6,150)
y = linspace(0,0.6,150)

%creating 2D arrays
[xx,yy] = meshgrid(x,y);

evaluate the stalagmite function
for i = 1:length(xx)
   for j = 1:length(yy)
   input_vector = xx(i,j);
   input_vector = yy(i,j);
    
   f(i,j) = stalagmite(input_vector);      %recalling the function
    end

end

surf(xx,yy,f)
shading interp
xlabel('x value')
ylabel('y value')

[inputs,fval] = ga(@stalagmite,2);


output

program on 3 studies

%program on optimise the stalagmite function of global maxima and study of f for maximum vs no. of iterations


clear all
close all
clc
%creating 1D array of x and y
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

num_cases = 50;             %GA runs 50 times during the program

%creating 2D array depends on x and y value
[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

%study1 - statistical behaviour
tic

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1)
    yopt(i) = inputs(2)
    
end

study1_time = toc;
%plotting
figure(1)
subplot(2,1,1)
hold on 
surfc(xx,yy,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')

tic
%study 2 - statistical behaviour with upper bounds and lower bounds

for i = 1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,1,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1)
    yopt(i) = inputs(2)
    
end

study2_time = toc;
%plotting
figure(2)
subplot(2,1,1)
hold on 
surfc(xx,yy,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')

%study 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'populasationsize',170);

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;
%plotting
figure(3)
subplot(2,1,1)
hold on 
surfc(xx,yy,f)
shading interp

plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with modified population axis')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function maximum')


function code that is used during this program.

function [f] = stalagmite(input_vector)

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

f1 = (sin(5.1*pi.*x+0.5)).^6;
f2 = (sin(5.1*pi.*y+0.5)).^6;
f3 = exp(-4log(2).*(x-0.0667).^2/0.64);
f3 = exp(-4log(2).*(y-0.0667).^2/0.64);

f = (f1*f2*f3*f4);

end

outputs

plot1

plot2

plot3

steps

errors

this error was show during this process it was solved by defining the inputs as inputs in the output values we got.

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.