您好我使用工具或cp-sat模型来解决一个调度(时间表)问题。
我的模型如下
我试着写下下面的约束条件,当两门课在同一天的时候,把它们粘在一起
def __lang_special_day_constraint_sticked(self, filtered_lessons, rooms, filtered_timeslots):
for l in filtered_lessons:
k = 0
for r in rooms:
for i in range(len(filtered_timeslots)):
if i != 0 and i != len(filtered_timeslots)-1 and (l.lesson['id'],r['code'],filtered_timeslots[i]) in self.tables and (l.lesson['id'],r['code'],filtered_timeslots[i+1]) in self.tables and (l.lesson['id'],r['code'],filtered_timeslots[i-1]) in self.tables:
self.model.AddBoolOr([self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i-1])],self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i+1])]]).OnlyEnforceIf(self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i])])
if self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i])] == True:
break
elif i == 0 and (l.lesson['id'],r['code'],filtered_timeslots[i]) in self.tables and (l.lesson['id'],r['code'],filtered_timeslots[i+1]) in self.tables:
self.model.AddBoolOr([self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i+1])]]).OnlyEnforceIf(self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i])])
if self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i])] == True:
break
elif i == (len(filtered_timeslots) - 1) and (l.lesson['id'],r['code'],filtered_timeslots[i]) in self.tables and (l.lesson['id'],r['code'],filtered_timeslots[i-1]) in self.tables:
self.model.AddBoolOr([self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i-1])]]).OnlyEnforceIf(self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i])])
if self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i])] == True:
break
我的目标是实现这一部分:
if self.tables[(l.lesson['id'],r['code'],filtered_timeslots[i])] == True:
break
我知道这是行不通的,但我想要类似的东西,所以当这个文字是真的,我会打破循环,停止进一步的迭代。
如有任何帮助,我们将不胜感激。
暂无答案!
目前还没有任何答案,快来回答吧!