Skip to content

Commit bf2c4c3

Browse files
blegatjoaquimg
andauthored
Pass attributes through Objective.FunctionConversionBridge (#287)
* Pass attributes through Objective.FunctionConversionBridge * Fix * add test * fix tol * fix test * add reverse test --------- Co-authored-by: joaquimg <joaquimdgarcia@gmail.com>
1 parent 43a8226 commit bf2c4c3

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/bridges.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
# Use of this source code is governed by an MIT-style license that can be found
44
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
55

6+
function MOI.get(
7+
model::MOI.ModelLike,
8+
attr::ObjectiveFunctionAttribute{ReverseObjectiveFunction,G},
9+
::MOI.Bridges.Objective.FunctionConversionBridge{T,F,G},
10+
) where {T,F,G}
11+
return MOI.get(
12+
model,
13+
ObjectiveFunctionAttribute{ReverseObjectiveFunction,F}(attr.attr),
14+
)
15+
end
16+
17+
function MOI.set(
18+
model::MOI.ModelLike,
19+
attr::ObjectiveFunctionAttribute{ForwardObjectiveFunction,G},
20+
::MOI.Bridges.Objective.FunctionConversionBridge{T,F,G},
21+
value,
22+
) where {T,F,G}
23+
return MOI.set(
24+
model,
25+
ObjectiveFunctionAttribute{ForwardObjectiveFunction,F}(attr.attr),
26+
value,
27+
)
28+
end
29+
630
function MOI.get(
731
model::MOI.ModelLike,
832
::ObjectiveFunctionAttribute{ReverseObjectiveFunction},

test/jump.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,49 @@ function runtests()
3131
return
3232
end
3333

34+
function test_single_variable_objective_forward()
35+
model = Model(() -> DiffOpt.diff_optimizer(SCS.Optimizer))
36+
@variable(model, x[1:7] >= 0)
37+
@constraint(model, c1, sum(x[i] for i in 1:6) == 10)
38+
@constraint(model, c2, x[7] == 10)
39+
@constraint(
40+
model,
41+
c3,
42+
LinearAlgebra.Symmetric([
43+
x[7] 0.0
44+
0.0 x[1]
45+
]) in PSDCone()
46+
)
47+
@objective(model, Max, x[7])
48+
optimize!(model)
49+
MOI.set(model, DiffOpt.ForwardObjectiveFunction(), sum(x))
50+
DiffOpt.forward_differentiate!(model)
51+
@test MOI.get(model, DiffOpt.ForwardVariablePrimal(), x[7]) 0 atol = ATOL
52+
return
53+
end
54+
55+
function test_single_variable_objective_reverse()
56+
model = Model(() -> DiffOpt.diff_optimizer(SCS.Optimizer))
57+
@variable(model, x[1:7] >= 0)
58+
@constraint(model, c1, sum(x[i] for i in 1:6) == 10)
59+
@constraint(model, c2, x[7] == 10)
60+
@constraint(
61+
model,
62+
c3,
63+
LinearAlgebra.Symmetric([
64+
x[7] 0.0
65+
0.0 x[1]
66+
]) in PSDCone()
67+
)
68+
@objective(model, Max, x[7])
69+
optimize!(model)
70+
MOI.set(model, DiffOpt.ReverseVariablePrimal(), x[7], 1.0)
71+
DiffOpt.reverse_differentiate!(model)
72+
func = MOI.get(model, DiffOpt.ReverseObjectiveFunction())
73+
@test JuMP.coefficient(func, x[7]) 0.0 atol = ATOL rtol = RTOL
74+
return
75+
end
76+
3477
function test_forward_on_trivial_qp()
3578
# using example on https://osqp.org/docs/examples/setup-and-solve.html
3679
Q = [4.0 1.0; 1.0 2.0]

0 commit comments

Comments
 (0)