Error using barrier Objective function is undefined at initial point. Fmincon cannot continue.
23 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Torsten
on 14 Sep 2023
Moved: Torsten
on 14 Sep 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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!