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)
    return 

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,

Moritz

Hi Moritz,

from what I understood, you want to couple the investments, not the actual flow values (which would also be dependent on the upfront investments), right? There is an example on how to achieve that in the tests for oemof.solph. Here is the module: oemof-solph/tests/test_scripts/test_solph/test_connect_invest/test_connect_invest.py at dev · oemof/oemof-solph · GitHub

You might want to have a look at lines 107-111 for the actual constraint definition. The main point here is that you need to access the respective variables of a FlowBlock, in your case the invest variable of an InvestmentFlowBlock. Also be aware that the last index has been newly introduced in the course of making oemof.solph work with multi-period investment modelling.

Best,
Johannes

Hey Johannes,

thanks a lot for your answer!
Currently, while trying to run the test model I get the error that the index for the line12 object is not valid for InvestmentFlowBlock.invest:
Index \'("<oemof.solph.components._transformer.Transformer: \'line12\'>", "<oemof.solph.buses._bus.Bus: \'electricity2\'>", 0)\' is not valid for indexed component \'InvestmentFlowBlock.invest\''
I can also reproduce this error when trying to execute the code in my model.
Is there potentially an error in how the line12 object is “fed” to the InvestmentFlowBlock.invest object?

All the best,
Mo