org.apache.commons.math.optimization.ConvergenceChecker Java Examples

The following examples show how to use org.apache.commons.math.optimization.ConvergenceChecker. 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: PowellOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the convergence checker.
 * It also indirectly sets the line search tolerances to the square-root
 * of the correponding tolerances in the checker.
 *
 * @param checker Convergence checker.
 */
public void setConvergenceChecker(ConvergenceChecker<RealPointValuePair> checker) {
    super.setConvergenceChecker(checker);

    // Line search tolerances can be much lower than the tolerances
    // required for the optimizer itself.
    final double minTol = 1e-4;
    final double rel = Math.min(Math.sqrt(checker.getRelativeThreshold()), minTol);
    final double abs = Math.min(Math.sqrt(checker.getAbsoluteThreshold()), minTol);
    line.setConvergenceChecker(new BrentOptimizer.BrentConvergenceChecker(rel, abs));
}
 
Example #2
Source File: MultiStartUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ConvergenceChecker<UnivariateRealPointValuePair> getConvergenceChecker() {
    return optimizer.getConvergenceChecker();
}
 
Example #3
Source File: GaussNewtonOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public VectorialPointValuePair doOptimize() throws MathUserException {

    final ConvergenceChecker<VectorialPointValuePair> checker
        = getConvergenceChecker();

    // iterate until convergence is reached
    VectorialPointValuePair current = null;
    int iter = 0;
    for (boolean converged = false; !converged;) {
        ++iter;

        // evaluate the objective function and its jacobian
        VectorialPointValuePair previous = current;
        updateResidualsAndCost();
        updateJacobian();
        current = new VectorialPointValuePair(point, objective);

        final double[] targetValues = getTargetRef();
        final double[] residualsWeights = getWeightRef();

        // build the linear problem
        final double[]   b = new double[cols];
        final double[][] a = new double[cols][cols];
        for (int i = 0; i < rows; ++i) {

            final double[] grad   = weightedResidualJacobian[i];
            final double weight   = residualsWeights[i];
            final double residual = objective[i] - targetValues[i];

            // compute the normal equation
            final double wr = weight * residual;
            for (int j = 0; j < cols; ++j) {
                b[j] += wr * grad[j];
            }

            // build the contribution matrix for measurement i
            for (int k = 0; k < cols; ++k) {
                double[] ak = a[k];
                double wgk = weight * grad[k];
                for (int l = 0; l < cols; ++l) {
                    ak[l] += wgk * grad[l];
                }
            }
        }

        try {
            // solve the linearized least squares problem
            RealMatrix mA = new BlockRealMatrix(a);
            DecompositionSolver solver = useLU ?
                    new LUDecompositionImpl(mA).getSolver() :
                    new QRDecompositionImpl(mA).getSolver();
            final double[] dX = solver.solve(b);

            // update the estimated parameters
            for (int i = 0; i < cols; ++i) {
                point[i] += dX[i];
            }
        } catch (SingularMatrixException e) {
            throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM);
        }

        // check convergence
        if (checker != null) {
            if (previous != null) {
                converged = checker.converged(iter, previous, current);
            }
        }
    }
    // we have converged
    return current;
}
 
Example #4
Source File: BaseAbstractVectorialOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void setConvergenceChecker(ConvergenceChecker<VectorialPointValuePair> convergenceChecker) {
    this.checker = convergenceChecker;
}
 
Example #5
Source File: MultiStartUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ConvergenceChecker<UnivariateRealPointValuePair> getConvergenceChecker() {
    return optimizer.getConvergenceChecker();
}
 
Example #6
Source File: SimplexOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() throws MathUserException {
    if (simplex == null) {
        throw new NullArgumentException();
    }

    // Indirect call to "computeObjectiveValue" in order to update the
    // evaluations counter.
    final MultivariateRealFunction evalFunc
        = new MultivariateRealFunction() {
            public double value(double[] point) throws MathUserException {
                return computeObjectiveValue(point);
            }
        };

    final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
    final Comparator<RealPointValuePair> comparator
        = new Comparator<RealPointValuePair>() {
        public int compare(final RealPointValuePair o1,
                           final RealPointValuePair o2) {
            final double v1 = o1.getValue();
            final double v2 = o2.getValue();
            return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
        }
    };

    // Initialize search.
    simplex.build(getStartPoint());
    simplex.evaluate(evalFunc, comparator);

    RealPointValuePair[] previous = null;
    int iteration = 0;
    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    while (true) {
        if (iteration > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.getSize(); i++) {
                RealPointValuePair prev = previous[i];
                converged &= checker.converged(iteration, prev, simplex.getPoint(i));
            }
            if (converged) {
                // We have found an optimum.
                return simplex.getPoint(0);
            }
        }

        // We still need to search.
        previous = simplex.getPoints();
        simplex.iterate(evalFunc, comparator);
        ++iteration;
    }
}
 
Example #7
Source File: BaseAbstractScalarOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param checker Convergence checker.
 */
