简体   繁体   English

Pulp 变量的条件

[英]Conditionals on Pulp variables

I am trying to solve a professor/class asignment problem using Pulp.我正在尝试使用 Pulp 解决教授/班级分配问题。 Below is a simplified example of my code.下面是我的代码的一个简化示例。 In the example there are 12 different subjects/year ('Maths_1', stands for Maths 1st year) to be given to 3 different groups (A, B, C).在示例中,每年有 12 个不同的科目('Maths_1',代表数学第一年)被分配给 3 个不同的组(A、B、C)。 There are a total of 36 classes to be asigned to 9 professors (4 classes each).共有36个班级分配给9位教授(每班4个班级)。 I want to minimize the number of different subjects a professor has to give.我想尽量减少教授必须教授的不同科目的数量。 This is: a professor has to be assigned 4 clases, then, for example, Maths_1_A, Maths_1_B, Maths_1_C & Programming_1A involves only two different subjects (Maths_1, and Programming_1) and is a better option than Maths_1_A, Maths_2_A, Physics_1_B, Chemistry_3_A that involves 4 different subjects (Maths_1, Maths_2, Physics_1, Chemistry_3).这是:必须为教授分配 4 个课程,然后,例如,Maths_1_A、Maths_1_B、Maths_1_C 和 Programming_1A 仅涉及两个不同的科目(Maths_1 和 Programming_1),并且比涉及的 Maths_1_A、Maths_2_A、Physics_1_B、Chemistry_3_A 更好4 个不同的科目(Maths_1、Maths_2、Physics_1、Chemistry_3)。 I try to do this by defining an objective function that is the sum of the number of different subjects a professor is assigned.我尝试通过定义一个目标 function 来做到这一点,该目标是教授分配的不同科目数量的总和。

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)

The problem I am having is in defining the objective function. I can only think in terms of conditionals on the pulp variable assign:我遇到的问题是定义目标 function。我只能根据纸浆变量赋值的条件来思考:

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

The question is: how can I make an objective function that counts the different number of subjects a professor is assigned?问题是:我怎样才能制定一个目标 function 来计算分配给教授的不同科目数量?

I think you will make life easier by keeping a 3-component index for your primary assignment variables:我认为通过为主要赋值变量保留一个由 3 个部分组成的索引会让生活变得更轻松:

assign={(prof, subject, group): pulp.LpVariable('prof_%r_subj_%r_grp_%r'%(prof, subj, grp), cat=pulp.LpBinary)
       for prof in professors
       for subj in subjects
       for grp in groups}

If you want to count the number of different subjects a professor is assigned to teach then you can introduce a specific set of binary variables:如果你想计算一位教授被分配教授的不同科目的数量,那么你可以引入一组特定的二元变量:

assign_subj={(prof, subject): pulp.LpVariable('prof_%r_subj_%r'%(prof, subj), cat=pulp.LpBinary)
           for prof in professors
           for subj in subjects}

And you can then set up constraints which in pseudocode are something like:然后您可以设置伪代码中的约束,例如:

for prof in professors:
    for subj in subjects:
        model += pulp.lpSum([assign[(prof, subj, grp)] for grp in groups]) <= assign_subj[(prof, subj)]*max_no_groups

In this last set of constraints you'd need to set max_no_groups to the maximum expected number of groups for any subject.在最后一组约束中,您需要将max_no_groups设置为任何主题的最大预期组数。 This constraint will mean that for any particular prof to have any assignments to a particular subj the appropriate assign_subj variable will have to be set to 1. You can then count these or do whatever you want with them in your objective.此约束将意味着对于任何特定prof要对特定assign_subj subj必须设置为 1。然后您可以计算这些或在您的目标中对它们进行任何您想做的事情。

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

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