gpt4 book ai didi

python - 获取空模型 pyomo

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:04 25 4
gpt4 key购买 nike

所以在我的一生中,我无法运行我的 pyomo 程序。

我有我的 python 文件:

from pyomo.environ import *
#pyomo solve --solver=glpk diet.py diet.dat
model = AbstractModel()

# Foodss

model.m = Param(within=NonNegativeIntegers)
model.n = Param(within=NonNegativeIntegers)

model.warehouses = RangeSet(1, model.m)
model.stores = RangeSet(1, model.n)




model.cost = Param(model.warehouses,model.stores)
model.a = Param(model.warehouses)
model.b = Param(model.warehouses)
model.d = Param(model.stores)
model.amounts = Var(model.warehouses, model.stores, within = NonNegativeIntegers)
model.pprint()






# Minimize the cost of food that is consumed
def cost_rule(model):
return sum(
model.cost[n,i] * model.amounts[n,i]
for n in model.warehouses
for i in model.stores
)
model.cost = Objective(rule=cost_rule)

def minDemandRule(store, model):
return sum(model.a[i]*model.amounts[i, store] for i in model.warehouses) >= model.d[store]
model.demandConstraint = Constraint(model.stores, rule=minDemandRule)
# Limit the volume of food consumed
def maxSupplyRule(warehouse,model):
return sum(model.amounts[warehouses,j] for j in model.stores) <= self.b[warehouse]
model.supplyConstraint = Constraint(model.warehouses, rule=maxSupplyRule)

加上.dat 文件:

set warehouses := warehouseone warehousetwo warehousethree  warehousefour;
set stores := storeone storetwo storethree storefour storefive storesix;

param cost:
storeone storetwo storethree storefour storefive storesix :=
warehouseone 23 12 34 25 27 16
warehousetwo 29 24 43 35 28 19
warehousethree 43 31 52 36 30 21
warehousefour 54 36 54 46 34 27;



param m := 4 ;
param n := 6 ;

param: a:=
warehouseone 15
warehousetwo 25
warehousethree 40
warehousefour 70;


param: b :=
warehouseone 10
warehousetwo 5
warehousethree 7
warehousefour 4;


param: d :=
storeone 45
storetwo 120
storethree 165
storefour 214
storefive 64
storesix 93;

根据我对其工作原理的理解,您可以将事物从一种映射到另一种。这对我来说似乎没问题,但是当我运行它时。

pyomo solve --solver=glpk transport.py data.dat 

[ 0.00] Setting up Pyomo environment
[ 0.00] Applying Pyomo preprocessing actions
2 Set Declarations
amounts_index : Dim=0, Dimen=2, Size=0, Domain=None, Ordered=True, Bounds=None
Virtual
cost_index : Dim=0, Dimen=2, Size=0, Domain=None, Ordered=True, Bounds=None
Virtual

2 RangeSet Declarations
stores : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=True, Bounds=None
Not constructed
warehouses : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=True, Bounds=None
Not constructed

6 Param Declarations
a : Size=0, Index=warehouses, Domain=Any, Default=None, Mutable=False
Not constructed
b : Size=0, Index=warehouses, Domain=Any, Default=None, Mutable=False
Not constructed
cost : Size=0, Index=cost_index, Domain=Any, Default=None, Mutable=False
Not constructed
d : Size=0, Index=stores, Domain=Any, Default=None, Mutable=False
Not constructed
m : Size=1, Index=None, Domain=NonNegativeIntegers, Default=None, Mutable=False
Not constructed
n : Size=1, Index=None, Domain=NonNegativeIntegers, Default=None, Mutable=False
Not constructed

1 Var Declarations
amounts : Size=0, Index=amounts_index
Not constructed

