Error using barrier Objective function is undefined at initial point. Fmincon cannot continue.

23 次查看(过去 30 天)
Can anyone see why I get this error:
Error using barrier
Objective function is undefined at initial point. Fmincon cannot continue.
Error in fmincon (line 895)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in untitled4 (line 19)
[x_opt, f_opt, exitflag] = fmincon(augmented_objective, x0, [], [], [], [], [], [], [], options);
When running this code:
main_function = @(x) (2*x(1) + x(2))^2 + x(2)^4 - 4*x(1) - 4*x(2);
constraints = @(x) [x(1)^2 + x(2)^2 - 4; -(4*x(1) + 5*x(2) - 4)];
max_it = 5;
x0 = [10; 10]; % Initial guess
Beta = 0.1; % Initial penalty parameter
% Increase max iterations due to initial guess
options = optimoptions('fmincon', 'Display', 'iter', 'MaxIterations', 1000);
for it = 1:max_it
% Define the barrier function
barrier_function = @(x) -sum(log(constraints(x) + 1e-6));
% Define the augmented objective function with the barrier term
augmented_objective = @(x) main_function(x) + (1/Beta) * barrier_function(x);
augmented_objective(x0)
% Using fmincon to minimize the problem
[x_opt, f_opt, exitflag] = fmincon(augmented_objective, x0, [], [], [], [], [], [], [], options);
% Check if the optimization converged successfully
if exitflag <= 0
fprintf('Optimization did not converge at iteration %d.\n', it);
break;
end
% Update the initial guess for the next iteration
x0 = x_opt;
Beta = Beta * 10;
fprintf('Iteration %d: x_opt = [%.4f, %.4f], f_opt = %.4f\n', it, x_opt, f_opt);
end
ans = 1.0723e+04 - 3.1416e+01i
Error using barrier
Objective function is undefined at initial point. Fmincon cannot continue.

Error in fmincon (line 891)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...

回答(1 个)

Torsten
Torsten 2023-9-14
移动:Torsten 2023-9-14
If you insert the line
augmented_objective(x0)
before the call to fmincon, you will see that your objective returns a complex number. This is forbidden in optimization problems.
The reason is that your constraint function takes "log" of a negative number.
  1 个评论
Tilde Netz
Tilde Netz 2023-9-15
Thank you so much! I still got error when I chaged that, but I rearranged the barrier function to be inside the augumented objective function and then it worked for some reason.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matched Filter and Ambiguity Function 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by