简体   繁体   English

问题不可行时的纸浆约束

[英]Pulp constraints when probelm is Infeasible

I am trying to solve a linear optimization problem using Pulp in Python.我正在尝试使用 Python 中的 Pulp 解决线性优化问题。

Here is the code:这是代码:

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)]})

Terminology: S_* is the production of a certain item, D_* is the demand of that item.术语:S_* 是某个项目的生产,D_* 是该项目的需求。 These are defined before the definition of the probelm.这些是在问题定义之前定义的。

This problem is not always feasible because sometimes production exceeds the time available.这个问题并不总是可行的,因为有时生产会超过可用时间。 In this case I would like the constraints on the cycles to be respected and the one on time should be broken in order to solve the problem.在这种情况下,我希望遵守对周期的限制,并且应该打破按时的限制以解决问题。

How can I achieve this?我怎样才能做到这一点?

Thanks, Carlotta.谢谢,卡洛塔。

They way I would do it is to add slack variables to the time limit constraint like so:他们这样做的方法是将松弛变量添加到时间限制约束中,如下所示:

First you add a new variable:首先添加一个新变量:

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

Then you penalize it in the objective function:然后你在目标 function 中惩罚它:

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

Finally you add it to your time limit constraint:最后,您将其添加到您的时间限制约束中:

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

you will then have a solution that violates the time constraint the least possible .然后,您将获得一个尽可能不违反时间限制的解决方案。 If you pick a good enough big_enough_number the model will only violate the time constraint if there is no other option.如果您选择了足够好的big_enough_number ,则 model 只会在没有其他选项的情况下违反时间限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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