Setting up for linprog in Matlab

蓝咒 提交于 2019-12-12 19:23:40

问题


I am having trouble setting up the follow type of constraints for linprog in Matlab

Max 9x1 + 8x2 + 7x3
Subject to: 2 <= x1 + x2 <= 3
            4 <= x2 + x3 <= 5
            x1 >= 0, x2 >= 0, x3 >= 0

Following mathwork's doc

A = [ 1 1
      1 1
      1
      1
      1 ]
f = [ 9 8 7 ]
b = ???
lb = [ 2 4 ]
ub = [ 3 5 ]

I don't know how to set up b since the example given on the website isn't bounded.

Also how does (if needed) the lb and ub take into account of x1 >= 0, x2 >=0, and x3 >=3?


回答1:


According to the linprog function, there are 3 forms of specifying constraints:

  1. Inequality constraints. Those like x1+x2 <= 3. For these constraints you must write all of them as a linear combination between the variables less than a constant. So finally you write them like Ax <= b.

  2. Equality constrains. Those like x1+x2 = 3. This case is like the above but instead of the less than sign, you use an equal sign. So your equality constraints will be represented as Aeq x = beq.

  3. Nature of variables. In these constraints you bound your variables. So here you write 0 <= x1 <= Inf as lb <= x <= ub.

So you should write your problem as:

Max 9 x1 + 8 x2 + 7 x3
Subject to:
        - x1 - x2 <= -2
        x1 + x2   <= 3
        -x2 - x3  <= -4
        x2 + x3   <= 5

        0x1 + 0x2 + 0x2 = 0

        0 <= x1 <= Inf
        0 <= x2 <= Inf
        0 <= x3 <= Inf

Now that you rewritten it you define your Matlab variables:

A = [-1 -1 0; 1 1 0; 0 -1 -1; 0 1 1]
b = [-2; 3; -4; 5]
Aeq = zeros(1,3)
beq = 0
lb = [0; 0; 0]
ub = [Inf; Inf; Inf]
f = [9 8 7]

Hope it helps you. Greetings

EDIT: linprog minimize so f must be [-9 -8 -7]




回答2:


The MathWorks doc that you link to shows several example of setting upper and lower bounds, and the code you show doesn't follow the doc. Your A isn't a valid matrix and your lb and ub don't have enough elements (and contain the wrong numbers).

You want

A = [1 1 0;-1 -1 0;0 1 1;0 -1 -1];
b = [3;-2;5;-4];
lb = [0;0;0];
ub = [inf;inf;inf];


来源:https://stackoverflow.com/questions/34714851/setting-up-for-linprog-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!