@Test public void testLineSearchDeterminesSaneStepLength1() { Function objectiveFunction = new QuadraticFunction1(); 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); LineSearch.doLineSearch(objectiveFunction, testDirection, lsr, 1.0); double stepSize = lsr.getStepSize(); boolean succCond = TOLERANCE < stepSize && stepSize <= 1; Assert.assertTrue(succCond); }
public static void doLineSearch(Function function, double[] direction, LineSearchResult lsr, double initialStepSize) { double stepSize = initialStepSize; int currFctEvalCount = lsr.getFctEvalCount(); double[] x = lsr.getNextPoint(); double[] gradAtX = lsr.getGradAtNext(); double valueAtX = lsr.getValueAtNext(); int dimension = x.length; double[] nextPoint = lsr.getCurrPoint(); double[] gradAtNextPoint = lsr.getGradAtCurr(); double valueAtNextPoint; double dirGradientAtX = ArrayMath.innerProduct(direction, gradAtX); double cachedProd = C * dirGradientAtX; while (true) { for (int i = 0; i < dimension; i++) { nextPoint[i] = x[i] + direction[i] * stepSize; } valueAtNextPoint = function.valueAt(nextPoint); currFctEvalCount++; if (valueAtNextPoint <= valueAtX + cachedProd * stepSize) break; stepSize *= RHO; } System.arraycopy(function.gradientAt(nextPoint), 0, gradAtNextPoint, 0, gradAtNextPoint.length); lsr.setAll(stepSize, valueAtX, valueAtNextPoint, gradAtX, gradAtNextPoint, x, nextPoint, currFctEvalCount); }
LineSearch { public static void doLineSearch(Function function, double[] direction, LineSearchResult lsr, double initialStepSize) { double stepSize = initialStepSize; int currFctEvalCount = lsr.getFctEvalCount(); double[] x = lsr.getNextPoint(); double[] gradAtX = lsr.getGradAtNext(); double valueAtX = lsr.getValueAtNext(); int dimension = x.length; double[] nextPoint = lsr.getCurrPoint(); double[] gradAtNextPoint = lsr.getGradAtCurr(); double valueAtNextPoint; double dirGradientAtX = ArrayMath.innerProduct(direction, gradAtX); double cachedProd = C * dirGradientAtX; while (true) { for (int i = 0; i < dimension; i++) { nextPoint[i] = x[i] + direction[i] * stepSize; } valueAtNextPoint = function.valueAt(nextPoint); currFctEvalCount++; if (valueAtNextPoint <= valueAtX + cachedProd * stepSize) break; stepSize *= RHO; } System.arraycopy(function.gradientAt(nextPoint), 0, gradAtNextPoint, 0, gradAtNextPoint.length); lsr.setAll(stepSize, valueAtX, valueAtNextPoint, gradAtX, gradAtNextPoint, x, nextPoint, currFctEvalCount); } }
LineSearch { public static void doLineSearch(Function function, double[] direction, LineSearchResult lsr, double initialStepSize) { double stepSize = initialStepSize; int currFctEvalCount = lsr.getFctEvalCount(); double[] x = lsr.getNextPoint(); double[] gradAtX = lsr.getGradAtNext(); double valueAtX = lsr.getValueAtNext(); int dimension = x.length; double[] nextPoint = lsr.getCurrPoint(); double[] gradAtNextPoint = lsr.getGradAtCurr(); double valueAtNextPoint; double dirGradientAtX = ArrayMath.innerProduct(direction, gradAtX); double cachedProd = C * dirGradientAtX; while (true) { for (int i = 0; i < dimension; i++) { nextPoint[i] = x[i] + direction[i] * stepSize; } valueAtNextPoint = function.valueAt(nextPoint); currFctEvalCount++; if (valueAtNextPoint <= valueAtX + cachedProd * stepSize) break; stepSize *= RHO; } System.arraycopy(function.gradientAt(nextPoint), 0, gradAtNextPoint, 0, gradAtNextPoint.length); lsr.setAll(stepSize, valueAtX, valueAtNextPoint, gradAtX, gradAtNextPoint, x, nextPoint, currFctEvalCount); }  }
LineSearch { public static void doLineSearch(Function function, double[] direction, LineSearchResult lsr, double initialStepSize) { double stepSize = initialStepSize; int currFctEvalCount = lsr.getFctEvalCount(); double[] x = lsr.getNextPoint(); double[] gradAtX = lsr.getGradAtNext(); double valueAtX = lsr.getValueAtNext(); int dimension = x.length; double[] nextPoint = lsr.getCurrPoint(); double[] gradAtNextPoint = lsr.getGradAtCurr(); double valueAtNextPoint; double dirGradientAtX = ArrayMath.innerProduct(direction, gradAtX); double cachedProd = C * dirGradientAtX; while (true) { for (int i = 0; i < dimension; i++) { nextPoint[i] = x[i] + direction[i] * stepSize; } valueAtNextPoint = function.valueAt(nextPoint); currFctEvalCount++; if (valueAtNextPoint <= valueAtX + cachedProd * stepSize) break; stepSize *= RHO; } System.arraycopy(function.gradientAt(nextPoint), 0, gradAtNextPoint, 0, gradAtNextPoint.length); lsr.setAll(stepSize, valueAtX, valueAtNextPoint, gradAtX, gradAtNextPoint, x, nextPoint, currFctEvalCount); }  static void doLineSearch(Function function,
      double[] direction, LineSearchResult lsr, double initialStepSize); static void doConstrainedLineSearch(Function function,
      double[] direction, LineSearchResult lsr, double l1Cost, double initialStepSize); }
LineSearch { public static void doLineSearch(Function function, double[] direction, LineSearchResult lsr, double initialStepSize) { double stepSize = initialStepSize; int currFctEvalCount = lsr.getFctEvalCount(); double[] x = lsr.getNextPoint(); double[] gradAtX = lsr.getGradAtNext(); double valueAtX = lsr.getValueAtNext(); int dimension = x.length; double[] nextPoint = lsr.getCurrPoint(); double[] gradAtNextPoint = lsr.getGradAtCurr(); double valueAtNextPoint; double dirGradientAtX = ArrayMath.innerProduct(direction, gradAtX); double cachedProd = C * dirGradientAtX; while (true) { for (int i = 0; i < dimension; i++) { nextPoint[i] = x[i] + direction[i] * stepSize; } valueAtNextPoint = function.valueAt(nextPoint); currFctEvalCount++; if (valueAtNextPoint <= valueAtX + cachedProd * stepSize) break; stepSize *= RHO; } System.arraycopy(function.gradientAt(nextPoint), 0, gradAtNextPoint, 0, gradAtNextPoint.length); lsr.setAll(stepSize, valueAtX, valueAtNextPoint, gradAtX, gradAtNextPoint, x, nextPoint, currFctEvalCount); }  static void doLineSearch(Function function,
      double[] direction, LineSearchResult lsr, double initialStepSize); static void doConstrainedLineSearch(Function function,
      double[] direction, LineSearchResult lsr, double l1Cost, double initialStepSize);  }