@Override public double computeDerivative() { if( !cachedGradient ) { cachedGradient = true; this.gradient.process(currentInput,currentGradient); } double dot = 0; for( int i = 0; i < N; i++ ) { dot += currentGradient[i]*direction[i]; } return dot; } }
public static boolean gradient( FunctionNtoS func , FunctionNtoN gradient , double param[] , double tol , double differenceScale ) { NumericalGradientForward numerical = new NumericalGradientForward(func,differenceScale); if( numerical.getN() != gradient.getN() ) throw new RuntimeException("N is not equal: "+numerical.getN() +" "+gradient.getN()); int N = numerical.getN(); double[] found = new double[N]; double[] expected = new double[N]; gradient.process(param, found); numerical.process(param,expected); for (int i = 0; i < N; i++) { if(Math.abs(found[i]-expected[i]) > tol) return false; } return true; }
public static boolean gradient( FunctionNtoS func , FunctionNtoN gradient , double param[] , double tol , double differenceScale ) { NumericalGradientForward numerical = new NumericalGradientForward(func,differenceScale); if( numerical.getN() != gradient.getN() ) throw new RuntimeException("N is not equal: "+numerical.getN() +" "+gradient.getN()); int N = numerical.getN(); double[] found = new double[N]; double[] expected = new double[N]; gradient.process(param, found); numerical.process(param,expected); for (int i = 0; i < N; i++) { if(Math.abs(found[i]-expected[i]) > tol) return false; } return true; }
@Override public double computeDerivative() { if( !cachedGradient ) { cachedGradient = true; this.gradient.process(currentInput,currentGradient); } double dot = 0; for( int i = 0; i < N; i++ ) { dot += currentGradient[i]*direction[i]; } return dot; } }
@Override public void computeGradient(double[] gradient) { if( !cachedGradient ) { cachedGradient = true; this.gradient.process(currentInput,currentGradient); } System.arraycopy(currentGradient, 0, gradient, 0, N); }
@Override public void computeGradient(double[] gradient) { if( !cachedGradient ) { cachedGradient = true; this.gradient.process(currentInput,currentGradient); } System.arraycopy(currentGradient, 0, gradient, 0, N); }
@Override public void computeGradient(double[] gradient) { if( !cachedGradient ) { cachedGradient = true; this.gradient.process(currentInput,currentGradient); } System.arraycopy(currentGradient, 0, gradient, 0, N); }
@Override public void computeGradient(double[] gradient) { if( !cachedGradient ) { cachedGradient = true; this.gradient.process(currentInput,currentGradient); } System.arraycopy(currentGradient, 0, gradient, 0, N); }
@Test public void compareToNumeric() { FuncLS funcLS = new FuncLS(); DerivLS derivLS = new DerivLS(); FunctionNtoS func = new LsToNonLinear(funcLS); FunctionNtoN deriv = new LsToNonLinearDeriv(funcLS,derivLS); FunctionNtoN derivNumeric = new NumericalGradientForward(func); double point[] = new double[]{1,2}; double expected[] = new double[2]; double found[] = new double[2]; deriv.process(point,found); derivNumeric.process(point,expected); for( int i = 0; i < expected.length; i++ ) { assertEquals(expected[i], found[i], 1e-4); } }
@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; } }
gradient.process(x,expectedG); assertEquals(func.process(x), foundF, 1e-8); for( int i = 0; i < 3; i++ )