-
Notifications
You must be signed in to change notification settings - Fork 33
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
base: feature/tap-changer-control-logic-linear-search
Are you sure you want to change the base?
Conversation
Signed-off-by: Jerry Guo <Jerry.Jinfeng.Guo@alliander.com>
…eature/tap-changer-control-logic-binary-search
…eature/tap-changer-control-logic-binary-search
…eature/tap-changer-control-logic-binary-search
…eature/tap-changer-control-logic-binary-search
…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>
@@ -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_; |
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.
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 bool
s 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 |
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.
// 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_; |
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.
optional different implementation:
bool const prefer_higher = control_at_tap_side_ ? !prefer_higher_ : prefer_higher_; | |
bool const prefer_higher = control_at_tap_side_ != prefer_higher_; |
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_; |
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.
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.
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
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 |
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.
rather than control_at_tap_side
, maybe you can combine this with the logic surrounding tap_reverse
?
linux build fails |
Updated control logic for binary search