Java Code Examples for org.apache.commons.math.exception.ConvergenceException

Following code examples demonstrate how to use org.apache.commons.math.exception.ConvergenceExceptionfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.apache.commons.math.exception.ConvergenceExceptionand various code implementation of this class.

    }

    private void checkUnsolvableProblem(DifferentiableMultivariateVectorialOptimizer optimizer,
                                        boolean solvable) {
        Random randomizer = new Random(1248788532l);
        for (int degree = 0; degree < 10; ++degree) {
            PolynomialFunction p = buildRandomPolynomial(degree, randomizer);

            PolynomialFitter fitter = new PolynomialFitter(degree, optimizer);

            for (double x = -1.0; x < 1.0; x += 0.01) {
                fitter.addObservedPoint(1.0, 0.0, p.value(0.0));
            }

            try {
                fitter.fit();
                assertTrue(solvable || (degree == 0));
            } catch(ConvergenceException e) {
                assertTrue((! solvable) && (degree > 0));
            }

        }

    } 


    @Override
    public VectorialPointValuePair doOptimize()
        throws FunctionEvaluationException {

        VectorialPointValuePair current = null;
        int iter = 0;
        for (boolean converged = false; !converged;) {
            ++iter;

            VectorialPointValuePair previous = current;
            updateResidualsAndCost();
            updateJacobian();
            current = new VectorialPointValuePair(point, objective);

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

            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];

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

                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 {
                RealMatrix mA = new BlockRealMatrix(a);
                DecompositionSolver solver = useLU ?
                        new LUDecompositionImpl(mA).getSolver() :
                        new QRDecompositionImpl(mA).getSolver();
                final double[] dX = solver.solve(b);

                for (int i = 0; i < cols; ++i) {
                    point[i] += dX[i];
                }
            } catch (InvalidMatrixException e) {
                throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM);
            }

            if (previous != null) {
                converged = getConvergenceChecker().converged(iter, previous, current);
            }
        }
        return current;
    }