我有一些基于成本的供应链管理代码,其中供应尝试满足需求,但这仅在供应大于需求时才有效。我有什么办法可以优化它使其同时工作(例如,在supply > demand
和supply < demand
时)?
from pyomo.core import *
model = AbstractModel()
model.warehouses = Set()
model.stores = Set()
model.supply = Param(model.warehouses)
model.demand = Param(model.stores)
model.costs = Param(model.warehouses, model.stores)
model.amounts = Var(model.warehouses, model.stores,
within=NonNegativeReals)
def costRule(model):
return sum(
model.costs[n,i]
for n in model.warehouses
for i in model.stores
)
model.cost=Objective(rule=costRule)
def minDemandRule(model, store):
return sum(model.amounts[i, store] for i in model.warehouses) >=
model.demand[store]
model.demandConstraint = Constraint(model.stores, rule=minDemandRule)
def maxSupplyRule(model, warehouse):
return sum(model.amounts[warehouse, j] for j in model.stores) <=
model.supply[warehouse]
model.supplyConstraint = Constraint(model.warehouses, rule=maxSupplyRule)
我的输入:
set warehouses := hyd ban coh;
set stores := cbe mdu cnr whc whe;
param: supply :=
hyd 10
ban 10
coh 10;
param: demand :=
cbe 5
mdu 5
cnr 5
whc 5
whe 5;
param costs:
cbe mdu cnr whc whe:=
hyd 1 3 2 2 1
ban 4 5 1 1 3
coh 2 3 3 2 1;
这里supply = 30
和demand = 25
。它有效,我得到的输出是
变量:
amounts[ban,cbe]:
Value: 5
amounts[ban,whc]:
Value: 5
amounts[coh,whe]:
Value: 5
amounts[hyd,cnr]:
Value: 5
amounts[hyd,mdu]:
Value: 5
但是,当供应量少于需求量时,它就不起作用。
这里supply = 30
和demand = 40
:
param: supply :=
hyd 10
ban 10
coh 10;
param: demand :=
cbe 5
mdu 5
cnr 5
whc 5
whe 20;
param costs:
cbe mdu cnr whc whe:=
hyd 1 3 2 2 1
ban 4 5 1 1 3
coh 2 3 3 2 1;
我需要以下期望的输出: 变量:
amounts[ban,cbe]:
Value: 5
amounts[ban,whc]:
Value: 5
amounts[coh,whe]:
Value: 10
amounts[hyd,cnr]:
Value: 5
amounts[hyd,mdu]:
Value: 5
whe_demand =10
答案 0 :(得分:2)
由于约束model.demandConstraint
,当需求大于供应时,您的模型似乎不可行。装运数量不能满足需求,因为否则将违反其他约束model.supplyConstraint
。
如果要对缺乏需求满意度的情况进行惩罚,则可以为相关约束定义松弛变量,并将这些条件添加到目标中。
这本书可能是您的理想资源:https://www.wiley.com/en-us/Model+Building+in+Mathematical+Programming%2C+5th+Edition-p-9781118443330