Interior-point and sequential quadratic programming give me the same answer. Is there an error with my code?

12 visualizaciones (últimos 30 días)
I am attempting to minimise a least squares regression model and compare answers given by SQP and IP, and the system is constrained by x(1) + x(2) + x(3) = 1, and each variable cannot be smaller than 0 or bigger than 1. However, I consistently get the same numerical answers for both methods, which should not be the case. I am wondering if I missed a step or something along those lines? My code is as follows;
fun=@(x) (x(1)-147/505).^2+(x(1)-167/530).^2+(x(2)-504/2525).^2+(x(2)-471/2120).^2+(x(3)-1011/2120).^2+(x(3)-1256/2525).^2;
x0=[0.3, 0.5, 0.2];
Aeq=[1, 1, 1];
beq=1;
lb=[0, 0, 0];
ub=[1, 1, 1];
IP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub);
fprintf('%.13f\n',IP)
options = optimoptions('fmincon','Algorithm','sqp');
SQP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub);
fprintf('%.13f\n',SQP)
These are my results;
IP:
0.3027134331491
0.2105086010671
0.4867779657839
SQP:
0.3027134331491
0.2105086010671
0.4867779657839
Any help or pointers would be appreciated. Thank you in advance.
  4 comentarios
Torsten
Torsten el 13 de Nov. de 2023
Maybe one needs more iterations with one of the methods (use 'Display','iter' in the options structure). But if you prescribe the accuracy of the solution to be equal for both methods (as you implicitly do in your code because you didn't change them in the optimoptions structure), in theory both solutions should be equally precise, shouldn't they ?

Iniciar sesión para comentar.

Respuesta aceptada

Torsten
Torsten el 13 de Nov. de 2023
Editada: Torsten el 13 de Nov. de 2023
I just saw in your code that you don't pass the options structure to fmincon. Thus in both calls, the same solver is used.
Use
fun=@(x) (x(1)-147/505).^2+(x(1)-167/530).^2+(x(2)-504/2525).^2+(x(2)-471/2120).^2+(x(3)-1011/2120).^2+(x(3)-1256/2525).^2;
x0=[0.3, 0.5, 0.2];
Aeq=[1, 1, 1];
beq=1;
lb=[0, 0, 0];
ub=[1, 1, 1];
options = optimoptions('fmincon','Algorithm','interior-point','Display','iter');
IP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub,[],options);
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 4 3.328630e-01 0.000e+00 1.455e+00 1 8 9.097657e-02 0.000e+00 1.369e+00 6.098e-01 2 12 2.579621e-03 0.000e+00 9.931e-02 1.857e-01 3 16 1.181553e-03 0.000e+00 7.284e-02 3.823e-02 4 20 1.098100e-03 0.000e+00 4.139e-02 2.558e-02 5 24 7.552129e-04 0.000e+00 2.454e-03 1.287e-02 6 28 7.552951e-04 0.000e+00 1.000e-03 8.613e-04 7 32 7.545440e-04 0.000e+00 2.015e-04 5.086e-04 8 36 7.545172e-04 0.000e+00 2.077e-06 1.144e-04 9 40 7.545172e-04 0.000e+00 2.001e-08 1.263e-06 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
fprintf('%.13f\n',IP)
0.3027134331492 0.2105086010669 0.4867779657839
options = optimoptions('fmincon','Algorithm','sqp','Display','iter');
SQP=fmincon(fun,x0,[],[],Aeq,beq,lb,ub,[],options);
Iter Func-count Fval Feasibility Step Length Norm of First-order step optimality 0 4 3.328630e-01 0.000e+00 1.000e+00 0.000e+00 1.156e+00 1 9 2.478717e-01 1.110e-16 7.000e-01 6.930e-01 1.335e+00 2 13 2.415543e-01 0.000e+00 1.000e+00 6.044e-01 1.139e+00 3 17 5.519559e-02 1.110e-16 1.000e+00 4.532e-01 5.134e-01 4 21 4.792452e-03 0.000e+00 1.000e+00 1.976e-01 1.459e-01 5 25 7.959527e-04 1.110e-16 1.000e+00 4.792e-02 1.485e-02 6 29 7.546824e-04 2.220e-16 1.000e+00 4.766e-03 8.850e-04 7 33 7.545173e-04 2.220e-16 1.000e+00 2.919e-04 1.947e-05 8 37 7.545172e-04 2.220e-16 1.000e+00 6.953e-06 3.359e-07 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
fprintf('%.13f\n',SQP)
0.3027134886159 0.2105086203988 0.4867778909853

Más respuestas (0)

Categorías

Más información sobre Linear Least Squares en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by