gpt4 book ai didi

python - PYOMO 优化以满足供给 < 需求

转载 作者:行者123 更新时间:2023-12-01 01:18:36 26 4
gpt4 key购买 nike

我有一些基于成本的供应链管理代码,其中供应尝试满足需求,但这仅在供应大于需求时才有效。有什么方法可以优化它以两种方式工作(即当 supply > demandsupply < 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 = 30demand = 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 = 30demand = 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

最佳答案

由于 model.demandConstraint 约束,当需求大于供给时,您的模型似乎不可行。发货数量无法满足需求,否则将违反另一个约束 model.supplyConstraint

如果您想惩罚需求满足不足的情况,那么您可以为相关约束定义松弛变量,并将这些项添加到目标中。

这本书对您来说可能是一个很好的资源:https://www.wiley.com/en-us/Model+Building+in+Mathematical+Programming%2C+5th+Edition-p-9781118443330

关于python - PYOMO 优化以满足供给 < 需求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54079713/

26 4 0
文章推荐: jquery - 超出的最大上传文件大小将被忽略
文章推荐: javascript - 从 SAPUI5 中的