混合整数编程:每个条件的变量赋值(如果不是) [英] Mixed integer programming: variable assignment per condition (if then else)
问题描述
我(混合)整数编程相对较新,并且对约束的制定感到困惑.
I am relatively new to (mixed) integer programming and got stuck with the formulation of a constraint.
在我的简化模型中,我有一个参数和两个变量,它们是正实数,上限为321.我要表达的逻辑在这里:
In my simplified model I have one Parameter and two Variables that are positive Reals having the value 321 as upper bound. The logic I want to express is here:
if Parameter > Variable1:
Variable2 = Variable1
else:
Variable2 = Parameter
**edit** (while Variable1 is always >= Variable2)
实际上可以用线性输入(等式)来描述吗?
Is it actually possible to describe this using linear in(equalities)?
如果有帮助:对于实现,我正在使用Python,Pyomo和最新的gurobi求解器.
If it helps: For the implementation I am using Python, Pyomo and the newest gurobi solver.
感谢您的帮助!
推荐答案
将Variable2
设置为等于Variable1
和Parameter
的最小值或最大值.
Setting Variable2
equal to min or max of Variable1
and Parameter
.
min(Parameter,Variable1)
:
min(Parameter,Variable1)
:
如果确定目标函数中的Variable2
希望"很小,那么您只需要Variable2
小于或等于Parameter
和Variable1
:
If you are sure that Variable2
"wants" to be small in the objective function, then you just need to require Variable2
to be less than or equal to both Parameter
and Variable1
:
Variable2 <= Variable1
Variable2 <= Parameter
max(Parameter,Variable1)
:
max(Parameter,Variable1)
:
如果您确定目标函数中的Variable2
希望"很大,那么您只需要求Variable2
大于或等于Parameter
和Variable1
:
If you are sure that Variable2
"wants" to be large in the objective function, then you just need to require Variable2
to be greater than or equal to both Parameter
and Variable1
:
Variable2 >= Variable1
Variable2 >= Parameter
无论哪种情况:
如果最好将Variable2
设置为严格小于min(Parameter,Variable1)
/严格大于max(Parameter,Variable1)
,那么(除了上述约束之外)您还需要引入一个新的如果Parameter > Variable1
:
If there's a chance that it will be optimal to set Variable2
to something strictly less than min(Parameter,Variable1)
/ strictly greater than max(Parameter,Variable1)
, then you will also (in addition to the constraints above) need to introduce a new binary variable that equals 1 if Parameter > Variable1
:
Parameter - Variable1 <= M * NewVar
Variable1 - Parameter <= M * (1 - NewVar)
其中M
是一个大数字.因此,如果Parameter > Variable1
则NewVar
必须等于1,而如果Parameter < Variable1
则NewVar
必须等于0.
where M
is a large number. So, if Parameter > Variable1
then NewVar
must equal 1, while if Parameter < Variable1
then NewVar
must equal 0.
min(Parameter,Variable1)
:
min(Parameter,Variable1)
:
介绍确保Variable2 >= min(Parameter,Variable1)
的约束:
Variable2 >= Parameter - M * NewVar
Variable2 >= Variable1 - M * (1 - NewVar)
因此,如果Parameter > Variable1
然后NewVar = 1
,则第一个约束无效,而第二个约束表示Variable2 >= Variable1
.如果Parameter < Variable1
然后NewVar = 0
,则第一个约束表示Variable2 >= Parameter
,第二个约束无效.
So, if Parameter > Variable1
then NewVar = 1
, the first constraint has no effect, and the second says Variable2 >= Variable1
. If Parameter < Variable1
then NewVar = 0
, the first constraint says Variable2 >= Parameter
, and the second constraint has no effect.
max(Parameter,Variable1)
:
max(Parameter,Variable1)
:
介绍确保Variable2 <= max(Parameter,Variable1)
的约束:
Variable2 <= NewVar * Parameter + M * (1 - NewVar)
Variable2 <= Variable1 + M * NewVar
因此,如果Parameter > Variable1
然后NewVar = 1
,则第一个约束条件表示为Variable2 <= Parameter
,而第二个约束条件则无效.如果Parameter < Variable1
然后NewVar = 0
,则第一个约束无效,第二个约束表示Variable2 <= Variable1
.
So, if Parameter > Variable1
then NewVar = 1
, the first constraint says Variable2 <= Parameter
, and the second constraint has no effect. If Parameter < Variable1
then NewVar = 0
, the first constraint has no effect, and the second says Variable2 <= Variable1
.
无论哪种情况:
请注意,M
应该尽可能小,同时仍要确保在约束中触发M
会使约束不具有约束力.我认为将其设置为|Parameter - Variable1|
可能获得的最大值就足够了.通常,这些大质谱"会削弱配方,导致求解时间更长,因此您始终希望它们尽可能小.
Note that M
should be as small as possible while still ensuring that triggering the M
in the constraint makes the constraint non-binding. I think it's sufficient to set it equal to the largest value that |Parameter - Variable1|
can possibly get. In general these "big-Ms" weaken the formulation and result in longer solve times, so you always want them as small as possible.
这篇关于混合整数编程:每个条件的变量赋值(如果不是)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!