LeastSquareAdjuster.java

  1. /* Copyright 2013-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.orekit.rugged.adjustment;

  18. import org.hipparchus.linear.LUDecomposer;
  19. import org.hipparchus.linear.QRDecomposer;
  20. import org.hipparchus.optim.nonlinear.vector.leastsquares.GaussNewtonOptimizer;
  21. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer;
  22. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
  23. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem;
  24. import org.hipparchus.optim.nonlinear.vector.leastsquares.LevenbergMarquardtOptimizer;
  25. import org.orekit.rugged.errors.RuggedInternalError;

  26. /** LeastSquareAdjuster
  27.  * Class for setting least square algorithm chosen for solving optimization problem.
  28.  * @author Jonathan Guinet
  29.  * @author Lucie Labat-Allee
  30.  * @author Guylaine Prat
  31.  * @since 2.0
  32.  */
  33. public class LeastSquareAdjuster {

  34.     /** Least square optimizer.*/
  35.     private final LeastSquaresOptimizer adjuster;

  36.     /** Least square optimizer choice.*/
  37.     private final OptimizerId optimizerID;

  38.     /** Constructor.
  39.      * @param optimizerID optimizer choice
  40.      */
  41.     public LeastSquareAdjuster(final OptimizerId optimizerID) {

  42.         this.optimizerID = optimizerID;
  43.         this.adjuster = this.selectOptimizer();
  44.     }

  45.     /** Default constructor with Gauss Newton with QR decomposition algorithm.*/
  46.     public LeastSquareAdjuster() {

  47.         this.optimizerID = OptimizerId.GAUSS_NEWTON_QR;
  48.         this.adjuster = this.selectOptimizer();
  49.     }

  50.     /** Solve the least square problem.
  51.      * @param problem the least square problem
  52.      * @return the solution
  53.      */
  54.     public Optimum optimize(final LeastSquaresProblem problem) {
  55.         return this.adjuster.optimize(problem);
  56.     }

  57.     /** Create the optimizer.
  58.      * @return the least square optimizer
  59.      */
  60.     private LeastSquaresOptimizer selectOptimizer() {

  61.         // Set up the optimizer
  62.         switch (this.optimizerID) {

  63.             case LEVENBERG_MARQUADT:
  64.                 return new LevenbergMarquardtOptimizer();

  65.             case GAUSS_NEWTON_LU :
  66.                 return new GaussNewtonOptimizer(new LUDecomposer(1e-11), true);

  67.             case GAUSS_NEWTON_QR :
  68.                 return new GaussNewtonOptimizer(new QRDecomposer(1e-11), false);

  69.             default :
  70.                 // this should never happen
  71.                 throw new RuggedInternalError(null);
  72.         }
  73.     }
  74. }