Skip to content

Commit db871ea

Browse files
Ar/quad coefficients (#213)
* Correct quadratic term and add test * Update test/jump.jl Co-authored-by: Mathieu Besançon <[email protected]>
1 parent 65bd1ab commit db871ea

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DiffOpt"
22
uuid = "930fe3bc-9c6b-11ea-2d94-6184641e85e7"
33
authors = ["Akshay Sharma", "Mathieu Besançon", "Joaquim Dias Garcia", "Benoît Legat"]
4-
version = "0.3.1"
4+
version = "0.3.2"
55

66
[deps]
77
BlockDiagonals = "0a1fb500-61f7-11e9-3c65-f5ef3456f9f0"

src/jump_moi_overloads.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ function JuMP.coefficient(func::MOItoJuMP, var_ref::JuMP.VariableRef)
240240
return JuMP.coefficient(func.func, JuMP.index(var_ref))
241241
end
242242
function quad_sym_half(func::MOItoJuMP, var1_ref::JuMP.VariableRef, var2_ref::JuMP.VariableRef)
243-
check_belongs_to_model(var1_ref, func.model)
244-
return quad_sym_half(func.func, JuMP.index(vi1), JuMP.index(var2_ref))
243+
check_belongs_to_model.([var1_ref, var2_ref], Ref(func.model))
244+
return quad_sym_half(func.func, JuMP.index(var1_ref), JuMP.index(var2_ref))
245245
end
246246
function JuMP.coefficient(func::MOItoJuMP, var1_ref::JuMP.VariableRef, var2_ref::JuMP.VariableRef)
247-
check_belongs_to_model(var2_ref, func.model)
248-
return JuMP.coefficient(func.func, JuMP.index(vi1), JuMP.index(var2_ref))
247+
check_belongs_to_model.([var1_ref, var2_ref], Ref(func.model))
248+
return JuMP.coefficient(func.func, JuMP.index(var1_ref), JuMP.index(var2_ref))
249249
end
250250
function Base.convert(::Type{JuMP.GenericAffExpr{T,JuMP.VariableRef}}, func::MOItoJuMP) where {T}
251251
return JuMP.GenericAffExpr{T,JuMP.VariableRef}(func.model, convert(MOI.ScalarAffineFunction{T}, func.func))

test/jump.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@ end
6868

6969
DiffOpt.backward(model)
7070

71-
grad = JuMP.constant(MOI.get(model, DiffOpt.BackwardOutConstraint(), ctr_le[]))
72-
@test grad -1.0 atol=ATOL rtol=RTOL
71+
grad_constraint = JuMP.constant(MOI.get(model, DiffOpt.BackwardOutConstraint(), ctr_le[]))
72+
@test grad_constraint -1.0 atol=ATOL rtol=RTOL
73+
74+
# Test some overloads from https://github.com/jump-dev/DiffOpt.jl/issues/211
75+
grad_obj = MOI.get(model, DiffOpt.BackwardOutObjective())
76+
@test JuMP.coefficient(grad_obj, x[1], x[2])
77+
DiffOpt.quad_sym_half.(grad_obj, x[1], x[2]) atol=ATOL rtol=RTOL
78+
79+
@test DiffOpt.quad_sym_half(grad_obj, x[1], x[1])
80+
2 * JuMP.coefficient(grad_obj, x[1], x[1]) atol=ATOL rtol=RTOL
7381

7482
# TODO: this simple show fails
7583
# @show ctr_le

0 commit comments

Comments
 (0)