lin prog optimization with recourse function.

Hi community, I have a problem with constructing the right code. Where I first want to minimize a code using linprog to find values for Y & Z and thereafter want to use these obtained values of Z & Y to find an optimal value for X.
%first stage making expectation of second stage to determine x
%Min c.'*x+E[Q(x,D)]
i=1;
j=1;
c = 3;
l = 0.25;
q = 11;
s = 2;
A1 = 1;
D= 100;
x1=110
f2 = [-s.',(l-q).']; %[ Y, Z]
E=sum(f2) %value for expected optimum solution Q(x,D)
Aeq = [eye(j),A1.'];
beq = x1;
lb = [zeros(1,i+j)]; %requires 4 bounds as there are 4 variables --> for versatility lb=[zeros(1,v) v= number of variables in V
ub = [inf(1,j),D];
sol = linprog(f2,[],[],Aeq,beq,lb,ub);
y = sol(1)
z = sol(2)
%V=[V1 V2 V3]= [X Y Z]
f1= [c.',-s.',(l-q).'];
Aeq = [0,eye(j), A1.'];
beq = [x];
lb = [0,0,0];%x>0
ub = [Inf,inf,D]; ?
sol = linprog(f1,[],[],Aeq,beq,lb,ub)
x = sol(1)
from the second linprog I only want a solution for the value of X, since the values of Z & Y should be the same as obtained from the f2 Linprog function. I do not now How to fix that the second linprog f1 uses the values obtained from optimization f2. As now As result of optimization f1 I get X=Y=Z=0. which is not satisfactory. Does anyone have an idea how I can fix this?
Thankyou!

20 Kommentare

Torsten
Torsten am 2 Mai 2019
What is x in "beq = [x]" ?
Why do you solve for Y and Z in the second linprog if you want to retain the values of the first linprog ?
x will be for this instance a number, beq=[x] is the same x as in the objective function and needs to be find by optimizing the objective function.
Min c.'*x-s.'*y+(l-q).'*z
s.t. x=y+A1.'*z
where
x>0, y>0, 0<z<D
Why do you solve for Y and Z in the second linprog if you want to retain the values of the first linprog ?
Because I do not know how to put it into linprog when I do not want to optimze Z and Y in the new situation but only want to optimize X using the already found Z & Y of the first linprog.
should f1 in the case of only optimizing x be:
f1= [c.'-s.'*y+(l-q).'*z];
or would X then be multiplied by all three parts of the function [c.',s.',(l-q).']?
Hope you can help me out
Torsten
Torsten am 2 Mai 2019
Bearbeitet: Torsten am 2 Mai 2019
As I already answered in another thread of yours, the constant term -s.'*y+(l-q).'*z doesn't matter in f1. Thus f1 is simply [c.'].
Thus your second optimization reads
f1 = [c.'];
Aeq = 1;
beq = y + A1.'*z;
lb = 0;
ub = Inf;
sol = linprog(f1,[],[],Aeq,beq,lb,ub)
x = sol(1)
bus14
bus14 am 2 Mai 2019
Thankyou, I understand that f1 should only have [c.'] as x is optimized. However, what I do not understand is how the optimization than takes into account the rest of the objective function in the optimization?
As it is: Min c.'*x-s.'*y+(l-q).'*z.
because when setting f1=[c.'] linprog will only take into account minimizing c.'*x am I right? and does nothing with the remaining value of -s.'*y+(l-q).'*z. Is it because the second part is merely a numerical value so it cannot be optimized further or do I misunderstand the working of the f[] in linprog?
Sorry to bother you with my many questions
Torsten
Torsten am 2 Mai 2019
What does it mean that a feasible x* is optimal ?
It means that
c.'x* <= c.'x
for all feasible vectors x.
Now adding the constant value -s.'y+(l-q).'z at both sides of this inequality gives
c.'x* -s.'y+(l-q).'z <= c.'x -s.'y+(l-q).'z
for all feasible vectors x.
Thus the same x* is also optimal for the problem
min: c.'x -s.'y+(l-q).'z
This means that adding a constant to the objective function doesn't change the optimal x (and thus does not need to be taken into account).
bus14
bus14 am 2 Mai 2019
Thanks once again Torsten!
Torsten, in improving the code and trying it with different parameters I encoutered a struggle. In finding this x value, as to optimize the unkown x value which is in the objective function as well as in the constraint. Matlab returns an error saying that x variable is undefined. However, x is the variable that I want to find using linprog(which is the reason that no value is assigned to it yet).
f1 = [c.'];
Aeq = y+A1.'*z;
beq = x;
lb = 0;
ub = Inf;
sol = linprog(f1,[],[],Aeq,beq,lb,ub)
x = sol(1)
In your answer to this question you stated that Aeq=1 and beq=y+A1.'*z but 1 does not represent the value of x.
Or am I misunderstanding it and did you use 1 because AeqX=beq and X=x ??
The value of x should satisfy the constrained and also Min the objective funtion of Min c'*x.
Really hope you can answer this question!
Torsten
Torsten am 7 Mai 2019
If Aeq = 1 and beq = y+A1.*z, your solution variable x satisfies
Aeq*x = x = y+A1.*z = beq
I think this is what you want.
bus14
bus14 am 8 Mai 2019
yes this was indeed what I was looiking for
Another question, Is it possible to have some sort of summation in the objective function in linprog? Or a loop
As i want to find y(k) and z(k) for multiple instances and use this to find the optimal value for x.
would look like:
Min c'*x+Sigma(pk(k)*((l-q).'*z(k)-s.'*y(k))
s.t y(k)-x+A1.'*z*(k)=0
y(k)>0, x>0 0<z(k)<k for k=1:1:200
or Is this not possible? As linprog only optimizes regarding Y and Z and not for Y(k) and Z(k)?
Torsten
Torsten am 8 Mai 2019
Bearbeitet: Torsten am 8 Mai 2019
It's possible by defining X, Y and Z as vectors of length 200.
Your objective function then reads
Min [c;-Pk*s;Pk*(l-q)].'*[X;Y;Z]
bus14
bus14 am 8 Mai 2019
Bearbeitet: bus14 am 8 Mai 2019
And saying that X returns just 1 value instead of 200 is not possible? As X should have the same value for every of the 200 instances
f2 = [c',pk(k)*-s.',pk(k)*(l-q).'];
Aeq = [-1,1,A1.']; %[x y z]
beq = [0];
lb = [0,0,0];
ub = [inf,inf,k];
sol = linprog(f2,[],[],Aeq,beq,lb,ub);
x= sol(1); % or x(k)=sol(1);
y(k) = sol(2);
z(k) = sol(3);
This is what I used, returns 0 for all vectors
Torsten
Torsten am 8 Mai 2019
Bearbeitet: Torsten am 8 Mai 2019
Then you have to define this as 200 constraints:
x = Y(k)+A1.'*Z(k) for 1 <= k <= 200.
This means that you only need one variable x instead of 200.
bus14
bus14 am 8 Mai 2019
Bearbeitet: bus14 am 8 Mai 2019
Yes indeed. First I tried this in the case of only Min c*x
then I used Aeq=1
beq= Y(k)+A1'*Z(k)
However having three variables, I do not know how to put this in the Aeq matrix in a way that it satifies x=y(k)+A1.'*z(k)
p.s the whole linprog is in a for loop for k=1:1:200 so that it runs from itself. My only problem is defining the Aeq and beq to fix this.
tried to use Aeq=[1,0,0] beq=[y(k)+A1.'*z(k)] but y and z are then undefined
Torsten
Torsten am 8 Mai 2019
Linprog can't be set in a loop if x is connected to all of the 200 Y(k) and Z(k) solution variables. It must be solved as one big problem.
Define the solution vector of this big problem as
V = [x Y(1) Y(2) ... Y(200) Z(1) Z(2) ... Z(200)]
Then V has 401 solution variables.
All Aeq, beq, lb, ub and c settings follow automatically from this setting of the solution vector.
Think about it.
bus14
bus14 am 8 Mai 2019
This would mean that Aeq becomes 1 huge matrix and beq can stil be set to 0, am I right
Torsten
Torsten am 8 Mai 2019
Bearbeitet: Torsten am 8 Mai 2019
Yes, you are right, if by "0" you mean a vector made up of 200 zeros for the 200 constraints.
bus14
bus14 am 8 Mai 2019
Aeq=[-1,ones(1,200),ones(200,1)]
beq=[zeros(200,1)]
Torsten
Torsten am 8 Mai 2019
Bearbeitet: Torsten am 8 Mai 2019
x = Y(k)+A1.'*Z(k) for 1 <= k <= 200
This gives a matrix Aeq of dimension 200 x 401.
What you define above as Aeq is not even a matrix.
bus14
bus14 am 8 Mai 2019
Bearbeitet: bus14 am 8 Mai 2019
Was in a bit of a hurry...
What I meant was:
Aeq=[-ones(200,1),eye(200),eye(200)];
beq = [zeros(200,1)];
Now new problems rise as the number of columns of Aeq must be the same as the number elements in f
f2 = [c';pk(k)*-s.';pk(k)*(l-q).']
However, without running the for loop. This ofcourse has only 3 elements. Is there a trick I can use to repeat this for the objective function f2 to put all 200 situation in them instead of just typing it out?
because there are indeed 200 instances of y(k) and 200 instances of z(k) do not know how to put this into f2.
Torsten
Torsten am 8 Mai 2019
I did not write pk(k), but Pk which is
Pk = [pk(1);pk(2);pk(3);...;pk(200)]

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Gefragt:

am 2 Mai 2019

Kommentiert:

am 8 Mai 2019

Community Treasure Hunt

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

Start Hunting!

Translated by