Skip to content

Commit

Permalink
Merge a814c83 into 270141c
Browse files Browse the repository at this point in the history
  • Loading branch information
IainNZ committed Sep 9, 2018
2 parents 270141c + a814c83 commit 5ee4fbd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/JuMP.jl
Expand Up @@ -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

Expand Down
26 changes: 17 additions & 9 deletions test/operator.jl
Expand Up @@ -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
Expand Down

0 comments on commit 5ee4fbd

Please sign in to comment.