Exemple #1
0
  /** line search */
  private void backtrackingLineSearch() throws AssertionError {
    double origDirDeriv = dirDeriv();
    // if a non-descent direction is chosen, the line search will break anyway, so throw here
    // The most likely reason for this is a bug in your function's gradient computation

    try {
      assert origDirDeriv < 0;
    } catch (AssertionError ae) {
      ae.printStackTrace();
      stderr.println("!! L-BFGS chose a non-descent direction: check your gradient!");
      // System.exit(1);

      throw new AssertionError(ae.toString());
    }

    double alpha = 1.0;
    double backoff = 0.5;
    if (iter == 1) {
      double normDir = Math.sqrt(ArrayUtils.dot(dir, dir));
      alpha = (1 / normDir);
      backoff = 0.1;

      if (DEBUG || verbose) {
        stderr.println("alpha:" + alpha);
        stderr.println("normDir:" + normDir);
      }
    }

    double oldValue = value;
    boolean first = true;
    while (true) {

      getNextPoint(alpha);
      value = evalL1();

      if (DEBUG || verbose) {
        stderr.println("alpha:" + alpha);
        stderr.println("value:" + value);

        stderr.println("oldValue:" + oldValue);
        stderr.println("C1:" + C1);
        stderr.println("origDirDeriv:" + origDirDeriv);

        stderr.println("alpha:" + alpha);

        assert !Double.isNaN(value);
        assert !Double.isNaN(origDirDeriv);
      }

      if (!first && value <= (oldValue + C1 * origDirDeriv * alpha)) {
        break;
      }

      first = false;

      if (!quiet) {
        stderr.print(".");
      }

      alpha *= backoff;
    }

    if (!quiet) {
      stderr.println();
    }
  }