@Override public double computeDerivative() { return derivative.process(input); } }
@Override public double computeFunction() { return function.process(input); }
@Override public double computeDerivative() { return derivative.process(input); } }
@Override public double computeDerivative() { if( !cachedDerivative ) { cachedDerivative = true; currentDerivative = lineDerivative.process(currentStep); } return currentDerivative; }
@Override public double computeDerivative() { if( !cachedDerivative ) { cachedDerivative = true; currentDerivative = lineDerivative.process(currentStep); } return currentDerivative; }
@Override public double computeFunction() { return function.process(input); }
@Override public double process(double x) { double temp; double valueOrig = function.process(x); double h = x != 0 ? differenceScale*Math.abs(x) : differenceScale; // backwards temp = x-h; double h0 = x-temp; double backwards = function.process(temp); // forward temp = x+h; double h1 = temp-x; double forwards = function.process(temp); return (forwards - backwards)/(h0+h1); } }
@Override public double process(double x) { double temp; double valueOrig = function.process(x); double h = x != 0 ? differenceScale*Math.abs(x) : differenceScale; // backwards temp = x-h; double h0 = x-temp; double backwards = function.process(temp); // forward temp = x+h; double h1 = temp-x; double forwards = function.process(temp); return (forwards - backwards)/(h0+h1); } }
/** * Give it a very simple function and see if it finds the minimum approximately. More * robustness and correctness tests are found in benchmark directory. */ @Test public void checkBasic() { double expected = 10; FunctionStoS f = new TrivialQuadraticStoS(expected); FunctionStoS d = new TrivialQuadraticDerivStoS(expected); // the initial value should pass all the tests with this setting LineSearch alg = new LineSearchMore94().setConvergence(0.0001,0.1,0.001); alg.setFunction(new Individual_to_CoupledDerivative(f,d),0); double valueZero = f.process(0); double derivZero = d.process(0); double initValue = f.process(1); alg.init(valueZero,derivZero,initValue,1,0,100); assertTrue(UtilOptimize.process(alg, 50)); double foundLoose = alg.getStep(); // now try it with tighter bounds alg = new LineSearchMore94().setConvergence(0.00001,0.000001,0.001); alg.setFunction(new Individual_to_CoupledDerivative(f,d),0); alg.init(valueZero,derivZero,initValue,1,0,100); assertTrue(UtilOptimize.process(alg, 50)); assertTrue(alg.isConverged()); double foundTight = alg.getStep(); // see if the tighter bounds is more accurate assertTrue(Math.abs(foundTight - expected) < Math.abs(foundLoose - expected)); // since it is a quadratic function it should find a perfect solution too assertEquals(expected, foundTight, 1e-5); }