Skip to content

Commit

Permalink
Fix operator_warn
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed May 26, 2018
1 parent 167a34b commit ca95f76
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JuMP.jl
Expand Up @@ -465,7 +465,7 @@ end
function operator_warn(lhs::GenericAffExpr,rhs::GenericAffExpr)
if length(linearterms(lhs)) > 50 || length(linearterms(rhs)) > 50
if length(linearterms(lhs)) > 1
m = first(linearterms(lhs))[1].m
m = first(linearterms(lhs))[2].m
m.operator_counter += 1
if m.operator_counter > 20000
Base.warn_once("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 append!(x,y) to modify x in place. If y is a single variable, you may also use push!(x, coef, y) in place of x += coef*y.")
Expand Down
10 changes: 10 additions & 0 deletions test/operator.jl
Expand Up @@ -643,4 +643,14 @@ Base.transpose(t::MySumType) = MySumType(t.a)
@test z[i].a == y[i].a == a
end
end

@testset "operator_warn" begin
m = Model()
@variable m x[1:51]
@test m.operator_counter == 0
# Triggers the increment of operator_counter since sum(x) has more than 50 terms
@test_expression(sum(x) + 2x[1])
# The following check verifies that this test covers the code incrementing `operator_counter`
@test m.operator_counter == 1
end
end

0 comments on commit ca95f76

Please sign in to comment.