Search in sources :

Example 1 with LeastSquaresProblem

use of org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem in project Orekit by CS-SI.

the class BatchLSEstimator method estimate.

/**
 * Estimate the orbital, propagation and measurements parameters.
 * <p>
 * The initial guess for all parameters must have been set before calling this method
 * using {@link #getOrbitalParametersDrivers(boolean)}, {@link #getPropagatorParametersDrivers(boolean)},
 * and {@link #getMeasurementsParametersDrivers(boolean)} and then {@link ParameterDriver#setValue(double)
 * setting the values} of the parameters.
 * </p>
 * <p>
 * For parameters whose reference date has not been set to a non-null date beforehand (i.e.
 * the parameters for which {@link ParameterDriver#getReferenceDate()} returns {@code null},
 * a default reference date will be set automatically at the start of the estimation to the
 * {@link NumericalPropagatorBuilder#getInitialOrbitDate() initial orbit date} of the first
 * propagator builder. For parameters whose reference date has been set to a non-null date,
 * this reference date is untouched.
 * </p>
 * <p>
 * After this method returns, the estimated parameters can be retrieved using
 * {@link #getOrbitalParametersDrivers(boolean)}, {@link #getPropagatorParametersDrivers(boolean)},
 * and {@link #getMeasurementsParametersDrivers(boolean)} and then {@link ParameterDriver#getValue()
 * getting the values} of the parameters.
 * </p>
 * <p>
 * As a convenience, the method also returns a fully configured and ready to use
 * propagator set up with all the estimated values.
 * </p>
 * <p>
 * For even more in-depth information, the {@link #getOptimum()} method provides detailed
 * elements (covariance matrix, estimated parameters standard deviation, weighted Jacobian, RMS,
 * χ², residuals and more).
 * </p>
 * @return propagators configured with estimated orbits as initial states, and all
 * propagators estimated parameters also set
 * @exception OrekitException if there is a conflict in parameters names
 * or if orbit cannot be determined
 */
public NumericalPropagator[] estimate() throws OrekitException {
    // set reference date for all parameters that lack one (including the not estimated parameters)
    for (final ParameterDriver driver : getOrbitalParametersDrivers(false).getDrivers()) {
        if (driver.getReferenceDate() == null) {
            driver.setReferenceDate(builders[0].getInitialOrbitDate());
        }
    }
    for (final ParameterDriver driver : getPropagatorParametersDrivers(false).getDrivers()) {
        if (driver.getReferenceDate() == null) {
            driver.setReferenceDate(builders[0].getInitialOrbitDate());
        }
    }
    for (final ParameterDriver driver : getMeasurementsParametersDrivers(false).getDrivers()) {
        if (driver.getReferenceDate() == null) {
            driver.setReferenceDate(builders[0].getInitialOrbitDate());
        }
    }
    // get all estimated parameters
    final ParameterDriversList estimatedOrbitalParameters = getOrbitalParametersDrivers(true);
    final ParameterDriversList estimatedPropagatorParameters = getPropagatorParametersDrivers(true);
    final ParameterDriversList estimatedMeasurementsParameters = getMeasurementsParametersDrivers(true);
    // create start point
    final double[] start = new double[estimatedOrbitalParameters.getNbParams() + estimatedPropagatorParameters.getNbParams() + estimatedMeasurementsParameters.getNbParams()];
    int iStart = 0;
    for (final ParameterDriver driver : estimatedOrbitalParameters.getDrivers()) {
        start[iStart++] = driver.getNormalizedValue();
    }
    for (final ParameterDriver driver : estimatedPropagatorParameters.getDrivers()) {
        start[iStart++] = driver.getNormalizedValue();
    }
    for (final ParameterDriver driver : estimatedMeasurementsParameters.getDrivers()) {
        start[iStart++] = driver.getNormalizedValue();
    }
    lsBuilder.start(start);
    // create target (which is an array set to 0, as we compute weighted residuals ourselves)
    int p = 0;
    for (final ObservedMeasurement<?> measurement : measurements) {
        if (measurement.isEnabled()) {
            p += measurement.getDimension();
        }
    }
    final double[] target = new double[p];
    lsBuilder.target(target);
    // set up the model
    final ModelObserver modelObserver = new ModelObserver() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void modelCalled(final Orbit[] newOrbits, final Map<ObservedMeasurement<?>, EstimatedMeasurement<?>> newEstimations) {
            BatchLSEstimator.this.orbits = newOrbits;
            BatchLSEstimator.this.estimations = newEstimations;
        }
    };
    final Model model = new Model(builders, measurements, estimatedMeasurementsParameters, modelObserver);
    lsBuilder.model(model);
    // add a validator for orbital parameters
    lsBuilder.parameterValidator(new Validator(estimatedOrbitalParameters, estimatedPropagatorParameters, estimatedMeasurementsParameters));
    lsBuilder.checker(new ConvergenceChecker<LeastSquaresProblem.Evaluation>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean converged(final int iteration, final LeastSquaresProblem.Evaluation previous, final LeastSquaresProblem.Evaluation current) {
            final double lInf = current.getPoint().getLInfDistance(previous.getPoint());
            return lInf <= parametersConvergenceThreshold;
        }
    });
    // set up the problem to solve
    final LeastSquaresProblem problem = new TappedLSProblem(lsBuilder.build(), model, estimatedOrbitalParameters, estimatedPropagatorParameters, estimatedMeasurementsParameters);
    try {
        // solve the problem
        optimum = optimizer.optimize(problem);
        // create a new configured propagator with all estimated parameters
        return model.createPropagators(optimum.getPoint());
    } catch (MathRuntimeException mrte) {
        throw new OrekitException(mrte);
    } catch (OrekitExceptionWrapper oew) {
        throw oew.getException();
    }
}
Also used : MathRuntimeException(org.hipparchus.exception.MathRuntimeException) OrekitExceptionWrapper(org.orekit.errors.OrekitExceptionWrapper) ParameterDriver(org.orekit.utils.ParameterDriver) ParameterDriversList(org.orekit.utils.ParameterDriversList) LeastSquaresProblem(org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem) OrekitException(org.orekit.errors.OrekitException) Map(java.util.Map) ParameterValidator(org.hipparchus.optim.nonlinear.vector.leastsquares.ParameterValidator)

Example 2 with LeastSquaresProblem

use of org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem in project Orekit by CS-SI.

the class AbstractPropagatorConverter method fit.

/**
 * Find the propagator that minimize the mean square error for a sample of {@link SpacecraftState states}.
 * @param initial initial estimation parameters (position, velocity, free parameters)
 * @return fitted parameters
 * @exception OrekitException if propagator cannot be adapted
 * @exception MathRuntimeException if maximal number of iterations is exceeded
 */
private double[] fit(final double[] initial) throws OrekitException, MathRuntimeException {
    final LeastSquaresProblem problem = new LeastSquaresBuilder().maxIterations(maxIterations).maxEvaluations(Integer.MAX_VALUE).model(getModel()).target(target).weight(new DiagonalMatrix(weight)).start(initial).checker(checker).build();
    optimum = optimizer.optimize(problem);
    return optimum.getPoint().toArray();
}
Also used : DiagonalMatrix(org.hipparchus.linear.DiagonalMatrix) LeastSquaresProblem(org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem) LeastSquaresBuilder(org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresBuilder)

Aggregations

LeastSquaresProblem (org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem)2 Map (java.util.Map)1 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)1 DiagonalMatrix (org.hipparchus.linear.DiagonalMatrix)1 LeastSquaresBuilder (org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresBuilder)1 ParameterValidator (org.hipparchus.optim.nonlinear.vector.leastsquares.ParameterValidator)1 OrekitException (org.orekit.errors.OrekitException)1 OrekitExceptionWrapper (org.orekit.errors.OrekitExceptionWrapper)1 ParameterDriver (org.orekit.utils.ParameterDriver)1 ParameterDriversList (org.orekit.utils.ParameterDriversList)1