Skip to content

Feature/custom attributes for investments#1174

Draft
ghost wants to merge 27 commits intodevfrom
feature/custom_attributes_for_investments
Draft

Feature/custom attributes for investments#1174
ghost wants to merge 27 commits intodevfrom
feature/custom_attributes_for_investments

Conversation

@ghost
Copy link

@ghost ghost commented Mar 12, 2025

Constraints Investments for costume attributes missed the possibility of constraining not only the investment flow, but the sum of investment and operation. Furthermore no OffSet for costume Attributes in investments were considers.

This PR adds both and explains it in an example.

It is handy to have these to run a restricted optimization, as explained in Issue 1171

@ghost ghost requested a review from p-snft March 12, 2025 18:10
…ribute

add tsam logic and weighting to additional total limit of costume attribute
Copy link
Member

@p-snft p-snft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, I would see this as a Showcase entry. Sure, the constraint is written in a generic way, but an example works without tests and discussions about the API.

(@gnn and I were discussing how to harmonise different types of variables, so that a "flow of money" would be constrained using the same code as energy flows. But maybe, this would rather be something for solph 2.)


Installation requirements
-------------------------
This example requires oemof.solph (v0.5.x), install by:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really solph 0.5?

model.GenericInvestmentStorageBlock.invest[st, p]
                 * getattr(storages[st][0].investment, keyword).get("cost", 0)


gets always calculated 2 times in the constraint. Idk where bug comes from.
@p-snft p-snft marked this pull request as draft September 3, 2025 09:00
@Maxhi77 Maxhi77 requested review from gnn and p-snft September 17, 2025 14:57
@Maxhi77 Maxhi77 self-assigned this Sep 17, 2025
Copy link
Member

@p-snft p-snft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your efforts. I see the following issues that should be addressed before a merge:

  1. The approach seems to duplicate a lot of additional_investment_flow_limit - which it extends. From my point of view, we do not really need both. So, you might just extend the existing constraint, rename it and provide a transitional wrapper. (You can have keyword arguments to still only consider flows and no offsets.)
  2. The constraint you introduce is completely untested.

@Maxhi77
Copy link
Contributor

Maxhi77 commented Sep 18, 2025

Thank you for the feedback.

To specify the offset or cost per unit of a custom attribute, I used the following:

custom_attributes={“space”: {“cost”: 1, “offset”: 5}},

The old version used instead:

custom_attributes={"space": 1}

This is relevant at the moment the constraint is set. If I put a wrapper around the old constraint, it only works if

  1. I check the structure of the custom attributes and look for cost or not. I don't like this idea.
  2. It would be easier to simply change the structure of the custom_attributes to the new one.
  3. Keep the old restriction and write a note that the new structure must be used in the future.

@p-snft
Copy link
Member

p-snft commented Sep 18, 2025

This is relevant at the moment the constraint is set. If I put a wrapper around the old constraint, it only works if ...

I see the point. As this is not about about arguments to the function only, a classical wrapper will not do. Maybe, just write a deprecation warning for the old constraint, telling that the new one should be used in the future. In that case, I'd suggest a test that proves that both constraints provide the same result if custom_attributes={"space": {"cost": 1}} (and no offset is given.)

PS: The name "cost" might be misleading. Maybe, just be mathematical here and call them "linear" and "offset".

…vestments' into feature/custom_attributes_for_investments

# Conflicts:
#	examples/emission_constraint/emission_constraint_invest.py
#	examples/emission_constraint/emission_constraint_invest_and_flow.py
import logging
import os

from PIL.ImageChops import offset

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'offset' is not used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also do not get why you import this.

from oemof import solph


def main(optimize=True):

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error, as implicit returns always return None.
"""

import pandas as pd
import pytest

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'pytest' is not used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this fixed?

@Maxhi77
Copy link
Contributor

Maxhi77 commented Sep 19, 2025

Thank you very much for the feedback.

I have implemented all the requested changes. I've also moved all examples that restrict a costume attribute to an example folder and renamed it Emission. Why this? I think this will be much easier for people to find. Also, there is now a better and more consistent structure in the examples with limits of: flow, invest, invest and flow.

@Maxhi77 Maxhi77 requested a review from p-snft September 19, 2025 12:48
Copy link
Member

@p-snft p-snft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not repeat the CodeQL comments, but two of those were (and still are) valuable.

"""

import pandas as pd
import pytest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this fixed?

import logging
import os

from PIL.ImageChops import offset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also do not get why you import this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants