Skip to content

Commit d49b644

Browse files
update
1 parent 9a63ce9 commit d49b644

File tree

2 files changed

+104
-252
lines changed

2 files changed

+104
-252
lines changed

test/conic_program.jl

Lines changed: 103 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function _test_simple_socp(eq_vec::Bool)
129129

130130
DiffOpt.reverse_differentiate!(model)
131131

132+
# FIXME: this is not working - https://github.com/jump-dev/DiffOpt.jl/issues/283
132133
# @test JuMP.coefficient(MOI.get(model, DiffOpt.ReverseConstraintFunction(), csoc).func.func.func, t.index) ≈ ds atol=ATOL rtol=RTOL
133134

134135
return
@@ -394,257 +395,108 @@ function test_differentiating_conic_with_PSD_and_SOC_constraints()
394395
return
395396
end
396397

397-
function test_differentiating_conic_with_PSD_and_POS_constraints()
398-
# refer psdt2test, https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Test/contconic.jl#L4306
399-
# find equivalent diffcp program here - https://github.com/AKS1996/jump-gsoc-2020/blob/master/diffcp_sdp_3_py.ipynb
400-
# model = DiffOpt.diff_optimizer(SCS.Optimizer)
401-
# MOI.set(model, MOI.Silent(), true)
402-
# x = MOI.add_variables(model, 7)
403-
# @test MOI.get(model, MOI.NumberOfVariables()) == 7
404-
# η = 10.0
405-
# c1 = MOI.add_constraint(
406-
# model,
407-
# MOI.VectorAffineFunction(
408-
# MOI.VectorAffineTerm.(1, MOI.ScalarAffineTerm.(-1.0, x[1:6])),
409-
# [η],
410-
# ),
411-
# MOI.Nonnegatives(1),
412-
# )
413-
# c2 = MOI.add_constraint(
414-
# model,
415-
# MOI.VectorAffineFunction(
416-
# MOI.VectorAffineTerm.(1:6, MOI.ScalarAffineTerm.(1.0, x[1:6])),
417-
# zeros(6),
418-
# ),
419-
# MOI.Nonnegatives(6),
420-
# )
421-
# α = 0.8
422-
# δ = 0.9
423-
# c3 = MOI.add_constraint(
424-
# model,
425-
# MOI.VectorAffineFunction(
426-
# MOI.VectorAffineTerm.(
427-
# [fill(1, 7); fill(2, 5); fill(3, 6)],
428-
# MOI.ScalarAffineTerm.(
429-
# [
430-
# δ / 2,
431-
# α,
432-
# δ,
433-
# δ / 4,
434-
# δ / 8,
435-
# 0.0,
436-
# -1.0,
437-
# -δ / (2 * √2),
438-
# -δ / 4,
439-
# 0,
440-
# -δ / (8 * √2),
441-
# 0.0,
442-
# δ / 2,
443-
# δ - α,
444-
# 0,
445-
# δ / 8,
446-
# δ / 4,
447-
# -1.0,
448-
# ],
449-
# [x[1:7]; x[1:3]; x[5:6]; x[1:3]; x[5:7]],
450-
# ),
451-
# ),
452-
# zeros(3),
453-
# ),
454-
# MOI.PositiveSemidefiniteConeTriangle(2),
455-
# )
456-
# c4 = MOI.add_constraint(
457-
# model,
458-
# MOI.VectorAffineFunction(
459-
# MOI.VectorAffineTerm.(
460-
# 1,
461-
# MOI.ScalarAffineTerm.(0.0, [x[1:3]; x[5:6]]),
462-
# ),
463-
# [0.0],
464-
# ),
465-
# MOI.Zeros(1),
466-
# )
467-
# MOI.set(
468-
# model,
469-
# MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
470-
# MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x[7])], 0.0),
471-
# )
472-
# MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
473-
# MOI.optimize!(model)
474-
# MOI.get(model, MOI.ObjectiveValue())
475-
# # dc = ones(7)
476-
# MOI.get.(model, MOI.VariablePrimal(), x)
477-
478-
# Make a JuMP model backed by DiffOpt.diff_optimizer(SCS.Optimizer)
479-
model = Model(() -> DiffOpt.diff_optimizer(SCS.Optimizer))
480-
set_silent(model) # just to suppress solver output
481-
482-
@variable(model, x[1:7])
483-
@test num_variables(model) == 7
484-
485-
η = 10.0
486-
# c1: sum(-x[1:6]) + η >= 0
487-
c1 = @constraint(model, -sum(x[i] for i in 1:6) + η 0)
488-
489-
# c2: x[i] >= 0 for each i in 1:6
490-
# (this replicates MOI.Nonnegatives(6) on x[1:6])
491-
c2 = [
492-
@constraint(model, x[i] 0)
493-
for i in 1:6
494-
]
495-
496-
# c3: A 2×2 PSD constraint. In raw MOI form you had
497-
# MOI.PositiveSemidefiniteConeTriangle(2),
498-
# which means a vector dimension=3 => [a11, a12, a22].
499-
# We'll build that in JuMP with `[ [a11 a12]; [a12 a22] ] in PSDCone()`.
500-
α = 0.8
501-
δ = 0.9
502-
c3 = @constraint(
503-
model,
504-
LinearAlgebra.Symmetric(hcat(
505-
[((δ/2)*x[1] + α*x[2] + δ*x[3] +/4)*x[4] +/8)*x[5] - 1.0*x[7]) # a11
506-
((-/(2*√2)))*x[1] -/4)*x[2] -/(8*√2))*x[5])],
507-
[((-/(2*√2)))*x[1] -/4)*x[2] -/(8*√2))*x[5])
508-
((δ/2)*x[1] +- α)*x[2] + 0.0*x[3] +/8)*x[5] +/4)*x[6] - x[7])])
509-
) in PSDCone()
510-
)
511-
512-
# c4: In the original MOI example it was some "useless" constraint on x[1:3] + x[5:6].
513-
# For demonstration, we replicate a dimension=1 equality to zero:
514-
c4 = @constraint(model, 0.0 * x[1] + 0.0 * x[2] + 0.0 * x[3] + 0.0 * x[5] + 0.0 * x[6] == 0)
515-
516-
# Make the objective: "maximize x[7]" exactly as in the original
517-
@objective(model, Max, x[7])
518-
519-
# Solve
520-
optimize!(model)
521-
522-
@test objective_value(model) 1.90192374 atol=ATOL rtol=RTOL
523-
524-
MOI.set(
525-
model,
526-
DiffOpt.ForwardObjectiveFunction(),
527-
sum(x)
528-
)
529-
# db = ones(11)
530-
# dA = ones(11, 7)
531-
MOI.set(
532-
model,
533-
DiffOpt.ForwardConstraintFunction(),
534-
c1,
535-
sum(x) + 1,
536-
)
537-
MOI.set.(
538-
model,
539-
DiffOpt.ForwardConstraintFunction(),
540-
c2,
541-
sum(x) + 1,
542-
)
543-
MOI.set.(
544-
model,
545-
DiffOpt.ForwardConstraintFunction(),
546-
c3,
547-
sum(x) + 1,
548-
)
549-
MOI.set(
550-
model,
551-
DiffOpt.ForwardConstraintFunction(),
552-
c4,
553-
sum(x) + 1,
554-
)
555-
DiffOpt.forward_differentiate!(model) # ERROR HERE
556-
@test model.diff.model.x
557-
[20 / 3.0, 0.0, 10 / 3.0, 0.0, 0.0, 0.0, 1.90192379] atol = ATOL rtol =
558-
RTOL
559-
@test model.diff.model.s [
560-
0.0,
561-
0.0,
562-
20 / 3.0,
563-
0.0,
564-
10 / 3.0,
565-
0.0,
566-
0.0,
567-
0.0,
568-
4.09807621,
569-
-2.12132,
570-
1.09807621,
571-
] atol = ATOL rtol = RTOL
572-
@test model.diff.model.y [
573-
0.0,
574-
0.19019238,
575-
0.0,
576-
0.12597667,
577-
0.0,
578-
0.14264428,
579-
0.14264428,
580-
0.01274047,
581-
0.21132487,
582-
0.408248,
583-
0.78867513,
584-
] atol = ATOL rtol = RTOL
585-
atol = 0.3
586-
rtol = 0.01
587-
# compare these with https://github.com/AKS1996/jump-gsoc-2020/blob/master/diffcp_sdp_3_py.ipynb
588-
# results are not exactly as: 1. there is some residual error 2. diffcp results are SCS specific, hence scaled
589-
dx = [-39.6066, 10.8953, -14.9189, 10.9054, 10.883, 10.9118, -21.7508]
590-
for (i, vi) in enumerate(x)
591-
@test dx[i] MOI.get(model, DiffOpt.ForwardVariablePrimal(), vi) atol =
592-
atol rtol = rtol
593-
end
594-
# @test dy ≈ [0.0, -3.56905, 0.0, -0.380035, 0.0, -0.41398, -0.385321, -0.00743119, -0.644986, -0.550542, -2.36765] atol=atol rtol=rtol
595-
# @test ds ≈ [0.0, 0.0, -50.4973, 0.0, -25.8066, 0.0, 0.0, 0.0, -7.96528, -1.62968, -2.18925] atol=atol rtol=rtol
596-
# TODO: future example, how to differentiate wrt a specific constraint/variable, refer QPLib article for more
597-
dA = zeros(11, 7)
598-
dA[3:8, 1:6] = Matrix{Float64}(LinearAlgebra.I, 6, 6) # differentiating only wrt POS constraint c2
599-
db = zeros(11)
600-
dc = zeros(7)
601-
# db = zeros(11)
602-
# dA = zeros(11, 7)
603-
# dA[3:8, 1:6] = Matrix{Float64}(LinearAlgebra.I, 6, 6) # differentiating only wrt POS constraint c2
604-
MOI.set(
605-
model,
606-
DiffOpt.ForwardConstraintFunction(),
607-
c1,
608-
MOI.Utilities.zero_with_output_dimension(
609-
MOI.VectorAffineFunction{Float64},
610-
1,
611-
),
612-
)
613-
MOI.set(
614-
model,
615-
DiffOpt.ForwardConstraintFunction(),
616-
c2,
617-
MOI.Utilities.vectorize(ones(6) .* x[1:6]),
618-
)
619-
MOI.set(
620-
model,
621-
DiffOpt.ForwardConstraintFunction(),
622-
c3,
623-
MOI.Utilities.zero_with_output_dimension(
624-
MOI.VectorAffineFunction{Float64},
625-
3,
626-
),
627-
)
628-
MOI.set(
629-
model,
630-
DiffOpt.ForwardConstraintFunction(),
631-
c4,
632-
MOI.Utilities.zero_with_output_dimension(
633-
MOI.VectorAffineFunction{Float64},
634-
1,
635-
),
636-
)
637-
DiffOpt.forward_differentiate!(model)
638-
# for (i, vi) in enumerate(X)
639-
# @test 0.0 ≈ MOI.get(model,
640-
# DiffOpt.ForwardVariablePrimal(), vi) atol=1e-2 rtol=RTOL
641-
# end
642-
# TODO add a test here, probably on duals
643-
# # note that there's no change in the PSD slack values or dual optimas
644-
# @test dy ≈ [0.0, 0.0, 0.0, 0.125978, 0.0, 0.142644, 0.142641, 0.0127401, 0.0, 0.0, 0.0] atol=atol rtol=RTOL
645-
# @test ds ≈ [0.0, 0.0, -6.66672, 0.0, -3.33336, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] atol=atol rtol=RTOL
646-
return
647-
end
398+
#FIXME: this test is not working - https://github.com/jump-dev/DiffOpt.jl/issues/285
399+
# Besides API errors, Reverse and Forward differentiation are not matching
400+
# function test_differentiating_conic_with_PSD_and_POS_constraints()
401+
# # refer psdt2test, https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Test/contconic.jl#L4306
402+
# # find equivalent diffcp program here - https://github.com/AKS1996/jump-gsoc-2020/blob/master/diffcp_sdp_3_py.ipynb
403+
# # Make a JuMP model backed by DiffOpt.diff_optimizer(SCS.Optimizer)
404+
# model = Model(() -> DiffOpt.diff_optimizer(SCS.Optimizer))
405+
# set_silent(model) # just to suppress solver output
406+
407+
# @variable(model, x[1:7])
408+
# @test num_variables(model) == 7
409+
410+
# η = 10.0
411+
# # c1: sum(-x[1:6]) + η >= 0
412+
# c1 = @constraint(model, -sum(x[i] for i in 1:6) + η ≥ 0)
413+
414+
# # c2: x[i] >= 0 for each i in 1:6
415+
# # (this replicates MOI.Nonnegatives(6) on x[1:6])
416+
# c2 = [
417+
# @constraint(model, x[i] ≥ 0)
418+
# for i in 1:6
419+
# ]
420+
421+
# # c3: A 2×2 PSD constraint. In raw MOI form you had
422+
# # MOI.PositiveSemidefiniteConeTriangle(2),
423+
# # which means a vector dimension=3 => [a11, a12, a22].
424+
# # We'll build that in JuMP with `[ [a11 a12]; [a12 a22] ] in PSDCone()`.
425+
# α = 0.8
426+
# δ = 0.9
427+
# c3 = @constraint(
428+
# model,
429+
# LinearAlgebra.Symmetric(hcat(
430+
# [((δ/2)*x[1] + α*x[2] + δ*x[3] + (δ/4)*x[4] + (δ/8)*x[5] - 1.0*x[7]) # a11
431+
# ((-(δ/(2*√2)))*x[1] - (δ/4)*x[2] - (δ/(8*√2))*x[5])],
432+
# [((-(δ/(2*√2)))*x[1] - (δ/4)*x[2] - (δ/(8*√2))*x[5])
433+
# ((δ/2)*x[1] + (δ - α)*x[2] + 0.0*x[3] + (δ/8)*x[5] + (δ/4)*x[6] - x[7])])
434+
# ) in PSDCone()
435+
# )
436+
437+
# # c4: In the original MOI example it was some "useless" constraint on x[1:3] + x[5:6].
438+
# # For demonstration, we replicate a dimension=1 equality to zero:
439+
# c4 = @constraint(model, 0.0 * x[1] + 0.0 * x[2] + 0.0 * x[3] + 0.0 * x[5] + 0.0 * x[6] == 0)
440+
441+
# # Make the objective: "maximize x[7]" exactly as in the original
442+
# @objective(model, Max, x[7])
443+
444+
# # Solve
445+
# optimize!(model)
446+
447+
# @test objective_value(model) ≈ 1.90192374 atol=ATOL rtol=RTOL
448+
449+
# # MOI.set(
450+
# # model,
451+
# # DiffOpt.ForwardObjectiveFunction(),
452+
# # 3*x[7]
453+
# # ) # FIXME: ERROR when forward_differentiate
454+
455+
# dx = [2.81765 7.35515e-6 3.33222 1.73763e-5 1.58653e-5 1.74104e-5 0.3617]
456+
# for i=1:7
457+
# @show i
458+
# MOI.set(
459+
# model,
460+
# DiffOpt.ForwardConstraintFunction(),
461+
# c1,
462+
# x[i]+0.0
463+
# )
464+
# DiffOpt.forward_differentiate!(model)
465+
# @test MOI.get(model, DiffOpt.ForwardVariablePrimal(), x[i]) ≈ dx[i] atol=ATOL rtol=RTOL
466+
# MOI.set(
467+
# model,
468+
# DiffOpt.ReverseVariablePrimal(),
469+
# x[i],
470+
# 1.0
471+
# )
472+
# DiffOpt.reverse_differentiate!(model)
473+
# @test JuMP.coefficient(MOI.get(model, DiffOpt.ReverseConstraintFunction(), c1), x[i]) ≈ dx[i] atol=ATOL rtol=RTOL
474+
# DiffOpt.empty_input_sensitivities!(model)
475+
# end
476+
# DiffOpt.empty_input_sensitivities!(model)
477+
# dx = [0.0 0.0 0.0 0.0 0.0 0.0 0.0]
478+
# for i=1:6
479+
# MOI.set(
480+
# model,
481+
# DiffOpt.ForwardConstraintFunction(),
482+
# c2[i],
483+
# x[i]+0.0
484+
# )
485+
# DiffOpt.forward_differentiate!(model)
486+
# # @test MOI.get(model, DiffOpt.ForwardVariablePrimal(), x[i]) ≈ dx[i] atol=ATOL rtol=RTOL
487+
# @show MOI.get(model, DiffOpt.ForwardVariablePrimal(), x[i])
488+
# MOI.set(
489+
# model,
490+
# DiffOpt.ReverseVariablePrimal(),
491+
# x[i],
492+
# 1.0
493+
# )
494+
# DiffOpt.reverse_differentiate!(model)
495+
# @show JuMP.coefficient(MOI.get(model, DiffOpt.ReverseConstraintFunction(), c2[i]), x[i])
496+
# end
497+
498+
# return
499+
# end
648500

649501
function test_differentiating_a_simple_psd()
650502
# refer _psd3test, https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Test/contconic.jl#L4484

test/jump_wrapper.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function test_jump_api()
3434
(DiffOpt.quadratic_diff_model, SCS.Optimizer),
3535
(DiffOpt.quadratic_diff_model, Ipopt.Optimizer),
3636
(DiffOpt.conic_diff_model, HiGHS.Optimizer),
37-
(DiffOpt.conic_diff_model, SCS.Optimizer), # conicmodel has a issue with sign
37+
(DiffOpt.conic_diff_model, SCS.Optimizer),
3838
(DiffOpt.conic_diff_model, Ipopt.Optimizer),
3939
# (DiffOpt.nonlinear_diff_model, HiGHS.Optimizer), # SQF ctr not supported?
4040
# (DiffOpt.nonlinear_diff_model, SCS.Optimizer), # returns zero for sensitivity

0 commit comments

Comments
 (0)