From beb1826f9e1496d446afbd9c828c55a34a59fa52 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 15 Sep 2023 08:35:13 +1200 Subject: [PATCH] Add support for min and max operators in GenericNonlinearExpr (#3509) --- src/nlp_expr.jl | 4 ++-- test/test_nlp_expr.jl | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/nlp_expr.jl b/src/nlp_expr.jl index b76cbcd706b..8ec71d21cf8 100644 --- a/src/nlp_expr.jl +++ b/src/nlp_expr.jl @@ -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) diff --git a/test/test_nlp_expr.jl b/test/test_nlp_expr.jl index bd309f4c1d8..386fca80097 100644 --- a/test/test_nlp_expr.jl +++ b/test/test_nlp_expr.jl @@ -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