How would you impliment this optimization in MATLAB?

4 Ansichten (letzte 30 Tage)
MCG
MCG am 4 Mär. 2022
Bearbeitet: John D'Errico am 4 Mär. 2022
I'm trying to minimize this in MATLAB, however, I'm having problems to figure out how! Could you help me? Thanks in advance!!

Akzeptierte Antwort

John D'Errico
John D'Errico am 4 Mär. 2022
Bearbeitet: John D'Errico am 4 Mär. 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)
C = 1×5
-0.9200 -0.7500 -0.5500 -0.6500 -0.1900
CEQ = []
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)
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
X = 1×5
0.3147 0.2913 0.2331 0.1609 0.0045
FVAL = 0.0045
EXITFLAG = 2
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 )
Optimization terminated: mesh size less than options.MeshTolerance and constraint violation is less than options.ConstraintTolerance.
X = 1×5
0.3164 0.2852 0.2383 0.1602 0.0533
FVAL = 0.0533
EXITFLAG = 1
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
  1 Kommentar
MCG
MCG am 4 Mär. 2022
Bearbeitet: MCG am 4 Mär. 2022
Dear @John D'Errico thank you very much for your solution and expertise.
It has worked very well and now I am able to understand how to program a problem like this.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by