Week 4.1 - Genetic Algorithm
https://in.mathworks.com/help/gads/ga.html#d123e47530
error for this program
i got got many errors in this pogram
i can't get solution also
for the stage-3 ga increasing the iterations i did't got the plot or figure
i tried figure it but i cant have the solution for it
and i acnt find the solution for the errors in picture what i have shared
if possible share me solutions of the specify errors thanks
explation of the optimise the stalagmite function and find the global maxima of the function program and syntax of ga in three stages
Here we are writing a program for the optimise the stalagmite function and find the global maxima of the function And also explaining the syntax of the ga.
Starting with the Clear all,close all and clc commands to clear all data, close all plots and figures and clear the command window
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
the above lines are for the defining the ‘x’ and ‘y’ using linspace command with 150 values from 0 to 0.6 and add the num_cases with 50 to run the ga 50 times.
%creating a 2D mesh
[xx yy] = meshgrid(x,y);
For creating the 2dimensional we are using the above line with meshgrid
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
by using the i,j we are creating the input_vector(1) & input_vector(2) array for the each value of the x&y.
now we are creating the function ‘stalagmite’ taking the input_vector values for the values for the ‘f’.
now creating the program for the Stalagmite Function
for that we having the formulas
f(x,y) = f1,x*f2,x*f1,y*f4,y
f1,x = [sin(5.1πx+0.5)]6
f1,y = [sin(5.1πy+0.5)]6
f2,x = exp[-4ln(2) (x-0.0667)2/0.64]
f2,y = exp[-4ln(2) (y-0.0667)2/0.64]
for the above formulas we have to create the stalagmite function program
function[f] = stalagmite(input_vector)
f1_x = (sin(5.1*pi*input_vector(1) + 0.5 ))^6;
f1_y = (sin(5.1*pi*input_vector(2) + 0.5 ))^6;
f2_x = exp(-4*log(2)*((input_vector(1)-0.0667)^2 / 0.64));
f2_y = exp(-4*log(2)*((input_vector(2)-0.0667)^2 / 0.64));
f = f1_x * f1_y * f2_x * f2_y;
end
with using the simple program as above.
Now we are studying in 3 stages using the ga
Study-1 satistical behaviour
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite, 2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
in 1st study we are using the ga with 2 variables and storing the xoptium and yoptium values and optium values of the function
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
adding the figure,subplot,surfc and shading interp for the creating the all figure,subplot anf surfc
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
using the plot3 command for 3 values are xopt, yopt, fpot among the 50 values of ga predicates
adding the title for the plot and creating another subplot for the optium values of function as ga predicates
later adding the xlabel and y label
xlabel('iterations')
ylabel('function minimum')
syntax for the ga in study – 1
[inputs, fopt(i)] = ga(@stalagmite, 2)
x = ga(fun,nvars,A,b)
finds a local minimum x to fun,
fun = stalagmite function
nvars = 2 variables
study-2 statistical behaviour with upper and lower bounds
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
syntax for ga in study – 2 for upper and lower bounds
[inputs, fopt(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1]);
x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun,
A,B and Aeq,beq will be linear inequaity constraint and linear equality constraints, if we don’t hv the values we will [ ], [ ]
And remaining all commands will be the same
Study – 3 Increasing the ga iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170)
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
syntax for ga in study – 3 for Increasing the ga iterations
[inputs, fopt(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1],options);
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
here where,
nonlcon = nonlinear constraints
option = optimization options
And remaining all commands will be the same
Program for the Stalagmite function using the given formula
function[f] = stalagmite(input_vector)
f1_x = (sin(5.1*pi*input_vector(1) + 0.5 ))^6;
f1_y = (sin(5.1*pi*input_vector(2) + 0.5 ))^6;
f2_x = exp(-4*log(2)*((input_vector(1)-0.0667)^2 / 0.64));
f2_y = exp(-4*log(2)*((input_vector(2)-0.0667)^2 / 0.64));
f = f1_x * f1_y * f2_x * f2_y;
end
Program for the optimise the stalagmite function and find the global maxima of the function.
clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%creating a 2D mesh
[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
% study 1 - satistiacl 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;
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 minimum')
%study2 : stastical behaviour with upper and lower bounds
tic
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time= toc
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 minimum')
%study3 : increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',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
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')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')