python - PuLP Python - 如何线性化涉及变量的不等式
问题描述
我正在研究应付铜问题,其目标函数是最大化一段时间内应付铜的总和,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。这被保留为一个变量,因为目的是随着时间的推移对库存进行建模,允许模型选择何时销售材料以最大化应付账款:
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
.
要得出这些限制,最好远离计算机并使用老式的笔和纸。
推荐阅读
- tampermonkey - Tampermonkey 的“存储”选项卡在哪里编辑存储内容?
- ruby - 尝试在 Ruby 中复制移动应用程序 POST 请求,出现 502 网关错误
- c++ - 正在跳过 cin 并且 cin.ignore 不起作用
- javascript - 使用 Javascript 更改类的 CSS 样式
- asp.net-core - 如何使用实体框架核心更新与普通 SQL 更新查询相同的多条记录列表?
- layout - 如何在颤动中制作自定义表格小部件?
- php - 当对数据库的调用位于包装器中时,PHP如何获取最后一个插入ID
- android - kotlin-runtime 和 kotlin-stdlib 之间的重复类
- python - ValueError:无法将字符串转换为浮点数:'(1/2)**576'
- arrays - Swift 枚举作为数组的索引