**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# lin prog optimization with recourse function.

2 views (last 30 days)

Show older comments

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 Comments

bus14
on 2 May 2019

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
on 2 May 2019

Edited: Torsten
on 2 May 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
on 2 May 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
on 2 May 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
on 6 May 2019

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!

bus14
on 8 May 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)?

bus14
on 8 May 2019

Edited: bus14
on 8 May 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

bus14
on 8 May 2019

Edited: bus14
on 8 May 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
on 8 May 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
on 8 May 2019

Edited: bus14
on 8 May 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.

### Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)