protected BaseAbstractScalarOptimizer(ConvergenceChecker<RealPointValuePair> checker) {
    this.checker = checker;
}
 
Example #8
Source File: BaseAbstractScalarOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void setConvergenceChecker(ConvergenceChecker<RealPointValuePair> convergenceChecker) {
    this.checker = convergenceChecker;
}
 
Example #9
Source File: BaseAbstractScalarOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public ConvergenceChecker<RealPointValuePair> getConvergenceChecker() {
    return checker;
}
 
Example #10
Source File: BaseAbstractVectorialOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param checker Convergence checker.
 */
protected BaseAbstractVectorialOptimizer(ConvergenceChecker<VectorialPointValuePair> checker) {
    this.checker = checker;
}
 
Example #11
Source File: BaseAbstractVectorialOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void setConvergenceChecker(ConvergenceChecker<VectorialPointValuePair> convergenceChecker) {
    this.checker = convergenceChecker;
}
 
Example #12
Source File: BaseAbstractVectorialOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public ConvergenceChecker<VectorialPointValuePair> getConvergenceChecker() {
    return checker;
}
 
Example #13
Source File: MultiStartUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void setConvergenceChecker(ConvergenceChecker<UnivariateRealPointValuePair> checker) {
    optimizer.setConvergenceChecker(checker);
}
 
Example #14
Source File: BaseAbstractScalarOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void setConvergenceChecker(ConvergenceChecker<RealPointValuePair> convergenceChecker) {
    this.checker = convergenceChecker;
}
 
Example #15
Source File: BaseAbstractScalarOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param checker Convergence checker.
 */
protected BaseAbstractScalarOptimizer(ConvergenceChecker<RealPointValuePair> checker) {
    this.checker = checker;
}
 
Example #16
Source File: SimplexOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() throws MathUserException {
    if (simplex == null) {
        throw new NullArgumentException();
    }

    // Indirect call to "computeObjectiveValue" in order to update the
    // evaluations counter.
    final MultivariateRealFunction evalFunc
        = new MultivariateRealFunction() {
            public double value(double[] point) throws MathUserException {
                return computeObjectiveValue(point);
            }
        };

    final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
    final Comparator<RealPointValuePair> comparator
        = new Comparator<RealPointValuePair>() {
        public int compare(final RealPointValuePair o1,
                           final RealPointValuePair o2) {
            final double v1 = o1.getValue();
            final double v2 = o2.getValue();
            return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
        }
    };

    // Initialize search.
    simplex.build(getStartPoint());
    simplex.evaluate(evalFunc, comparator);

    RealPointValuePair[] previous = null;
    int iteration = 0;
    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    while (true) {
        if (iteration > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.getSize(); i++) {
                RealPointValuePair prev = previous[i];
                converged &= checker.converged(iteration, prev, simplex.getPoint(i));
            }
            if (converged) {
                // We have found an optimum.
                return simplex.getPoint(0);
            }
        }

        // We still need to search.
        previous = simplex.getPoints();
        simplex.iterate(evalFunc, comparator);
        ++iteration;
    }
}
 
Example #17
Source File: BaseAbstractScalarOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public ConvergenceChecker<RealPointValuePair> getConvergenceChecker() {
    return checker;
}
 
Example #18
Source File: MultiStartUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void setConvergenceChecker(ConvergenceChecker<UnivariateRealPointValuePair> checker) {
    optimizer.setConvergenceChecker(checker);
}
 
Example #19
Source File: AbstractUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ConvergenceChecker<UnivariateRealPointValuePair> getConvergenceChecker() {
    return checker;
}
 
Example #20
Source File: AbstractUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void setConvergenceChecker(ConvergenceChecker<UnivariateRealPointValuePair> c) {
    checker = c;
}
 
Example #21
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param checker Convergence checker.
 */
protected AbstractLeastSquaresOptimizer(ConvergenceChecker<VectorialPointValuePair> checker) {
    super(checker);
}
 
Example #22
Source File: AbstractScalarDifferentiableOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param checker Convergence checker.
 */
protected AbstractScalarDifferentiableOptimizer(ConvergenceChecker<RealPointValuePair> checker) {
    super(checker);
}
 
