I'm trying to set up a parameter for a Pyomo model, but I keep getting an error that the too many arguments were given and I cannot figure out why.
from pyomo.environ import *
model = ConcreteModel()
model.x = Set(initialize=['apple', 'orange', 'pineapple', 'jelly', 'broccoli'])
model.y = Set(initialize=[('1','2'), ('1','3'), ('2','1'), ('2','3'), ('3','1'), ('3','2')])
model.testing = Var(model.x, model.y, bounds=(0,100), within=NonNegativeIntegers)
fruit = {
('1','2'): {'apple': 7,'orange': 13,'pineapple': 30, 'jelly': 17,'broccoli': 20},
('1','3'): {'apple': 8, 'orange': 14, 'pineapple': 30, 'jelly': 16, 'broccoli': 21},
('2','1'): {'apple': 9, 'orange': 15, 'pineapple': 31, 'jelly': 15, 'broccoli': 22},
('2','3'): {'apple': 10, 'orange': 16, 'pineapple': 31, 'jelly': 14, 'broccoli': 23},
('3','1'): {'apple': 11, 'orange': 17, 'pineapple': 31, 'jelly': 12, 'broccoli': 23},
('3','2'): {'apple': 12, 'orange': 18, 'pineapple': 31, 'jelly': 13, 'broccoli': 24}
}
model.fruittesting = Param(model.y, model.x, initialize=lambda model, xx, yy: fruit[y][x], within=NonNegativeIntegers)
That results in this error:
ERROR: Rule failed for Param 'fruittesting' with index ('1', '2', 'apple'):
TypeError: <lambda>() takes 3 positional arguments but 4 were given
ERROR: Constructing component 'fruittesting' from data=None failed:
TypeError: <lambda>() takes 3 positional arguments but 4 were given
Any ideas on how to fix this?
By default, Pyomo "flattens" all indexing. Your Param fruittesting
is indexed by model.x
(dimension 1) and model.y
(dimension 2). That means that the Param is dimension 3, and your initialization function should take 4 arguments: the owning block (model), and the 3 indices:
model.fruittesting = Param(
model.y, model.x,
initialize=lambda m, y1, y2, x: fruit[y1, y2][x],
within=NonNegativeIntegers
)