diff --git a/src/Utilities/copy/index_map.jl b/src/Utilities/copy/index_map.jl index 34a446f3b1..080b73ac89 100644 --- a/src/Utilities/copy/index_map.jl +++ b/src/Utilities/copy/index_map.jl @@ -20,6 +20,9 @@ struct IndexMap <: AbstractDict{MOI.Index,MOI.Index} 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 """ @@ -33,6 +36,11 @@ function IndexMap() 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}, @@ -121,3 +129,21 @@ function map_indices(map::IndexMap, x::X)::X where {X<:_OnlyVariableIndices} end return map_indices(Base.Fix1(getindex, map), x) end + +# Common subexpressions, that is, `MOI.ScalarNonlinearFunction` objects that are +# 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 diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 0c3016bc7b..0186e03d37 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -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) @@ -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 @@ -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 diff --git a/test/FileFormats/NL/test_read.jl b/test/FileFormats/NL/test_read.jl index 6e8a456e4e..e9f7e0c747 100644 --- a/test/FileFormats/NL/test_read.jl +++ b/test/FileFormats/NL/test_read.jl @@ -648,8 +648,10 @@ function test_hs071() end model_print = sprint(print, model) _in_ = @static Sys.iswindows() ? "in" : "∈" + # Julia versions differ in how they print unary operators. + unary_plus = Expr(:call, :+, 2.0) for line in [ - "(1.1 * v[1] + 1.4 * v[2] + 1.2 * v[3] + 1.3 * v[4]) + +2.0", + "(1.1 * v[1] + 1.4 * v[2] + 1.2 * v[3] + 1.3 * v[4]) + $unary_plus", "v[1] >= 1.1", "v[2] >= 1.4", "v[3] >= 1.2", diff --git a/test/Utilities/test_copy.jl b/test/Utilities/test_copy.jl index f8800e494d..525e4f4d7c 100644 --- a/test/Utilities/test_copy.jl +++ b/test/Utilities/test_copy.jl @@ -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()