得到空模型pyomo

时间:2017-11-03 00:32:14

标签: python pyomo

所以对于我的生活,我无法让我的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文件没有被正确读取,但是当我看到其他示例时,这就是它的完成方式,所以我有点困惑。

1 个答案:

答案 0 :(得分:1)

您的代码中存在几个问题,从错别字到错误的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
$