jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.17k stars 390 forks source link

Add tests for issue #3736 #3737

Closed odow closed 2 months ago

odow commented 2 months ago

Closes #3736

Needs release with https://github.com/jump-dev/MutableArithmetics.jl/pull/283 before merging.

julia> function test_multiply_expr_MA_Zero()
           model = Model()
           @variable(model, x)
           for f in (x, x^2, sin(x))
               @test @expression(model, f * sum(x for i in 1:0)) == 0.0
               @test @expression(model, sum(x for i in 1:0) * f) == 0.0
               @test @expression(model, -f * sum(x for i in 1:0)) == 0.0
               @test @expression(model, sum(x for i in 1:0) * -f) == 0.0
               @test @expression(model, (f + f) * sum(x for i in 1:0)) == 0.0
               @test @expression(model, sum(x for i in 1:0) * (f + f)) == 0.0

               @test isequal_canonical(@expression(model, f + sum(x for i in 1:0)), f)
               @test isequal_canonical(@expression(model, sum(x for i in 1:0) + f), f)
               @test isequal_canonical(
                   @expression(model, -f + sum(x for i in 1:0)),
                   -1.0 * f,  # Needed for f = sin(x)
               )
               @test isequal_canonical(
                   @expression(model, sum(x for i in 1:0) + -f),
                   -1.0 * f,  # Needed for f = sin(x)
               )
               @test isequal_canonical(
                   @expression(model, (f + f) + sum(x for i in 1:0)),
                   f + f,
               )
               @test isequal_canonical(
                   @expression(model, sum(x for i in 1:0) + (f + f)),
                   f + f,
               )
           end
           return
       end
test_multiply_expr_MA_Zero (generic function with 1 method)

julia> @testset "" begin
           test_multiply_expr_MA_Zero()
       end
Test Summary: | Pass  Total  Time
              |   36     36  0.0s
Test.DefaultTestSet("", Any[], 36, false, false, true, 1.713934627962967e9, 1.713934627963343e9, false, "REPL[60]")
codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.40%. Comparing base (509ab61) to head (04f0fc9).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #3737 +/- ## ======================================= Coverage 98.40% 98.40% ======================================= Files 43 43 Lines 5820 5820 ======================================= Hits 5727 5727 Misses 93 93 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

odow commented 2 months ago

Where are the methods?

I added because the stack trace from #3736 includes

  [1] _instantiate_zero(::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:28
  [2] promote_operation_fallback(op::typeof(*), ::Type{QuadExpr}, ::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:51
  [3] promote_operation(::typeof(*), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:113
  [4] mutability(::Type, ::Function, ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:273
  [5] mutability(::QuadExpr, ::Function, ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:281
  [6] operate!!(::typeof(*), ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:616

This is going to additionally call zero(QuadExpr) to check whether the expression is mutable.

blegat commented 2 months ago

Then we should probably implement promote_operation instead ?

odow commented 2 months ago

Changed to implement promote_operation

blegat commented 2 months ago

This should be in MA next to https://github.com/jump-dev/MutableArithmetics.jl/blob/a6ed0f5ef5dfe67e463382d0b2d9ada60d77c673/src/rewrite.jl#L61

odow commented 2 months ago

See https://github.com/jump-dev/MutableArithmetics.jl/pull/284