diff --git a/src/JuMP_wrapper.jl b/src/JuMP_wrapper.jl index 73d5d2f..331b860 100644 --- a/src/JuMP_wrapper.jl +++ b/src/JuMP_wrapper.jl @@ -15,7 +15,7 @@ include("operators.jl") end Iterator `iterators[index.value]`. -""" # TODO remove +""" struct IteratorInExpr iterators::Iterators index::IteratorIndex diff --git a/src/operators.jl b/src/operators.jl index 1fd2308..447b444 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -254,6 +254,12 @@ end JuMP._is_real(::LazySum) = true JuMP.variable_ref_type(s::LazySum) = JuMP.variable_ref_type(s.expr) + +# `JuMP.:+(::AbstractJuMPScalar, ::AbstractJuMPScalar)` calls `iszero` which +# defaults to `x == zero(x)`. A `LazySum` is never simplified away so we +# short-circuit it here, this avoids needing `zero(::Type{<:LazySum})`. +Base.iszero(::LazySum) = false + function JuMP.check_belongs_to_model(s::LazySum, model::JuMP.AbstractModel) return JuMP.check_belongs_to_model(s.expr, model) end diff --git a/test/operators.jl b/test/operators.jl index 001d2d4..8dd55c2 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -8,6 +8,7 @@ module TestOperators using Test using GenOpt import JuMP +import MathOptInterface as MOI function runtests() for name in names(@__MODULE__; all = true) @@ -65,6 +66,23 @@ function test_multivariate() @test ijxx isa GenOpt.ExprTemplate{JuMP.QuadExpr,JuMP.VariableRef} end +function test_lazy_sum_sum() + model = JuMP.Model() + JuMP.@variable(model, x[1:2]) + a = lazy_sum(x[i]^2 for i in 1:2) + b = lazy_sum(2 * x[i] for i in 1:2) + @test a isa GenOpt.LazySum{JuMP.QuadExpr,JuMP.VariableRef} + @test b isa GenOpt.LazySum{JuMP.AffExpr,JuMP.VariableRef} + JuMP.@objective(model, Min, a + b) + F = MOI.get(model, MOI.ObjectiveFunctionType()) + @test F == MOI.ScalarNonlinearFunction + func = MOI.get(model, MOI.ObjectiveFunction{F}()) + @test func.head == :+ + @test func.args[1] isa SumGenerator{MOI.ScalarQuadraticFunction{Float64}} + @test func.args[2] isa SumGenerator{MOI.ScalarAffineFunction{Float64}} + return +end + end # module TestOperators.runtests()