Skip to content

Commit 2298c58

Browse files
authored
Add has_start_value function (#3157)
1 parent ee382c1 commit 2298c58

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

docs/src/reference/variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ variable_by_name
3838
## Start values
3939

4040
```@docs
41+
has_start_value
4142
set_start_value
4243
start_value
4344
```

src/variables.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,15 @@ function start_value(v::VariableRef)::Union{Nothing,Float64}
10001000
return MOI.get(owner_model(v), MOI.VariablePrimalStart(), v)
10011001
end
10021002

1003+
"""
1004+
has_start_value(variable::AbstractVariableRef)
1005+
1006+
Return `true` if the variable has a start value set otherwise return `false`.
1007+
1008+
See also [`set_start_value`](@ref).
1009+
"""
1010+
has_start_value(v::AbstractVariableRef)::Bool = start_value(v) !== nothing
1011+
10031012
"""
10041013
set_start_value(variable::VariableRef, value::Union{Real,Nothing})
10051014

test/JuMPExtension.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ function JuMP.unfix(vref::MyVariableRef)
247247
)
248248
end
249249
function JuMP.start_value(vref::MyVariableRef)::Union{Nothing,Float64}
250-
return variable_info(vref).start
250+
info = variable_info(vref)
251+
return info.has_start ? info.start : nothing
251252
end
252253
function JuMP.set_start_value(vref::MyVariableRef, start)
253254
info = variable_info(vref)

test/variable.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ function test_variable_starts_set_get(ModelType, ::Any)
220220
JuMP.set_start_value.(x, x0)
221221
@test JuMP.start_value.(x) == x0
222222
@test JuMP.start_value.([x[1], x[2], x[3]]) == x0
223-
223+
@test JuMP.has_start_value(x[1]) == true
224224
@variable(model, y[1:3, 1:2])
225+
@test JuMP.has_start_value(y[1, 1]) == false
225226
@test_throws DimensionMismatch JuMP.set_start_value.(y, collect(1:6))
226227
end
227228

0 commit comments

Comments
 (0)