"Failure in initial objective function evaluation"

Hello everybody,
I'm trying to set up a optimization. As a function for the optimization I want to use a Class I wrote with a bunch of calculations. The class itself is working as it should. But when I try to run the fmincon solver I get an error warning: "Failure in initial objective function evaluation. FMINCON cannot continue"
I think I'm calling the method from the Class in the wrong way, wherever my attempts to change the way I call it didn't work either. Has anybody some advice?
%Instanz erstellen
myHoltropObject = Holtrop_R_T(12.86, 900);
x0 = [50 12 3.2 -4.5];
lb = [40 10 3 -5];
ub = [60 15 5 5];
gs = GlobalSearch;
options = optimoptions('fmincon','Algorithm', 'interior-point',...
'UseParallel',true, 'Display','off','MaxFunctionEvaluations',6000, 'TolCon', 1e-4, 'TolFun', 1e-4);
objectiveA = @(in) calcRT(in);
constraintA = @(in) RT_equal84(in, calcRT);
problem = createOptimProblem('fmincon', ...
'objective',objectiveA, ...
'x0',x0, ...
'lb',lb, ...
'ub',ub, ...
'nonlcon',constraintA, ...
'options',options)
% Run optim search
%[resultVector,~,exitFlag,~,~] = run(gs,problem); % this is for global optimization
[resultVector,~,exitFlag,~,~,~,~] = fmincon(problem) % this is for local search of optimum
% ERROR:
problem =
objective: @(in)calcRT(in)
x0: [50 12 3.2000 -4.5000]
Aineq: []
bineq: []
Aeq: []
beq: []
lb: [40 10 3 -5]
ub: [60 15 5 5]
nonlcon: @(in)RT_equal84(in,calcRT)
solver: 'fmincon'
options: [1×1 optim.options.Fmincon]
Unable to resolve the name myHoltropObject.getR_T.
Error in User>calcRT (line 31)
RT = myHoltropObject.getR_T([Lwl B T lcb]);
Error in User (line 10)
objectiveA = @(in) calcRT(in);
Error in fmincon (line 552)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
%
%Ergebnisausgabe
fprintf('Optimale Abmessungen: \nLänge: %5.3f \nBreite: %5.3f \nTiefgang: %5.3f \nlcb: %5.3f \n',resultVector(1), resultVector(2), resultVector(3), resultVector(4));
%Objective function
function RT = calcRT(in)
Lwl = in(1);
B = in(2);
T = in(3);
lcb = in(4);
RT = myHoltropObject.getR_T([Lwl B T lcb]);
end
%Constraint function
function [c, ceq] = RT_equal84(in)
RT = calcRT(in);
ceq = RT-616;
c = [];
end

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 7 Apr. 2020

0 Stimmen

Leon - where have you defined your objective and constraint functions relative to the "main" code (i.e. that code where you create myHoltropObject, set up the optimization problem, etc.)? Are they in separate m-files? Is your main code a script?
The error message is suggesting that when the objective function is called (or even the constraint function since it calls the ojective function too), that the myHoltropObject.getR_T is unknown. If these two functions are defined in separate m-files, then they won't "know" about this object. If this is the case, then I recommend that you put your main code in a function and then nest the constraint and objective functions so that they have access to the variables defined in the parent/main function. Perhaps something like
function myMainFunction
myHoltropObject = Holtrop_R_T(12.86, 900);
x0 = [50 12 3.2 -4.5];
lb = [40 10 3 -5];
ub = [60 15 5 5];
gs = GlobalSearch;
options = optimoptions('fmincon','Algorithm', 'interior-point',...
'UseParallel',true, 'Display','off','MaxFunctionEvaluations',6000, 'TolCon', 1e-4, 'TolFun', 1e-4);
objectiveA = @(in) calcRT(in);
constraintA = @(in) RT_equal84(in, calcRT);
problem = createOptimProblem('fmincon', ...
'objective',objectiveA, ...
'x0',x0, ...
'lb',lb, ...
'ub',ub, ...
'nonlcon',constraintA, ...
'options',options)
% Run optim search
%[resultVector,~,exitFlag,~,~] = run(gs,problem); % this is for global optimization
[resultVector,~,exitFlag,~,~,~,~] = fmincon(problem) % this is for local search of optimum
% Objective function
function RT = calcRT(in)
Lwl = in(1);
B = in(2);
T = in(3);
lcb = in(4);
RT = myHoltropObject.getR_T([Lwl B T lcb]);
end
%Constraint function
function [c, ceq] = RT_equal84(in)
RT = calcRT(in);
ceq = RT-616;
c = [];
end
end
where the above code is saved to a file named myMainFunction.m. This may help. Please see nested functions for more information.

11 Kommentare

Leon Stolp
Leon Stolp am 8 Apr. 2020
Bearbeitet: Leon Stolp am 8 Apr. 2020
The Code I posted is from my mainscript. I want to make it a livescript.
I'm defining everything in that livescript, except the class with all the calculations, which is defined in the Class script "Holtrop_R_T"
I just quickly tried to implement your solution and I get a new Error, that I don't have enough input arguments. I will have a further look at this later today.
Torsten
Torsten am 8 Apr. 2020
Your constraint function only has one input argument, but you defined it with two.
To deal with the error message, just call calc_RT(x0) before invoking fmincon and see what is returned.
The Problem seems to be, that the function is not recognizing the input
Lwl = x0(1);
correctly. x0 ist not recognzied as the input vektor.
Torsten
Torsten am 8 Apr. 2020
What do you get if you call calc_RT(x0) (e.g. after defining x0 in your main program) ?
Leon Stolp
Leon Stolp am 8 Apr. 2020
If I try it in my livescript, I get the "unable to resolve the Name myHoltropObject.getR_T" message.
If I try it with the function, I still get the "not enough input" message.
Leon - if you use the function, which line is throwing the error "Not enough input parameters"? Please copy and paste the full error message to here.
Not enough input arguments.
Error in userfunc/calcRT (line 27)
Lwl = x0(1);
Error in userfunc>@(in)RT_equal84(x0,calcRT) (line 11)
constraintA = @(in) RT_equal84(x0, calcRT);
Error in fmincon (line 639)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in userfunc (line 22)
[resultVector,~,exitFlag,~,~,~,~] = fmincon(problem); % this is for local search of optimum
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
Torsten
Torsten am 8 Apr. 2020
It seems you did not yet delete the error with the constraint function. You defined it with two input arguments, but you wrote it with only one.
Leon Stolp
Leon Stolp am 8 Apr. 2020
Bearbeitet: Leon Stolp am 8 Apr. 2020
I adressed that error too, but I didn't made any difference so far. But thanks anyways, you saved me some hassle later on!
Torsten
Torsten am 8 Apr. 2020
The error message from above stems from this error. So what is the error message now after correction ?
Leon Stolp
Leon Stolp am 8 Apr. 2020
Bearbeitet: Leon Stolp am 8 Apr. 2020
So, I finally had some time to have a detailed look, and you were right all the time. Yes, I adressed the error before, but I created a new problem at the same time. So with a real and careful look at the problem and your advice in mind I made it work. Thank you very much! Sorry for the hassle.
Now I'll try to find a way to incorporate this in my live script and make it work there.
Thanks to both of you again! You really helped me out!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by