001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.optimization.general;
019
020import org.apache.commons.math3.exception.ConvergenceException;
021import org.apache.commons.math3.exception.NullArgumentException;
022import org.apache.commons.math3.exception.MathInternalError;
023import org.apache.commons.math3.exception.util.LocalizedFormats;
024import org.apache.commons.math3.linear.ArrayRealVector;
025import org.apache.commons.math3.linear.BlockRealMatrix;
026import org.apache.commons.math3.linear.DecompositionSolver;
027import org.apache.commons.math3.linear.LUDecomposition;
028import org.apache.commons.math3.linear.QRDecomposition;
029import org.apache.commons.math3.linear.RealMatrix;
030import org.apache.commons.math3.linear.SingularMatrixException;
031import org.apache.commons.math3.optimization.ConvergenceChecker;
032import org.apache.commons.math3.optimization.SimpleVectorValueChecker;
033import org.apache.commons.math3.optimization.PointVectorValuePair;
034
035/**
036 * Gauss-Newton least-squares solver.
037 * <p>
038 * This class solve a least-square problem by solving the normal equations
039 * of the linearized problem at each iteration. Either LU decomposition or
040 * QR decomposition can be used to solve the normal equations. LU decomposition
041 * is faster but QR decomposition is more robust for difficult problems.
042 * </p>
043 *
044 * @deprecated As of 3.1 (to be removed in 4.0).
045 * @since 2.0
046 *
047 */
048@Deprecated
049public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer {
050    /** Indicator for using LU decomposition. */
051    private final boolean useLU;
052
053    /**
054     * Simple constructor with default settings.
055     * The normal equations will be solved using LU decomposition and the
056     * convergence check is set to a {@link SimpleVectorValueChecker}
057     * with default tolerances.
058     * @deprecated See {@link SimpleVectorValueChecker#SimpleVectorValueChecker()}
059     */
060    @Deprecated
061    public GaussNewtonOptimizer() {
062        this(true);
063    }
064
065    /**
066     * Simple constructor with default settings.
067     * The normal equations will be solved using LU decomposition.
068     *
069     * @param checker Convergence checker.
070     */
071    public GaussNewtonOptimizer(ConvergenceChecker<PointVectorValuePair> checker) {
072        this(true, checker);
073    }
074
075    /**
076     * Simple constructor with default settings.
077     * The convergence check is set to a {@link SimpleVectorValueChecker}
078     * with default tolerances.
079     *
080     * @param useLU If {@code true}, the normal equations will be solved
081     * using LU decomposition, otherwise they will be solved using QR
082     * decomposition.
083     * @deprecated See {@link SimpleVectorValueChecker#SimpleVectorValueChecker()}
084     */
085    @Deprecated
086    public GaussNewtonOptimizer(final boolean useLU) {
087        this(useLU, new SimpleVectorValueChecker());
088    }
089
090    /**
091     * @param useLU If {@code true}, the normal equations will be solved
092     * using LU decomposition, otherwise they will be solved using QR
093     * decomposition.
094     * @param checker Convergence checker.
095     */
096    public GaussNewtonOptimizer(final boolean useLU,
097                                ConvergenceChecker<PointVectorValuePair> checker) {
098        super(checker);
099        this.useLU = useLU;
100    }
101
102    /** {@inheritDoc} */
103    @Override
104    public PointVectorValuePair doOptimize() {
105        final ConvergenceChecker<PointVectorValuePair> checker
106            = getConvergenceChecker();
107
108        // Computation will be useless without a checker (see "for-loop").
109        if (checker == null) {
110            throw new NullArgumentException();
111        }
112
113        final double[] targetValues = getTarget();
114        final int nR = targetValues.length; // Number of observed data.
115
116        final RealMatrix weightMatrix = getWeight();
117        // Diagonal of the weight matrix.
118        final double[] residualsWeights = new double[nR];
119        for (int i = 0; i < nR; i++) {
120            residualsWeights[i] = weightMatrix.getEntry(i, i);
121        }
122
123        final double[] currentPoint = getStartPoint();
124        final int nC = currentPoint.length;
125
126        // iterate until convergence is reached
127        PointVectorValuePair current = null;
128        int iter = 0;
129        for (boolean converged = false; !converged;) {
130            ++iter;
131
132            // evaluate the objective function and its jacobian
133            PointVectorValuePair previous = current;
134            // Value of the objective function at "currentPoint".
135            final double[] currentObjective = computeObjectiveValue(currentPoint);
136            final double[] currentResiduals = computeResiduals(currentObjective);
137            final RealMatrix weightedJacobian = computeWeightedJacobian(currentPoint);
138            current = new PointVectorValuePair(currentPoint, currentObjective);
139
140            // build the linear problem
141            final double[]   b = new double[nC];
142            final double[][] a = new double[nC][nC];
143            for (int i = 0; i < nR; ++i) {
144
145                final double[] grad   = weightedJacobian.getRow(i);
146                final double weight   = residualsWeights[i];
147                final double residual = currentResiduals[i];
148
149                // compute the normal equation
150                final double wr = weight * residual;
151                for (int j = 0; j < nC; ++j) {
152                    b[j] += wr * grad[j];
153                }
154
155                // build the contribution matrix for measurement i
156                for (int k = 0; k < nC; ++k) {
157                    double[] ak = a[k];
158                    double wgk = weight * grad[k];
159                    for (int l = 0; l < nC; ++l) {
160                        ak[l] += wgk * grad[l];
161                    }
162                }
163            }
164
165            try {
166                // solve the linearized least squares problem
167                RealMatrix mA = new BlockRealMatrix(a);
168                DecompositionSolver solver = useLU ?
169                        new LUDecomposition(mA).getSolver() :
170                        new QRDecomposition(mA).getSolver();
171                final double[] dX = solver.solve(new ArrayRealVector(b, false)).toArray();
172                // update the estimated parameters
173                for (int i = 0; i < nC; ++i) {
174                    currentPoint[i] += dX[i];
175                }
176            } catch (SingularMatrixException e) {
177                throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM);
178            }
179
180            // Check convergence.
181            if (previous != null) {
182                converged = checker.converged(iter, previous, current);
183                if (converged) {
184                    cost = computeCost(currentResiduals);
185                    // Update (deprecated) "point" field.
186                    point = current.getPoint();
187                    return current;
188                }
189            }
190        }
191        // Must never happen.
192        throw new MathInternalError();
193    }
194}