Skip to content

Commit 8feae60

Browse files
committed
Add canonical
1 parent 4c98be5 commit 8feae60

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/Utilities/functions.jl

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

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

10591082
"""

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)