Skip to content

Commit

Permalink
Added type info in NL error message (#1872)
Browse files Browse the repository at this point in the history
* Added type info in NL error message

* Fix typo (twice)

* Add tests for errors when using Aff/Quadx in NLexpressions

* Fix typo
  • Loading branch information
coreysharris authored and mlubin committed May 12, 2019
1 parent ebd368b commit 4811cd2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/parse_nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,17 @@ end
function _parse_NL_expr_runtime(m::Model, x::GenericQuadExpr, tape, parent, values)
error("Unexpected quadratic expression $x in nonlinear expression. " *
"Quadratic expressions (e.g., created using @expression) and " *
"nonlinear expression cannot be mixed.")
"nonlinear expressions cannot be mixed.")
end

function _parse_NL_expr_runtime(m::Model, x::GenericAffExpr, tape, parent, values)
error("Unexpected affine expression $x in nonlinear expression. " *
"Affine expressions (e.g., created using @expression) and " *
"nonlinear expressions cannot be mixed.")
end

function _parse_NL_expr_runtime(m::Model, x, tape, parent, values)
error("Unexpected object $x in nonlinear expression.")
error("Unexpected object $x (of type $(typeof(x)) in nonlinear expression.")
end

function _expression_complexity(ex::Expr)
Expand Down
26 changes: 26 additions & 0 deletions test/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -619,4 +619,30 @@
evaluator = JuMP.NLPEvaluator(model)
@test !(:Hess in MOI.features_available(evaluator))
end

@testset "Error on using AffExpr in NLexpression" begin
model = Model()
@variable(model, x)
@variable(model, y)
A = x + y
expected_exception = ErrorException(
"Unexpected affine expression x + y in nonlinear expression. " *
"Affine expressions (e.g., created using @expression) and " *
"nonlinear expressions cannot be mixed."
)
@test_throws expected_exception @NLexpression(model, A)
end

@testset "Error on using QuadExpr in NLexpression" begin
model = Model()
@variable(model, x)
@variable(model, y)
A = x*y
expected_exception = ErrorException(
"Unexpected quadratic expression x*y in nonlinear expression. " *
"Quadratic expressions (e.g., created using @expression) and " *
"nonlinear expressions cannot be mixed."
)
@test_throws expected_exception @NLexpression(model, A)
end
end

0 comments on commit 4811cd2

Please sign in to comment.