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

Throw better error for non-constant variable bounds and starting value #3583

Merged
merged 3 commits into from
Nov 26, 2023
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
29 changes: 25 additions & 4 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,27 @@ function _moi_add_variable(
return var_ref
end

_to_value(::Type{T}, value::T, ::String) where {T} = value

function _to_value(::Type{T}, value, msg::String) where {T}
try
return convert(T, value)
catch
error(
"Unable to use `$value::$(typeof(value))` as the $msg of a " *
"variable because it is not convertable to type `::$T`.",
)
end
end

function _to_value(::Type{T}, value::AbstractJuMPScalar, msg::String) where {T}
return error(
"Unable to use `$value::$(typeof(value))` as the $msg of a variable. " *
"The $msg must be a constant value of type `::$T`. You cannot use " *
"JuMP variables or expressions.",
)
end

function _moi_constrain_variable(
moi_backend::MOI.ModelLike,
index,
Expand All @@ -1762,21 +1783,21 @@ function _moi_constrain_variable(
_moi_add_constraint(
moi_backend,
index,
MOI.GreaterThan{T}(info.lower_bound),
MOI.GreaterThan{T}(_to_value(T, info.lower_bound, "lower bound")),
)
end
if info.has_ub
_moi_add_constraint(
moi_backend,
index,
MOI.LessThan{T}(info.upper_bound),
MOI.LessThan{T}(_to_value(T, info.upper_bound, "upper bound")),
)
end
if info.has_fix
_moi_add_constraint(
moi_backend,
index,
MOI.EqualTo{T}(info.fixed_value),
MOI.EqualTo{T}(_to_value(T, info.fixed_value, "fixed value")),
)
end
if info.binary
Expand All @@ -1790,7 +1811,7 @@ function _moi_constrain_variable(
moi_backend,
MOI.VariablePrimalStart(),
index,
convert(T, info.start),
_to_value(T, info.start, "start value"),
)
end
end
Expand Down
32 changes: 32 additions & 0 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1569,4 +1569,36 @@ function test_variable_ref_type_unsupported()
return
end

function test_bad_bound_types()
T = Int
model = GenericModel{T}()
function err(value, msg, T)
return ErrorException(
"Unable to use `$value::$(typeof(value))` as the $msg of a " *
"variable because it is not convertable to type `::$T`.",
)
end
for v in (1.2, "abc", :d)
@test_throws err(v, "upper bound", T) @variable(model, x <= v)
@test_throws err(v, "lower bound", T) @variable(model, x >= v)
@test_throws err(v, "fixed value", T) @variable(model, x == v)
@test_throws err(v, "start value", T) @variable(model, x, start = v)
end
function err2(value, msg, T)
return ErrorException(
"Unable to use `$value::$(typeof(value))` as the $msg of a variable. " *
"The $msg must be a constant value of type `::$T`. You cannot use " *
"JuMP variables or expressions.",
)
end
@variable(model, y)
for v in (y, T(2) * y, sin(y))
@test_throws err2(v, "upper bound", T) @variable(model, x <= v)
@test_throws err2(v, "lower bound", T) @variable(model, x >= v)
@test_throws err2(v, "fixed value", T) @variable(model, x == v)
@test_throws err2(v, "start value", T) @variable(model, x, start = v)
end
return
end

end # module TestVariable
Loading