Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include("operators.jl")
end

Iterator `iterators[index.value]`.
""" # TODO remove
"""
struct IteratorInExpr
iterators::Iterators
index::IteratorIndex
Expand Down
6 changes: 6 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Loading