Azzera filtri
Azzera filtri

fmincon sometimes returns "incorrect number of elements of objective gradient" error.

6 visualizzazioni (ultimi 30 giorni)
Hello everyone,
This is my first time asking question on the forum.
I am using the fmincon function to optimize my propeller design. I have 14 design variables to define my propeller geometry and use the propeller_analysis function to do propeller analysis. My objective function is to find the highest propeller efficiency, and the only inequality constraint is that the resultant thrust has to be larger or equal to the required thrust.
Here is my fmincon setup:
xLast = [];
myf = [];
ineq = [];
eq = [];
outputFcn_global_data = [];
options = optimoptions(@fmincon,'Display','iter-detailed', ...
'Algorithm','sqp', ...
'FiniteDifferenceType', 'forward',...
'FunValCheck', 'off',...
'UseParallel', true, ...
'DiffMinChange', 0.1,...
'OptimalityTolerance', 1e-3, ...
'StepTolerance', 5e-5, ...
'PlotFcn', {@optimplotfval, @optimplotx, ...
@optimplotfirstorderopt, @optimplotfunccount, ...
@optimplotstepsize, @optimplotconstrviolation, ...
@optimplotfunccount}, ...
'OutputFcn', @outputFcn_global, ...
'MaxIterations', 10, ...
'MaxFunctionEvaluations', 200);
[x,f,eflag,output] = fmincon(@(x) objfun(x),x0,[],[],[],[],lb,ub,@(x) constr(x),options);
And my objective function and constraints:
[myf, ineq, eq, ~, ~] = propeller_analysis(x, rOverR, Oper, const);
Lastly, this is how I normalized the design variables, objective value, and the constraint:
% Design variable
dist = abs(ub-lb); % Distance between bounds
mid_p = (ub+lb) / 2; % Find the midpoint
xdes_0 = (x0 - mid_p).*(2./dist); % Normalized design variables
lb_norm = (lb - mid_p).*(2./dist); % Normalized lower bound
ub_norm = (ub - mid_p).*(2./dist); % Normalized upper bound
% obejctive and constraints
myf = (2-effy)/2;
ineq = 1 - (T/T_req); % Inequality constraints
eq = []; % Consistency constraints (equality)
This way I makes sure that the normalized upper and lower bounds are 1 and -1, respectively. Morevoer, the normalized deisgn variables are always within this range. For the objective value and constraint, the output values are normalized by the ideal efficiency and required thrust. The efficiency from XROTOR is 4 decimal precision.
However, if there is an infeasible design that trigger my conditions, I return the following:
myf = NaN;
ineq = NaN;
eq = [];
However, I have a trouble of getting the following problem:
I suspected some settings causing this issue:
  1. The step change is too small and cause the objective gradient doesn't change.
  2. The NaN return causes the error.
Yet, I couldn't pinpoint the root cause. My MATLAB version is 2021b. Hope anyone can help me. Thanks.
  2 Commenti
Matt J
Matt J il 18 Lug 2023
The error message mentions a "provided objective gradient", but your optimoptions do not show SpecifyobjectiveGradient=true. This makes me wonder if you are showing the code faithfully.
Yi-Kai Peng
Yi-Kai Peng il 18 Lug 2023
Modificato: Yi-Kai Peng il 18 Lug 2023
Hi Matt. I showed everything I have. But I didn't know SpecifyobjectiveGradient setting. It seems like this is not the option for sqp algorithm.

Accedi per commentare.

Risposte (1)

Matt J
Matt J il 18 Lug 2023
Modificato: Matt J il 18 Lug 2023
My best guess is that the MaxFunctionEvaluations limit was reached midway through finite differencing operations needed to complete a gradient evaluation. There is no reason that I can see to set MaxFunctionEvaluations=200 if you are only running 10 iterations. You should just set it to Inf.
Incidentally, your OptimalityTolerance and StepTolerance seem high as well. I wonder if you have a good reason not to use the default values.
  1 Commento
Yi-Kai Peng
Yi-Kai Peng il 18 Lug 2023
Hi Matt,
I set a higher OptimalityTolerance and StepTolerance because I have to use this optimization setup for several propeller designs. Therefore, I have to increase the tolerance to decrease the computational costs.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by