Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions src/Utilities/copy/index_map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
typeof(CleverDicts.index_to_key),
}
con_map::DoubleDicts.IndexDoubleDict
# This field is experimental and may be removed in a future release. Do not
# access it directly; use `map_indices` instead.
nl_cache::IdDict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}
end

"""
Expand All @@ -33,6 +36,11 @@
return IndexMap(var_map, con_map)
end

function IndexMap(var_map, con_map)
nl_cache = IdDict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}()
return IndexMap(var_map, con_map, nl_cache)
end

function _identity_constraints_map(
model,
map::DoubleDicts.IndexDoubleDictInner{F,S},
Expand Down Expand Up @@ -121,3 +129,21 @@
end
return map_indices(Base.Fix1(getindex, map), x)
end

# Common subexpressions, i.e., `MOI.ScalarNonlinearFunction` objects that are

Check failure on line 133 in src/Utilities/copy/index_map.jl

View workflow job for this annotation

GitHub Actions / build

[vale] reported by reviewdog 🐶 [Google.Latin] Use 'that is' instead of 'i.e.,'. Raw Output: {"message":"[Google.Latin] Use 'that is' instead of 'i.e.,'.","location":{"path":"src/Utilities/copy/index_map.jl","range":{"start":{"line":133,"column":26},"end":{"line":133,"column":31}}},"severity":"ERROR","code":{"value":"Google.Latin","url":"https://developers.google.com/style/abbreviations"}}
# shared between functions, are mapped only once, and the mapped functions
# share the corresponding mapped subexpressions.
function map_indices(map::IndexMap, f::MOI.ScalarNonlinearFunction)
if CleverDicts.is_identity(map.var_map)
return f
end
return map_indices(Base.Fix1(getindex, map), f, map.nl_cache)
end

function map_indices(map::IndexMap, f::MOI.VectorNonlinearFunction)
if CleverDicts.is_identity(map.var_map)
return f
end
rows = MOI.ScalarNonlinearFunction[map_indices(map, row) for row in f.rows]
return MOI.VectorNonlinearFunction(rows)
end
14 changes: 14 additions & 0 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ end
function map_indices(
index_map::F,
f::MOI.ScalarNonlinearFunction,
nl_cache = nothing,
) where {F<:Function}
if !isnothing(nl_cache) && haskey(nl_cache, f)
return nl_cache[f]
end
root = MOI.ScalarNonlinearFunction(f.head, similar(f.args))
stack = Tuple{MOI.ScalarNonlinearFunction,Int,MOI.ScalarNonlinearFunction}[]
for (i, fi) in enumerate(f.args)
Expand All @@ -380,6 +384,10 @@ function map_indices(
while !isempty(stack)
parent, i, arg = pop!(stack)
if arg isa MOI.ScalarNonlinearFunction
if !isnothing(nl_cache) && haskey(nl_cache, arg)
parent.args[i] = nl_cache[arg]
continue
end
child = MOI.ScalarNonlinearFunction(arg.head, similar(arg.args))
for (j, argj) in enumerate(arg.args)
if argj isa MOI.ScalarNonlinearFunction
Expand All @@ -389,10 +397,16 @@ function map_indices(
end
end
parent.args[i] = child
if !isnothing(nl_cache)
nl_cache[arg] = child
end
else
parent.args[i] = MOI.Utilities.map_indices(index_map, arg)
end
end
if !isnothing(nl_cache)
nl_cache[f] = root
end
return root
end

Expand Down
34 changes: 34 additions & 0 deletions test/Utilities/test_copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,40 @@ function test_map_indices_identity_index_map()
return
end

function test_map_indices_common_subexpressions()
x = MOI.VariableIndex.(1:2)
index_map = MOI.Utilities.IndexMap()
index_map[x[1]] = x[2]
index_map[x[2]] = x[1]
sin_x = MOI.ScalarNonlinearFunction(:sin, Any[x[1]])
f = MOI.ScalarNonlinearFunction(:+, Any[sin_x, sin_x, x[2]])
g = MOI.ScalarNonlinearFunction(:*, Any[2.0, sin_x])
f_dest = MOI.Utilities.map_indices(index_map, f)
@test f_dest ≈ MOI.ScalarNonlinearFunction(
:+,
Any[
MOI.ScalarNonlinearFunction(:sin, Any[x[2]]),
MOI.ScalarNonlinearFunction(:sin, Any[x[2]]),
x[1],
],
)
@test f_dest.args[1] === f_dest.args[2]
g_dest = MOI.Utilities.map_indices(index_map, g)
@test g_dest.args[2] === f_dest.args[1]
@test MOI.Utilities.map_indices(index_map, f) === f_dest
h = MOI.VectorNonlinearFunction([f, g])
h_dest = MOI.Utilities.map_indices(index_map, h)
@test h_dest.rows[1] === f_dest
@test h_dest.rows[2] === g_dest
# The cache is not used if the variable map is the identity.
identity_map = MOI.Utilities.IndexMap()
identity_map[x[1]] = x[1]
identity_map[x[2]] = x[2]
@test MOI.Utilities.map_indices(identity_map, f) === f
@test MOI.Utilities.map_indices(identity_map, h) === h
return
end

end # module

TestCopy.runtests()
Loading