Skip to content
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

Refactor GenericNonlinearExpr and NonlinearOperator constructors #3489

Merged
merged 7 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 23 additions & 42 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,11 @@ struct GenericNonlinearExpr{V<:AbstractVariableRef} <: AbstractJuMPScalar
head::Symbol
args::Vector{Any}

function GenericNonlinearExpr(head::Symbol, args::Vector{Any})
index = findfirst(Base.Fix2(isa, AbstractJuMPScalar), args)
if index === nothing
error(
"Unable to create a nonlinear expression because it did not " *
"contain any JuMP scalars. head = $head, args = $args.",
)
end
return new{variable_ref_type(args[index])}(head, args)
Comment on lines -84 to -92
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the motivation for this. Unfortunately, it will break InfiniteOpt, but I can look into refactoring the workflow. If supporting array arguments necessitates this change, then I support it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably just keep this method then. It's not necessary. Let me make some changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that this works with InfiniteOpt: infiniteopt/InfiniteOpt.jl#321

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, you beat me to it :)

function GenericNonlinearExpr{V}(
head::Symbol,
args::Vararg{Any}
) where {V<:AbstractVariableRef}
return new{V}(head, Any[a for a in args])
end

function GenericNonlinearExpr{V}(
Expand All @@ -110,15 +106,6 @@ const NonlinearExpr = GenericNonlinearExpr{VariableRef}

variable_ref_type(::GenericNonlinearExpr{V}) where {V} = V

# We include this method so that we can refactor the internal representation of
# GenericNonlinearExpr without having to rewrite the method overloads.
function GenericNonlinearExpr{V}(
head::Symbol,
args...,
) where {V<:AbstractVariableRef}
return GenericNonlinearExpr{V}(head, Any[args...])
end

const _PREFIX_OPERATORS =
(:+, :-, :*, :/, :^, :||, :&&, :>, :<, :(<=), :(>=), :(==))

Expand Down Expand Up @@ -502,6 +489,8 @@ end

moi_function(x::Number) = x

moi_function(x::AbstractArray) = moi_function.(x)
odow marked this conversation as resolved.
Show resolved Hide resolved

function moi_function(f::GenericNonlinearExpr{V}) where {V}
ret = MOI.ScalarNonlinearFunction(f.head, similar(f.args))
stack = Tuple{MOI.ScalarNonlinearFunction,Int,GenericNonlinearExpr{V}}[]
Expand All @@ -527,6 +516,10 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
return ret
end

jump_function(::GenericModel{T}, x::Number) where {T} = convert(T, x)

jump_function(model::GenericModel, x::AbstractArray) = jump_function.(model, x)
odow marked this conversation as resolved.
Show resolved Hide resolved

function jump_function(model::GenericModel, f::MOI.ScalarNonlinearFunction)
V = variable_ref_type(typeof(model))
ret = GenericNonlinearExpr{V}(f.head, Any[])
Expand All @@ -542,8 +535,6 @@ function jump_function(model::GenericModel, f::MOI.ScalarNonlinearFunction)
for child in reverse(arg.args)
push!(stack, (new_ret, child))
end
elseif arg isa Number
push!(parent.args, arg)
else
push!(parent.args, jump_function(model, arg))
end
Expand Down Expand Up @@ -833,33 +824,23 @@ function Base.show(io::IO, f::NonlinearOperator)
return print(io, "NonlinearOperator($(f.func), :$(f.head))")
end

# Fast overload for unary calls

(f::NonlinearOperator)(x) = f.func(x)

(f::NonlinearOperator)(x::AbstractJuMPScalar) = NonlinearExpr(f.head, Any[x])

# Fast overload for binary calls

(f::NonlinearOperator)(x, y) = f.func(x, y)

function (f::NonlinearOperator)(x::AbstractJuMPScalar, y)
return GenericNonlinearExpr(f.head, Any[x, y])
end
variable_ref_type(::NonlinearOperator, ::Any) = nothing
odow marked this conversation as resolved.
Show resolved Hide resolved

function (f::NonlinearOperator)(x, y::AbstractJuMPScalar)
return GenericNonlinearExpr(f.head, Any[x, y])
function variable_ref_type(::NonlinearOperator, x::AbstractJuMPScalar)
return variable_ref_type(x)
end

function (f::NonlinearOperator)(x::AbstractJuMPScalar, y::AbstractJuMPScalar)
return GenericNonlinearExpr(f.head, Any[x, y])
function variable_ref_type(
::NonlinearOperator,
::AbstractArray{T},
) where {T<:AbstractJuMPScalar}
return variable_ref_type(T)
odow marked this conversation as resolved.
Show resolved Hide resolved
end

# Fallback for more arguments
function (f::NonlinearOperator)(x, y, z...)
args = (x, y, z...)
if any(Base.Fix2(isa, AbstractJuMPScalar), args)
return GenericNonlinearExpr(f.head, Any[a for a in args])
function (f::NonlinearOperator)(args::Vararg{Any,N}) where {N}
types = variable_ref_type.(Ref(f), args)
if (i = findfirst(!isnothing, types)) !== nothing
return GenericNonlinearExpr{types[i]}(f.head, args...)
end
return f.func(args...)
end
Expand Down
11 changes: 5 additions & 6 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,27 @@ function Base.:/(lhs::GenericAffExpr, rhs::_Constant)
return map_coefficients(c -> c / rhs, lhs)
end

function Base.:^(lhs::AbstractVariableRef, rhs::Integer)
T = value_type(typeof(lhs))
function Base.:^(lhs::V, rhs::Integer) where {V<:AbstractVariableRef}
if rhs == 0
return one(T)
return one(value_type(V))
elseif rhs == 1
return lhs
elseif rhs == 2
return lhs * lhs
else
return GenericNonlinearExpr(:^, Any[lhs, rhs])
return GenericNonlinearExpr{V}(:^, Any[lhs, rhs])
end
end

function Base.:^(lhs::GenericAffExpr{T}, rhs::Integer) where {T}
function Base.:^(lhs::GenericAffExpr{T,V}, rhs::Integer) where {T,V}
if rhs == 0
return one(T)
elseif rhs == 1
return lhs
elseif rhs == 2
return lhs * lhs
else
return GenericNonlinearExpr(:^, Any[lhs, rhs])
return GenericNonlinearExpr{V}(:^, Any[lhs, rhs])
end
end

Expand Down
12 changes: 6 additions & 6 deletions test/test_nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ function test_parse_expression_nonlinearexpr_call()
model = Model()
@variable(model, x)
@variable(model, y)
f = GenericNonlinearExpr(:ifelse, Any[x, 0, y])
f = NonlinearExpr(:ifelse, Any[x, 0, y])
@NLexpression(model, ref, f)
nlp = nonlinear_model(model)
expr = :(ifelse($x, 0, $y))
Expand All @@ -1617,7 +1617,7 @@ function test_parse_expression_nonlinearexpr_or()
model = Model()
@variable(model, x)
@variable(model, y)
f = GenericNonlinearExpr(:||, Any[x, y])
f = NonlinearExpr(:||, Any[x, y])
@NLexpression(model, ref, f)
nlp = nonlinear_model(model)
expr = :($x || $y)
Expand All @@ -1629,7 +1629,7 @@ function test_parse_expression_nonlinearexpr_and()
model = Model()
@variable(model, x)
@variable(model, y)
f = GenericNonlinearExpr(:&&, Any[x, y])
f = NonlinearExpr(:&&, Any[x, y])
@NLexpression(model, ref, f)
nlp = nonlinear_model(model)
expr = :($x && $y)
Expand All @@ -1641,7 +1641,7 @@ function test_parse_expression_nonlinearexpr_unsupported()
model = Model()
@variable(model, x)
@variable(model, y)
f = GenericNonlinearExpr(:foo, Any[x, y])
f = NonlinearExpr(:foo, Any[x, y])
@test_throws(
MOI.UnsupportedNonlinearOperator,
@NLexpression(model, ref, f),
Expand All @@ -1653,8 +1653,8 @@ function test_parse_expression_nonlinearexpr_nested_comparison()
model = Model()
@variable(model, x)
@variable(model, y)
f = GenericNonlinearExpr(:||, Any[x, y])
g = GenericNonlinearExpr(:&&, Any[f, x])
f = NonlinearExpr(:||, Any[x, y])
g = NonlinearExpr(:&&, Any[f, x])
@NLexpression(model, ref, g)
nlp = nonlinear_model(model)
expr = :(($x || $y) && $x)
Expand Down
73 changes: 43 additions & 30 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -447,46 +447,46 @@ function test_extension_nl_macro(
@variable(model, x)
@test isequal_canonical(
@expression(model, ifelse(x, 1, 2)),
GenericNonlinearExpr(:ifelse, Any[x, 1, 2]),
GenericNonlinearExpr{VariableRefType}(:ifelse, Any[x, 1, 2]),
)
@test isequal_canonical(
@expression(model, x || 1),
GenericNonlinearExpr(:||, Any[x, 1]),
GenericNonlinearExpr{VariableRefType}(:||, Any[x, 1]),
)
@test isequal_canonical(
@expression(model, x && 1),
GenericNonlinearExpr(:&&, Any[x, 1]),
GenericNonlinearExpr{VariableRefType}(:&&, Any[x, 1]),
)
@test isequal_canonical(
@expression(model, x < 0),
GenericNonlinearExpr(:<, Any[x, 0]),
GenericNonlinearExpr{VariableRefType}(:<, Any[x, 0]),
)
@test isequal_canonical(
@expression(model, x > 0),
GenericNonlinearExpr(:>, Any[x, 0]),
GenericNonlinearExpr{VariableRefType}(:>, Any[x, 0]),
)
@test isequal_canonical(
@expression(model, x <= 0),
GenericNonlinearExpr(:<=, Any[x, 0]),
GenericNonlinearExpr{VariableRefType}(:<=, Any[x, 0]),
)
@test isequal_canonical(
@expression(model, x >= 0),
GenericNonlinearExpr(:>=, Any[x, 0]),
GenericNonlinearExpr{VariableRefType}(:>=, Any[x, 0]),
)
@test isequal_canonical(
@expression(model, x == 0),
GenericNonlinearExpr(:(==), Any[x, 0]),
GenericNonlinearExpr{VariableRefType}(:(==), Any[x, 0]),
)
@test isequal_canonical(
@expression(model, 0 < x <= 1),
GenericNonlinearExpr(
GenericNonlinearExpr{VariableRefType}(
:&&,
Any[@expression(model, 0 < x), @expression(model, x <= 1)],
),
)
@test isequal_canonical(
@expression(model, ifelse(x > 0, x^2, sin(x))),
GenericNonlinearExpr(
GenericNonlinearExpr{VariableRefType}(
:ifelse,
Any[@expression(model, x > 0), x^2, sin(x)],
),
Expand All @@ -501,7 +501,7 @@ function test_register_univariate()
@test f isa NonlinearOperator
@test sprint(show, f) == "NonlinearOperator($(f.func), :f)"
@test isequal_canonical(@expression(model, f(x)), f(x))
@test isequal_canonical(f(x), GenericNonlinearExpr(:f, Any[x]))
@test isequal_canonical(f(x), NonlinearExpr(:f, Any[x]))
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test MOI.UserDefinedFunction(:f, 1) in attrs
return
Expand All @@ -522,7 +522,7 @@ function test_register_univariate_gradient()
@variable(model, x)
@operator(model, f, 1, x -> x^2, x -> 2 * x)
@test isequal_canonical(@expression(model, f(x)), f(x))
@test isequal_canonical(f(x), GenericNonlinearExpr(:f, Any[x]))
@test isequal_canonical(f(x), NonlinearExpr(:f, Any[x]))
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test MOI.UserDefinedFunction(:f, 1) in attrs
return
Expand All @@ -533,7 +533,7 @@ function test_register_univariate_gradient_hessian()
@variable(model, x)
@operator(model, f, 1, x -> x^2, x -> 2 * x, x -> 2.0)
@test isequal_canonical(@expression(model, f(x)), f(x))
@test isequal_canonical(f(x), GenericNonlinearExpr(:f, Any[x]))
@test isequal_canonical(f(x), NonlinearExpr(:f, Any[x]))
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test MOI.UserDefinedFunction(:f, 1) in attrs
return
Expand All @@ -545,7 +545,7 @@ function test_register_multivariate()
f = (x...) -> sum(x .^ 2)
@operator(model, foo, 2, f)
@test isequal_canonical(@expression(model, foo(x...)), foo(x...))
@test isequal_canonical(foo(x...), GenericNonlinearExpr(:foo, Any[x...]))
@test isequal_canonical(foo(x...), NonlinearExpr(:foo, Any[x...]))
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test MOI.UserDefinedFunction(:foo, 2) in attrs
return
Expand All @@ -558,7 +558,7 @@ function test_register_multivariate_gradient()
∇f = (g, x...) -> (g .= 2 .* x)
@operator(model, foo, 2, f, ∇f)
@test isequal_canonical(@expression(model, foo(x...)), foo(x...))
@test isequal_canonical(foo(x...), GenericNonlinearExpr(:foo, Any[x...]))
@test isequal_canonical(foo(x...), NonlinearExpr(:foo, Any[x...]))
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test MOI.UserDefinedFunction(:foo, 2) in attrs
return
Expand All @@ -576,7 +576,7 @@ function test_register_multivariate_gradient_hessian()
end
@operator(model, foo, 2, f, ∇f, ∇²f)
@test isequal_canonical(@expression(model, foo(x...)), foo(x...))
@test isequal_canonical(foo(x...), GenericNonlinearExpr(:foo, Any[x...]))
@test isequal_canonical(foo(x...), NonlinearExpr(:foo, Any[x...]))
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test MOI.UserDefinedFunction(:foo, 2) in attrs
return
Expand All @@ -587,7 +587,7 @@ function test_register_multivariate_many_args()
@variable(model, x[1:10])
f = (x...) -> sum(x .^ 2)
@operator(model, foo, 10, f)
@test isequal_canonical(foo(x...), GenericNonlinearExpr(:foo, Any[x...]))
@test isequal_canonical(foo(x...), NonlinearExpr(:foo, Any[x...]))
@test foo((1:10)...) == 385
return
end
Expand All @@ -607,18 +607,6 @@ function test_register_errors()
return
end

function test_expression_no_variable()
head, args = :sin, Any[1]
@test_throws(
ErrorException(
"Unable to create a nonlinear expression because it did not " *
"contain any JuMP scalars. head = $head, args = $args.",
),
GenericNonlinearExpr(head, args),
)
return
end

function test_value_expression()
model = Model()
@variable(model, x)
Expand Down Expand Up @@ -676,7 +664,7 @@ end
function test_nonlinear_expr_owner_model()
model = Model()
@variable(model, x)
f = GenericNonlinearExpr(:sin, Any[x])
f = NonlinearExpr(:sin, Any[x])
# This shouldn't happen in regular code, but let's test against it to check
# we get something similar to AffExpr and QuadExpr.
empty!(f.args)
Expand Down Expand Up @@ -900,4 +888,29 @@ function test_ma_zero_in_operate!!()
return
end

function test_nonlinear_operator_vector_args()
model = Model()
@variable(model, x[1:2, 1:2])
op_det = NonlinearOperator(LinearAlgebra.det, :det)
@objective(model, Min, log(op_det(x)))
@test isequal_canonical(objective_function(model), log(op_det(x)))
f = MOI.get(model, MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction}())
g = MOI.ScalarNonlinearFunction(:det, Any[index.(x)])
@test f ≈ MOI.ScalarNonlinearFunction(:log, Any[g])
return
end

function test_nonlinear_operator_inferred()
model = Model()
@variable(model, x)
@inferred op_less_than_or_equal_to(x, 1)
@test @inferred(op_less_than_or_equal_to(1, 2)) == true
@variable(model, y[1:2, 1:2])
op_det = NonlinearOperator(LinearAlgebra.det, :det)
@inferred log(op_det(y))
z = rand(2, 2)
@inferred log(op_det(z))
return
end

end # module
2 changes: 1 addition & 1 deletion test/test_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ function test_complex_pow()
@test y^0 == (1.0 + 0im)
@test y^1 == y
@test y^2 == y * y
@test isequal_canonical(y^3, GenericNonlinearExpr(:^, Any[y, 3]))
@test isequal_canonical(y^3, NonlinearExpr(:^, Any[y, 3]))
return
end

Expand Down