@Test public void wolfeCondition() { UnconMinTrustRegionBFGS_F64 alg = createAlg(); alg.fx = 1; alg.f_prev = 1.1; DMatrixRMaj s = new DMatrixRMaj(new double[][]{{1},{2}}); DMatrixRMaj y = new DMatrixRMaj(new double[][]{{2},{4}}); DMatrixRMaj g = new DMatrixRMaj(new double[][]{{0.1},{0.4}}); assertTrue(alg.wolfeCondition(s,y,g)); // if the change in state is perpendicular to change in gradient it should fail s = new DMatrixRMaj(new double[][]{{1},{2}}); y = new DMatrixRMaj(new double[][]{{2},{-4}}); assertFalse(alg.wolfeCondition(s,y,g)); }
@Override protected void functionGradientHessian(DMatrixRMaj x, boolean sameStateAsCost, DMatrixRMaj gradient, HessianBFGS hessian) { functionGradient.process(x.data, gradient.data); if( !firstIteration ) { // compute the change in Gradient CommonOps_DDRM.subtract(gradient, gradientPrevious, y); CommonOps_DDRM.subtract(x, xPrevious, s); // Only update when the Wolfe condition is true of the Hessian gets corrected very quickly if( wolfeCondition(s,y,gradientPrevious)) { // Apply DFP equation and update H // Note: there is some duplication in math between Wolfe, update(), and inverseUpdate() hessian.update(s,y); gradientPrevious.set(gradient); xPrevious.set(x); f_prev = fx; } } else { firstIteration = false; gradientPrevious.set(gradient); xPrevious.set(x); f_prev = fx; } }
@Override protected void functionGradientHessian(DMatrixRMaj x, boolean sameStateAsCost, DMatrixRMaj gradient, HessianBFGS hessian) { functionGradient.process(x.data, gradient.data); if( !firstIteration ) { // compute the change in Gradient CommonOps_DDRM.subtract(gradient, gradientPrevious, y); CommonOps_DDRM.subtract(x, xPrevious, s); // Only update when the Wolfe condition is true of the Hessian gets corrected very quickly if( wolfeCondition(s,y,gradientPrevious)) { // Apply DFP equation and update H // Note: there is some duplication in math between Wolfe, update(), and inverseUpdate() hessian.update(s,y); gradientPrevious.set(gradient); xPrevious.set(x); f_prev = fx; } } else { firstIteration = false; gradientPrevious.set(gradient); xPrevious.set(x); f_prev = fx; } }