How would you impliment this optimization in MATLAB?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm trying to minimize this in MATLAB, however, I'm having problems to figure out how! Could you help me? Thanks in advance!!

0 Commenti
Risposta accettata
John D'Errico
il 4 Mar 2022
Modificato: John D'Errico
il 4 Mar 2022
This is a moderately simple problem with 5 unknowns. Another solver may be safer, because of the absolute values, but they are in the constraints, and fmincon can probably at least take a shot at it. So I'll put it into fmincon and then patternsearch to see if they come up with consistent solutions.
The 5 unknowns are w1,w2,w3,w4,X. Put them all into a vector of unknowns called U.
Aeq = [1 1 1 1 0];
beq = 1;
small = 1e-6;
LB = [small small small small -inf];
UB = [1 1 1 1 inf];
A = [];
b = [];
U0 = [.25 .25 .25 .25 1];
I've defined the variable "small" as a value close to zero, but sufficiently far enough away that we will not have a divide by zero.
First, does the nonlinear constraint function evaluate as desired? Is this an initial feasible point?
[C,CEQ] = NLcons(U0)
Clearly, the initial point is feasible. The requirement is that C must be <= 0. In fact, your constraints make it clear that ANY set of values w1=w2=w3=w4 will be a feasible solution, as long as X is on the order of 1 or more. So as long as I choose w1=w2=w3=w4=0.25, it also trivially satisfies the equality constraint. These thigns are good, since it is valuable to start a solver off so the constraints are satisfied. Then it does not need to hunt for an initial feasible point to then work from.
[X,FVAL,EXITFLAG] = fmincon(@(U) U(5),U0,A,b,Aeq,beq,LB,UB,@NLcons)
I tend to dislike absolute values in optimizations, as they cause singularities in the derivitives. But did that cause a problem for fmincon? See if patternsearch finds a similar solution.
[X,FVAL,EXITFLAG] = patternsearch(@(U) U(5),U0,A,b,Aeq,beq,LB,UB,@NLcons )
So the two solvers have found fairly similar solutions. fmincon seems to have survived the absolute values in the constraints. I might have pushed patternsearch a little harder and it probably would have come to the same solution as fmincon.
function [C,CEQ] = NLcons(U)
w = U(1:4);X = U(5);
C(1) = abs(w(1)/w(2) - 1.08) - X;
C(2) = abs(w(2)/w(3) - 1.25) - X;
C(3) = abs(w(3)/w(4) - 1.45) - X;
C(4) = abs(w(1)/w(3) - 1.35) - X;
C(5) = abs(w(2)/w(4) - 1.81) - X;
CEQ = []; % No NONLINEAR equality constraints
end
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!