-
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
Changes from 12 commits
c112b6a
bd1c1c5
dab0cf8
8ea7a25
78f18fd
efc0ece
ca84d42
8ba0040
b03d915
a1482d3
6bcd0d2
6b5a7f1
a48c699
8bf25d4
999f53c
28dc2d2
dfae5ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,42 +6,174 @@ | |||||
*/ | ||||||
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 java.util.*; | ||||||
|
||||||
/** | ||||||
* @author Slimane Amar <slimane.amar 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 constructor to prevent instantiation of this utility class. | ||||||
|
||||||
*/ | ||||||
private BusbarSectionFinderTraverser() { | ||||||
throw new UnsupportedOperationException(); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Finds the best busbar section connected to the given terminal. | ||||||
* Uses a breadth-first search algorithm to explore all possible paths. | ||||||
* | ||||||
* @param terminal the starting terminal | ||||||
* @return the best busbar result according to selection criteria, or null if none found | ||||||
*/ | ||||||
public static BusbarSectionResult findBestBusbar(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); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Selects the best busbar from a list of candidates using a priority-based approach: | ||||||
* Priority 1: Busbar with closed last switch (minimum depth, then minimum switches before last) | ||||||
* Priority 2: Busbar with open last switch (minimum depth, then minimum switches before last) | ||||||
* Priority 3: Busbar without switch (direct connection, minimum depth) | ||||||
|
||||||
* | ||||||
* @param results list of all found busbar results | ||||||
* @return the best busbar according to selection criteria | ||||||
*/ | ||||||
private static BusbarSectionResult selectBestBusbar(List<BusbarSectionResult> results) { | ||||||
// Priority 1: 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::switchesBeforeLast)).orElse(null); | ||||||
} | ||||||
|
||||||
// Priority 2: 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::switchesBeforeLast)).orElse(null); | ||||||
} | ||||||
|
||||||
// Priority 3: 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)).orElse(null); | ||||||
} | ||||||
|
||||||
private final boolean onlyConnectedBbs; | ||||||
// Fallback: select first busbar | ||||||
|
||||||
return results.getFirst(); | ||||||
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. can be empty 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 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 |
||||||
} | ||||||
|
||||||
private String firstTraversedBbsId; | ||||||
/** | ||||||
* Searches all accessible busbars from a starting node using breadth-first search. | ||||||
* Explores the node-breaker topology through switches. | ||||||
* | ||||||
* @param view the node-breaker view of the voltage level | ||||||
* @param startNode the starting node index | ||||||
* @return list of all busbar results found | ||||||
*/ | ||||||
private static List<BusbarSectionResult> searchAllBusbars(VoltageLevel.NodeBreakerView view, int startNode) { | ||||||
List<BusbarSectionResult> results = new ArrayList<>(); | ||||||
Set<Integer> visited = new HashSet<>(); | ||||||
Queue<NodePath> queue = new LinkedList<>(); | ||||||
|
Queue<NodePath> queue = new LinkedList<>(); | |
Queue<NodePath> pathsToVisit = new LinkedList<>(); |
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.
nodePathsToVisit maybe ?
Outdated
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.
private static boolean hasNotBeenVisited(int node, Set<Integer> visited) { | |
private static boolean hasBeenVisited(int node, Set<Integer> visited) { |
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.
done
Outdated
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.
return !visited.contains(node); | |
return visited.contains(node); |
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.
done
Outdated
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.
private static Optional<BusbarSectionResult> tryCreateBusbarResult(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) { | |
private static Optional<BusbarSectionResult> getDirectlyConnectedBusbarSection(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) { |
Something like this ? Here, we check if a busbar section is directly connected to the node and return it if yes
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.
something better than getDirectlyConnectedBusbarSection() to be found
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.
findBusbarSectionAtNode maybe ?
Outdated
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.
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> queue) { | |
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> pathsToVisit) { |
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.
done
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 is not efficient. Maybe we can do something better
Outdated
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.
comments on records to remove ?
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.
removed
Outdated
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.
int node1, int node2 are not used
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.
I had used them for debugging purposes, but now we don't need them anymore
removed
Outdated
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.
What's the difference between depth and switchesBeforeLast ? It is redondant nope ?
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.
Depth is not the same as switchesBeforeLast (busbarSectionId)
but switchesBeforeLast to be removed
Outdated
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.
Move the records and this method just under the constructor
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.
done
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.
Ghazwa
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.
done