Skip to content
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

Removed inefficient setdiff calls #125

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/backends/moi_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ end
# Graph MOI Utilities
#

# NOTE: These utilities are meant to take model expressions defined over optinodes and
# NOTE: These utilities are meant to take model expressions defined over optinodes and
# map them to the underlying optigraph backend indices. This way, nodes and edges
# can be mapped to multiple possible optigraph backends.

Expand Down Expand Up @@ -718,9 +718,10 @@ function _add_backend_variables(backend::GraphMOIBackend, var::NodeVariableRef)
end

function _add_backend_variables(backend::GraphMOIBackend, vars::Vector{NodeVariableRef})
vars_to_add = setdiff(vars, keys(backend.element_to_graph_map.var_map))
for var in vars_to_add
MOI.add_variable(backend, var)
for var in vars
if !(var in keys(backend.element_to_graph_map.var_map))
MOI.add_variable(backend, var)
end
end
return nothing
end
Expand Down Expand Up @@ -760,8 +761,8 @@ end
# Aggregate MOI backends
#

# Note that these methods do not create copies of nodes or edges; they create model
# data for new backends. The nodes and edges will then reference data for multiple backends.
# Note that these methods do not create copies of nodes or edges; they create model
# data for new backends. The nodes and edges will then reference data for multiple backends.

"""
_copy_subgraph_backends!(graph::OptiGraph)
Expand Down Expand Up @@ -888,11 +889,12 @@ function _copy_node_variables(
# add new MOI variables to the destination MOI backend
# note that existing node variables are not copied per-se; the references
# now point to multiple MOI backends.
vars_to_add = setdiff(node_variables, keys(dest.element_to_graph_map.var_map))
for var in vars_to_add
src_graph_index = graph_index(var)
dest_graph_index = MOI.add_variable(dest, var)
index_map[src_graph_index] = dest_graph_index
for var in node_variables
if !(var in keys(dest.element_to_graph_map.var_map))
src_graph_index = graph_index(var)
dest_graph_index = MOI.add_variable(dest, var)
index_map[src_graph_index] = dest_graph_index
end
end

# pass variable attributes
Expand Down
Loading