Example #23
Source File: GaussNewtonOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public VectorialPointValuePair doOptimize() throws MathUserException {

    final ConvergenceChecker<VectorialPointValuePair> checker
        = getConvergenceChecker();

    // iterate until convergence is reached
    VectorialPointValuePair current = null;
    int iter = 0;
    for (boolean converged = false; !converged;) {
        ++iter;

        // evaluate the objective function and its jacobian
        VectorialPointValuePair previous = current;
        updateResidualsAndCost();
        updateJacobian();
        current = new VectorialPointValuePair(point, objective);

        final double[] targetValues = getTargetRef();
        final double[] residualsWeights = getWeightRef();

        // build the linear problem
        final double[]   b = new double[cols];
        final double[][] a = new double[cols][cols];
        for (int i = 0; i < rows; ++i) {

            final double[] grad   = weightedResidualJacobian[i];
            final double weight   = residualsWeights[i];
            final double residual = objective[i] - targetValues[i];

            // compute the normal equation
            final double wr = weight * residual;
            for (int j = 0; j < cols; ++j) {
                b[j] += wr * grad[j];
            }

            // build the contribution matrix for measurement i
            for (int k = 0; k < cols; ++k) {
                double[] ak = a[k];
                double wgk = weight * grad[k];
                for (int l = 0; l < cols; ++l) {
                    ak[l] += wgk * grad[l];
                }
            }
        }

        try {
            // solve the linearized least squares problem
            RealMatrix mA = new BlockRealMatrix(a);
            DecompositionSolver solver = useLU ?
                    new LUDecompositionImpl(mA).getSolver() :
                    new QRDecompositionImpl(mA).getSolver();
            final double[] dX = solver.solve(b);

            // update the estimated parameters
            for (int i = 0; i < cols; ++i) {
                point[i] += dX[i];
            }
        } catch (SingularMatrixException e) {
            throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM);
        }

        // check convergence
        if (checker != null) {
            if (previous != null) {
                converged = checker.converged(iter, previous, current);
            }
        }
    }
    // we have converged
    return current;
}
 
Example #24
Source File: DirectSearchOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
protected RealPointValuePair doOptimize()
    throws FunctionEvaluationException {

    final double[] startPoint = getStartPoint();
    if ((startConfiguration == null) ||
        (startConfiguration.length != startPoint.length)) {
        // No initial configuration has been set up for simplex
        // build a default one from a unit hypercube.
        final double[] unit = new double[startPoint.length];
        Arrays.fill(unit, 1.0);
        setStartConfiguration(unit);
    }
    
    final boolean isMinim = (getGoalType() == GoalType.MINIMIZE);
    final Comparator<RealPointValuePair> comparator
        = new Comparator<RealPointValuePair>() {
        public int compare(final RealPointValuePair o1,
                           final RealPointValuePair o2) {
            final double v1 = o1.getValue();
            final double v2 = o2.getValue();
            return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1);
        }
    };

    // Initialize search.
    buildSimplex(startPoint);
    evaluateSimplex(comparator);

    RealPointValuePair[] previous = new RealPointValuePair[simplex.length];
    int iteration = 0;
    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    while (true) {
        if (iteration > 0) {
            boolean converged = true;
            for (int i = 0; i < simplex.length; ++i) {
                converged &= checker.converged(iteration, previous[i], simplex[i]);
            }
            if (converged) {
                // we have found an optimum
                return simplex[0];
            }
        }

        // We still need to search.
        System.arraycopy(simplex, 0, previous, 0, simplex.length);
        iterateSimplex(comparator);
        ++iteration;
    }
}
 
Example #25
Source File: MultiDirectional.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void iterateSimplex(final Comparator<RealPointValuePair> comparator)
    throws FunctionEvaluationException {

    final ConvergenceChecker<RealPointValuePair> checker = getConvergenceChecker();
    int iteration = 0;
    while (true) {
        ++iteration;

        // Save the original vertex.
        final RealPointValuePair[] original = simplex;
        final RealPointValuePair best = original[0];

        // Perform a reflection step.
        final RealPointValuePair reflected = evaluateNewSimplex(original, 1.0, comparator);
        if (comparator.compare(reflected, best) < 0) {

            // Compute the expanded simplex.
            final RealPointValuePair[] reflectedSimplex = simplex;
            final RealPointValuePair expanded = evaluateNewSimplex(original, khi, comparator);
            if (comparator.compare(reflected, expanded) <= 0) {
                // Accept the reflected simplex.
                simplex = reflectedSimplex;
            }

            return;
        }

        // Compute the contracted simplex.
        final RealPointValuePair contracted = evaluateNewSimplex(original, gamma, comparator);
        if (comparator.compare(contracted, best) < 0) {
            // Accept the contracted simplex.
            return;
        }

        // Check convergence.
        boolean converged = true;
        for (int i = 0; i < simplex.length; ++i) {
            converged &= checker.converged(iteration, original[i], simplex[i]);
        }
        if (converged) {
            return;
        }
    }
}
 
Example #26
Source File: MultiStartUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ConvergenceChecker<UnivariateRealPointValuePair> getConvergenceChecker() {
    return optimizer.getConvergenceChecker();
}
 
Example #27
Source File: MultiStartUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void setConvergenceChecker(ConvergenceChecker<UnivariateRealPointValuePair> checker) {
    optimizer.setConvergenceChecker(checker);
}
 
Example #28
Source File: AbstractUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ConvergenceChecker<UnivariateRealPointValuePair> getConvergenceChecker() {
    return checker;
}
 
Example #29
Source File: AbstractUnivariateRealOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void setConvergenceChecker(ConvergenceChecker<UnivariateRealPointValuePair> checker) {
    this.checker = checker;
}
 
Example #30
Source File: BaseAbstractVectorialOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public ConvergenceChecker<VectorialPointValuePair> getConvergenceChecker() {
    return checker;
}