I am new to Pyomo, and am getting an error when I try to declare my objective. I would prefer to use a def for the objective rather than expr= if possible.
The code is below.
# Import pyomo
from pyomo.environ import *
N = ['Harlingen', 'Memphis', 'Ashland']
M = ['NYC', 'LA', 'Chicago', 'Houston']
d = {('Harlingen', 'NYC'): 1956, \
('Harlingen', 'LA'): 1606, \
('Harlingen', 'Chicago'): 1410, \
('Harlingen', 'Houston'): 330, \
('Memphis', 'NYC'): 1096, \
('Memphis', 'LA'): 1792, \
('Memphis', 'Chicago'): 531, \
('Memphis', 'Houston'): 567, \
('Ashland', 'NYC'): 485, \
('Ashland', 'LA'): 2322, \
('Ashland', 'Chicago'): 324, \
('Ashland', 'Houston'): 1236 }
P = 2
model = ConcreteModel("warehouse location problem")
model.N = Set(dimen=1, initialize=N)
model.M = Set(dimen=1, initialize=M)
model.d = Param(model.N, model.M, within=PositiveIntegers, initialize=d)
model.P = Param(initialize=P)
model.y = Var(model.N, within=PositiveIntegers)
model.x = Var(model.N, model.M, bounds=(0,1))
def obj_rule(model):
return sum(model.d[n,m] * model.x[n,m] for n in model.N, m in model.M)
model.obj = Objective(rule=obj_rule)
The error I receive is
File "warehouseLocation.py", line 35
return sum(model.d[n,m] * model.x[n,m] for n in model.N, m in model.M)
^
SyntaxError: Generator expression must be parenthesized
All help appreciated
Write your objective function in this way
def obj_rule(model):
return sum(model.d[n,m]* model.x[n,m] for n in model.N for m in model.M)
model.obj = Objective(rule=obj_rule)
To check it
model.obj.pprint()