线性规划条件约束

时间:2019-03-19 14:03:50

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

有22个驱动程序。每个驾驶员必须至少工作7.6个小时,并且最多可以工作10个小时。每个驱动程序的成本和生产率都不同。

如果某些驾驶员加班(超过7.6小时),则在开始的2小时内,我们需要支付1.5倍的费用。剩下的0.4小时,我们需要支付2倍。

195小时的工作必须由22位司机完成。我们需要以尽可能降低成本的方式进行调度。

Driver,Cost,Productivity
A,70,0.8
B,22,0.8
C,24,0.8
D,26,0.8
E,28,0.8
F,30,0.8
G,32,0.8
H,34,0.8
I,36,0.8
J,38,0.8
K,40,0.8
L,42,0.9
M,44,0.9
N,46,0.9
O,48,0.9
P,50,0.9
Q,52,0.9
R,54,0.9
S,56,0.9
T,58,0.9
U,60,0.9
V,62,0.5

决策变量:

X1,X2 ... X22代表分配给每个驾驶员的总小时数

目标功能:

最小Z = 20 * X1 + 22 * X2 ...... 62 * X22

约束:

X1> = 7.6,X2> = 7.6 .... X22> = 7.6

X1 <= 10,X2 <= 10 .... X22 <= 10

X1 + X2 ..... + X22 <= 195

到目前为止,我已经尝试了以下python程序。

import pulp
import pandas as pd


def main():
    model = pulp.LpProblem("Cost minimising scheduling problem", pulp.LpMinimize)

    totalHours = 192
    minHourEachDriver = 7.6
    maxHourEachDriver = 10

    # importing data from CSV

    drivers = pd.DataFrame.from_csv('csv/drivers.csv', index_col=['Driver', 'Cost', 'Productivity'])

    # Decision Variables
    drv = pulp.LpVariable.dicts("driverName", indexs=((i) for i, j, k in drivers.index), lowBound=0,
                                cat='Continuous')

    # Objective
    model += pulp.lpSum([j * (1 / k) * drv[i] for i, j, k in drivers.index]), "Cost"

    # Constraints

    # total no of hours work to be done
    model += pulp.lpSum([drv[i] for i, j, k in drivers.index]) == totalHours

    for i, j, k in drivers.index:
        # minimum hours driver has to work
        model += drv[i] >= minHourEachDriver
        # Maximum hour driver can work
        model += drv[i] <= maxHourEachDriver

    model.solve()

    # model status
    print(pulp.LpStatus[model.status])

    # Total Cost
    print(pulp.value(model.objective))

    # No of hrs allocated to each driver

    for i, j, k in drivers.index:
        var_value = drv[i].varValue
        # print(var_value)
        print("The number hours for driver {0} are {1}".format(i, var_value))


if __name__ == '__main__':
    main()

但是,我无法弄清楚我们如何放置以下约束。

  

如果某些驾驶员加班(超过7.6小时),则在开始的2小时内,我们   需要支付1.5倍。剩下的0.4小时,我们需要支付2倍。

1 个答案:

答案 0 :(得分:1)

If for each driver is mandatory to work 7.6h, there is no need to put it in the conditions. It is just static time (cost) that can be subtracted from total hours (costs) because it always happen:

195 - (NumDrivers * 7.6) = is the remaining time that need to be flexibly distributed between drivers as their overtimes to reach the 195 hours (when total hours > NumDrivers*7,6).

I would represent each driver with two variables (one for time working at 1.5 rate and second working time at double rate) and make following LP:

Xij = represents hours allocated to i-driver in j-working mode (let's say j=1 for 1,5 and j=2 for 2)

Based on the provided input file:


Min Z = 70*1,5*X11 + 70*2*X12 + 22*1,5*X21 + 22*2*X22 + ... 62*1,5*X221 + 62*2*X222

Constraints:

X11+X12+X21+X22+...X221+X222 = 27,8 (195 - (22*7,6))

X11+X12 <= 3,4 X21+X22 <= 3,4 ... X221+X222 <= 3,4

X11<=2 X21<=2 ... X221<=2

For the completeness there should be also set of conditions representing that each driver can start with j mode (2*) only after completing 2 hours at 1.5* but in this case objective function should make it automatically.