"Failure in initial objective function evaluation"

24 views (last 30 days)
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 7 Apr 2020
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 Comments
Torsten
Torsten on 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 on 8 Apr 2020
Edited: Leon Stolp on 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!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!