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

23 visualizzazioni (ultimi 30 giorni)
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, ...

Risposte (1)

Torsten
Torsten il 14 Set 2023
Spostato: Torsten il 14 Set 2023
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 Commento
Tilde Netz
Tilde Netz il 15 Set 2023
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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by