I have a relatively simple Pyomo model, where I wish to create the constraints in parallel. I have created the following functions:
def constraintA():
constraintA = pyo.ConstraintList()
for y, h in model.YH:
constraintA.add(model.varA[y, h] <= data[y, h])
return constraintA
def constraintB():
constraintB = pyo.ConstraintList()
for y, h in model.YH:
constraintB.add(model.varB[y, h] >= other_data[y, h])
return constraintB
def constraintC():
constraintC = pyo.ConstraintList()
for y, h in model.YH:
constraintC.add(model.varC[y, h] <= data[y,h] * 0.5)
return constraintC
Note that the model
, data
and other_data
are considered 'global' and therefore can be used within these constraint functions. Now, what I would like to do is create attributes in the pyomo model with the respective constraints, but then in parallel.
So I start with:
constraints = [constraintA, constraintB, constraintC]
with ProcessPoolExecutor() as pool:
for work_item in enumerate(constraints):
fs = [pool.submit(c) for c in constraints]
However, I get "finished raised AttributeError". Does this mean that despite Python only has to read and copy it, it is locking the object and therefore cannot access the attribute and therefore is unable to create the constraints?
In order to resolve this issue, I was wondering whether I need to explicitly copy the model and the data and other_data to the constraint functions themselves. Or is there another way to achieve this, without "passing variables around"?