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

조회 수: 26 (최근 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