Global maxima of a stalagmite function
OBJECTIVE:
Writing a code to calculate the global maxima of a stalagmite function.
THEORY:
Syntax for ga:
x=ga(func,nvars)
x=ga(func,nvars,A,B)
x=ga(func,nvars,A,B,Aequ,Bequ)
x=ga(func,nvars,A,B,Aequ,Bequ,lb,ub)
x=ga(func,nvars,A,B,Aequ,Bequ,lb,ub,nonlcon)
x=ga(func,nvars,A,B,Aequ,Bequ,lb,ub,nonlcon,options)
x=ga(func,nvars,A,B,[],[],lb,ub,nonlcon,IntCon)
x=ga(func,nvars,A,B,[],[],lb,ub,nonlcon,IntCon,options)
x=ga(problem)
[x,fval]=ga(_)
[x,fval,exitfalg,output]=ga(_)
[x,fval,exitfalg,output,population,scores]=ga(_)
DESCRIPTIONS:
x=ga(func,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
x=ga(func,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(func,nvars,A,B,Aequ,Bequ) 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(func,nvars,A,B,Aequ,Bequ,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(func,nvars,A,B,Aequ,Bequ,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(func,nvars,A,B,Aequ,Bequ,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 optimoption.
x=ga(func,nvars,A,B,[],[],lb,ub,nonlcon,IntCon) requires that the variables listed in IntCon take integer values.
x=ga(func,nvars,A,B,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.
x=ga(problem) finds the minimum for problem, where problem is a structure.
[x,fval]=ga(_) , for any previous input arguments, also returns fval, the value of the fitness function at x.
[x,fval,exitfalg,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,exitfalg,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.
ga:
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.
To maximize the objective function we need to add an negative(-) sign to the equation that we are executing in the function
ex. y=x(1)+x(2)
To maximize the function we need to add an negative sign(-)
y=-(x(1)+x(2))
How Genetic Algorithm Work :
The Genetic Algorithm works on a population using a set of operators that are applied to the population. A population is a set of points in the design space. The initial population is generated randomly by default. The next generation of the population is computed using the fitness of the individuals in the current generation.
FORMULA USED:
where,
PROGRAM:
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[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)=stal_func(input_vector);
end
end
surfc(xx,yy,f)
shading interp
xlabel(\'x value\')
ylabel(\'y value\')
[output,fval]=ga(@stal_func,2)
OUTPUT:
FUNCTION PROGRAM:
In the function program the negative sign(-) is used to plot the graph in inverted position.
function [out]=stal_func(input_vector);
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x)+0.5).^6;
f1_y=(sin(5.1*pi*y)+0.5).^6;
f2_x=exp((-4*0.6931)*(x-0.0667)^2/(0.64));
f2_y=exp((-4*0.6931)*(y-0.0667)^2/(0.64));
out=-f1_x*f1_y*f2_x*f2_y;
end
PROGRAM FOR FINDING GLOBAL MAXIMA:
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_case=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)=stal_func(input_vector);
end
end
%for case 1
tic
for i=1:num_case
[input,fopt(i)]=ga(@stal_func,2);
xopt(i)=input(1);
yopt(i)=input(2);
end
study_time_1 = toc;
%plot
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(\'iteration\')
ylabel(\'function maximum\')
%for case2
tic
for i=1:num_case
[input,fopt(i)]=ga(@stal_func,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=input(1);
yopt(i)=input(2);
end
study_time_2 = toc
%plot
figure(2)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading \'interp\'
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'b\');
title(\'bounded inputs\')
subplot(2,1,2);
plot(fopt);
xlabel(\'iterations\')
ylabel(\'function maximum\')
%case3
options = optimoptions(\'ga\');
options = optimoptions(options,\'populationsize\',250);
tic
for i=1:num_case
[input,fopt(i)]=ga(@stal_func,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=input(1);
yopt(i)=input(2);
end
study_time_3=toc;
%plot
figure(3)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading \'interp\'
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'y\');
title(\'bounded input with modified population size\')
subplot(2,1,2);
plot(fopt);
xlabel(\'iterations\')
ylabel(\'function maximum\')
OUTPUT:
1.Unbounded inputs
Time taken unbounded inputs is 8.5517 sec
2.Bounded inputs
Time taken bounded inputs is 9.6634 sec
3.Bounded inputs with modified population size
Time taken is bounded inputs with modified population size 38.7255 sec
https://drive.google.com/open?id=1rbJTS63xa6guFD3-Stzxuvh5H0Ldguw1
https://drive.google.com/open?id=1V3GNpOVwRnWKpftNECaAzA_AirPcFpaN
https://drive.google.com/open?id=1On_NjaWqeUpXjCjF6E4S5xWxFmujSCCV