Skip to content

Commit

Permalink
fix: export combine
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlyAL15 committed Jun 19, 2024
1 parent 1b063e0 commit 93bc6fa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/OPFSDP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include("decompose/cliques.jl")
include("decompose/cliquetree.jl")
include("decompose/extension/extension.jl")
include("decompose/merge/merge.jl")
include("decompose/combine.jl")

using .ReadMatpower
using .ReadRawGo
Expand All @@ -36,6 +37,7 @@ export read_matpower, read_rawgo, read_network
export display_opf
export chordal_extension
export merge_cliques!, merge_molzahn!
export combine,
export solve

end
65 changes: 65 additions & 0 deletions src/utils/graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ function edges(adj::SparseMatrixCSC)
return edges_list
end

function nedge(adj::SparseMatrixCSC)
nedge = 0
for i in 1:adj.n-1
for j in i+1:adj.n
if adj[i, j] != 0 || adj[j, i] != 0
nedge += 1
end
end
end
return nedge
end

"""
peo = mcs(A)
Maximum cardinality search for graph adjacency matrix A.
Expand Down Expand Up @@ -112,6 +124,9 @@ function neighbors(adj::SparseMatrixCSC, v::Int; exclude=[v])
return [i for i in 1:adj.n if (adj[v, i] != 0 || adj[i, v] != 0) && i exclude]
end

function check_complete(adj::SparseMatrixCSC)
nedge(adj) == ((adj.n) * (adj.n - 1)) / 2
end

"""
is_complete = check_neighboor_complete(adj, v, v_sub)
Expand Down Expand Up @@ -193,3 +208,53 @@ function make_subgraph_complete!(adj::SparseMatrixCSC, vertices::Vector{Int})
end
end
end


function get_edges_under_dist(adj::SparseMatrixCSC, source::Int, max_dist::Int)
dist = ones(adj.n) * Inf
current_vertices = [source]
vertices = []
for i in 1:max_dist
next_vertices = []
for v in current_vertices
for b in neighbors(adj, v)
if !(b in vertices) && b != source
push!(vertices, b)
push!(next_vertices, b)
end
end
end
current_vertices = next_vertices
end
return vertices
end

function add_random_edge!(adj::SparseMatrixCSC, nb_edge::Int, max_dist::Int=3, max_stable_it::Int=5)
nedge_adj = nedge(adj)
nedge_max = (adj.n * (adj.n - 1)) / 2
stable_it = 0
while nb_edge > 0 && nedge_adj != nedge_max && stable_it < max_stable_it
v1 = rand(1:adj.n)
edges_choice = get_edges_under_dist(adj, v1, max_dist)
v2 = edges_choice[rand(1:length(edges_choice))]
if adj[v1, v2] == 1
stable_it += 1
continue
end
stable_it = 0
adj[v1, v2] = 1
adj[v2, v1] = 1
nedge_adj += 1
nb_edge -= 1
end
end

function add_random_edge_vertex!(adj::SparseMatrixCSC, nb_edge::Float64, max_dist::Int=3, max_stable_it::Int=5)
nb_edge_int = trunc(Int, nb_edge * adj.n)
add_random_edge!(adj, nb_edge_int, max_dist, max_stable_it)
end

function add_random_edge_edge!(adj::SparseMatrixCSC, nb_edge::Float64, max_dist::Int=3, max_stable_it::Int=5)
nb_edge_int = trunc(Int, nb_edge * nedge(adj))
add_random_edge!(adj, nb_edge_int, max_dist, max_stable_it)
end

0 comments on commit 93bc6fa

Please sign in to comment.