[英]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.