clear all
close all
clc
%inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50;
%creating a 2d mesh
[xx yy]= meshgrid(x,y);
% evaluating stalagmatic function
for i=1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stalagmite(input_vector);
end
end
%study 1
tic
for i=1:num_spaces
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time1=toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function minimum')
%study 2
tic
for i=1:num_spaces
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
title('Bounded inputs')
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function minimum')
%studz 3 increasing Ga iterations
options=optimoptions('ga');
options=optimoptions(options,'populationsize',250);
tic
for i=1:num_spaces
[inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopi(1)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
title('increased iterations')
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
%stalagmite functiuon
function A = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log(2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2)*((y-0.0667).^2))/0.64);
A=-(f1*f2*f3*f4);
end
%Genetic Algorithm:
%1)It is an optimization technique used to solve non differential and non
%linear optimization problems.It starts with initial set of ramdom
%solutions called populatioins.Each individual in a popultion is called
%chromosome representing solution to the problem.These chromosomes evolve
%through successive iterations called generations.
%During each generation the chromosomes are eveluated using some measure of
%fitness. a)Merging two chromosomes from current generation by crossover
%operator. b) by mutating chromosomes.
%A new generation is formed by selection according to fitness value.
% GA syntax: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,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) 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.)
%code explanation: First i have initiated values for x and y which takes
%150 values between 0 to 0.6.By using mesh grid command i have created 2D
%array.i Have taken number of iterations as 50.A stlagmite function is
%created which is used to calcuate maxima of the function and we take
%output as -f as GA minimises a given function. 1st case: we took number of
%iterations and used the GA to calcuate the maxima of the function,we store
%the outputs in inputs and fopt. we used plot3 command to make a plot of
%xopt,yopt,-fopt and also plotted fopt for each iterations.tic toc commands
%are used to measure the time of GA operation.the result in 1st case is
%that the optimal values are outside x and y values.
%study 2: here we have used a GA and specified the upper and lower for x
%and y.3D plot is made by using plot 3 command for xopt,yopt,fopt and also
%the fopt value. the results is the optimal values are within the
%limits,but exact maxima values is not given.
%Study 3: we have increased the number of initial population from 50 to
%250.time taken to get the output is more as population size is increased but we get exact maxima for the function.So to increase accuracy of GA increase the population size.