Im not sure if the problem relies on the versions im using or if im not importing something, but the sources from where i got this example didnt mention any other environment rather than that of pyomo.environ (im working on python 3.6). I downloaded coopr because i thought it was important but there were no changes. I already try to make a .dat file but when i run the solver i get the same error message. any suggestions? Im reading the book of pyomo (2012) and this is the easiest way to declare data so i would like to learn it. Also if you know about a pyomo course (formal or informal), please share. Thanks a lot for your time.
from pyomo.environ import*
from coopr.pyomo import*
model = AbstractModel()
# nodes in the networks
model.N = Set()
#network arcs
model.A = Set(within=model.N*model.N) #within=set used for validation, it is a sueprset off the set being declared
# model parameters
#source node
model.s = Param(within=model.N)
#sink node
model.t = Param(within=model.N)
#flow capacity limits
model.c = Param(model.A)
#the flow over each arc
model.f = Var(model.A, within=NonNegativeReals)
# Maximize the flow into the sink nodes
def total_rule(model):
return sum(model.f[i,j] for (i,j) in model.A if j== value(model.t))
model.total = Objective(rule=total_rule, sense=maximize)
# Enforce an upper limit on the flow across each arc
def limit_rule(model, i, j):
return model.f[i,j] <= model.c[i,j]
model.limit = Constraint(model.A, rule=limit_rule)
# Enforce flow through each node
def flow_rule(model, k):
if k == value(model.s) or k == value(model.t):
return Constraint.Skip
inFlow = sum(model.f[i,j] for (i,j) in model.A if j==k)
outFlow = sum(model.f[i,j] for (i,j) in model.A if i==k)
return inFlow == outflow
model.flow = Constraint(model.N, rule=flow_rule)
set N := Zoo A B C D E Home; <-----
File "<ipython-input-6-1d6651500789>", line 1 <-----
set: N := Zoo A B C D E Home; <------
^ <------
SyntaxError: invalid syntax <------
I would start with concrete models... just because it relieves you of working with separate data files and Python is super easy to make your sets and data within the framework of the model. You can start your model like:
from pyomo.environ import*
#from coopr.pyomo import*
model = ConcreteModel() # AbstractModel()
# nodes in the networks
nodes = {'Zoo', 'A', 'B', 'C', 'D', 'E', 'Home'}
model.N = Set(initialize=nodes)
If you go the abstract route, it appears above you have your set definition inside the python file?? That data needs to be in a separate file.