定义变量
x = LpVariable.dicts(CourseTimeSlot, [(course, time_slot) for course in courses for time_slot in time_slots], 0, 1, LpBinary)
目标函数
prob += lpSum([costs[(course, time_slot)] * x[(course, time_slot)] for course in courses for time_slot in time_slots])
添加约束
for teacher in teachers:
prob += lpSum([x[(course, time_slot)] for course in courses if teacher in course.teachers for time_slot in time_slots]) <= 1
for classroom in classrooms:
prob += lpSum([x[(course, time_slot)] for course in courses if classroom in course.classrooms for time_slot in time_slots]) <= 1
求解
prob.solve()
输出结果
for v in prob.variables():
if v.varValue == 1:
print(v.name)