简体   繁体   English

如何在多个索引上正确编写约束?

[英]How to properly write constraints over multiple indexes?

I am trying to implement an employee (nurse) scheduling problem and seek some advice on how to implement a specific constraint.The problem is as follows: There is a set of employees and days (both labeled by integer numbers).我正在尝试实施员工(护士)调度问题,并就如何实施特定约束寻求一些建议。问题如下:有一组员工和天数(均由 integer 数字标记)。 Each employee can be assigned a day shift D[(i, j)], nightshift N[(i, j)] or a day off V[(i, j)].可以为每个员工分配白班 D[(i, j)]、夜班 N[(i, j)] 或休息日 V[(i, j)]。 These are my decision variables:这些是我的决策变量:

D = LpVariable.dicts(name="Dagdienst", indexs=[(i, j) for i in employees for j in days], cat='Binary')
N = LpVariable.dicts(name="Nachtdienst", indexs=[(i, j) for i in employees for j in days], cat='Binary')
V = LpVariable.dicts(name="Vrij", indexs=[(i, j) for i in employees for j in days], cat='Binary')

An example constraint to produce sensible schedules is the following.产生合理时间表的示例约束如下。

for i in employees:
    for j in days:
        m += D[(i, j)] + N[(i, j)] + V[(i, j)] == 1

Another constraint that does seem to have effect (ie no employees were assigned day shifts the following two days after a night shift after implementing this constraint)另一个似乎确实有效的约束(即在实施此约束后,在夜班后的两天内没有为员工分配白班)

for i in employees:
    for j in range(1, len(days)-1):
        m += N[(i, j)] + D[(i, (j + 1))] <= 1
        m += N[(i, j)] + D[(i, (j + 2))] <= 1

I hope the implementation of the aforementioned constraint speaks for itself.我希望上述约束的实施不言自明。 If clarification is needed please ask!如果需要澄清,请询问!

In the same fashion I am trying to enforce a constraint that there is a maximum numbers of consecutive days an employee can (day) shifts assigned.以同样的方式,我试图强制执行一个约束,即员工可以分配(天)班次的最大连续天数。

max_consecutive_days = 4
for i in medewerkers:
    for j in range(1, (len(dagen)+1 - max_consecutive_days)):
        m += D[(i, j)] + D[(i, j + 1)] + D[(i, j + 2)] + D[(i, j + 3)] <= max_consecutive_days

However, this does not seem to have any effect on the produced schedules as employees are still assigned 4+ days in a row.但是,这似乎对生成的时间表没有任何影响,因为员工仍被连续分配 4 天以上。

What am I missing here?我在这里想念什么? Thanks in advance!提前致谢!

You can see that你可以看到

D[(i, j)] + D[(i, j + 1)] + D[(i, j + 2)] + D[(i, j + 3)] <= 4

is never binding.从不具有约束力。 I think you want:我想你想要:

D[(i, j)] + D[(i, j + 1)] + D[(i, j + 2)] + D[(i, j + 3)] + D[(i, j + 4)] <= 4

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

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