public void setVerbose( PrintStream out , int level ) { this.verbose = out; if( level != 0 ) this.lineSearch.setVerbose(out,level); }
/** * 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); }
if( lineSearch.iterate() ) { if( !lineSearch.isConverged() ) { if( firstStep ) { double step = lineSearch.getStep(); double fstp = lineSearch.getFunction();
private void invokeLineInitialize(double funcAtStart, double maxStep) { function.setInput(initialStep); double funcAtInit = function.computeFunction(); lineSearch.init(funcAtStart,derivAtZero,funcAtInit,initialStep,0,maxStep); firstStep = true; }
/** * Specify the function being optimized * @param function Function to optimize * @param funcMinValue Minimum possible function value. E.g. 0 for least squares. */ public void setFunction( GradientLineFunction function , double funcMinValue ) { this.function = function; this.funcMinValue = funcMinValue; lineSearch.setFunction(function,funcMinValue); N = function.getN(); B = new DMatrixRMaj(N,N); searchVector = new DMatrixRMaj(N,1); g = new DMatrixRMaj(N,1); s = new DMatrixRMaj(N,1); y = new DMatrixRMaj(N,1); x = new DMatrixRMaj(N,1); temp0_Nx1 = new DMatrixRMaj(N,1); temp1_Nx1 = new DMatrixRMaj(N,1); }
private boolean setupLineSearch( double funcAtStart , double[] startPoint , double[] startDeriv, double[] direction ) { // derivative of the line search is the dot product of the gradient and search direction derivAtZero = 0; for( int i = 0; i < N; i++ ) { derivAtZero += startDeriv[i]*direction[i]; } // degenerate case if( derivAtZero > 0 ) return false; else if( derivAtZero == 0 ) return true; // setup line functions function.setLine(startPoint, direction); // use wolfe condition to set the maximum step size maxStep = (funcMinValue-funcAtStart)/(lineSearch.getGTol()*derivAtZero); initialStep = 1 < maxStep ? 1 : maxStep; invokeLineInitialize(funcAtStart,maxStep); return true; }
if( lineSearch.iterate() ) { if( !lineSearch.isConverged() ) { if( firstStep ) { double step = lineSearch.getStep(); double fstp = lineSearch.getFunction();
private void invokeLineInitialize(double funcAtStart, double maxStep) { function.setInput(initialStep); double funcAtInit = function.computeFunction(); lineSearch.init(funcAtStart,derivAtZero,funcAtInit,initialStep,0,maxStep); firstStep = true; }
/** * Specify the function being optimized * @param function Function to optimize * @param funcMinValue Minimum possible function value. E.g. 0 for least squares. */ public void setFunction( GradientLineFunction function , double funcMinValue ) { this.function = function; this.funcMinValue = funcMinValue; lineSearch.setFunction(function,funcMinValue); N = function.getN(); B = new DMatrixRMaj(N,N); searchVector = new DMatrixRMaj(N,1); g = new DMatrixRMaj(N,1); s = new DMatrixRMaj(N,1); y = new DMatrixRMaj(N,1); x = new DMatrixRMaj(N,1); temp0_Nx1 = new DMatrixRMaj(N,1); temp1_Nx1 = new DMatrixRMaj(N,1); }
private boolean setupLineSearch( double funcAtStart , double[] startPoint , double[] startDeriv, double[] direction ) { // derivative of the line search is the dot product of the gradient and search direction derivAtZero = 0; for( int i = 0; i < N; i++ ) { derivAtZero += startDeriv[i]*direction[i]; } // degenerate case if( derivAtZero > 0 ) return false; else if( derivAtZero == 0 ) return true; // setup line functions function.setLine(startPoint, direction); // use wolfe condition to set the maximum step size maxStep = (funcMinValue-funcAtStart)/(lineSearch.getGTol()*derivAtZero); initialStep = 1 < maxStep ? 1 : maxStep; invokeLineInitialize(funcAtStart,maxStep); return true; }
public void setVerbose( PrintStream out , int level ) { this.verbose = out; if( level != 0 ) this.lineSearch.setVerbose(out,level); }