Pyomo Convert string back to EqualityExpression?

Viewed 43

After creating a model:

from pyomo.environ import *    
model = ConcreteModel()
# do stuff that loads many variables into model
# as in model.v_volume_in = Var(list(df['volume_in'].itertuples()))
# as in model.v_volume_out = Var(list(df['volume_out'].itertuples()))

and creating a flow balance constraint as in:

def make_balance(model, key):
    eq = ( sum(model.v_volume_in[key_origin, key])
           ==
           sum(model.v_volume_out[key, key_destination])
    )
eq = make_balance(model, key=0)

and then taking it's string value (eq_string = eq.to_string()), can I make it back to a EqualityExpression object or any other way to add as a Constraint later?

If I try adding the constraint from the string I get an error:

model.c_flow = Constraint(list_of_indexes)
model.c_flow.add(key, eq_string)

#error:
#Expecting a tuple or equation. Examples:
#   sum(model.costs) == model.income
#   (0, model.price[item], 50)

I know I have to pass the eq and not the to_string() value. But what if I lose the eq and only have the strings or if I had MANY data and decided to make the equation as strings some other way before passing to the model

0 Answers
Related