def test_hessian(): """ Test the the numerical hessian for the rosenbrock function with and without explicit gradient and a function of a higher order """ def F(x): return x[0]**3+x[1]**3+x[2]**3+x[0]**2 *x[1]**2 *x[2]**2 def F_hessian(x): return array([ [6.*x[0] + 2.*x[1]**2*x[2]**2, 4.*x[0]*x[1]*x[2]**2, 4.*x[0]*x[1]**2 *x[2]] , [4.*x[0]*x[1]*x[2]**2, 6.*x[1] + 2.*x[0]**2*x[2]**2, 4.*x[0]**2 *x[1]*x[2]] , [4.*x[0]*x[1]**2 *x[2], 4.*x[0]**2 *x[1]*x[2],6.*x[2] + 2.*x[0]**2*x[1]**2 ]]) opt1 = p.OptimizationProblem(rosen) opt2 = p.OptimizationProblem(rosen, rosen_der) opt3 = p.OptimizationProblem(F,3) for i in range(-3,3): for j in range(-3,3): x = array([i, j, 3], dtype=double) k = opt1.hessian(x) - rosen_hess(x) kk = opt2.hessian(x) - rosen_hess(x) kkk = opt3.hessian(x) - F_hessian(x) print k, abs(k) <1e-2 print kk, abs(kk) <1e-2 print kkk, abs(kkk) <1e-2 assert all( abs(k) <1e-2 ) assert all( abs(kk) <1e-2 ) assert all( abs(kkk) <1e-2 )
def test_hess(self): # Compare rosen_hess(x) times p with rosen_hess_prod(x,p). See gh-1775 x = np.array([3, 4, 5]) p = np.array([2, 2, 2]) hp = optimize.rosen_hess_prod(x, p) dothp = np.dot(optimize.rosen_hess(x), p) assert_equal(hp, dothp)
def test_hess(self): """Compare rosen_hess(x) times p with rosen_hess_prod(x,p) (ticket #1248)""" x = np.array([3, 4, 5]) p = np.array([2, 2, 2]) hp = optimize.rosen_hess_prod(x, p) dothp = np.dot(optimize.rosen_hess(x), p) assert_equal(hp, dothp)
def test_hess(self): """Compare rosen_hess(x) times p with rosen_hess_prod(x,p) (ticket #1248)""" x = array([3, 4, 5]) p = array([2, 2, 2]) hp = optimize.rosen_hess_prod(x, p) dothp = np.dot(optimize.rosen_hess(x), p) assert_equal(hp, dothp)
def test_optimization_trust(): print("**********************************************") print("TEST Newton trust region ") x0 = [1.3, 0.7, 0.8, 1.9, 1.2] res = optimize.minimize( optimize.rosen, x0, method='trust-ncg', jac=optimize.rosen_der, hess=optimize.rosen_hess, options={'gtol': 1e-8, 'disp': True}) print(res.x) print(optimize.rosen(x0).shape) print(optimize.rosen_der(x0).shape) print(optimize.rosen_hess(x0).shape) return res.fun
def rosen_inverse_hess(x): H = rosen_hess(x) return inv(H)
def hess_rosen(xx): yy = xx.flatten() return spo.rosen_hess(yy)
def func_grad_hess(x,*args): f = optimize.rosen(x) g = optimize.rosen_der(x) h= optimize.rosen_hess(x) return (f,g,h)
def rosenbrock_hess_f(x): return rosen_hess(x)
def func_grad_hess(x, *args): f = optimize.rosen(x) g = optimize.rosen_der(x) h = optimize.rosen_hess(x) return (f, g, h)