// ToDo Convert to parametric test
  private static boolean testCase(String inFile, String expectedFile) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(inFile));

    int N = Integer.parseInt(in.readLine().trim());

    int[] a = new int[N];

    for (int i = 0; i < N; i++) {
      a[i] = Integer.parseInt(in.readLine().trim());
    }

    BufferedReader expected = new BufferedReader(new FileReader(expectedFile));
    boolean error = false;

    final String filename = new File(inFile).getName();
    try {
      final int maxSum = Integer.parseInt(expected.readLine());
      final int nonCont = Integer.parseInt(expected.readLine());
      assertEquals("Error in file " + filename + ",", new Response(maxSum, nonCont), maxsum(a, N));
    } catch (AssertionError e) {
      System.out.print(HEADER);
      e.printStackTrace(System.out);
      error = true;
    }

    return error;
  }
예제 #2
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();
    }
  }