简体   繁体   English

如何使用范围不等式向 pyomo 添加约束

[英]How do I add constraint to pyomo with a range inequality

I am working on a constraint optimization problem and I am using pyomo with abc solver and I am trying to add a constraint with a range.我正在研究一个约束优化问题,我正在使用 pyomo 和 abc 求解器,我正在尝试添加一个具有范围的约束。

The code I have written so far is到目前为止我写的代码是

# Initialize model
model = ConcreteModel()

# binary variables representing if a worker is scheduled somewhere
model.works = Var(((worker, day, shift) for worker in workers for day in date for shift in days_shifts[day]),
                  within=Binary, initialize=0)

# binary variables representing if a worker is necessary
model.needed = Var(workers, within=Binary, initialize=0)

def obj_rule(m):
    c = len(workers)
    return sum(m.needed[worker] for worker in workers)

model.obj = Objective(rule=obj_rule, sense=minimize)

 # Create a set of constraints
model.constraints = ConstraintList()

for day in date:
    for shift in days_shifts[day]:
        model.constraints.add(33 == sum(model.works[worker, day, shift] for worker in workers))                       

# Constraint: no more than 52 hours worked
for worker in workers:
    model.constraints.add(
        52 >= sum(12 * model.works[worker, day, shift] for day in date for shift in days_shifts[day]))

The constraint I am trying to add that the minimum hours of shift should be 8 hours and maximum hours of shift should be 12 hours.我试图补充的限制条件是,最少轮班时间应为 8 小时,而最长轮班时间应为 12 小时。 The total hours work in a week should not exceed 52 hours.一周的总工作时间不应超过 52 小时。 I am following the below article for optimized shift allocations.我正在关注以下文章以优化班次分配。

https://towardsdatascience.com/modeling-and-optimization-of-a-weekly-workforce-with-python-and-pyomo-29484ba065bb https://towardsdatascience.com/modeling-and-optimization-of-a-weekly-workforce-with-python-and-pyomo-29484ba065bb

The last constraint ensures of 12 hour shift, and I am not sure how to add a constraint for 8 hour shift.最后一个约束确保 12 小时轮班,我不确定如何为 8 小时轮班添加约束。

I am very new to pyomo and optimization problem.我对 pyomo 和优化问题很陌生。

Is the length of a shift something that is given ie a model parameter or something that you want your model to decide ie a decision variable?移位的长度是给定的,即模型参数还是您希望模型决定的,即决策变量?

Depending on the case, you will need to define either a parameter or a variable, respectively, indexed per [day, shift] to represent this.根据情况,您需要分别定义一个参数或一个变量,按 [day, shift] 进行索引以表示这一点。 If for instance, we call this shift_len[day, shift] , then your constraint will become:例如,如果我们称此为shift_len[day, shift] ,那么您的约束将变为:

for worker in workers:
    model.constraints.add(
        52 >= sum(shift_len[day, shift] * model.works[worker, day, shift] for day in date for shift in days_shifts[day]))

Note that if it is a decision variable, then your model becomes nonlinear due to the product of two variables (there are still ways to linearize the product, though).请注意,如果它是一个决策变量,那么由于两个变量的乘积,您的模型将变为非线性(不过,仍有一些方法可以使乘积线性化)。 If it is a parameter, then your model remains linear.如果它是一个参数,那么您的模型将保持线性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM