Skip to content

Commit

Permalink
Add support for min and max operators in GenericNonlinearExpr (#3509)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Sep 14, 2023
1 parent 0c53e0b commit beb1826
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit beb1826

Please sign in to comment.