Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix methods for calling value on a ::Number #3776

Merged
merged 1 commit into from
Jun 17, 2024
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
8 changes: 6 additions & 2 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading