[英]Non-negative ratio constraint with linear programming in lp_solve
With lp_solve I need to constrain the ratio of two linear functions to be non-negative: 使用lp_solve,我需要将两个线性函数的比率限制为非负值:
min: 13.21 y0 + 27.46 y1 + 35.66 y2 + 89.21 y3 + 60.69 y4;
y0 + y1 + y2 + y3 + y4 >= 50000;
y0 <= 69148;
y1 <= 25460;
y2 <= 34020;
y3 <= 69873;
y4 <= 737299;
/* Spezification */
(-0.275 y0 + 0.15 y1 + 0.15 y2 + 0.236 y3 + 0.14745 y4) / (-0.175 y0 + 0.05 y1 + 0.05 y2 + 0.136 y3 + 0.04745 y4) >= 0;
But lp_solve does not provide parentheses. 但是lp_solve不提供括号。 Is it possible to solve it, so I don't need parentheses, or is this a general problem with lp_solve?
有可能解决它,所以我不需要括号,或者这是lp_solve的普遍问题?
You are trying to add a constraint of the following form to a linear program: 您正在尝试将以下形式的约束添加到线性程序中:
(-0.275 y0 + 0.15 y1 + 0.15 y2 + 0.236 y3 + 0.14745 y4) / (-0.175 y0 + 0.05 y1 + 0.05 y2 + 0.136 y3 + 0.04745 y4) >= 0;
For notational simplicity, I'll define A = (-0.275 y0 + 0.15 y1 + 0.15 y2 + 0.236 y3 + 0.14745 y4)
and B = (-0.175 y0 + 0.05 y1 + 0.05 y2 + 0.136 y3 + 0.04745 y4)
. 为了简化符号,我将定义
A = (-0.275 y0 + 0.15 y1 + 0.15 y2 + 0.236 y3 + 0.14745 y4)
和B = (-0.175 y0 + 0.05 y1 + 0.05 y2 + 0.136 y3 + 0.04745 y4)
。 Your constraint is therefore: 因此,您的约束是:
A / B >= 0
This means one of the following two conditions must hold: 这意味着必须满足以下两个条件之一:
A >= 0
and B >= 0
A >= 0
且B >= 0
A <= 0
and B <= 0
A <= 0
和B <= 0
This introduces a non-convexity in your formulation, because points (A, B) = (4, 4)
and (A, B) = (-2, -6)
are both feasible, but their midpoint (A, B) = (1, -1)
is not feasible. 这会在您的公式中引入非凸性,因为点
(A, B) = (4, 4)
和(A, B) = (-2, -6)
都是可行的,但是它们的中点(A, B) = (1, -1)
不可行。 Because your feasible set is non-convex, it is actually provably impossible to model your situation using a linear program with all continuous variables as you have tried to do in your code. 因为您的可行集是非凸的,所以实际上证明用线性程序对所有连续变量进行建模是不可能的,就像您在代码中尝试做的那样。
You will need to introduce a binary variable into your formulation to model this non-convexity. 您将需要在您的公式中引入一个二进制变量以对此非凸性建模。 A natural way to model this would be to make binary variable
z
equal 1 if A >= 0
and B >= 0
and to make z
equal 0 if A <= 0
and B <= 0
. 对此建模的自然方法是,如果
A >= 0
并且B >= 0
,则使二进制变量z
等于1;如果A <= 0
并且B <= 0
,则使z
等于B <= 0
。 Then you could introduce the following constraints (here M
is a large positive constant): 然后,您可以引入以下约束(此处
M
是一个大的正常数):
A <= Mz
B <= Mz
A >= M(z-1)
B >= M(z-1)
z binary
If z=0
, then the constraints give us -M <= A <= 0
and -M <= B <= 0
, while if z=1
, then the constraints give us 0 <= A <= M
and 0 <= B <= M
. 如果
z=0
,那么约束给我们-M <= A <= 0
和-M <= B <= 0
,如果z=1
,那么约束给我们0 <= A <= M
和0 <= B <= M
If the selected value of M
is sufficiently large, this will capture your situation. 如果选择的
M
值足够大,则可以捕获您的情况。
Your solution is well formulated, and lpsolve solved fine. 您的解决方案格式正确,而lpsolve解决得很好。 But the results may not be what you would expect.
但是结果可能不是您期望的。 For example, I got: z = 0 and x187 = 0 (A=B=0).
例如,我得到:z = 0和x187 = 0(A = B = 0)。 The thing is that A and B are expressions depending only of x187, so you should simplify that division expression!
事实是A和B是仅取决于x187的表达式,因此您应该简化该除法表达式! The chosen big M, should be smaller?
选择的大M,应该较小吗?
Model name: 'model build from GLP-Solve' - run #1
Objective: Minimize(R0)
SUBMITTED
Model size: 12 constraints, 53 variables, 311 non-zeros.
Sets: 0 GUB, 0 SOS.
Using DUAL simplex for phase 1 and PRIMAL simplex for phase 2.
The primal and dual simplex pricing strategy set to 'Devex'.
Relaxed solution 276710632.306 after 23 iter is B&B base.
Feasible solution 276710632.306 after 23 iter, 0 nodes (gap 0.0%)
Optimal solution 276710632.306 after 23 iter, 0 nodes (gap 0.0%).
Excellent numeric accuracy ||*|| = 0
MEMO: lp_solve version 5.5.2.0 for 64 bit OS, with 64 bit REAL variables.
In the total iteration count 23, 17 (73.9%) were bound flips.
There were 0 refactorizations, 0 triggered by time and 0 by density.
... on average 6.0 major pivots per refactorization.
The largest [LUSOL v2.2.1.0] fact(B) had 13 NZ entries, 1.0x largest basis.
The maximum B&B level was 1, 0.5x MIP order, 1 at the optimal solution.
The constraint matrix inf-norm is 1e+06, with a dynamic range of 6.39386e+08.
Time to load data was 0.001 seconds, presolve used 0.000 seconds,
... 0.000 seconds in simplex solver, in total 0.001 seconds.
If we remove the A, B and z restrictions we obtain the same results: 如果删除A,B和z限制,我们将获得相同的结果:
Model name: 'model build from GLP-Solve' - run #1
Objective: Minimize(R0)
SUBMITTED
Model size: 6 constraints, 50 variables, 299 non-zeros.
Sets: 0 GUB, 0 SOS.
Using DUAL simplex for phase 1 and PRIMAL simplex for phase 2.
The primal and dual simplex pricing strategy set to 'Devex'.
Optimal solution 276710632.306 after 22 iter.
Excellent numeric accuracy ||*|| = 0
MEMO: lp_solve version 5.5.2.0 for 64 bit OS, with 64 bit REAL variables.
In the total iteration count 22, 17 (77.3%) were bound flips.
There were 0 refactorizations, 0 triggered by time and 0 by density.
... on average 5.0 major pivots per refactorization.
The largest [LUSOL v2.2.1.0] fact(B) had 7 NZ entries, 1.0x largest basis.
The constraint matrix inf-norm is 1, with a dynamic range of 639.386.
Time to load data was 0.002 seconds, presolve used 0.001 seconds,
... 0.001 seconds in simplex solver, in total 0.004 seconds.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.