Eigenvalue
Eigenvalue

Reputation: 1133

getting empty model pyomo

So for the life of me, I can't get my pyomo program to run.

I have my python file :

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)

plus .dat file :

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;

From my understanding of how this works, you sort of map things from one to another. This seems okay to me but when I run it.

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'"

I feel like the dat file isn't being read properly, but when I look at other examples, this is how it is done, so I'm a bit perplexed.

Upvotes: 1

Views: 866

Answers (1)

Timofey Chernousov
Timofey Chernousov

Reputation: 1294

there was several issues in you code from typos to wrong pyomo usage. Below is fixed version. If it is work for you and you have more specific questions, please post new question for it.

File 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)

File 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;

Example run (note I am using CLP solver here, but is not principal):

$ 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
$ 

Upvotes: 1

Related Questions