Java Code Examples for org.apache.commons.math3.util.Incrementor#incrementCount()

The following examples show how to use org.apache.commons.math3.util.Incrementor#incrementCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LeastSquaresFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Count the evaluations of a particular problem. The {@code counter} will be
 * incremented every time {@link LeastSquaresProblem#evaluate(RealVector)} is called on
 * the <em>returned</em> problem.
 *
 * @param problem the problem to track.
 * @param counter the counter to increment.
 * @return a least squares problem that tracks evaluations
 */
public static LeastSquaresProblem countEvaluations(final LeastSquaresProblem problem,
                                                   final Incrementor counter) {
    return new LeastSquaresAdapter(problem) {

        public Evaluation evaluate(final RealVector point) {
            counter.incrementCount();
            return super.evaluate(point);
        }

        // Delegate the rest.
    };
}
 
Example 2
Source File: LeastSquaresFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Count the evaluations of a particular problem. The {@code counter} will be
 * incremented every time {@link LeastSquaresProblem#evaluate(RealVector)} is called on
 * the <em>returned</em> problem.
 *
 * @param problem the problem to track.
 * @param counter the counter to increment.
 * @return a least squares problem that tracks evaluations
 */
public static LeastSquaresProblem countEvaluations(final LeastSquaresProblem problem,
                                                   final Incrementor counter) {
    return new LeastSquaresAdapter(problem) {

        public Evaluation evaluate(final RealVector point) {
            counter.incrementCount();
            return super.evaluate(point);
        }

        // Delegate the rest.
    };
}
 
Example 3
Source File: GaussNewtonOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Optimum optimize(final LeastSquaresProblem lsp) {
    //create local evaluation and iteration counts
    final Incrementor evaluationCounter = lsp.getEvaluationCounter();
    final Incrementor iterationCounter = lsp.getIterationCounter();
    final ConvergenceChecker<Evaluation> checker
            = lsp.getConvergenceChecker();

    // Computation will be useless without a checker (see "for-loop").
    if (checker == null) {
        throw new NullArgumentException();
    }

    RealVector currentPoint = lsp.getStart();

    // iterate until convergence is reached
    Evaluation current = null;
    while (true) {
        iterationCounter.incrementCount();

        // evaluate the objective function and its jacobian
        Evaluation previous = current;
        // Value of the objective function at "currentPoint".
        evaluationCounter.incrementCount();
        current = lsp.evaluate(currentPoint);
        final RealVector currentResiduals = current.getResiduals();
        final RealMatrix weightedJacobian = current.getJacobian();
        currentPoint = current.getPoint();

        // Check convergence.
        if (previous != null) {
            if (checker.converged(iterationCounter.getCount(), previous, current)) {
                return new OptimumImpl(
                        current,
                        evaluationCounter.getCount(),
                        iterationCounter.getCount());
            }
        }

        // solve the linearized least squares problem
        final RealVector dX = this.decomposition.solve(weightedJacobian, currentResiduals);
        // update the estimated parameters
        currentPoint = currentPoint.add(dX);
    }
}
 
Example 4
Source File: GaussNewtonOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public Optimum optimize(final LeastSquaresProblem lsp) {
    //create local evaluation and iteration counts
    final Incrementor evaluationCounter = lsp.getEvaluationCounter();
    final Incrementor iterationCounter = lsp.getIterationCounter();
    final ConvergenceChecker<Evaluation> checker
            = lsp.getConvergenceChecker();

    // Computation will be useless without a checker (see "for-loop").
    if (checker == null) {
        throw new NullArgumentException();
    }

    RealVector currentPoint = lsp.getStart();

    // iterate until convergence is reached
    Evaluation current = null;
    while (true) {
        iterationCounter.incrementCount();

        // evaluate the objective function and its jacobian
        Evaluation previous = current;
        // Value of the objective function at "currentPoint".
        evaluationCounter.incrementCount();
        current = lsp.evaluate(currentPoint);
        final RealVector currentResiduals = current.getResiduals();
        final RealMatrix weightedJacobian = current.getJacobian();
        currentPoint = current.getPoint();

        // Check convergence.
        if (previous != null) {
            if (checker.converged(iterationCounter.getCount(), previous, current)) {
                return new OptimumImpl(
                        current,
                        evaluationCounter.getCount(),
                        iterationCounter.getCount());
            }
        }

        // solve the linearized least squares problem
        final RealVector dX = this.decomposition.solve(weightedJacobian, currentResiduals);
        // update the estimated parameters
        currentPoint = currentPoint.add(dX);
    }
}