Skip to content
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

Step-up transformer tap changer support: binary search control logic #894

Open
wants to merge 12 commits into
base: feature/tap-changer-control-logic-linear-search
Choose a base branch
from

Conversation

Jerry-Jinfeng-Guo
Copy link
Contributor

Updated control logic for binary search

Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
@Jerry-Jinfeng-Guo Jerry-Jinfeng-Guo added the feature New feature or request label Feb 12, 2025
@Jerry-Jinfeng-Guo Jerry-Jinfeng-Guo self-assigned this Feb 12, 2025
…eature/tap-changer-control-logic-binary-search
Jerry-Jinfeng-Guo and others added 9 commits February 12, 2025 17:32
…eature/tap-changer-control-logic-binary-search
…eature/tap-changer-control-logic-binary-search
…eature/tap-changer-control-logic-binary-search
Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
…eature/tap-changer-control-logic-binary-search
Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
…eature/tap-changer-control-logic-binary-search
…eature/tap-changer-control-logic-binary-search
Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
@Jerry-Jinfeng-Guo Jerry-Jinfeng-Guo marked this pull request as ready for review February 13, 2025 21:17
@@ -719,7 +722,7 @@ class TapPositionOptimizerImpl<std::tuple<TransformerTypes...>, StateCalculator,
}

void propose_new_pos(bool strategy_max, bool above_range) {
bool const is_down = above_range == tap_reverse_;
bool const is_down = (above_range == tap_reverse_) ^ control_at_tap_side_;
Copy link
Member

Choose a reason for hiding this comment

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

please use != instead of bitwise XOR for boolean values.

Rationale: the exact implementation of bool is not not specified in the C/C++ standard. As a result, bitwise operations on bools are undefined behavior.

Example: if you have a bool variable represented by an int8_t and its binary representation is 00000001 another bool with a binary representation 00000010, then their boolean values are both 1. The boolean XOR should therefore be 0. However, the bitwise XOR is 00000011, which represents 1, not 0.

@@ -777,25 +781,27 @@ class TapPositionOptimizerImpl<std::tuple<TransformerTypes...>, StateCalculator,
}
}

IntS search(bool prefer_higher = true) const {
IntS search(bool prefer_higher_) const {
// This logic is used to determin which of the middle points could be of interest
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// This logic is used to determin which of the middle points could be of interest
// This logic is used to determine which of the middle points could be of interest

// This logic is used to determin which of the middle points could be of interest
// given strategy used in optimization:
// Since in BinarySearch we insist on absolute upper and lower bounds, we only need to
// find the corresponding mid point. std::midpoint returns always the lower mid point
// if the range is of even length. This is why we need to adjust bounds accordingly.
// Not because upper bound and lower bound might be reversed, which is not possible.
bool const prefer_higher = control_at_tap_side_ ? !prefer_higher_ : prefer_higher_;
Copy link
Member

Choose a reason for hiding this comment

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

optional different implementation:

Suggested change
bool const prefer_higher = control_at_tap_side_ ? !prefer_higher_ : prefer_higher_;
bool const prefer_higher = control_at_tap_side_ != prefer_higher_;

Comment on lines +791 to 793
bool const prefer_higher = control_at_tap_side_ ? !prefer_higher_ : prefer_higher_;
auto const primary_bound = prefer_higher ? upper_bound_ : lower_bound_;
auto const secondary_bound = prefer_higher ? lower_bound_ : upper_bound_;
Copy link
Member

Choose a reason for hiding this comment

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

this logic was originally introduced to swap upper and lower bound. Maybe it's a good idea to make the control_at_tap_side the new default, e.g.

Suggested change
bool const prefer_higher = control_at_tap_side_ ? !prefer_higher_ : prefer_higher_;
auto const primary_bound = prefer_higher ? upper_bound_ : lower_bound_;
auto const secondary_bound = prefer_higher ? lower_bound_ : upper_bound_;
bool const prefer_lower = control_at_tap_side_ == prefer_higher_;
auto const primary_bound = prefer_lower ? lower_bound_ : upper_bound_ ;
auto const secondary_bound = prefer_lower ? upper_bound_ : lower_bound_ ;

or otherwise explicitly mention

Suggested change
bool const prefer_higher = control_at_tap_side_ ? !prefer_higher_ : prefer_higher_;
auto const primary_bound = prefer_higher ? upper_bound_ : lower_bound_;
auto const secondary_bound = prefer_higher ? lower_bound_ : upper_bound_;
bool const inverted_bounds = control_at_tap_side_ != prefer_higher_;
auto const primary_bound = inverted_bounds ? upper_bound_ : lower_bound_;
auto const secondary_bound = inverted_bounds ? lower_bound_ : upper_bound_;

bool last_check_{false}; // last run checked
bool tap_reverse_{false}; // tap range normal or reversed
bool inevitable_run_{false}; // inevitable run
bool control_at_tap_side_{false}; // regulator control side is at tap side
Copy link
Member

Choose a reason for hiding this comment

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

rather than control_at_tap_side, maybe you can combine this with the logic surrounding tap_reverse?

@mgovers
Copy link
Member

mgovers commented Feb 17, 2025

linux build fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants