BatchLSEstimator.java

  1. /* Copyright 2002-2018 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. package org.orekit.estimation.leastsquares;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.Comparator;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.hipparchus.exception.LocalizedCoreFormats;
  25. import org.hipparchus.exception.MathIllegalArgumentException;
  26. import org.hipparchus.exception.MathRuntimeException;
  27. import org.hipparchus.linear.RealMatrix;
  28. import org.hipparchus.linear.RealVector;
  29. import org.hipparchus.optim.ConvergenceChecker;
  30. import org.hipparchus.optim.nonlinear.vector.leastsquares.EvaluationRmsChecker;
  31. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresBuilder;
  32. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer;
  33. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
  34. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem;
  35. import org.hipparchus.optim.nonlinear.vector.leastsquares.ParameterValidator;
  36. import org.hipparchus.util.Incrementor;
  37. import org.orekit.errors.OrekitException;
  38. import org.orekit.errors.OrekitExceptionWrapper;
  39. import org.orekit.estimation.measurements.EstimatedMeasurement;
  40. import org.orekit.estimation.measurements.EstimationsProvider;
  41. import org.orekit.estimation.measurements.ObservedMeasurement;
  42. import org.orekit.orbits.Orbit;
  43. import org.orekit.propagation.conversion.NumericalPropagatorBuilder;
  44. import org.orekit.propagation.conversion.PropagatorBuilder;
  45. import org.orekit.propagation.numerical.NumericalPropagator;
  46. import org.orekit.utils.ParameterDriver;
  47. import org.orekit.utils.ParameterDriversList;
  48. import org.orekit.utils.ParameterDriversList.DelegatingDriver;


  49. /** Least squares estimator for orbit determination.
  50.  * @author Luc Maisonobe
  51.  * @since 8.0
  52.  */
  53. public class BatchLSEstimator {

  54.     /** Builders for propagators. */
  55.     private final NumericalPropagatorBuilder[] builders;

  56.     /** Measurements. */
  57.     private final List<ObservedMeasurement<?>> measurements;

  58.     /** Solver for least squares problem. */
  59.     private final LeastSquaresOptimizer optimizer;

  60.     /** Convergence threshold on normalized parameters. */
  61.     private double parametersConvergenceThreshold;

  62.     /** Builder for the least squares problem. */
  63.     private final LeastSquaresBuilder lsBuilder;

  64.     /** Observer for iterations. */
  65.     private BatchLSObserver observer;

  66.     /** Last estimations. */
  67.     private Map<ObservedMeasurement<?>, EstimatedMeasurement<?>> estimations;

  68.     /** Last orbits. */
  69.     private Orbit[] orbits;

  70.     /** Optimum found. */
  71.     private Optimum optimum;

  72.     /** Counter for the evaluations. */
  73.     private Incrementor evaluationsCounter;

  74.     /** Counter for the iterations. */
  75.     private Incrementor iterationsCounter;

  76.     /** Simple constructor.
  77.      * <p>
  78.      * If multiple {@link PropagatorBuilder propagator builders} are set up,
  79.      * the orbits of several spacecrafts will be used simultaneously.
  80.      * This is useful if the propagators share some model or measurements
  81.      * parameters (typically pole motion, prime meridian correction or
  82.      * ground stations positions).
  83.      * </p>
  84.      * <p>
  85.      * Setting up multiple {@link PropagatorBuilder propagator builders} is
  86.      * also useful when inter-satellite measurements are used, even if only one
  87.      * of the orbit is estimated and the other ones are fixed. This is typically
  88.      * used when very high accuracy GNSS measurements are needed and the
  89.      * navigation bulletins are not considered accurate enough and the navigation
  90.      * constellation must be propagated numerically.
  91.      * </p>
  92.      * @param optimizer solver for least squares problem
  93.      * @param propagatorBuilder builders to use for propagation
  94.      * @exception OrekitException if some propagator parameter cannot be retrieved
  95.      */
  96.     public BatchLSEstimator(final LeastSquaresOptimizer optimizer,
  97.                             final NumericalPropagatorBuilder... propagatorBuilder)
  98.         throws OrekitException {

  99.         this.builders                       = propagatorBuilder;
  100.         this.measurements                   = new ArrayList<ObservedMeasurement<?>>();
  101.         this.optimizer                      = optimizer;
  102.         this.parametersConvergenceThreshold = Double.NaN;
  103.         this.lsBuilder                      = new LeastSquaresBuilder();
  104.         this.observer                       = null;
  105.         this.estimations                    = null;
  106.         this.orbits                         = new Orbit[builders.length];

  107.         // our model computes value and Jacobian in one call,
  108.         // so we don't use the lazy evaluation feature
  109.         lsBuilder.lazyEvaluation(false);

  110.         // we manage weight by ourselves, as we change them during
  111.         // iterations (setting to 0 the identified outliers measurements)
  112.         // so the least squares problem should not see our weights
  113.         lsBuilder.weight(null);

  114.     }

  115.     /** Set an observer for iterations.
  116.      * @param observer observer to be notified at the end of each iteration
  117.      */
  118.     public void setObserver(final BatchLSObserver observer) {
  119.         this.observer = observer;
  120.     }

  121.     /** Add a measurement.
  122.      * @param measurement measurement to add
  123.      * @exception OrekitException if the measurement has a parameter
  124.      * that is already used
  125.      */
  126.     public void addMeasurement(final ObservedMeasurement<?> measurement)
  127.         throws OrekitException {
  128.         measurements.add(measurement);
  129.     }

  130.     /** Set the maximum number of iterations.
  131.      * <p>
  132.      * The iterations correspond to the top level iterations of
  133.      * the {@link LeastSquaresOptimizer least squares optimizer}.
  134.      * </p>
  135.      * @param maxIterations maxIterations maximum number of iterations
  136.      * @see #setMaxEvaluations(int)
  137.      * @see #getIterationsCount()
  138.      */
  139.     public void setMaxIterations(final int maxIterations) {
  140.         lsBuilder.maxIterations(maxIterations);
  141.     }

  142.     /** Set the maximum number of model evaluations.
  143.      * <p>
  144.      * The evaluations correspond to the orbit propagations and
  145.      * measurements estimations performed with a set of estimated
  146.      * parameters.
  147.      * </p>
  148.      * <p>
  149.      * For {@link org.hipparchus.optim.nonlinear.vector.leastsquares.GaussNewtonOptimizer
  150.      * Gauss-Newton optimizer} there is one evaluation at each iteration,
  151.      * so the maximum numbers may be set to the same value. For {@link
  152.      * org.hipparchus.optim.nonlinear.vector.leastsquares.LevenbergMarquardtOptimizer
  153.      * Levenberg-Marquardt optimizer}, there can be several evaluations at
  154.      * some iterations (typically for the first couple of iterations), so the
  155.      * maximum number of evaluations may be set to a higher value than the
  156.      * maximum number of iterations.
  157.      * </p>
  158.      * @param maxEvaluations maximum number of model evaluations
  159.      * @see #setMaxIterations(int)
  160.      * @see #getEvaluationsCount()
  161.      */
  162.     public void setMaxEvaluations(final int maxEvaluations) {
  163.         lsBuilder.maxEvaluations(maxEvaluations);
  164.     }

  165.     /** Get the orbital parameters supported by this estimator.
  166.      * <p>
  167.      * If there are more than one propagator builder, then the names
  168.      * of the drivers have an index marker in square brackets appended
  169.      * to them in order to distinguish the various orbits. So for example
  170.      * with one builder generating Keplerian orbits the names would be
  171.      * simply "a", "e", "i"... but if there are several builders the
  172.      * names would be "a[0]", "e[0]", "i[0]"..."a[1]", "e[1]", "i[1]"...
  173.      * </p>
  174.      * @param estimatedOnly if true, only estimated parameters are returned
  175.      * @return orbital parameters supported by this estimator
  176.      * @exception OrekitException if different parameters have the same name
  177.      */
  178.     public ParameterDriversList getOrbitalParametersDrivers(final boolean estimatedOnly)
  179.         throws OrekitException {

  180.         final ParameterDriversList estimated = new ParameterDriversList();
  181.         for (int i = 0; i < builders.length; ++i) {
  182.             final String suffix = builders.length > 1 ? "[" + i + "]" : null;
  183.             for (final DelegatingDriver delegating : builders[i].getOrbitalParametersDrivers().getDrivers()) {
  184.                 if (delegating.isSelected() || !estimatedOnly) {
  185.                     for (final ParameterDriver driver : delegating.getRawDrivers()) {
  186.                         if (suffix != null && !driver.getName().endsWith(suffix)) {
  187.                             // we add suffix only conditionally because the method may already have been called
  188.                             // and suffixes may have already been appended
  189.                             driver.setName(driver.getName() + suffix);
  190.                         }
  191.                         estimated.add(driver);
  192.                     }
  193.                 }
  194.             }
  195.         }
  196.         return estimated;

  197.     }

  198.     /** Get the propagator parameters supported by this estimator.
  199.      * @param estimatedOnly if true, only estimated parameters are returned
  200.      * @return propagator parameters supported by this estimator
  201.      * @exception OrekitException if different parameters have the same name
  202.      */
  203.     public ParameterDriversList getPropagatorParametersDrivers(final boolean estimatedOnly)
  204.         throws OrekitException {

  205.         final ParameterDriversList estimated = new ParameterDriversList();
  206.         for (PropagatorBuilder builder : builders) {
  207.             for (final DelegatingDriver delegating : builder.getPropagationParametersDrivers().getDrivers()) {
  208.                 if (delegating.isSelected() || !estimatedOnly) {
  209.                     for (final ParameterDriver driver : delegating.getRawDrivers()) {
  210.                         estimated.add(driver);
  211.                     }
  212.                 }
  213.             }
  214.         }
  215.         return estimated;

  216.     }

  217.     /** Get the measurements parameters supported by this estimator (including measurements and modifiers).
  218.      * @param estimatedOnly if true, only estimated parameters are returned
  219.      * @return measurements parameters supported by this estimator
  220.      * @exception OrekitException if different parameters have the same name
  221.      */
  222.     public ParameterDriversList getMeasurementsParametersDrivers(final boolean estimatedOnly)
  223.         throws OrekitException {

  224.         final ParameterDriversList parameters =  new ParameterDriversList();
  225.         for (final  ObservedMeasurement<?> measurement : measurements) {
  226.             for (final ParameterDriver driver : measurement.getParametersDrivers()) {
  227.                 if ((!estimatedOnly) || driver.isSelected()) {
  228.                     parameters.add(driver);
  229.                 }
  230.             }
  231.         }

  232.         parameters.sort();

  233.         return parameters;

  234.     }

  235.     /**
  236.      * Set convergence threshold.
  237.      * <p>
  238.      * The convergence used for estimation is based on the estimated
  239.      * parameters {@link ParameterDriver#getNormalizedValue() normalized values}.
  240.      * Convergence is considered to have been reached when the difference
  241.      * between previous and current normalized value is less than the
  242.      * convergence threshold for all parameters. The same value is used
  243.      * for all parameters since they are normalized and hence dimensionless.
  244.      * </p>
  245.      * <p>
  246.      * Normalized values are computed as {@code (current - reference)/scale},
  247.      * so convergence is reached when the following condition holds for
  248.      * all estimated parameters:
  249.      * {@code |current[i] - previous[i]| <= threshold * scale[i]}
  250.      * </p>
  251.      * <p>
  252.      * So the convergence threshold specified here can be considered as
  253.      * a multiplication factor applied to scale. Since for all parameters
  254.      * the scale is often small (typically about 1 m for orbital positions
  255.      * for example), then the threshold should not be too small. A value
  256.      * of 10⁻³ is often quite accurate.
  257.      *
  258.      * @param parametersConvergenceThreshold convergence threshold on
  259.      * normalized parameters (dimensionless, related to parameters scales)
  260.      * @see EvaluationRmsChecker
  261.      */
  262.     public void setParametersConvergenceThreshold(final double parametersConvergenceThreshold) {
  263.         this.parametersConvergenceThreshold = parametersConvergenceThreshold;
  264.     }

  265.     /** Estimate the orbital, propagation and measurements parameters.
  266.      * <p>
  267.      * The initial guess for all parameters must have been set before calling this method
  268.      * using {@link #getOrbitalParametersDrivers(boolean)}, {@link #getPropagatorParametersDrivers(boolean)},
  269.      * and {@link #getMeasurementsParametersDrivers(boolean)} and then {@link ParameterDriver#setValue(double)
  270.      * setting the values} of the parameters.
  271.      * </p>
  272.      * <p>
  273.      * For parameters whose reference date has not been set to a non-null date beforehand (i.e.
  274.      * the parameters for which {@link ParameterDriver#getReferenceDate()} returns {@code null},
  275.      * a default reference date will be set automatically at the start of the estimation to the
  276.      * {@link NumericalPropagatorBuilder#getInitialOrbitDate() initial orbit date} of the first
  277.      * propagator builder. For parameters whose reference date has been set to a non-null date,
  278.      * this reference date is untouched.
  279.      * </p>
  280.      * <p>
  281.      * After this method returns, the estimated parameters can be retrieved using
  282.      * {@link #getOrbitalParametersDrivers(boolean)}, {@link #getPropagatorParametersDrivers(boolean)},
  283.      * and {@link #getMeasurementsParametersDrivers(boolean)} and then {@link ParameterDriver#getValue()
  284.      * getting the values} of the parameters.
  285.      * </p>
  286.      * <p>
  287.      * As a convenience, the method also returns a fully configured and ready to use
  288.      * propagator set up with all the estimated values.
  289.      * </p>
  290.      * <p>
  291.      * For even more in-depth information, the {@link #getOptimum()} method provides detailed
  292.      * elements (covariance matrix, estimated parameters standard deviation, weighted Jacobian, RMS,
  293.      * χ², residuals and more).
  294.      * </p>
  295.      * @return propagators configured with estimated orbits as initial states, and all
  296.      * propagators estimated parameters also set
  297.      * @exception OrekitException if there is a conflict in parameters names
  298.      * or if orbit cannot be determined
  299.      */
  300.     public NumericalPropagator[] estimate() throws OrekitException {

  301.         // set reference date for all parameters that lack one (including the not estimated parameters)
  302.         for (final ParameterDriver driver : getOrbitalParametersDrivers(false).getDrivers()) {
  303.             if (driver.getReferenceDate() == null) {
  304.                 driver.setReferenceDate(builders[0].getInitialOrbitDate());
  305.             }
  306.         }
  307.         for (final ParameterDriver driver : getPropagatorParametersDrivers(false).getDrivers()) {
  308.             if (driver.getReferenceDate() == null) {
  309.                 driver.setReferenceDate(builders[0].getInitialOrbitDate());
  310.             }
  311.         }
  312.         for (final ParameterDriver driver : getMeasurementsParametersDrivers(false).getDrivers()) {
  313.             if (driver.getReferenceDate() == null) {
  314.                 driver.setReferenceDate(builders[0].getInitialOrbitDate());
  315.             }
  316.         }

  317.         // get all estimated parameters
  318.         final ParameterDriversList estimatedOrbitalParameters      = getOrbitalParametersDrivers(true);
  319.         final ParameterDriversList estimatedPropagatorParameters   = getPropagatorParametersDrivers(true);
  320.         final ParameterDriversList estimatedMeasurementsParameters = getMeasurementsParametersDrivers(true);

  321.         // create start point
  322.         final double[] start = new double[estimatedOrbitalParameters.getNbParams() +
  323.                                           estimatedPropagatorParameters.getNbParams() +
  324.                                           estimatedMeasurementsParameters.getNbParams()];
  325.         int iStart = 0;
  326.         for (final ParameterDriver driver : estimatedOrbitalParameters.getDrivers()) {
  327.             start[iStart++] = driver.getNormalizedValue();
  328.         }
  329.         for (final ParameterDriver driver : estimatedPropagatorParameters.getDrivers()) {
  330.             start[iStart++] = driver.getNormalizedValue();
  331.         }
  332.         for (final ParameterDriver driver : estimatedMeasurementsParameters.getDrivers()) {
  333.             start[iStart++] = driver.getNormalizedValue();
  334.         }
  335.         lsBuilder.start(start);

  336.         // create target (which is an array set to 0, as we compute weighted residuals ourselves)
  337.         int p = 0;
  338.         for (final ObservedMeasurement<?> measurement : measurements) {
  339.             if (measurement.isEnabled()) {
  340.                 p += measurement.getDimension();
  341.             }
  342.         }
  343.         final double[] target = new double[p];
  344.         lsBuilder.target(target);

  345.         // set up the model
  346.         final ModelObserver modelObserver = new ModelObserver() {
  347.             /** {@inheritDoc} */
  348.             @Override
  349.             public void modelCalled(final Orbit[] newOrbits,
  350.                                     final Map<ObservedMeasurement<?>, EstimatedMeasurement<?>> newEstimations) {
  351.                 BatchLSEstimator.this.orbits      = newOrbits;
  352.                 BatchLSEstimator.this.estimations = newEstimations;
  353.             }
  354.         };
  355.         final Model model = new Model(builders, measurements, estimatedMeasurementsParameters,
  356.                                       modelObserver);
  357.         lsBuilder.model(model);

  358.         // add a validator for orbital parameters
  359.         lsBuilder.parameterValidator(new Validator(estimatedOrbitalParameters,
  360.                                                    estimatedPropagatorParameters,
  361.                                                    estimatedMeasurementsParameters));

  362.         lsBuilder.checker(new ConvergenceChecker<LeastSquaresProblem.Evaluation>() {
  363.             /** {@inheritDoc} */
  364.             @Override
  365.             public boolean converged(final int iteration,
  366.                                      final LeastSquaresProblem.Evaluation previous,
  367.                                      final LeastSquaresProblem.Evaluation current) {
  368.                 final double lInf = current.getPoint().getLInfDistance(previous.getPoint());
  369.                 return lInf <= parametersConvergenceThreshold;
  370.             }
  371.         });

  372.         // set up the problem to solve
  373.         final LeastSquaresProblem problem = new TappedLSProblem(lsBuilder.build(),
  374.                                                                 model,
  375.                                                                 estimatedOrbitalParameters,
  376.                                                                 estimatedPropagatorParameters,
  377.                                                                 estimatedMeasurementsParameters);

  378.         try {

  379.             // solve the problem
  380.             optimum = optimizer.optimize(problem);

  381.             // create a new configured propagator with all estimated parameters
  382.             return model.createPropagators(optimum.getPoint());

  383.         } catch (MathRuntimeException mrte) {
  384.             throw new OrekitException(mrte);
  385.         } catch (OrekitExceptionWrapper oew) {
  386.             throw oew.getException();
  387.         }

  388.     }

  389.     /** Get the last estimations performed.
  390.      * @return last estimations performed
  391.      */
  392.     public Map<ObservedMeasurement<?>, EstimatedMeasurement<?>> getLastEstimations() {
  393.         return Collections.unmodifiableMap(estimations);
  394.     }

  395.     /** Get the optimum found.
  396.      * <p>
  397.      * The {@link Optimum} object contains detailed elements (covariance matrix, estimated
  398.      * parameters standard deviation, weighted Jacobian, RMS, χ², residuals and more).
  399.      * </p>
  400.      * <p>
  401.      * Beware that the returned object is the raw view from the underlying mathematical
  402.      * library. At this ral level, parameters have {@link ParameterDriver#getNormalizedValue()
  403.      * normalized values} whereas the space flight parameters have {@link ParameterDriver#getValue()
  404.      * physical values} with their units. So there are {@link ParameterDriver#getScale() scaling
  405.      * factors} to apply when using these elements.
  406.      * </p>
  407.      * @return optimum found after last call to {@link #estimate()}
  408.      */
  409.     public Optimum getOptimum() {
  410.         return optimum;
  411.     }

  412.     /** Get the covariances matrix in space flight dynamics physical units.
  413.      * <p>
  414.      * This method retrieve the {@link
  415.      * org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation#getCovariances(double)
  416.      * covariances} from the [@link {@link #getOptimum() optimum} and applies the scaling factors
  417.      * to it in order to convert it from raw normalized values back to physical values.
  418.      * </p>
  419.      * @param threshold threshold to identify matrix singularity
  420.      * @return covariances matrix in space flight dynamics physical units
  421.      * @exception OrekitException if the covariance matrix cannot be computed (singular problem).
  422.      * @since 9.1
  423.      */
  424.     public RealMatrix getPhysicalCovariances(final double threshold)
  425.         throws OrekitException {
  426.         final RealMatrix covariances;
  427.         try {
  428.             // get the normalized matrix
  429.             covariances = optimum.getCovariances(threshold).copy();
  430.         } catch (MathIllegalArgumentException miae) {
  431.             // the problem is singular
  432.             throw new OrekitException(miae);
  433.         }

  434.         // retrieve the scaling factors
  435.         final double[] scale = new double[covariances.getRowDimension()];
  436.         int index = 0;
  437.         for (final ParameterDriver driver : getOrbitalParametersDrivers(true).getDrivers()) {
  438.             scale[index++] = driver.getScale();
  439.         }
  440.         for (final ParameterDriver driver : getPropagatorParametersDrivers(true).getDrivers()) {
  441.             scale[index++] = driver.getScale();
  442.         }
  443.         for (final ParameterDriver driver : getMeasurementsParametersDrivers(true).getDrivers()) {
  444.             scale[index++] = driver.getScale();
  445.         }

  446.         // unnormalize the matrix, to retrieve physical covariances
  447.         for (int i = 0; i < covariances.getRowDimension(); ++i) {
  448.             for (int j = 0; j < covariances.getColumnDimension(); ++j) {
  449.                 covariances.setEntry(i, j, scale[i] * scale[j] * covariances.getEntry(i, j));
  450.             }
  451.         }

  452.         return covariances;

  453.     }

  454.     /** Get the number of iterations used for last estimation.
  455.      * @return number of iterations used for last estimation
  456.      * @see #setMaxIterations(int)
  457.      */
  458.     public int getIterationsCount() {
  459.         return iterationsCounter.getCount();
  460.     }

  461.     /** Get the number of evaluations used for last estimation.
  462.      * @return number of evaluations used for last estimation
  463.      * @see #setMaxEvaluations(int)
  464.      */
  465.     public int getEvaluationsCount() {
  466.         return evaluationsCounter.getCount();
  467.     }

  468.     /** Wrapper used to tap the various counters. */
  469.     private class TappedLSProblem implements LeastSquaresProblem {

  470.         /** Underlying problem. */
  471.         private final LeastSquaresProblem problem;

  472.         /** Multivariate function model. */
  473.         private final Model model;

  474.         /** Estimated orbital parameters. */
  475.         private final ParameterDriversList estimatedOrbitalParameters;

  476.         /** Estimated propagator parameters. */
  477.         private final ParameterDriversList estimatedPropagatorParameters;

  478.         /** Estimated measurements parameters. */
  479.         private final ParameterDriversList estimatedMeasurementsParameters;

  480.         /** Simple constructor.
  481.          * @param problem underlying problem
  482.          * @param model multivariate function model
  483.          * @param estimatedOrbitalParameters estimated orbital parameters
  484.          * @param estimatedPropagatorParameters estimated propagator parameters
  485.          * @param estimatedMeasurementsParameters estimated measurements parameters
  486.          */
  487.         TappedLSProblem(final LeastSquaresProblem problem,
  488.                         final Model model,
  489.                         final ParameterDriversList estimatedOrbitalParameters,
  490.                         final ParameterDriversList estimatedPropagatorParameters,
  491.                         final ParameterDriversList estimatedMeasurementsParameters) {
  492.             this.problem                         = problem;
  493.             this.model                           = model;
  494.             this.estimatedOrbitalParameters      = estimatedOrbitalParameters;
  495.             this.estimatedPropagatorParameters   = estimatedPropagatorParameters;
  496.             this.estimatedMeasurementsParameters = estimatedMeasurementsParameters;
  497.         }

  498.         /** {@inheritDoc} */
  499.         @Override
  500.         public Incrementor getEvaluationCounter() {
  501.             // tap the evaluations counter
  502.             BatchLSEstimator.this.evaluationsCounter = problem.getEvaluationCounter();
  503.             model.setEvaluationsCounter(BatchLSEstimator.this.evaluationsCounter);
  504.             return BatchLSEstimator.this.evaluationsCounter;
  505.         }

  506.         /** {@inheritDoc} */
  507.         @Override
  508.         public Incrementor getIterationCounter() {
  509.             // tap the iterations counter
  510.             BatchLSEstimator.this.iterationsCounter = problem.getIterationCounter();
  511.             model.setIterationsCounter(BatchLSEstimator.this.iterationsCounter);
  512.             return BatchLSEstimator.this.iterationsCounter;
  513.         }

  514.         /** {@inheritDoc} */
  515.         @Override
  516.         public ConvergenceChecker<Evaluation> getConvergenceChecker() {
  517.             return problem.getConvergenceChecker();
  518.         }

  519.         /** {@inheritDoc} */
  520.         @Override
  521.         public RealVector getStart() {
  522.             return problem.getStart();
  523.         }

  524.         /** {@inheritDoc} */
  525.         @Override
  526.         public int getObservationSize() {
  527.             return problem.getObservationSize();
  528.         }

  529.         /** {@inheritDoc} */
  530.         @Override
  531.         public int getParameterSize() {
  532.             return problem.getParameterSize();
  533.         }

  534.         /** {@inheritDoc} */
  535.         @Override
  536.         public Evaluation evaluate(final RealVector point) {

  537.             // perform the evaluation
  538.             final Evaluation evaluation = problem.evaluate(point);

  539.             // notify the observer
  540.             if (observer != null) {
  541.                 try {
  542.                     observer.evaluationPerformed(iterationsCounter.getCount(),
  543.                                                  evaluationsCounter.getCount(),
  544.                                                  orbits,
  545.                                                  estimatedOrbitalParameters,
  546.                                                  estimatedPropagatorParameters,
  547.                                                  estimatedMeasurementsParameters,
  548.                                                  new Provider(),
  549.                                                  evaluation);
  550.                 } catch (OrekitException oe) {
  551.                     throw new OrekitExceptionWrapper(oe);
  552.                 }
  553.             }

  554.             return evaluation;

  555.         }

  556.     }

  557.     /** Provider for evaluations. */
  558.     private class Provider implements EstimationsProvider {

  559.         /** Sorted estimations. */
  560.         private EstimatedMeasurement<?>[] sortedEstimations;

  561.         /** {@inheritDoc} */
  562.         @Override
  563.         public int getNumber() {
  564.             return estimations.size();
  565.         }

  566.         /** {@inheritDoc} */
  567.         @Override
  568.         public EstimatedMeasurement<?> getEstimatedMeasurement(final int index)
  569.             throws OrekitException {

  570.             // safety checks
  571.             if (index < 0 || index >= estimations.size()) {
  572.                 throw new OrekitException(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE,
  573.                                           index, 0, estimations.size());
  574.             }

  575.             if (sortedEstimations == null) {

  576.                 // lazy evaluation of the sorted array
  577.                 sortedEstimations = new EstimatedMeasurement<?>[estimations.size()];
  578.                 int i = 0;
  579.                 for (final Map.Entry<ObservedMeasurement<?>, EstimatedMeasurement<?>> entry : estimations.entrySet()) {
  580.                     sortedEstimations[i++] = entry.getValue();
  581.                 }

  582.                 // sort the array, primarily chronologically
  583.                 Arrays.sort(sortedEstimations, 0, sortedEstimations.length, Comparator.naturalOrder());

  584.             }

  585.             return sortedEstimations[index];

  586.         }

  587.     }

  588.     /** Validator for estimated parameters. */
  589.     private static class Validator implements ParameterValidator {

  590.         /** Estimated orbital parameters. */
  591.         private final ParameterDriversList estimatedOrbitalParameters;

  592.         /** Estimated propagator parameters. */
  593.         private final ParameterDriversList estimatedPropagatorParameters;

  594.         /** Estimated measurements parameters. */
  595.         private final ParameterDriversList estimatedMeasurementsParameters;

  596.         /** Simple constructor.
  597.          * @param estimatedOrbitalParameters estimated orbital parameters
  598.          * @param estimatedPropagatorParameters estimated propagator parameters
  599.          * @param estimatedMeasurementsParameters estimated measurements parameters
  600.          */
  601.         Validator(final ParameterDriversList estimatedOrbitalParameters,
  602.                   final ParameterDriversList estimatedPropagatorParameters,
  603.                   final ParameterDriversList estimatedMeasurementsParameters) {
  604.             this.estimatedOrbitalParameters      = estimatedOrbitalParameters;
  605.             this.estimatedPropagatorParameters   = estimatedPropagatorParameters;
  606.             this.estimatedMeasurementsParameters = estimatedMeasurementsParameters;
  607.         }

  608.         /** {@inheritDoc} */
  609.         @Override
  610.         public RealVector validate(final RealVector params)
  611.             throws OrekitExceptionWrapper {

  612.             try {
  613.                 int i = 0;
  614.                 for (final ParameterDriver driver : estimatedOrbitalParameters.getDrivers()) {
  615.                     // let the parameter handle min/max clipping
  616.                     driver.setNormalizedValue(params.getEntry(i));
  617.                     params.setEntry(i++, driver.getNormalizedValue());
  618.                 }
  619.                 for (final ParameterDriver driver : estimatedPropagatorParameters.getDrivers()) {
  620.                     // let the parameter handle min/max clipping
  621.                     driver.setNormalizedValue(params.getEntry(i));
  622.                     params.setEntry(i++, driver.getNormalizedValue());
  623.                 }
  624.                 for (final ParameterDriver driver : estimatedMeasurementsParameters.getDrivers()) {
  625.                     // let the parameter handle min/max clipping
  626.                     driver.setNormalizedValue(params.getEntry(i));
  627.                     params.setEntry(i++, driver.getNormalizedValue());
  628.                 }

  629.                 return params;
  630.             } catch (OrekitException oe) {
  631.                 throw new OrekitExceptionWrapper(oe);
  632.             }
  633.         }
  634.     }

  635. }