Skip to content

Don't copy ScalarNonlinearFunction in VectorOfConstraints #2803

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,29 @@ function canonical(f::MOI.AbstractFunction)
return g
end

# Workaround: both `is_canonical` and `canonicalize!` would be slow otherwise
canonical(f::MOI.ScalarNonlinearFunction) = f

function canonical(f::MOI.ScalarNonlinearFunction)
cache = Dict{MOI.AbstractScalarFunction,MOI.AbstractScalarFunction}()
# Don't use recursion here. This gets called for all scalar nonlinear
# constraints.
stack = Any[arg for arg in f.args]
while !isempty(stack)
arg = pop!(stack)
if arg isa MOI.ScalarNonlinearFunction
for a in arg.args
push!(stack, a)
end
else
if !is_canonical(arg)
return false
end
end
end
return true
end

canonicalize!(f::Union{MOI.VectorOfVariables,MOI.VariableIndex}) = f

"""
Expand Down
11 changes: 9 additions & 2 deletions src/Utilities/vector_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function MOI.add_constraint(
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
# We canonicalize the constraint so that solvers can avoid having to
# canonicalize it most of the time (they can check if they need to with
# `is_canonical`.
# `is_canonical`).
# Note that the canonicalization is not guaranteed if for instance
# `modify` is called and adds a new term.
# See https://github.com/jump-dev/MathOptInterface.jl/pull/1118
Expand Down Expand Up @@ -103,7 +103,14 @@ function MOI.get(
) where {F,S}
MOI.throw_if_not_valid(v, ci)
f, _ = v.constraints[ci]::Tuple{F,S}
return copy(f)
# Since `MA.mutability(MOI.ScalarNonlinearFunction)` is `MA.IsNotMutable`,
# this does not copy `MOI.ScalarNonlinearFunction`. This is important if the
# function share aliases of the same subexpression at different parts of
# it's expression graph or the expression graph of other functions of the
# model. If we `copy`, they won't be aliases of the same subexpression
# anymore hence `MOI.Nonlinear.ReverseAD` won't detect them as common
# subexpressions.
return MA.copy_if_mutable(f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ function Base.copy(f::ScalarNonlinearFunction)
# We need some sort of hint so that the next time we see this on the
# stack we evaluate it using the args in `result_stack`. One option
# would be a custom type. Or we can just wrap in (,) and then check
# for a Tuple, which isn't (curretly) a valid argument.
# for a Tuple, which isn't (currently) a valid argument.
push!(stack, (arg,))
for child in arg.args
push!(stack, child)
Expand Down
Loading