1   /* Copyright 2013-2019 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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  
18  package org.orekit.rugged.adjustment;
19  
20  import org.hipparchus.linear.LUDecomposer;
21  import org.hipparchus.linear.QRDecomposer;
22  import org.hipparchus.optim.nonlinear.vector.leastsquares.GaussNewtonOptimizer;
23  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer;
24  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
25  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem;
26  import org.hipparchus.optim.nonlinear.vector.leastsquares.LevenbergMarquardtOptimizer;
27  import org.orekit.rugged.errors.RuggedInternalError;
28  
29  /** LeastSquareAdjuster
30   * Class for setting least square algorithm chosen for solving optimization problem.
31   * @author Jonathan Guinet
32   * @author Lucie Labat-Allee
33   * @author Guylaine Prat
34   * @since 2.0
35   */
36  public class LeastSquareAdjuster {
37  
38      /** Least square optimizer.*/
39      private final LeastSquaresOptimizer adjuster;
40  
41      /** Least square optimizer choice.*/
42      private final OptimizerId optimizerID;
43  
44      /** Constructor.
45       * @param optimizerID optimizer choice
46       */
47      public LeastSquareAdjuster(final OptimizerId optimizerID) {
48  
49          this.optimizerID = optimizerID;
50          this.adjuster = this.selectOptimizer();
51      }
52  
53      /** Default constructor with Gauss Newton with QR decomposition algorithm.*/
54      public LeastSquareAdjuster() {
55  
56          this.optimizerID = OptimizerId.GAUSS_NEWTON_QR;
57          this.adjuster = this.selectOptimizer();
58      }
59  
60      /** Solve the least square problem.
61       * @param problem the least square problem
62       * @return the solution
63       */
64      public Optimum optimize(final LeastSquaresProblem problem) {
65          return this.adjuster.optimize(problem);
66      }
67  
68      /** Create the optimizer.
69       * @return the least square optimizer
70       */
71      private LeastSquaresOptimizer selectOptimizer() {
72  
73          // Set up the optimizer
74          switch (this.optimizerID) {
75  
76              case LEVENBERG_MARQUADT:
77                  return new LevenbergMarquardtOptimizer();
78  
79              case GAUSS_NEWTON_LU :
80                  return new GaussNewtonOptimizer(new LUDecomposer(1e-11), true);
81  
82              case GAUSS_NEWTON_QR :
83                  return new GaussNewtonOptimizer(new QRDecomposer(1e-11), false);
84  
85              default :
86                  // this should never happen
87                  throw new RuggedInternalError(null);
88          }
89      }
90  }