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

Add support for min and max operators in GenericNonlinearExpr #3509

Merged
merged 1 commit into from
Sep 14, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,13 @@ end

# Multivariate operators

# The multivariate operators in MOI are +, -, *, ^, /, ifelse, atan
# The multivariate operators in MOI are +, -, *, ^, /, ifelse, atan, min, max
#
# However, ifelse is a builtin, so we can't add methods to it.

# We need only very generic fallbacks for these, because all other cases are
# caught with more specific methods.
for f in (:+, :-, :*, :^, :/, :atan)
for f in (:+, :-, :*, :^, :/, :atan, :min, :max)
op = Meta.quot(f)
@eval begin
function Base.$(f)(x::AbstractJuMPScalar, y::_Constant)
Expand Down
16 changes: 16 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,20 @@ function test_generic_nonlinear_expr_infer_variable_type()
return
end

function test_operator_min()
model = Model()
@variable(model, x)
@test isequal_canonical(min(x, 1), NonlinearExpr(:min, Any[x, 1.0]))
@test isequal_canonical(min(1, x, x^2), min(min(1.0, x), x^2))
return
end

function test_operator_max()
model = Model()
@variable(model, x)
@test isequal_canonical(max(x, 1), NonlinearExpr(:max, Any[x, 1.0]))
@test isequal_canonical(max(1, x, x^2), max(max(1.0, x), x^2))
return
end

end # module