LineSearch { public static LineSearchResult doLineSearch(DifferentiableFunction function, double[] direction, LineSearchResult lsr) { return doLineSearch(function, direction, lsr, false); }  static LineSearchResult doLineSearch(DifferentiableFunction function, double[] direction, LineSearchResult lsr); static LineSearchResult doLineSearch(DifferentiableFunction function, double[] direction, LineSearchResult lsr, boolean verbose);  }
@Test public void testLineSearchDeterminesSaneStepLength01() { DifferentiableFunction objectiveFunction = new QuadraticFunction(); double[] testX = new double[] { 0 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { 1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertTrue(succCond); }
@Test public void testLineSearchDeterminesSaneStepLength02() { DifferentiableFunction objectiveFunction = new QuadraticFunction02(); double[] testX = new double[] { -2 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { 1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertTrue(succCond); }
@Test public void testLineSearchFailsWithWrongDirection01() { DifferentiableFunction objectiveFunction = new QuadraticFunction(); double[] testX = new double[] { 0 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { -1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertFalse(succCond); assertEquals(0.0, stepSize, TOLERANCE); }
@Test public void testLineSearchFailsWithWrongDirection02() { DifferentiableFunction objectiveFunction = new QuadraticFunction02(); double[] testX = new double[] { -2 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { -1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertFalse(succCond); assertEquals(0.0, stepSize, TOLERANCE); }
@Test public void testLineSearchFailsWithWrongDirection03() { DifferentiableFunction objectiveFunction = new QuadraticFunction(); double[] testX = new double[] { 4 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { 1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertFalse(succCond); assertEquals(0.0, stepSize, TOLERANCE); }
@Test public void testLineSearchFailsWithWrongDirection04() { DifferentiableFunction objectiveFunction = new QuadraticFunction02(); double[] testX = new double[] { 2 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { 1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertFalse(succCond); assertEquals(0.0, stepSize, TOLERANCE); }
@Test public void testLineSearchFailsAtMaxima01() { DifferentiableFunction objectiveFunction = new QuadraticFunction02(); double[] testX = new double[] { 0 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { -1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertFalse(succCond); assertEquals(0.0, stepSize, TOLERANCE); }
@Test public void testLineSearchFailsAtMaxima02() { DifferentiableFunction objectiveFunction = new QuadraticFunction02(); double[] testX = new double[] { 0 }; double testValueX = objectiveFunction.valueAt(testX); double[] testGradX = objectiveFunction.gradientAt(testX); double[] testDirection = new double[] { 1 }; LineSearchResult lsr = LineSearchResult.getInitialObject(testValueX, testGradX, testX); double stepSize = LineSearch.doLineSearch(objectiveFunction, testDirection, lsr).getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; assertFalse(succCond); assertEquals(0.0, stepSize, TOLERANCE); }