what is GA?
genetic algorithm(GA) is a serach based optimization technique based on the principles of genetic and natural selection.
itis frequently used to find optimal or near optimal solution to difficult programs which would otherwise take a life time to solve. it is frequently used to solve optimization problems in research and in machine learnings.
SYNTAX
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.
MATLAB CODES:
clear all
close all
clc
% defining search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50
% creating 2d mesh
[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_func(input_vector)
end
end
%surfc(xx,yy,f)
surfc(x,y,-f)
[inputs,fval] =ga(@stalagmite,2)
tic
%study 1=statical behaviour
num_case=50
for i=1:num_cases
[input,fopt(i)]=ga(@stalagmite_func,2)
xopt(i)=input(1);
yopt(i)=input(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 input')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
tic
%study_2=upper and lower bound behaviour
for i=1: num_cases
[input,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=input(1);
yopt(i)=input(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot 3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title ('bounded input')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
tic
%study_3= boundary vs population
option=optimoptions('ga')
option=optimoptions(option,'populationsize',170)
for i=1(num_case)
[input,fopt(i)]=ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],option);
xopt(i)=input(1)
yopt(i)=input(2)
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot 3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded input')
subplot(2,1,2)
plot(fopt)
xlabel('itrations')
ylabel('function minimum')
STALAGMITE FUNCTION:
function[f]=stalagmite_func(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5)).^6;
f2_x=exp((-4)*log(2)*(x-0.0667).^2/0.64);
f1_y=(sin(5.1*pi*y+0.5)).^6;
f2_y=exp((-4)*log(2)*(y-0.0667).^2/0.64);
f=-(f1_x*f2_x*f1_y*f2_y);
end