我有一个Matlab优化函数,其中目标函数最后评估的迭代函数值与最终函数值非常不同。这是不正确的,请参阅下面的问题代码。
globalvars.m
global sc_element_global;
global B_j_global;
% other global variables
sim_launcher.m
function sim_loop
%loop
sc_array={0.1,0.5,0.8};
for sc_element_idx=1:length(sc_array)
% Set 'x' as constant array 50
sc_element=sc_array{sc_element_idx};
find_network_productivity(sc_element,x)
end
end
find_network_productivity.m
%caller function to fmincon
function [a, obj_f, x0, optimized_exit_flag]=find_network_productivity(sc_element,x)
globalvars;
% Set global sc_element
sc_element_global=sc_element;
% Setting lb, ub, x0
options = optimoptions(@fmincon,'Display','iter-detailed','DerivativeCheck','off','Diagnostics','on','Algorithm','sqp');
[a, obj_f, optimized_exit_flag]=fmincon('objfunnwprod',x0,[],[],[],[],lb,ub,'confunnwprod',options);
obj_f=-obj_f; % Because we want to find the maximum, not the minimum
if optimized_exit_flag==-2
optimized_exit_flag
end
obj_f % print the final function value obj_f
end
objfunnwprod.m
%OBJ FN
function [f]=objfunnwprod(x)
globalvars;
f=B_j_global*x;
f=-f % print the iteration function value f
end
confunnwprod.m
%CONSTRAINT FN
function [c, ceq]=confunnwprod(x)
globalvars;
for i=1:length(eNodeBs_global),
B_j_global(i)=...
calculate_blocking_probability_with_x(eNodeBs_global(i),x(i));
c(i)=B_j_global(i)-eta_global(i);
end
ceq=[];
end
calculate_blocking_probability_with_x.m
% function to calculate probability based on x
function prob=calculate_blocking_probability_with_x(eNodeBs_global_var,x_var)
globalvars;
% based on sc_element_global and x_var, calculate probability
prob=blocking_probability_with_x;
end
输入:
% Inputs set
x=[50 50 50 50 50 50];
sc_array={0.1,0.5,0.8};
% Output set
sc_element=0.1
x=50
f=41.0172
opt_f=39.09
sc_element=0.5
x=50
f=770.43
opt_f=39.09
在这里,我可以看到f
值与最终opt_f
值不同。它应该更接近最终值。这是期望,但我没有看到这样的结果。
问题:为什么迭代中的函数值f
随sc_element
而变化,但优化的函数值opt_f
保持不变?
看起来sc_element
中的任何更改都不会对opt_f
产生影响,但内部f
值会发生变化。