In linear programming, How can I give a value to a binary variable y depending on whether a continuous variable is 0 (y = 0) or positive (y = 1)?

huangapple go评论72阅读模式
英文:

In linear programming, How can I give a value to a binary variable y depending on whether a continuous variable is 0 (y = 0) or positive (y = 1)?

问题

我有一个二进制变量 y 和一个连续变量 x,我想在线性规划中(使用 Python mip)实现以下条件:

if x > 0:
   y = 1
else:
   y = 0

我尝试了以下方法:

self.model.add_constr(y >= 0, name="Lin 2.1")
self.model.add_constr(y * 10000000 >= x, name="Lin 2.2")
self.model.add_constr(y <= x * 10000000, name="Lin 2.2")

希望这能帮助您实现您的线性规划条件。

英文:

I have a binary variable y and a continuos variable x, and I would like to implement in linear programming (Python mip) the next condition:

if x &gt; 0:
   y = 1
else:
   y = 0

I have tried the following:

self.model.add_constr(y &gt;= 0, name=f&quot;Lin 2.1&quot;)
self.model.add_constr(y * 10000000 &gt;= x, name=f&quot;Lin 2.2&quot;)
self.model.add_constr(y &lt;= x * 10000000, name=f&quot;Lin 2.2&quot;)

答案1

得分: 1

x ≥ 0.001 - M(1-y)
x ≤ 0 + My
y ∈ {0,1}

其中 M 是一个足够大的常数(经过谨慎选择)。

英文:
x &gt;= 0.001 - M(1-y)
x &lt;= 0     + My
y ∈ {0,1}

where M is a large enough constant (carefully chosen).

huangapple
  • 本文由 发表于 2023年6月26日 19:16:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定