在PuLP中使用四个维度设置目标功能

时间:2018-08-17 14:43:55

标签: python-3.x optimization linear-programming pulp integer-programming

我正在尝试为在不同职位,工作日和轮班工作的员工建立日程安排优化。我想根据时间表(分配矩阵)优化偏好得分。但是,我在定义目标函数时遇到了麻烦。我正在使用PuLP设置代码,这是我的设置:

from __future__ import print_function
import numpy as np
import pandas as pd
import pulp
from random import randint
from itertools import product

employees = range(11)
roles = range(5)
days = range(6)
shifts = range(3)

变量X是由0和1s组成的赋值矩阵

X = pulp.LpVariable.dicts("X", product(employees, roles, days, shifts),
                          cat=pulp.LpBinary)

P是偏好得分

for k in employees:
    for j in roles:
        for i in days: 
            for h in nr_shifts:
                P[(k, j, i,h)] = np.random.rand() - 0.5

问题,使偏好得分最大化:

scheduling_problem = pulp.LpProblem("Employee Scheduling", pulp.LpMaximize)

目标函数(导致错误)

scheduling_problem += (pulp.lpSum(X[(k, j, i, h)] * P[(k, j, i, h)])
        for k in employees for j in roles for i in days for h in shifts
)

运行目标函数后的错误:

TypeError: Can only add LpConstraintVar, LpConstraint, LpAffineExpression or True objects

我的目标是汇总员工,职位,工作日和轮班的偏好得分,我认为将我的偏好得分乘以分配(简单地为0和1)就可以解决问题。

对此有何想法?

Thnx

1 个答案:

答案 0 :(得分:0)

最后一条语句在Python中无效:检查括号。您可能是说:

scheduling_problem += pulp.lpSum(X[(k, j, i, h)] * P[(k, j, i, h)]
        for k in employees for j in roles for i in days for h in shifts)