纸浆目标函数语法不正确

2024-05-19 01:12:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在为调度优化问题定义目标函数时遇到了一些问题。我只是想尽量减少产能不足。我的设置:

import pulp

nr_employees = 61 #len(dfRolePreferences['Naam'])
nr_shifts = 3
nr_roles = 5 #len(dfRolePreferences.columns) - 1
nr_days = 5

employees = range(1,nr_employees + 1)
roles = range(1, nr_roles + 1)
days = range(1, nr_days + 1)
shifts = range(1, nr_shifts + 1)
hours = range(24) #Export proces

D = {}          # Demand matrix
X = {}          # Assignment matrix

分配矩阵

^{pr2}$

需求矩阵 这是我从Excel导入的熊猫数据框。它只有三列

  • “工作日”-工作日(1-5)
  • “小时”-特定的时间段(0-23)
  • “需求”-组合所需的资源

我将需求数据帧转换为一个简单的dict:

for d in days: 
    for h in hours:
        D[(d, h)] = pulp.LpVariable(int(dfDemand.loc[(dfDemand['Weekday']==d) & (dfDemand['Hour']==h), "Demand"]))

但现在我想创建这样的目标函数:

# Create the problem
scheduling_problem = pulp.LpProblem("Employee Scheduling", pulp.LpMinimize)

obj = None

for d in days:
    for h in hours: 
        obj += (sum(X[(e,d,s,h)] for e in employees for s in shifts) - D[(d,h)])

scheduling_problem += obj
scheduling_problem

我想从X(X值的雇员总数)中减去demand值,但我觉得这个语法不正确,无法编写以下公式:

enter image description here

你能帮我解释一下这个公式的正确语法吗?在


Tags: inobj目标forrangedaysnrpulp
1条回答
网友
1楼 · 发布于 2024-05-19 01:12:55

您可以通过以下方式对目标函数进行建模:

prob += pulp.lpSum(pulp.lpSum([X[(e,d,s,h)] for e in employees for s in shifts] - D[(d,h)]) for d in days for h in hours)

这与写作有着同样的目的:

^{pr2}$

示例:

import pulp
import itertools

employees = range(2)
days = range(2)
shifts = range(2)
hours = range(2) 


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


prob = pulp.LpProblem("example", pulp.LpMinimize)

prob+= pulp.lpSum(pulp.lpSum([X[(e,d,s,h)] for e in employees for s in shifts] - D[(d,h)]) for d in days for h in hours)

结果是:

^{4}$

相关问题 更多 >

    热门问题