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

Add checks and tests for NaN in constraints #2272

Merged
merged 1 commit into from
Jul 11, 2020
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
6 changes: 6 additions & 0 deletions src/aff_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ function _assert_isfinite(a::AffExpr)
for (coef, var) in linear_terms(a)
isfinite(coef) || error("Invalid coefficient $coef on variable $var.")
end
if isnan(a.constant)
error(
"Expression contains an invalid NaN constant. This could be " *
"produced by `Inf - Inf`."
)
end
end

"""
Expand Down
5 changes: 4 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ end
# three-argument build_constraint is used for two-sided constraints.
function build_constraint(_error::Function, func::AbstractJuMPScalar,
lb::Real, ub::Real)
if isnan(lb) || isnan(ub)
_error("Invalid bounds, cannot contain NaN: [$(lb), $(ub)].")
end
return build_constraint(_error, func, MOI.Interval(Float64(lb), Float64(ub)))
end

Expand Down Expand Up @@ -1182,7 +1185,7 @@ macro variable(args...)
_error("Ambiguous variable name $x detected. To specify an anonymous binary " *
"variable, use `@variable(model, binary = true)` instead.")
elseif x == :PSD
_error("Size of anonymous square matrix of positive semidefinite anonymous variables is not specified. To specify size of square matrix " *
_error("Size of anonymous square matrix of positive semidefinite anonymous variables is not specified. To specify size of square matrix " *
"use `@variable(model, [1:n, 1:n], PSD)` instead.")
end
anon_singleton = false
Expand Down
19 changes: 19 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,25 @@ end
) @NLparameter(model, p[axes(A)...] == x)
end

@testset "NaN in constraints" begin
model = Model()
@variable(model, x >= 0)
@test_throws(
ErrorException(
"Expression contains an invalid NaN constant. This could be produced by `Inf - Inf`."
),
@constraint(model, x >= NaN)
)
@test_throws ErrorException(
"Expression contains an invalid NaN constant. This could be produced by `Inf - Inf`."
) @constraint(model, 1 <= x + NaN <= 2)
@test_throws ErrorException(
"Expression contains an invalid NaN constant. This could be produced by `Inf - Inf`."
) @constraint(model, 1 <= x + Inf <= 2)
@test_throws ErrorException(
"In `@constraint(model, 1 <= x <= NaN)`: Invalid bounds, cannot contain NaN: [1, NaN]."
) @constraint(model, 1 <= x <= NaN)
end
end

@testset "Macros for JuMPExtension.MyModel" begin
Expand Down