探针不可行时的纸浆约束

时间:2020-07-06 10:25:23

标签: python optimization constraints pulp minimization

我正在尝试使用Python中的Pulp解决线性优化问题。

代码如下:

import pandas as pd
import pulp

D_XB = 20
D_XP = 0
D_XC = 0

Available_Time = 1440 #in minutes

test = [['A1', 'A2', 'A3', 'A4', 'A5'], [1,2,1,0,3], [16,32,0,16,32], [10,10,10,10,10], [120,210,180,180,350]]

Cycles = pd.DataFrame(test, index=['Cycles', 'QTA1', 'QTA2', 'QTA3', 'T_TOT']).T

A1 = pulp.LpVariable("Cycle_A1", lowBound=0, cat='Integer')
A2 = pulp.LpVariable("Cycle_A2", lowBound=0, cat='Integer')
A3 = pulp.LpVariable("Cycle_A3", lowBound=0, cat='Integer')
A4 = pulp.LpVariable("Cycle_A4", lowBound=0, cat='Integer')
A5 = pulp.LpVariable("Cycle_A5", lowBound=0, cat='Integer')
    
# Defining the problem as a minimization problem (Minimize Storage)
problem_5 = pulp.LpProblem("Storage_Minimization_3", pulp.LpMinimize)

S_XB = pulp.lpSum((Cycles.iloc[0]["QTA1"])*A1 + (Cycles.iloc[1]["QTA1"])*A2 + (Cycles.iloc[2]["QTA1"])*A3 + (Cycles.iloc[3]["QTA1"])*A4 + (Cycles.iloc[4]["QTA1"])*A5)
S_XP = pulp.lpSum((Cycles.iloc[0]["QTA2"])*A1 + (Cycles.iloc[1]["QTA2"])*A2 + (Cycles.iloc[2]["QTA2"])*A3 + (Cycles.iloc[3]["QTA2"])*A4 + (Cycles.iloc[4]["QTA2"])*A5)
S_XC = pulp.lpSum((Cycles.iloc[0]["QTA3"])*A1 + (Cycles.iloc[1]["QTA3"])*A2 + (Cycles.iloc[2]["QTA3"])*A3 + (Cycles.iloc[3]["QTA3"])*A4 + (Cycles.iloc[4]["QTA3"])*A5)

Tot_Time = pulp.lpSum((Cycles.iloc[0]["T_TOT"])*A1 + (Cycles.iloc[1]["T_TOT"])*A2 + (Cycles.iloc[2]["T_TOT"])*A3 + (Cycles.iloc[3]["T_TOT"])*A4 + (Cycles.iloc[4]["T_TOT"])*A5)

Stock_XB = (S_XB - D_XB)
Stock_XP = (S_XP - D_XP)
Stock_XC = (S_XC - D_XC)

problem_5 += Stock_XB + Stock_XP + Stock_XC
    
# Constraints: Time constraint present
problem_5 += S_XB >= D_XB
problem_5 += S_XP >= D_XP
problem_5 += S_XC >= D_XC
problem_5 += A1 >= 0
problem_5 += A2 >= 0
problem_5 += A3 >= 0
problem_5 += A4 >= 0
problem_5 += A5 >= 0
problem_5 += Tot_Time <= Available_Time
    
# Solving the probelm
status = problem_5.solve()

result = pd.DataFrame({'A1':[pulp.value(A1)], 'A2':[pulp.value(A2)], 'A3':[pulp.value(A3)], 'A4':[pulp.value(A4)], 'A5':[pulp.value(A5)], 
                       'Demand XB':[D_XB], 'Demand XP':[D_XP], 'Demand XC':[D_XC],  'Minimum storage':[pulp.value(problem_5.objective)], 
                       'Stock_XB':[pulp.value(Stock_XB)], 'Stock_XP':[pulp.value(Stock_XP)], 'Stock_XC':[pulp.value(Stock_XC)], 
                       'Total Time needed':[pulp.value(Tot_Time)]})

术语: S_ *是某个项目的产生, D_ *是该项目的需求。 这些是在定义探针之前定义的。

此问题并非总是可行的,因为有时产量超过了可用时间。在这种情况下,我希望遵守对周期的约束,并且应该打破一个准时以解决问题。

我该如何实现?

谢谢, 卡洛塔。

1 个答案:

答案 0 :(得分:1)

我这样做的方法是将松弛变量添加到时间限制约束中,如下所示:

首先,您添加一个新变量:

#(...)
slack_time = pulp.LpVariable("slack_time", lowBound=0, cat=pulp.LpContinuous)
#(...)

然后在目标函数中对其进行惩罚:

#(...)
big_enough_number = 10000
problem_5 += Stock_XB + Stock_XP + Stock_XC + slack_time*big_enough_number
#(...)

最后,将其添加到时间限制中:

#(...)
problem_5 += Tot_Time - slack_time <= Available_Time
#(...)

您将获得一个解决方案,该方案违反了最不可能的时间限制。如果您选择足够好的big_enough_number,则该模型只会在没有其他选择的情况下违反时间限制。