Generalizing the "equate_variables" as a constraint for all investment flows

Dear Forum,

I have recently tried to add a constraint to my energy systems model that makes use of the “equate_variables” function in order to have all investment flows, where the input-output are the same (but reversed), be “equalized”. My intention with this is to model grid transformers as “transformer”-components instead of links, to allow for investment.
However my following code is running into some issues, so I was hoping someone could help me better understand the syntax where I’m going wrong:

def equalized_invest(optimization_model, flows = None):
    Equalize the investments of components that have another component's output 
    as their own input and vice-versa.
    for (i, o) in optimization_model.flows:
        if hasattr(optimization_model.flows[i, o], 'investment'):
            if optimization_model.flows[i, o] == optimization_model.flows[o, i]:
                var1 = optimization_model.flows[i, o]
                var2 = optimization_model.flows[o, i]
                solph.constraints.equate_variables(optimization_model, var1, var2)

I hope this code snippet is sufficient to solve this error and am looking forward to your answers!
@uwe.krien I saw that you had many months ago answered a similar forum post with (what I believe to be) the first skeleton for the implementation of the “equate_variables” function, so maybe you can help? :smile:

All the best,