11 Declarations: m n warehouses stores cost_index cost a b d amounts_index amounts
WARNING: Implicitly replacing the Component attribute cost (type=<class 'pyomo.core.base.param.IndexedParam'>) on block unknown with a new Component (type=<class 'pyomo.core.base.objective.SimpleObjective'>).
This is usually indicative of a modelling error.
To avoid this warning, use block.del_component() and block.add_component().
[ 0.01] Creating model
ERROR: Constructing component 'a' from data={'warehousefour': 70, 'warehouseone': 15, 'warehousethree': 40, 'warehousetwo': 25} failed:
RuntimeError: Failed to set value for param=a, index=warehousefour, value=70.
source error message="Error setting parameter value: Index 'warehousefour' is not valid for array Param 'a'"
[ 0.02] Pyomo Finished
ERROR: Unexpected exception while running model:
Failed to set value for param=a, index=warehousefour, value=70.
source error message="Error setting parameter value: Index 'warehousefour' is not valid for array Param 'a'"

我觉得dat文件没有被正确读取,但是当我查看其他示例时,这就是它的完成方式,所以我有点困惑。

最佳答案

您的代码中存在几个问题,从拼写错误到错误的 pyomo 使用。以下是固定版本。如果它适合您并且您有更具体的问题,请为此发布新问题。

文件diet.py:

from pyomo.environ import *
#pyomo solve --solver=glpk diet.py diet.dat
model = AbstractModel()

# Foodss

model.warehouses = Set()
model.stores = Set()


model.a = Param(model.warehouses)
model.b = Param(model.warehouses)
model.d = Param(model.stores)
model.cost = Param(model.warehouses, model.stores)
model.amounts = Var(model.warehouses, model.stores, within = NonNegativeIntegers)
model.pprint()


# Minimize the cost of food that is consumed
def cost_rule(model):
return sum(
model.cost[n,i] * model.amounts[n,i]
for n in model.warehouses
for i in model.stores
)
model.costObjective = Objective(rule=cost_rule)

def minDemandRule(model, store):
return sum(model.a[i]*model.amounts[i, store] for i in model.warehouses) >= model.d[store]
model.demandConstraint = Constraint(model.stores, rule=minDemandRule)

# Limit the volume of food consumed
def maxSupplyRule(model, warehouse):
return sum(model.amounts[warehouse,j] for j in model.stores) <= model.b[warehouse]
model.supplyConstraint = Constraint(model.warehouses, rule=maxSupplyRule)

文件diet.dat:

param: warehouses:
a b :=
warehouseone 15 10
warehousetwo 25 5
warehousethree 40 7
warehousefour 70 4;


param: stores:
d :=
storeone 45
storetwo 120
storethree 165
storefour 214
storefive 64
storesix 93;


param cost:
storeone storetwo storethree storefour storefive storesix :=
warehouseone 23 12 34 25 27 16
warehousetwo 29 24 43 35 28 19
warehousethree 43 31 52 36 30 21
warehousefour 54 36 54 46 34 27;

示例运行(请注意,我在这里使用 CLP 求解器,但不是主要的):

$ pyomo solve --solver=clp test.py test.dat
[ 0.00] Setting up Pyomo environment
[ 0.00] Applying Pyomo preprocessing actions
4 Set Declarations
amounts_index : Dim=0, Dimen=2, Size=0, Domain=None, Ordered=False, Bounds=None
Virtual
cost_index : Dim=0, Dimen=2, Size=0, Domain=None, Ordered=False, Bounds=None
Virtual
stores : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
Not constructed
warehouses : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
Not constructed

4 Param Declarations
a : Size=0, Index=warehouses, Domain=Any, Default=None, Mutable=False
Not constructed
b : Size=0, Index=warehouses, Domain=Any, Default=None, Mutable=False
Not constructed
cost : Size=0, Index=cost_index, Domain=Any, Default=None, Mutable=False
Not constructed
d : Size=0, Index=stores, Domain=Any, Default=None, Mutable=False
Not constructed

1 Var Declarations
amounts : Size=0, Index=amounts_index
Not constructed

9 Declarations: warehouses stores a b d cost_index cost amounts_index amounts
[ 0.00] Creating model
[ 0.02] Applying solver
[ 0.03] Processing results
Number of solutions: 1
Solution Information
Gap: None
Status: optimal
Function Value: 532.113571429
Solver results file: results.json
[ 0.04] Applying Pyomo postprocessing actions
[ 0.04] Pyomo Finished
$

关于python - 获取空模型 pyomo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47086708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com