我正在尝试使用 Pulp 解决教授/班级分配问题。下面是我的代码的简化示例。在示例中,每年有 12 个不同的科目(“Maths_1”,代表数学第一年)分配给 3 个不同的组(A、B、C)。共有 36 个班级分配给 9 位教授(每个教授 4 个班级)。我想尽量减少教授必须提供的不同科目的数量。这是:必须为教授分配 4 个类,然后,例如,Maths_1_A、Maths_1_B、Maths_1_C 和 Programming_1A 只涉及两个不同的科目(Maths_1 和 Programming_1),是比 Maths_1_A、Maths_2_A、Physics_2_A、Physics_1_B、Chemistry_3_A 更好的选择4 个不同的科目(Maths_1、Maths_2、Physics_1、Chemistry_3)。
from itertools import product
import pulp
subjects=['Maths_1','Maths_2','Maths_3', 'Physics_1','Physics_2','Physics_3',
'Quemistry_1', 'Quemistry_2', 'Quemistry_3',
'Programming_1', 'Programming_2', 'Programming_3']
groups=['A','B','C']
clases=[a[0]+'_'+a[1] for a in product(subjects, groups)]
professors=['professor'+str(i) for i in range(1,10)]
number_of_clases_per_professor=4
model=pulp.LpProblem('Class assignmnet', sense=pulp.LpMaximize)
assign={(prof, clas): pulp.LpVariable('prof_%r_class_%r'%(prof, clas), cat=pulp.LpBinary)
for prof in professors
for clas in clases}
#CONSTRAINTS
# 1. Each "class" has to be assigned exactly once:
for clas in clases:
model.addConstraint(sum(assign[(prof, clas)] for prof in professors)==1)
#2. The number of classes per professor cannot exceed 4
for prof in professors:
model.addConstraint(sum(assign[(prof, clas)] for clas in clases)<=4)
我遇到的问题是定义目标函数。我只能考虑纸浆变量分配的条件:
obj=0
for prof in professors:
subjects_for_prof=[]
for subject in subjects:
for group in groups:
clas=subject+'_'+group
if assign[(prof, clas)]:
if subject not in subjects_for_prof:
subjects_for_prof.append(subject)
obj+=len(subjects_for_prof)
model+=obj
问题是:如何创建一个目标函数来计算教授分配的不同科目数量?