Skip to content

Commit 124e9da

Browse files
authored
fix connected components computation time (#645)
closes #504
1 parent 6100ee7 commit 124e9da

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PowerModels.jl Change Log
55
- Added native DC Power Flow solver and AdmittanceMatrix data structures
66
- Added PTDF-based OPF problem specification
77
- Added iterative flow limit cut OPF solvers (#619)
8+
- Improved connected components computation time (#504)
89
- Removed `Inf` bounds from variables (#630)
910
- Removal of unused functions in `solution.jl`: `get_solution`, `add_generator_power_setpoint`, `add_storage_setpoint`, `add_branch_flow_setpoint`, `add_dcline_flow_setpoint` (breaking) (#637)
1011

src/core/data.jl

+19-15
Original file line numberDiff line numberDiff line change
@@ -2419,30 +2419,32 @@ end
24192419
computes the connected components of the network graph
24202420
returns a set of sets of bus ids, each set is a connected component
24212421
"""
2422-
function calc_connected_components(data::Dict{String,<:Any}; edges=["branch", "dcline"])
2422+
function calc_connected_components(data::Dict{String,<:Any}; edges=["branch", "dcline", "switch"])
24232423
if InfrastructureModels.ismultinetwork(data)
24242424
Memento.error(_LOGGER, "connected_components does not yet support multinetwork data")
24252425
end
24262426

24272427
active_bus = Dict(x for x in data["bus"] if x.second["bus_type"] != 4)
24282428
active_bus_ids = Set{Int64}([bus["bus_i"] for (i,bus) in active_bus])
24292429

2430-
neighbors = Dict(i => [] for i in active_bus_ids)
2431-
for line_type in edges
2432-
for line in values(get(data, line_type, Dict()))
2433-
if get(line, "br_status", 1) != 0 && line["f_bus"] in active_bus_ids && line["t_bus"] in active_bus_ids
2434-
push!(neighbors[line["f_bus"]], line["t_bus"])
2435-
push!(neighbors[line["t_bus"]], line["f_bus"])
2430+
neighbors = Dict(i => Int[] for i in active_bus_ids)
2431+
for comp_type in edges
2432+
status_key = get(pm_component_status, comp_type, "status")
2433+
status_inactive = get(pm_component_status_inactive, comp_type, 0)
2434+
for edge in values(get(data, comp_type, Dict()))
2435+
if get(edge, status_key, 1) != status_inactive && edge["f_bus"] in active_bus_ids && edge["t_bus"] in active_bus_ids
2436+
push!(neighbors[edge["f_bus"]], edge["t_bus"])
2437+
push!(neighbors[edge["t_bus"]], edge["f_bus"])
24362438
end
24372439
end
24382440
end
24392441

2440-
component_lookup = Dict(i => Set{Int64}([i]) for i in active_bus_ids)
2442+
component_lookup = Dict(i => Set{Int}([i]) for i in active_bus_ids)
24412443
touched = Set{Int64}()
24422444

24432445
for i in active_bus_ids
24442446
if !(i in touched)
2445-
_dfs(i, neighbors, component_lookup, touched)
2447+
_cc_dfs(i, neighbors, component_lookup, touched)
24462448
end
24472449
end
24482450

@@ -2453,17 +2455,19 @@ end
24532455

24542456

24552457
"""
2456-
perModels DFS on a graph
2458+
DFS on a graph
24572459
"""
2458-
function _dfs(i, neighbors, component_lookup, touched)
2460+
function _cc_dfs(i, neighbors, component_lookup, touched)
24592461
push!(touched, i)
24602462
for j in neighbors[i]
24612463
if !(j in touched)
2462-
new_comp = union(component_lookup[i], component_lookup[j])
2463-
for k in new_comp
2464-
component_lookup[k] = new_comp
2464+
for k in component_lookup[j]
2465+
push!(component_lookup[i], k)
24652466
end
2466-
_dfs(j, neighbors, component_lookup, touched)
2467+
for k in component_lookup[j]
2468+
component_lookup[k] = component_lookup[i]
2469+
end
2470+
_cc_dfs(j, neighbors, component_lookup, touched)
24672471
end
24682472
end
24692473
end

0 commit comments

Comments
 (0)