Optimization Live Editor task Error "Your objective function must return a scalar value"

5 visualizzazioni (ultimi 30 giorni)
Hi,I'm trying to maximize a function with genetic algorithm or patternsearch using Optimization Live Editor task. But it confuses me that an Error "Your objective function must return a scalar value" always occurs, and I have alreay checked out the output of my objective function. Can somebody tell me how to fix this problem? Would appreciate any help!
I checked out the the output of my objective function as follows:
input = [0 0.5];
MaxSidelobe = FindBestPlacingGA(input);
TF = isscalar(MaxSidelobe);
disp(TF);
The objective function and other functions needed:
function MaxSidelobe= FindBestPlacingGA(input)
input(1) = deg2rad(input(1));
mic_pos = [0 0.24 0
-0.2078 -0.12 0
0.2078 -0.12 0];
mic_pos = [Array3N(input(1),input(2));mic_pos];
MaxSidelobe= FPSF_Function(mic_pos,500,0:1:80);
end
function mic_pos = Array3N(theta,rho)
theta3N = [theta+pi/2;theta+pi*7/6;theta+pi*11/6];
mic_pos = zeros(3,3);
mic_pos(:,3) = 0;
[mic_pos(:,1),mic_pos(:,2)] = pol2cart(theta3N,rho);
end
function MSL= FPSF_Function(mic_pos,f,El)
Num_mic = size(mic_pos,1);
Az = -180:1: 180;
c = 343;
k0 = [0 0 -1];
numAz = length(Az);
numEl = length(El);
K = zeros(3, numAz, numEl);
for i = 1:numAz
for j = 1:numEl
az_rad = deg2rad(Az(i));
el_rad = deg2rad(El(j));
x = cos(az_rad) * sin(el_rad);
y = sin(az_rad) * sin(el_rad);
z = cos(el_rad);
K(:, i, j) = [x; y; z];
end
end
W = zeros(numAz,numEl);
for p = 1:numAz
for q = 1:numEl
for n = 1:Num_mic
W(p,q) = exp(-1i*dot(K(:,p,q)'-k0,mic_pos(n,:))*2*pi*f/c) + W(p,q);
end
end
end
W = W/Num_mic;
Y = 10*log10((abs(W)).^2);
local_max = imregionalmax(Y);
max_values = Y(local_max);
Mainlobe = max(max_values(:));
sidelobes = max_values(max_values~=Mainlobe);
MSL = Mainlobe - max(sidelobes(:));
end

Risposta accettata

Cris LaPierre
Cris LaPierre il 14 Ago 2024
Modificato: Cris LaPierre il 14 Ago 2024
I believe the issue is related to your initial point. If your dimensions are 1x2, then your initial point should be 1x2 as well. Since it is just 0, when you run the optimization, the first input tried is [0,0]. Your objective function returns an empty array is this scenario, which results in the error about the output not being scalar.
MaxSidelobe= FindBestPlacingGA([0 0])
MaxSidelobe = 0x1 empty double column vector
Use the dropdown menu to change the initial point from 0 to a variable containing your initial point. Just make sure that variable is defined before you run the optmiization task. I used a variable named input0.
One other difference I noticed. In my version of MATLAB, the patternsearch solver is not available, so I used fmincon. The solution is very different between these two solvers:
  • patternsearch: [59.8330, 0.2899]
  • fmincon: [0.6559, 0.5000]
function MaxSidelobe= FindBestPlacingGA(input)
input(1) = deg2rad(input(1));
mic_pos = [0 0.24 0
-0.2078 -0.12 0
0.2078 -0.12 0];
mic_pos = [Array3N(input(1),input(2));mic_pos];
MaxSidelobe= FPSF_Function(mic_pos,500,0:1:80);
end
function mic_pos = Array3N(theta,rho)
theta3N = [theta+pi/2;theta+pi*7/6;theta+pi*11/6];
mic_pos = zeros(3,3);
mic_pos(:,3) = 0;
[mic_pos(:,1),mic_pos(:,2)] = pol2cart(theta3N,rho);
end
function MSL= FPSF_Function(mic_pos,f,El)
Num_mic = size(mic_pos,1);
Az = -180:1: 180;
c = 343;
k0 = [0 0 -1];
numAz = length(Az);
numEl = length(El);
K = zeros(3, numAz, numEl);
for i = 1:numAz
for j = 1:numEl
az_rad = deg2rad(Az(i));
el_rad = deg2rad(El(j));
x = cos(az_rad) * sin(el_rad);
y = sin(az_rad) * sin(el_rad);
z = cos(el_rad);
K(:, i, j) = [x; y; z];
end
end
W = zeros(numAz,numEl);
for p = 1:numAz
for q = 1:numEl
for n = 1:Num_mic
W(p,q) = exp(-1i*dot(K(:,p,q)'-k0,mic_pos(n,:))*2*pi*f/c) + W(p,q);
end
end
end
W = W/Num_mic;
Y = 10*log10((abs(W)).^2);
local_max = imregionalmax(Y);
max_values = Y(local_max);
Mainlobe = max(max_values(:));
sidelobes = max_values(max_values~=Mainlobe);
MSL = Mainlobe - max(sidelobes(:));
end
  3 Commenti
Cris LaPierre
Cris LaPierre il 15 Ago 2024
Modificato: Cris LaPierre il 15 Ago 2024
Your thought is correct - setting Initial Point to 0 does set both values to 0, but your obejctive function does not return a value in that case.
隐
il 15 Ago 2024
I see, then there's another bug in my objective function code to fix. Thank you again.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by