Skip to content

Fix bug handling Adjoints in macros #1698

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

Merged
merged 6 commits into from
Dec 29, 2018
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: 4 additions & 2 deletions src/parse_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ function destructive_add!(ex::AbstractArray{<:GenericAffExpr}, c::Number,
return result
end


destructive_add!(ex, c, x) = ex .+ c * x
# For some reason, the broadcast syntax `ex .+ c * x` fails if `x` is an
# `Adjoint`. But if we explicitly call `broadcast` it seems to work.
# See JuMP PR #1698 for more discussion.
destructive_add!(ex, c, x) = broadcast(+, ex, c * x)

destructive_add_with_reorder!(ex, arg) = destructive_add!(ex, 1.0, arg)
# Special case because "Val{false}()" is used as the default empty expression.
Expand Down
14 changes: 14 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ end
"DenseAxisArray, or SparseAxisArray.")
@test_throws exception @variable(model, x[1:3], container=Oops)
end

@testset "Adjoints" begin
model = Model()
@variable(model, x[1:2])
obj = @objective(model, Min, x' * ones(2, 2) * x)
@test JuMP.isequal_canonical(obj, x[1]^2 + 2 * x[1] * x[2] + x[2]^2)
cref = @constraint(model, x' * ones(2, 2) * x <= 1)
c = JuMP.constraint_object(cref)
@test JuMP.isequal_canonical(c.func, x[1]^2 + 2 * x[1] * x[2] + x[2]^2)
@test c.set == MOI.LessThan(1.0)
@test JuMP.isequal_canonical(
JuMP.destructive_add!(0.0, x', ones(2, 2)), x' * ones(2, 2)
)
end
end

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