@@ -671,6 +671,55 @@ function test_diff_errors()
671
671
return
672
672
end
673
673
674
+ function is_empty (cache:: DiffOpt.InputCache )
675
+ return isempty (cache. dx) &&
676
+ isempty (cache. scalar_constraints) &&
677
+ isempty (cache. vector_constraints) &&
678
+ cache. objective === nothing
679
+ end
680
+
681
+ # Credit to @klamike
682
+ function test_empty_cache ()
683
+ m = Model (
684
+ () -> DiffOpt. diff_optimizer (
685
+ HiGHS. Optimizer;
686
+ with_parametric_opt_interface = true ,
687
+ ),
688
+ )
689
+ @variable (m, x)
690
+ @variable (m, p ∈ Parameter (1.0 ))
691
+ @variable (m, q ∈ Parameter (2.0 ))
692
+ @constraint (m, x ≥ p)
693
+ @constraint (m, x ≥ q)
694
+ @objective (m, Min, x)
695
+ optimize! (m)
696
+ @assert is_solved_and_feasible (m)
697
+
698
+ function get_sensitivity (m, xᵢ, pᵢ)
699
+ DiffOpt. empty_input_sensitivities! (m)
700
+ @test is_empty (unsafe_backend (m). optimizer. input_cache)
701
+ if ! isnothing (unsafe_backend (m). optimizer. diff) &&
702
+ ! isnothing (unsafe_backend (m). optimizer. diff. model. input_cache)
703
+ @test is_empty (unsafe_backend (m). optimizer. diff. model. input_cache)
704
+ end
705
+ MOI. set (
706
+ m,
707
+ DiffOpt. ForwardConstraintSet (),
708
+ ParameterRef (pᵢ),
709
+ Parameter (1.0 ),
710
+ )
711
+ DiffOpt. forward_differentiate! (m)
712
+ return MOI. get (m, DiffOpt. ForwardVariablePrimal (), xᵢ)
713
+ end
714
+
715
+ sp1 = get_sensitivity (m, x, p)
716
+ sp2 = get_sensitivity (m, x, q)
717
+ sp3 = get_sensitivity (m, x, p)
718
+ @test sp1 ≈ sp3
719
+ @test sp2 ≠ sp3
720
+ return
721
+ end
722
+
674
723
end # module
675
724
676
725
TestParameters. runtests ()
0 commit comments