I received an unexpected wrong result that contradict the constraint condition when using optimproblem
Mostrar comentarios más antiguos
function sol = optimize_crop_allocation(A_init, P, Q, C, D, U, V, M, N, S, B_1, B_2)
% P,Q 1x41
% C,D 1x41
% U,V 54x41
% M,N 54x41
% S 1x54
% B_1,B_2 54x41
prob = optimproblem('ObjectiveSense','maximize');
A = optimvar('A', 2, 54, 41, 'Type', 'continuous', 'LowerBound', double(0));
X=A(1);
Y=A(2);
phi_f=@(X) double(0.5*min(sum(X .* M)+sum(X .* M),sum(X .* M)+C)*P'-sum(sum(X.*U)));
f_expr=fcn2optimexpr(phi_f,X);
phi_g=@(Y) double(0.5*min(sum(Y .* N)+sum(Y .* N),D+sum(Y .* N))*Q'-sum(sum(Y.*V)));
g_expr=fcn2optimexpr(phi_g,Y);
prob.Objective = f_expr+g_expr;
prob.Constraints.cons1 = sum(A(1), 2) <= S'; % the output fails to satisfy this condition
prob.Constraints.cons2 = sum(A(2), 2) <= S'; % and this
A0.A=double(A_init);
sol = solve(prob,A0);
disp(sol);
end
thanks for your help
9 comentarios
John D'Errico
el 7 de Sept. de 2024
Editada: John D'Errico
el 7 de Sept. de 2024
You don't provide the data that went into it, so there is nothing we can test.
You don't show how far the constraints failed by. Is it less than the constraint tolerance? If so, then it did not actually fail. That sort of thing is to be expected.
We cannot see inside your computer, or see into your mind. If you want help, then you need to make it possible for us to help you.
昀泽
el 7 de Sept. de 2024
idris
el 7 de Sept. de 2024
Your error's description is probably as a result of how the summation and dimensionality of your variables are set in relation to the constraints. Can you provide more facts?
昀泽
el 7 de Sept. de 2024
John D'Errico
el 7 de Sept. de 2024
Editada: John D'Errico
el 7 de Sept. de 2024
Still missing information to recreate what you did.
What was A_init?
As my first comment without even trying yet to run your code, I see that your objective function has calls to min in it. That makes your objective not a differentiable one. And since the solver will probably decide to use fmincon, the call to min can often make fmincon fail to properly converge.
昀泽
el 7 de Sept. de 2024
昀泽
el 7 de Sept. de 2024
As @idris said: you should experiment first with numerical matrices what you get by your summation and indexing operations. E.g. I can't believe that you want to set these constraints:
prob.Constraints.cons1 = sum(A(1), 2) <= S'; % the output fails to satisfy this condition
prob.Constraints.cons2 = sum(A(2), 2) <= S'; % and this
A(1) and A(2) are scalar values like 4 and 5. So why would it be necessary to sum over them ?
Also setting
X=A(1);
Y=A(2);
is obscure. Maybe you mean
X=A(1,:,:);
Y=A(2,:,:);
I don't know.
昀泽
el 17 de Sept. de 2024
Respuestas (0)
Categorías
Más información sobre Choose a Solver en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!