-
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
base: main
Are you sure you want to change the base?
Changes from 13 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,123 @@ | |||||
*/ | ||||||
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> | ||||||
* @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 node, List<SwitchInfo> pathSwitches, SwitchInfo lastSwitch) { } | ||||||
|
||||||
private String firstTraversedBbsId; | ||||||
public record SwitchInfo(String id, SwitchKind kind, boolean isOpen) { } | ||||||
|
||||||
|
||||||
public BusbarSectionFinderTraverser(boolean onlyConnectedBbs) { | ||||||
this.onlyConnectedBbs = onlyConnectedBbs; | ||||||
public record BusbarSectionResult(String busbarSectionId, int depth, SwitchInfo lastSwitch) { } | ||||||
|
||||||
public static String findBusbarSectionId(Terminal terminal) { | ||||||
BusbarSectionResult result = getBusbarSectionResult(terminal); | ||||||
return result != null ? result.busbarSectionId() : null; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public TraverseResult traverse(Terminal terminal, boolean connected) { | ||||||
if (terminal.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) { | ||||||
firstTraversedBbsId = terminal.getConnectable().getId(); | ||||||
return TraverseResult.TERMINATE_TRAVERSER; | ||||||
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 TraverseResult.CONTINUE; | ||||||
return selectBestBusbar(allResults); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public TraverseResult traverse(Switch aSwitch) { | ||||||
if (onlyConnectedBbs && aSwitch.isOpen()) { | ||||||
return TraverseResult.TERMINATE_PATH; | ||||||
private static BusbarSectionResult selectBestBusbar(List<BusbarSectionResult> results) { | ||||||
List<BusbarSectionResult> withoutSwitch = results.stream().filter(r -> r.lastSwitch() == null).toList(); | ||||||
if (!withoutSwitch.isEmpty()) { | ||||||
return withoutSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)).orElse(null); | ||||||
} | ||||||
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)).orElse(null); | ||||||
} | ||||||
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)).orElse(null); | ||||||
} | ||||||
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 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
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 like this ?
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.
The term 'Path' is a generic name
it is ok for startNode and traversedSwitches