From 0bbb7562cdf25405ca160c604222cf6d1ae25842 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 18 Jun 2024 09:27:52 +1200 Subject: [PATCH] Fix methods for calling value on a ::Number --- src/variables.jl | 8 ++++++-- test/test_variable.jl | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index 89f649580ef..64189327e34 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -2457,8 +2457,12 @@ function value(::AbstractArray{<:AbstractJuMPScalar}) ) end -value(::_MA.Zero) = 0.0 -value(x::Number) = x +# Fallback. See JuMP#3775 +value(::_MA.Zero; result::Int = 1) = 0.0 + +value(x::Number; result::Int = 1) = x + +value(::Function, x::Number) = x function _info_from_variable(v::GenericVariableRef) has_lb = has_lower_bound(v) diff --git a/test/test_variable.jl b/test/test_variable.jl index e744ae85723..22b5ff2ba93 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1651,4 +1651,15 @@ function test_variable_normalized_coefficient_vector_quadratic() return end +function test_value_number() + model = Model() + @variable(model, x, start = 2) + @expression(model, expr[i in 0:2], (1 + x)^i) + y = Containers.DenseAxisArray([1, 3, 9], 0:2) + @test ≈(value.(start_value, expr), y) + @test value(expr[0]; result = 1) == 1 + @test value(expr[0]; result = 2) == 1 + return +end + end # module TestVariable