简体   繁体   中英

PuLP objective function syntax not right

I am having some issues with defining my objective function for a scheduling optimization problem. I simply want to minimize undercapacity. My setup:

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

Assignment matrix

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

Demand matrix This is a pandas dataframe I import from an Excel. It has simply three columns

  • 'Weekday' - the weekday (1-5)
  • 'Hour'- the specific hour slot (0-23)
  • 'Demand' - the required resources for the combination

I transform the demand dataframe to a pulp dict:

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

But now I want to create the objective function like this:

# 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

I want to substract the demand value from X (sum of employees for X value) but I have the feeling that this syntax is incorrect to program the following formula:

在此处输入图片说明

Could you help me with the right syntax for this formula?

You can model the objective function by writing:

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)

this leads to the same objective as writing:

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

prob+= obj # or  prob.setObjective(obj)

Example:

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)

which results in:

MINIMIZE
-1*D_(0,_0) + -1*D_(0,_1) + -1*D_(1,_0) + -1*D_(1,_1) + 1*X_(0,_0,_0,_0) + 1*X_(0,_0,_0,_1) + 1*X_(0,_0,_1,_0) + 1*X_(0,_0,_1,_1) + 1*X_(0,_1,_0,_0) + 1*X_(0,_1,_0,_1) + 1*X_(0,_1,_1,_0) + 1*X_(0,_1,_1,_1) + 1*X_(1,_0,_0,_0) + 1*X_(1,_0,_0,_1) + 1*X_(1,_0,_1,_0) + 1*X_(1,_0,_1,_1) + 1*X_(1,_1,_0,_0) + 1*X_(1,_1,_0,_1) + 1*X_(1,_1,_1,_0) + 1*X_(1,_1,_1,_1) + 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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