Challenge

Week 4.1 - Genetic Algorithm

Aim:

optimise the stalagmite function and find the global maxima of the function.

Objective:

3 case study of 3 case of options using Genetic Algorithm.

find out the global maxima optimizing stalagmite function.

Coding of stalagmite function in matlab.

Genetic Algorithm:

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.

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.

Syntax Genetic Algorithm:

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(___)

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

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

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

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

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

Governing equations:

Stalagmite function formula:

f(x,y)=f1,x f2,x f1,y f2,y

f1.x=[sin(5.1πx+0.5)]^6

f1.y=[sin(5.1πx+0.5)]^6

f2,x=exp[-4In(2)(x-0.0667)^2/0.64]

f2,y=exp[-4In(2)(y-0.0667)^2/0.64]

matlab program:

%the stalagmite function

function [f]=get_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;

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

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

f=-f1*f2*f3*f4;

study 1: unbounded input

end

clear all

close all

clc

x=linspace(0,0.6,150);

y=linspace(0,0.6,150);

[xx,yy] = meshgrid(x,y);

num_cases = 50;

for i=1:length(xx)

for j=1:length(yy)

input(1)=xx(i,j);

input(2)=yy(i,j);

f(i,j)=get_stalagmite(input);

end

end

%study1- satistical behavior

tic

for i=1:num_cases

[inputs,fopt(i)] = ga(@get_stalagmite,2);

xopt(i)=inputs(1);

yopt(i)=inputs(2);

end

study1_time=toc

%plotting results

figure(1)

subplot(2,1,1)

surfc(xx,yy,-f)

hold on

shading interp

xlabel('x axis')

ylabel('y axis')

plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'MarkerFacecolor','r')

title('unbounded input')

subplot(2,1,2)

plot(-fopt)

xlabel('iterations')

ylabel('function minimum')

study1_time =

3.6216

Study 2 : bounded input

clear all

close all

clc

x=linspace(0,0.6,150);

y=linspace(0,0.6,150);

[xx,yy] = meshgrid(x,y);

num_cases = 50;

for i=1:length(xx)

for j=1:length(yy)

input(1)=xx(i,j);

input(2)=yy(i,j);

f(i,j)=get_stalagmite(input);

end

end

%study2 statistical behavior with upper and lower bounds

tic

for i=1:num_cases

[inputs,fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1]);

xopt(i)=inputs(1);

yopt(i)=inputs(2);

end

study2_time=toc

%plotting results

figure(2)

subplot(2,1,1)

surfc(xx,yy,-f)

hold on

xlabel('x axis')

ylabel('y axis')

shading interp

plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'MarkerFacecolor','r')

title('bounded input')

subplot(2,1,2)

plot(-fopt)

xlabel('iterations')

ylabel('function minimum')

study2_time =

5.1807

Study 3: bounded input with modified population size (250)

clear all

close all

clc

x=linspace(0,0.6,150);

y=linspace(0,0.6,150);

[xx,yy] = meshgrid(x,y);

num_cases = 50;

for i=1:length(xx)

for j=1:length(yy)

input(1)=xx(i,j);

input(2)=yy(i,j);

f(i,j)=get_stalagmite(input);

end

end

%study3 increasing GA iterations

options = optimoptions('ga');

options = optimoptions(options,'PopulationSize',250);

tic

for i=1:num_cases

[inputs,fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);

xopt(i)=inputs(1);

yopt(i)=inputs(2);

end

study3_time=toc

%plotting results

figure(3)

subplot(2,1,1)

surfc(xx,yy,-f)

hold on

shading interp

xlabel('x axis')

ylabel('y axis')

plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'MarkerFacecolor','r')

title('bounded input with modified population size ')

subplot(2,1,2)

plot(-fopt)

xlabel('iterations')

ylabel('function minimum')

study3_time =

11.4959

Error:

Unrecognized function or variable 'stalagmite'.

Error in ga_statistics (line 13)

f(i,j)=stalagmite(input);

correction:

for i=1:length(xx)

for j=1:length(yy)

input(1)=xx(i,j);

input(2)=yy(i,j);

f(i,j)=get_stalagmite(input);

end

end

the error was file name and function name must be same

after putting the same the results are showing properly

references:

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

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

conclusion:

finally we found the global maxima by optimizing stalagmite function using genetic algorithm in matlab.

%the stalagmite function

function [f]=get_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;
f3=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f4=exp(-4*log(2)*((y-0.0667)^2)/0.64);

f=-f1*f2*f3*f4;



study 1: unbounded input 
end  


clear all
close all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_cases = 50;               %number of interation for which the function will evaluate 
                               the fitness
   for i=1:length(xx)
             for j=1:length(yy)
             input(1)=xx(i,j);
             input(2)=yy(i,j);
             f(i,j)=get_stalagmite(input);   %values of function in given search space

             end    
   end
    
%study1- satistical behavior
tic
for i=1:num_cases
    [inputs,fopt(i)] = ga(@get_stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end    

study1_time=toc
%plotting results
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)     %generation the surface
hold on
shading interp       %to remove the lines produced by the mesh grid
xlabel('x axis')
ylabel('y axis')

plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'MarkerFacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')





clear all
close all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_cases = 50;
   for i=1:length(xx)
             for j=1:length(yy)
             input(1)=xx(i,j);
             input(2)=yy(i,j);
             f(i,j)=get_stalagmite(input);
             end    
   end
    
%study2 statistical behavior with upper and lower bounds
tic            % calculating the study time through tic toc command
for i=1:num_cases
    [inputs,fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[1;1]);   %calculating the optimized value at each iteration with boundation
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end    

 study2_time=toc     %displaying the time taken to evaluate
%plotting results
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
hold on
xlabel('x axis')
ylabel('y axis')

shading interp
plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'MarkerFacecolor','r')
title('bounded input')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')



clear all
close all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_cases = 50;
   for i=1:length(xx)
             for j=1:length(yy)
             input(1)=xx(i,j);
             input(2)=yy(i,j);
             f(i,j)=get_stalagmite(input);
             end    
   end
    
%study3 increasing GA iterations

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',250); %increasing the population by using optimazation function
tic
for i=1:num_cases
    [inputs,fopt(i)] = ga(@get_stalagmite,2,[],[],[],[],[0;0],[0.4;0.4],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end    

 study3_time=toc
%plotting results

figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
hold on
shading interp
xlabel('x axis')
ylabel('y axis')

plot3(xopt,yopt,-fopt,'Marker','o','Markersize',5,'MarkerFacecolor','r')
title('bounded input with modified population size ')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
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.