gpt4 book ai didi

python - 实现 LHS 不是二进制的蕴涵?

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:50 24 4
gpt4 key购买 nike

尝试在 Python + Gurobi 中实现指标约束,其中指标(LHS)是二元决策变量的总和。

嗨,我想在 Python + Gurobi 中实现以下功能:

Y_i_d and U_d are binary decision variables:
Y_i_d = model.addVars(passengers, drivers, vtype=grb.GRB.BINARY, name = "Y")
U0_d = model.addVars( drivers, vtype=grb.GRB.BINARY, name = "U0")
U1_d = model.addVars( drivers, vtype=grb.GRB.BINARY, name = "U1")
U2_d = model.addVars( drivers, vtype=grb.GRB.BINARY, name = "U2")
U3_d = model.addVars( drivers, vtype=grb.GRB.BINARY, name = "U3")

我想以某种方式得到以下暗示:

model.addConstr((U0_d[d]+U1_d[d]+U2_d[d]+U3_d[d])==1)
model.addConstr( (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 0) >> (U0_d[d] == 1))
model.addConstr( (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 1) >> (U1_d[d] == 1))
model.addConstr( (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 2) >> (U2_d[d] == 1))
model.addConstr( (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 3) >> (U3_d[d] == 1))

但是,这不起作用,因为指示符约束将指示符变量声明为二进制类型。有解决方法吗?稍后我必须使用 U_d 变量来定义析取约束。

提前谢谢您!

最佳答案

尝试向后写约束:

model.addConstr((U0_d[d] == 1) >> (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 0))
model.addConstr((U1_d[d] == 1) >> (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 1))
model.addConstr((U2_d[d] == 1) >> (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 2))
model.addConstr((U3_d[d] == 1) >> (grb.quicksum(Y_i_d[ i, d] for i in passengers) == 3))

关于python - 实现 LHS 不是二进制的蕴涵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56984167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com