-
Notifications
You must be signed in to change notification settings - Fork 12
logic: add forecast grid charge target strategy #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
filiplajszczak
wants to merge
1
commit into
MaStr:main
Choose a base branch
from
filiplajszczak:logic-grid-charge-target-strategy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """Effective grid-charge SoC target calculation.""" | ||
|
|
||
| from typing import Optional, Sequence | ||
|
|
||
| GRID_CHARGE_TARGET_STRATEGY_FIXED = 'fixed' | ||
| GRID_CHARGE_TARGET_STRATEGY_FORECAST = 'forecast' | ||
| GRID_CHARGE_TARGET_STRATEGIES = ( | ||
| GRID_CHARGE_TARGET_STRATEGY_FIXED, | ||
| GRID_CHARGE_TARGET_STRATEGY_FORECAST, | ||
| ) | ||
|
|
||
|
|
||
| def _ordered_values(values) -> Sequence[float]: | ||
| if isinstance(values, dict): | ||
| if not values: | ||
| return [] | ||
| keys = set(values.keys()) | ||
| error_message = ( | ||
| "forecast dict values must use consecutive integer " | ||
| "indices starting at 0" | ||
| ) | ||
| if not all(isinstance(index, int) for index in keys): | ||
| raise ValueError(error_message) | ||
| expected_keys = set(range(max(keys) + 1)) | ||
| if keys != expected_keys: | ||
| raise ValueError(error_message) | ||
| return [values[index] for index in range(max(keys) + 1)] | ||
| return list(values) | ||
|
|
||
|
|
||
| def _calculate_min_dynamic_price_difference( | ||
| current_price: float, | ||
| min_price_difference: float, | ||
| min_price_difference_rel: float) -> float: | ||
| return max(min_price_difference, | ||
| min_price_difference_rel * abs(current_price)) | ||
|
|
||
|
|
||
| def calculate_effective_grid_charge_soc( | ||
| strategy: str, | ||
| configured_min_grid_charge_soc: Optional[float], | ||
| max_charging_from_grid_limit: float, | ||
| max_capacity: float, | ||
| min_soc_energy: float, | ||
| production, | ||
| consumption, | ||
| prices, | ||
| min_price_difference: float, | ||
| min_price_difference_rel: float = 0.0, | ||
| pv_forecast_factor: float = 1.0) -> Optional[float]: | ||
| """Calculate the effective minimum grid-charge SoC for this evaluation. | ||
|
|
||
| ``fixed`` returns the configured target unchanged. ``forecast`` treats the | ||
| configured target as a floor and raises it when future expensive-slot net | ||
| demand implies a higher target. Forecast PV can be discounted with | ||
| ``pv_forecast_factor`` to account for uncertain PV ramps. | ||
| """ | ||
| if configured_min_grid_charge_soc is None: | ||
| return None | ||
| if strategy not in GRID_CHARGE_TARGET_STRATEGIES: | ||
| raise ValueError( | ||
| f"grid_charge_target_strategy must be one of " | ||
| f"{GRID_CHARGE_TARGET_STRATEGIES}, got '{strategy}'" | ||
| ) | ||
| if strategy == GRID_CHARGE_TARGET_STRATEGY_FIXED: | ||
| return configured_min_grid_charge_soc | ||
| if max_capacity <= 0: | ||
| raise ValueError("max_capacity must be greater than 0") | ||
| if not 0 <= pv_forecast_factor <= 1: | ||
| raise ValueError( | ||
| "grid_charge_forecast_pv_factor must be between 0 and 1, " | ||
| f"got {pv_forecast_factor}" | ||
| ) | ||
|
|
||
| production_values = _ordered_values(production) | ||
| consumption_values = _ordered_values(consumption) | ||
| price_values = _ordered_values(prices) | ||
| max_slot = min(len(production_values), len(consumption_values), len(price_values)) | ||
| if max_slot < 2: | ||
| return configured_min_grid_charge_soc | ||
|
|
||
| current_price = price_values[0] | ||
| min_dynamic_price_difference = _calculate_min_dynamic_price_difference( | ||
| current_price, | ||
| min_price_difference, | ||
| min_price_difference_rel, | ||
| ) | ||
|
|
||
| # Evaluate until the next price slot that is no more expensive than the | ||
| # current slot. This keeps the target tied to the current cheap/economical | ||
| # charging window rather than charging for the whole forecast horizon. | ||
| for slot in range(1, max_slot): | ||
| if price_values[slot] <= current_price: | ||
| max_slot = slot | ||
| break | ||
|
|
||
| forecast_need = 0.0 | ||
| for slot in range(1, max_slot): | ||
| if price_values[slot] <= current_price + min_dynamic_price_difference: | ||
| continue | ||
| discounted_pv = production_values[slot] * pv_forecast_factor | ||
| forecast_need += max(0.0, consumption_values[slot] - discounted_pv) | ||
|
|
||
| forecast_target = (min_soc_energy + forecast_need) / max_capacity | ||
| forecast_target = min(forecast_target, max_charging_from_grid_limit) | ||
| return max(configured_min_grid_charge_soc, forecast_target) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in f5261de.