How do I use the max() function on an optimization expression when using the genetic algorithm?
15 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Alan Weiss
on 23 Dec 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 Comments
Alan Weiss
on 28 Dec 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.
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!