Java Code Examples for org.apache.commons.math.optimization.ConvergenceChecker#converged()

The following examples show how to use org.apache.commons.math.optimization.ConvergenceChecker#converged() . 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: 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 2
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++) {
                @SuppressWarnings("null") // Cannot be null when iteration > 0
                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 3
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 4
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 5
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 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: 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 8
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;
    }
}