diff --git a/src/JuMP.jl b/src/JuMP.jl index 776ec1996e8..fd73703d2c0 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -724,14 +724,16 @@ function operator_warn(::AbstractModel) end function operator_warn(model::Model) model.operator_counter += 1 if model.operator_counter > 20000 - Base.warn_once( + Compat.@warn( "The addition operator has been used on JuMP expressions a large " * "number of times. This warning is safe to ignore but may " * "indicate that model generation is slower than necessary. For " * "performance reasons, you should not add expressions in a loop. " * "Instead of x += y, use add_to_expression!(x,y) to modify x in " * "place. If y is a single variable, you may also use " * - "add_to_expression!(x, coef, y) for x += coef*y.") + "add_to_expression!(x, coef, y) for x += coef*y.", maxlog=1) + # NOTE: On Julia 1.0 (at least), maxlog=1 does not work correctly. + # See https://github.com/JuliaLang/julia/issues/28786. end end diff --git a/test/operator.jl b/test/operator.jl index d9486a420bc..66368f24dcd 100644 --- a/test/operator.jl +++ b/test/operator.jl @@ -318,15 +318,23 @@ function operators_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType:: @test dot(floats, anys) == 10 + 40 + 2x end - @testset "JuMP PR #943" begin - pull943 = ModelType() - @variable(pull943, x[1 : 10^6]); - JuMP.set_start_value.(x, 1 : 10^6) - @expression(pull943, testsum, sum(x[i] * i for i = 1 : 10^6)) - @expression(pull943, testdot1, dot(x, 1 : 10^6)) - @expression(pull943, testdot2, dot(1 : 10^6, x)) - @test JuMP.value(testsum, JuMP.start_value) ≈ JuMP.value(testdot1, JuMP.start_value) - @test JuMP.value(testsum, JuMP.start_value) ≈ JuMP.value(testdot2, JuMP.start_value) + if ModelType <: Model + # Only `Model` guaranteed to have `operator_counter`, so + # only test for that case. + @testset "JuMP PR #943" begin + pull943 = ModelType() + @test pull943.operator_counter == 0 + @variable(pull943, x[1:100]) + JuMP.set_start_value.(x, 1:100) + @expression(pull943, testsum, sum(x[i] * i for i in 1:100)) + @expression(pull943, testdot1, dot(x, 1:100)) + @expression(pull943, testdot2, dot(1:100, x)) + @test pull943.operator_counter == 0 + testadd = testdot1 + testdot2 + @test pull943.operator_counter == 1 # Check triggerable. + @test JuMP.value(testsum, JuMP.start_value) ≈ JuMP.value(testdot1, JuMP.start_value) + @test JuMP.value(testsum, JuMP.start_value) ≈ JuMP.value(testdot2, JuMP.start_value) + end end end end