-
Notifications
You must be signed in to change notification settings - Fork 0
Depth-first search algorithm for BusBarSection IDs #293
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
Closed
ghazwarhili
wants to merge
17
commits into
main
from
BusbarSectionFinderTraverser-breadth-first-search-algorithm
+627
−29
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c112b6a
enhance BusbarSectionFinderTraverser to handle all cases
ghazwarhili bd1c1c5
add TU for fork topo
ghazwarhili dab0cf8
enhance coverage code
ghazwarhili 8ea7a25
Merge branch 'main' into fix-busbar-section-finder-traverser
ghazwarhili 78f18fd
enhance TU
ghazwarhili efc0ece
breadth-first busbasrsection id search algorithm
ghazwarhili ca84d42
add TU
ghazwarhili 8ba0040
resolving conflicts
ghazwarhili b03d915
unused import
ghazwarhili a1482d3
fix sonar issues
ghazwarhili 6bcd0d2
enhance TU to add bypass topo
ghazwarhili 6b5a7f1
enhance TU with mixed topo kind
ghazwarhili a48c699
EtienneH code review remarks
ghazwarhili 8bf25d4
add author
ghazwarhili 999f53c
etienneH code review remarks part 2
ghazwarhili 28dc2d2
clean code
ghazwarhili dfae5ec
add comments
ghazwarhili 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,42 +6,141 @@ | |
*/ | ||
package org.gridsuite.network.map.dto.definition.extension; | ||
|
||
import com.powsybl.iidm.network.IdentifiableType; | ||
import com.powsybl.iidm.network.Switch; | ||
import com.powsybl.iidm.network.Terminal; | ||
import com.powsybl.math.graph.TraverseResult; | ||
import com.powsybl.iidm.network.*; | ||
import com.powsybl.iidm.network.extensions.BusbarSectionPosition; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author Slimane Amar <slimane.amar at rte-france.com> | ||
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com> | ||
*/ | ||
public class BusbarSectionFinderTraverser implements Terminal.TopologyTraverser { | ||
// TODO : to remove when this class is available in network-store | ||
public final class BusbarSectionFinderTraverser { | ||
|
||
private BusbarSectionFinderTraverser() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private final boolean onlyConnectedBbs; | ||
private record NodePath(int startNode, List<SwitchInfo> traversedSwitches, SwitchInfo lastSwitch) { } | ||
|
||
private String firstTraversedBbsId; | ||
public record SwitchInfo(String id, boolean isOpen) { } | ||
|
||
public BusbarSectionFinderTraverser(boolean onlyConnectedBbs) { | ||
this.onlyConnectedBbs = onlyConnectedBbs; | ||
public record BusbarSectionResult(String busbarSectionId, int depth, SwitchInfo lastSwitch, int busbarIndex, int sectionIndex) { } | ||
|
||
public static String findBusbarSectionId(Terminal terminal) { | ||
BusbarSectionResult result = getBusbarSectionResult(terminal); | ||
return result != null ? result.busbarSectionId() : terminal.getVoltageLevel().getNodeBreakerView().getBusbarSections().iterator().next().getId(); | ||
} | ||
|
||
@Override | ||
public TraverseResult traverse(Terminal terminal, boolean connected) { | ||
public static BusbarSectionResult getBusbarSectionResult(Terminal terminal) { | ||
VoltageLevel.NodeBreakerView view = terminal.getVoltageLevel().getNodeBreakerView(); | ||
int startNode = terminal.getNodeBreakerView().getNode(); | ||
List<BusbarSectionResult> allResults = searchAllBusbars(view, startNode); | ||
if (allResults.isEmpty()) { | ||
return null; | ||
} | ||
return selectBestBusbar(allResults); | ||
} | ||
|
||
private static BusbarSectionResult selectBestBusbar(List<BusbarSectionResult> results) { | ||
// Priority 1: Busbars without switch direct connection | ||
List<BusbarSectionResult> withoutSwitch = results.stream().filter(r -> r.lastSwitch() == null).toList(); | ||
if (!withoutSwitch.isEmpty()) { | ||
return withoutSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth) | ||
.thenComparingInt(BusbarSectionResult::busbarIndex) | ||
.thenComparingInt(BusbarSectionResult::sectionIndex)).orElse(null); | ||
} | ||
// Priority 2: Search for busbar with closed last switch | ||
List<BusbarSectionResult> withClosedSwitch = results.stream().filter(r -> r.lastSwitch() != null && !r.lastSwitch().isOpen()).toList(); | ||
if (!withClosedSwitch.isEmpty()) { | ||
return withClosedSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth) | ||
.thenComparingInt(BusbarSectionResult::busbarIndex) | ||
.thenComparingInt(BusbarSectionResult::sectionIndex)).orElse(null); | ||
} | ||
// Priority 3: Search for busbar with open last switch | ||
List<BusbarSectionResult> withOpenSwitch = results.stream().filter(r -> r.lastSwitch() != null && r.lastSwitch().isOpen()).toList(); | ||
if (!withOpenSwitch.isEmpty()) { | ||
return withOpenSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth) | ||
.thenComparingInt(BusbarSectionResult::busbarIndex) | ||
.thenComparingInt(BusbarSectionResult::sectionIndex)).orElse(null); | ||
} | ||
return results.getFirst(); | ||
} | ||
|
||
private static List<BusbarSectionResult> searchAllBusbars(VoltageLevel.NodeBreakerView view, int startNode) { | ||
List<BusbarSectionResult> results = new ArrayList<>(); | ||
Set<Integer> visited = new HashSet<>(); | ||
Queue<NodePath> nodePathsToVisit = new LinkedList<>(); | ||
nodePathsToVisit.offer(new NodePath(startNode, new ArrayList<>(), null)); | ||
while (!nodePathsToVisit.isEmpty()) { | ||
NodePath currentNodePath = nodePathsToVisit.poll(); | ||
if (hasBeenVisited(currentNodePath.startNode(), visited)) { | ||
continue; | ||
} | ||
visited.add(currentNodePath.startNode()); | ||
Optional<BusbarSectionResult> busbarSectionResult = findBusbarSectionAtNode(view, currentNodePath); | ||
if (busbarSectionResult.isPresent()) { | ||
results.add(busbarSectionResult.get()); | ||
} else { | ||
exploreAdjacentNodes(view, currentNodePath, visited, nodePathsToVisit); | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
private static boolean hasBeenVisited(int node, Set<Integer> visited) { | ||
return visited.contains(node); | ||
} | ||
|
||
private static Optional<BusbarSectionResult> findBusbarSectionAtNode(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) { | ||
Optional<Terminal> nodeTerminal = view.getOptionalTerminal(currentNodePath.startNode()); | ||
if (nodeTerminal.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
Terminal terminal = nodeTerminal.get(); | ||
if (terminal.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) { | ||
firstTraversedBbsId = terminal.getConnectable().getId(); | ||
return TraverseResult.TERMINATE_TRAVERSER; | ||
String busbarSectionId = terminal.getConnectable().getId(); | ||
int depth = currentNodePath.traversedSwitches().size(); | ||
SwitchInfo lastSwitch = currentNodePath.lastSwitch(); | ||
BusbarSection busbarSection = (BusbarSection) terminal.getConnectable(); | ||
int busbarIndex = 1; | ||
int sectionIndex = 1; | ||
var busbarSectionPosition = busbarSection.getExtension(BusbarSectionPosition.class); | ||
if (busbarSectionPosition != null) { | ||
busbarIndex = busbarSectionPosition.getBusbarIndex(); | ||
sectionIndex = busbarSectionPosition.getSectionIndex(); | ||
} | ||
return Optional.of(new BusbarSectionResult(busbarSectionId, depth, lastSwitch, busbarIndex, sectionIndex)); | ||
} | ||
return TraverseResult.CONTINUE; | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public TraverseResult traverse(Switch aSwitch) { | ||
if (onlyConnectedBbs && aSwitch.isOpen()) { | ||
return TraverseResult.TERMINATE_PATH; | ||
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> nodePathsToVisit) { | ||
view.getSwitchStream().forEach(sw -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not efficient. Maybe we can do something better |
||
int node1 = view.getNode1(sw.getId()); | ||
int node2 = view.getNode2(sw.getId()); | ||
Optional<Integer> nextNode = getNextNodeIfAdjacent(currentNodePath.startNode(), node1, node2); | ||
if (nextNode.isPresent() && !visited.contains(nextNode.get())) { | ||
NodePath newNodePath = createNodePath(currentNodePath, sw, nextNode.get()); | ||
nodePathsToVisit.offer(newNodePath); | ||
} | ||
}); | ||
} | ||
|
||
private static Optional<Integer> getNextNodeIfAdjacent(int currentNode, int node1, int node2) { | ||
if (node1 == currentNode) { | ||
return Optional.of(node2); | ||
} | ||
if (node2 == currentNode) { | ||
return Optional.of(node1); | ||
} | ||
return TraverseResult.CONTINUE; | ||
return Optional.empty(); | ||
} | ||
|
||
public String getFirstTraversedBbsId() { | ||
return firstTraversedBbsId; | ||
private static NodePath createNodePath(NodePath currentNodePath, Switch sw, int nextNode) { | ||
List<SwitchInfo> newPathSwitches = new ArrayList<>(currentNodePath.traversedSwitches()); | ||
SwitchInfo switchInfo = new SwitchInfo(sw.getId(), sw.isOpen()); | ||
newPathSwitches.add(switchInfo); | ||
return new NodePath(nextNode, newPathSwitches, switchInfo); | ||
} | ||
} |
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.
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.
can be empty
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 case is handled by returning result.busbarSectionId() when result is not null, and otherwise the Id of the first busbar section in the terminal’s voltage level