diff --git a/src/aff_expr.jl b/src/aff_expr.jl index 86570a9ad2b..4c76f6ddc2e 100644 --- a/src/aff_expr.jl +++ b/src/aff_expr.jl @@ -204,8 +204,14 @@ function Base.one(::Type{GenericAffExpr{C,V}}) where {C,V} return GenericAffExpr{C,V}(one(C), OrderedDict{V,C}()) end +function Base.oneunit(::Type{GenericAffExpr{C,V}}) where {C,V} + return GenericAffExpr{C,V}(oneunit(C), OrderedDict{V,C}()) +end + Base.one(a::GenericAffExpr) = one(typeof(a)) +Base.oneunit(a::GenericAffExpr) = oneunit(typeof(a)) + Base.copy(a::GenericAffExpr) = GenericAffExpr(copy(a.constant), copy(a.terms)) Base.broadcastable(a::GenericAffExpr) = Ref(a) diff --git a/src/variables.jl b/src/variables.jl index 203722e9e00..7a4a16b5088 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -361,10 +361,16 @@ end Base.one(v::AbstractVariableRef) = one(typeof(v)) +Base.oneunit(v::AbstractVariableRef) = oneunit(typeof(v)) + function Base.one(::Type{V}) where {V<:AbstractVariableRef} return one(GenericAffExpr{value_type(V),V}) end +function Base.oneunit(::Type{V}) where {V<:AbstractVariableRef} + return oneunit(GenericAffExpr{value_type(V),V}) +end + """ coefficient(v1::GenericVariableRef{T}, v2::GenericVariableRef{T}) where {T} diff --git a/test/test_variable.jl b/test/test_variable.jl index 773c2527a2d..1d5750da8bc 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1616,4 +1616,12 @@ function test_variable_eltype() return end +function test_variable_one() + model = Model() + @variable(model, x) + @test Base.one(x) == 1 + @test Base.oneunit(x) == 1 + return +end + end # module TestVariable