Skip to content

Commit 548e04b

Browse files
committed
Add canonical
1 parent 75eaa7e commit 548e04b

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/Utilities/functions.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,29 @@ function canonical(f::MOI.AbstractFunction)
10561056
return g
10571057
end
10581058

1059+
# Workaround: both `is_canonical` and `canonicalize!` would be slow otherwise
1060+
canonical(f::MOI.ScalarNonlinearFunction) = f
1061+
1062+
function canonical(f::MOI.ScalarNonlinearFunction)
1063+
cache = Dict{MOI.AbstractScalarFunction,MOI.AbstractScalarFunction}()
1064+
# Don't use recursion here. This gets called for all scalar nonlinear
1065+
# constraints.
1066+
stack = Any[arg for arg in f.args]
1067+
while !isempty(stack)
1068+
arg = pop!(stack)
1069+
if arg isa MOI.ScalarNonlinearFunction
1070+
for a in arg.args
1071+
push!(stack, a)
1072+
end
1073+
else
1074+
if !is_canonical(arg)
1075+
return false
1076+
end
1077+
end
1078+
end
1079+
return true
1080+
end
1081+
10591082
canonicalize!(f::Union{MOI.VectorOfVariables,MOI.VariableIndex}) = f
10601083

10611084
"""

src/Utilities/vector_of_constraints.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function MOI.add_constraint(
7373
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
7474
# We canonicalize the constraint so that solvers can avoid having to
7575
# canonicalize it most of the time (they can check if they need to with
76-
# `is_canonical`.
76+
# `is_canonical`).
7777
# Note that the canonicalization is not guaranteed if for instance
7878
# `modify` is called and adds a new term.
7979
# See https://github.com/jump-dev/MathOptInterface.jl/pull/1118

src/functions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function Base.copy(f::ScalarNonlinearFunction)
360360
# We need some sort of hint so that the next time we see this on the
361361
# stack we evaluate it using the args in `result_stack`. One option
362362
# would be a custom type. Or we can just wrap in (,) and then check
363-
# for a Tuple, which isn't (curretly) a valid argument.
363+
# for a Tuple, which isn't (currently) a valid argument.
364364
push!(stack, (arg,))
365365
for child in arg.args
366366
push!(stack, child)

0 commit comments

Comments
 (0)