Skip to content

Commit

Permalink
Fix converting (Aff,Quad)Expr to NonlinearExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Dec 26, 2023
1 parent 57bd36a commit dc12f6b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ function Base.convert(
if !iszero(x.constant) || isempty(args)
push!(args, x.constant)
end
if length(args) == 1
if length(args) == 1 && args[1] isa GenericNonlinearExpr{V}
return args[1]
end
return GenericNonlinearExpr{V}(:+, args)
Expand Down Expand Up @@ -752,7 +752,7 @@ function Base.convert(
if !iszero(x.aff.constant) || isempty(args)
push!(args, x.aff.constant)
end
if length(args) == 1
if length(args) == 1 && args[1] isa GenericNonlinearExpr{V}
return args[1]
end
return GenericNonlinearExpr{V}(:+, args)
Expand Down
6 changes: 4 additions & 2 deletions test/Kokako.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ end
function include_modules_to_test(dir::String, files::Vector{String})
modules = Pair{String,Module}[]
for file in files
@info "Loading $file"
push!(modules, file => Base.include(Main, joinpath(dir, file)))
if isfile(joinpath(dir, file))
@info "Loading $file"
push!(modules, file => Base.include(Main, joinpath(dir, file)))
end
end
return modules
end
Expand Down
17 changes: 13 additions & 4 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ function test_extension_aff_expr_convert(
model = ModelType()
@variable(model, x)
_to_string(x) = string(convert(GenericNonlinearExpr{VariableRefType}, x))
@test _to_string(AffExpr(0.0)) == "0.0"
@test _to_string(AffExpr(1.0)) == "1.0"
@test _to_string(AffExpr(0.0)) == "+(0.0)"
@test _to_string(AffExpr(1.0)) == "+(1.0)"
@test _to_string(x + 1) == "x + 1.0"
@test _to_string(2x + 1) == "(2.0 * x) + 1.0"
@test _to_string(2x) == "2.0 * x"
Expand All @@ -248,8 +248,8 @@ function test_extension_quad_expr_convert(
model = ModelType()
@variable(model, x)
_to_string(x) = string(convert(GenericNonlinearExpr{VariableRefType}, x))
@test _to_string(QuadExpr(AffExpr(0.0))) == "0.0"
@test _to_string(QuadExpr(AffExpr(1.0))) == "1.0"
@test _to_string(QuadExpr(AffExpr(0.0))) == "+(0.0)"
@test _to_string(QuadExpr(AffExpr(1.0))) == "+(1.0)"
@test _to_string(x^2 + 1) == "(x * x) + 1.0"
@test _to_string(2x^2 + 1) == "(2.0 * x * x) + 1.0"
@test _to_string(2x^2) == "2.0 * x * x"
Expand Down Expand Up @@ -1010,4 +1010,13 @@ function test_printing_truncation()
return
end

function test_convert_vector_aff_expr()
model = Model()
@variable(model, x)
@test [sin(x), x] isa Vector{NonlinearExpr}
@test [sin(x), x + 1] isa Vector{NonlinearExpr}
@test [sin(x), AffExpr(x)] isa Vector{NonlinearExpr}
return
end

end # module

0 comments on commit dc12f6b

Please sign in to comment.