@@ -197,3 +197,43 @@ def test_entropy_lbfgs(self):
197197 hessian_approximation = 'limited-memory' )
198198 # Minimum entropy distribution is concentrated on one point
199199 assert np .sum (q .value > 1e-8 ) == 1
200+
201+ def test_hessian_mode_switch_rebuilds_cached_oracle (self ):
202+ """Switch hessian_approximation between solves must refresh the diff oracle"""
203+ n = 4
204+ x = cp .Variable (n , bounds = [- 1 , 1 ])
205+ np .random .seed (0 )
206+ A = cp .Parameter ((n , n ), value = np .random .rand (n , n ))
207+ prob = cp .Problem (cp .Minimize (cp .sum (A @ cp .exp (x ))))
208+
209+ prob .solve (solver = cp .IPOPT , nlp = True ,
210+ hessian_approximation = 'limited-memory' )
211+ o1 = prob ._solver_cache ['NLP' ]['oracles' ]
212+ assert o1 .use_hessian is False
213+
214+ A .value = np .random .rand (n , n )
215+ prob .solve (solver = cp .IPOPT , nlp = True ,
216+ hessian_approximation = 'exact' )
217+ o2 = prob ._solver_cache ['NLP' ]['oracles' ]
218+ assert o2 is not o1
219+ assert o2 .use_hessian is True
220+ rows , _ = o2 .hessianstructure ()
221+ assert rows .size > 0
222+
223+ def test_same_hessian_mode_reuses_cached_oracle (self ):
224+ """Same Hessian mode across solves should not refresh cached diff oracle."""
225+ n = 4
226+ x = cp .Variable (n , bounds = [- 1 , 1 ])
227+ np .random .seed (0 )
228+ A = cp .Parameter ((n , n ), value = np .random .rand (n , n ))
229+ prob = cp .Problem (cp .Minimize (cp .sum (A @ cp .exp (x ))))
230+
231+ prob .solve (solver = cp .IPOPT , nlp = True ,
232+ hessian_approximation = 'exact' )
233+ o1 = prob ._solver_cache ['NLP' ]['oracles' ]
234+
235+ A .value = np .random .rand (n , n )
236+ prob .solve (solver = cp .IPOPT , nlp = True ,
237+ hessian_approximation = 'exact' )
238+ o2 = prob ._solver_cache ['NLP' ]['oracles' ]
239+ assert o2 is o1
0 commit comments