How do I use the max() function on an optimization expression when using the genetic algorithm?

20 visualizaciones (últimos 30 días)
I am using the genetic algorithm to optimize a 5 by 5 planar antenna array in the aim of achieving a predetermined sidelobe level (SLL_imp in the following code) with as few antenna elements as possible. Moreover, the optimization variable is a 5 by 5 binary matrix (A_mn in the following code) which represents the state (either included or eliminated) of the individual antenna elements. However, I am getting an 'Invalid data type' error when I use max() on AF (an optimization expression in the following code that will be ultimately used to construct the objective/error function). How can I get around this problem?
% Sparse array optimization using GA
clear, clc
% Create binary optimization variables
A_mn = optimvar('A_mn',5,5,'Type','integer','LowerBound',0,'UpperBound',1);
% Create an optimization problem with the objective function
prob = optimproblem("Objective",error_fcn(A_mn));
options = optimoptions("ga","PlotFcn","gaplotbestf");
rng default % For reproducibility
[sol,fval,exitflag] = solve(prob,"Solver","ga","Options",options);
function y = error_fcn(A_mn)
% Define some variables to be used in AF (array factor) computation
j = sqrt(-1);
M = 5; N = 5;
f = 5.4e+9;
c = (3e+8)/sqrt(1);
lambda = c/f;
k = (2*pi)/lambda;
d_x = 0.5*lambda;
d_y = 0.5*lambda;
theta = linspace(-180, 180, 1001);
phi = 0;
theta_0 = 0;
phi_0 = 0;
beta_x = -k*d_x*sind(theta_0)*cosd(phi_0);
beta_y = -k*d_y*sind(theta_0)*sind(phi_0);
psi_x = k*d_x*sind(theta).*cosd(phi) + beta_x;
psi_y = k*d_y*sind(theta).*sind(phi) + beta_y;
% Compute the AF (array factor)
AF = 0;
for n = 1:N
for m = 1:M
AF = AF + (exp(j*((m-1)*psi_x + (n-1)*psi_y)));
end
end
AF_mag = abs(AF);
AF = 0;
for n = 1:N
for m = 1:M
AF = AF + (A_mn(m,n)*AF_mag);
end
end
% Compute the normalized AF (array factor) in decibel
AF_max = max(AF); % ERROR OCCURS ON THIS LINE
AF_dB = 20*log10(AF/AF_max);
% Find the sidelobes of the AF (array factor)
lobes = findpeaks(AF_dB);
% Identify the maximum sidelobe level
SLL = lobes(floor(0.5*length(lobes)));
SLL_imp = -12.0425; % the imposed sidelobe level that we want to optimize for
y = (SLL - SLL_imp)^2;
end

Respuesta aceptada

Alan Weiss
Alan Weiss el 23 de Dic. de 2021
If you check the Supported Operations for Optimization Variables and Expressions, you see that max is not supported. (Why? I believe it is because max is not differentiable.) Therefore you must use fcn2optimexpr to work around the issue of unsupported functions. So instead of the line
prob = optimproblem("Objective",error_fcn(A_mn));
use
prob = optimproblem("Objective",fcn2optimexpr(@error_fcn,A_mn));
Alan Weiss
MATLAB mathematical toolbox documentation
  2 comentarios
Saeed Raffoul
Saeed Raffoul el 27 de Dic. de 2021
Thank you, fcn2optimexpr solves the issue of the unsupported max function. However, I am getting an error, concerning the solve function, which I am failing to understand:
Error using optim.problemdef.OptimizationProblem/solve
Array indices must be positive integers or logical values.
Error in ArrayThinning_GA (line 13)
[sol,fval,exitflag] = solve(prob,"Solver","ga","Options",options);
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
Which array is not being indexed with positive integers or logical values? I can confirm that the array, lobes, is only indexed by postive integers.
Alan Weiss
Alan Weiss el 28 de Dic. de 2021
Without seeing the entire function call and the functions they call, I am not sure. The problem might relate to some supposedly integer values being slightly different than integers. Sometimes, you can fix this issue with a call to round on the appropriate variables just before the function gives its outputs.
However, the issue might lie elsewhere. For more helpful answers, please provide more details.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Genetic Algorithm en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by