[英]PuLP Python - How to linearize an inequality involving a variable
我正在研究应付铜问题,其中目标 function 是在一段时间内最大化应付铜的总和,T。
是否可以制定一个约束来动态地比较一个变量与两个常量之间的关系:
IE
lower_bound, operator_1 (>, >=), variable, operator_2(<, <=) up_bound
问题描述
应付吨的总量,即客户将支付的金额取决于销售材料的铜含量。
import io
import pandas as pd
customer_payables = """customer, tier, specvalue_1, specoperator_1, specvalue_2, \
specoperator_2, coeff
'abc', 1, 0, '>=', 20, '<=', 96.0
'abc', 2, 20, '>', 24, '<=', 96.5
'abc', 3, 24, '>', 100, '<=', 96.65
'def', 1, 0, '>=', 20, '<=', 96.0
'def', 2, 20, '>=', 22, '<=', 96.66
'def', 3, 22, '>=', 100', '<=', 97.0
"""
_cust_data = io.StringIO(customer_payables)
cust_df = pd.read_csv(_cust_data, sep=",")
cust_df = cust_df.set_index('customer')
cust_df
##库存数据
stockpile_data_dict = {
'Warehouse 1':{
'Stockpile 1': {'cu': 27},
'Stockpile 2': {'cu': 18}
},
'Warehouse 2': {
'Stockpile 1':{'cu': 22},
'Stockpile 2': {'cu': 16}}}
stockpile_df = pd.concat({k: pd.DataFrame(v).T for k, v in stockpile_data_dict.items()}, axis=0)
stockpile_df
问题我创建了一个变量来表示每个仓库的铜浓度,即 stockpile。 这保留为变量,因为目的是随着时间的推移 model 库存,允许 model 选择何时出售材料以最大化应付账款:
cu_spec_of_sale_material = pulp.LpVariable.dicts(
'Copper spec of sale material',
((warehouse, stockpile)
for warehouse, stockpile in stockpile_df.index),
cat='Continuous')
如何创建一个线性约束,以返回关于此变量的铜浓度 VALUE 的正确应付系数?
在伪代码术语中,它评估如下内容:
for customer, effective_tier in effective_payable_coefficient:
if customer_lower_bound_val < cu_spec_sales_material[warehouse, stockpile] < customer_up_bound_val:
PULP += effective_payable_coefficient[customer, effective_tier] == 1
我不经常使用 Pulp,所以请多多包涵。
感谢所有帮助,谢谢。
我认为您正在寻找含义的线性公式:
a < x < b => y = 1
其中 a,b 是常数,x 是连续变量,y 是二进制变量。
我们可以这样写:
x ≤ a + M1 ⋅ δ + M1 ⋅ y
x ≥ b - M2 ⋅ (1-δ) - M2 ⋅ y
δ,y ∈ {0,1}
x ∈ [L,U]
M1 = U-a
M2 = b-L
δ
是另一个二元变量, L
, U
是x
的下限/上限, M1
, M2
是常数。
直觉:这些约束实现了含义:
y = 0 => x ≤ a or x ≥ b
这意味着如果a < x < b
我们必须有y=1
。
要得出这些限制,最好远离计算机并使用老式的笔和纸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.