ParameterDriver.java

  1. /* Copyright 2002-2017 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.utils;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.util.FastMath;
  21. import org.hipparchus.util.Precision;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.time.AbsoluteDate;


  25. /** Class allowing to drive the value of a parameter.
  26.  * <p>
  27.  * This class is typically used as a bridge between an estimation
  28.  * algorithm (typically orbit determination or optimizer) and an
  29.  * internal parameter in a physical model that needs to be tuned,
  30.  * or a bridge between a finite differences algorithm and an
  31.  * internal parameter in a physical model that needs to be slightly
  32.  * offset. The physical model will expose to the algorithm a
  33.  * set of instances of this class so the algorithm can call the
  34.  * {@link #setValue(double)} method to update the
  35.  * parameter value. Each time the value is set, the physical model
  36.  * will be notified as it will register a {@link ParameterObserver
  37.  * ParameterObserver} for this purpose.
  38.  * </p>
  39.  * <p>
  40.  * This design has two major goals. First, it allows an external
  41.  * algorithm to drive internal parameters almost anonymously, as it only
  42.  * needs to get a list of instances of this class, without knowing
  43.  * what they really drive. Second, it allows the physical model to
  44.  * not expose directly setters methods for its parameters. In order
  45.  * to be able to modify the parameter value, the algorithm
  46.  * <em>must</em> retrieve a parameter driver.
  47.  * </p>
  48.  * @see ParameterObserver
  49.  * @author Luc Maisonobe
  50.  * @since 8.0
  51.  */
  52. public class ParameterDriver {

  53.     /** Name of the parameter. */
  54.     private String name;

  55.     /** Reference value. */
  56.     private final double referenceValue;

  57.     /** Scaling factor. */
  58.     private final double scale;

  59.     /** Minimum value. */
  60.     private final double minValue;

  61.     /** Maximum value. */
  62.     private final double maxValue;

  63.     /** Reference date.
  64.      * @since 9.0
  65.      */
  66.     private AbsoluteDate referenceDate;

  67.     /** Current value. */
  68.     private double value;

  69.     /** Selection status.
  70.      * <p>
  71.      * Selection is used for estimated parameters in orbit determination,
  72.      * or to compute the Jacobian matrix in partial derivatives computation.
  73.      * </p>
  74.      */
  75.     private boolean selected;

  76.     /** Observers observing this driver. */
  77.     private final List<ParameterObserver> observers;

  78.     /** Simple constructor.
  79.      * <p>
  80.      * At construction, the parameter is configured as <em>not</em> selected,
  81.      * the reference date is set to {@code null} and the value is set to the
  82.      * {@code referenceValue}.
  83.      * </p>
  84.      * @param name name of the parameter
  85.      * @param referenceValue reference value of the parameter
  86.      * @param scale scaling factor to convert the parameters value to
  87.      * non-dimensional (typically set to the expected standard deviation of the
  88.      * parameter), it must be non-zero
  89.      * @param minValue minimum value
  90.      * @param maxValue maximum value
  91.      * @exception OrekitException if scale is too close to zero
  92.      */
  93.     public ParameterDriver(final String name, final double referenceValue,
  94.                            final double scale, final double minValue,
  95.                            final double maxValue)
  96.         throws OrekitException {
  97.         if (FastMath.abs(scale) <= Precision.SAFE_MIN) {
  98.             throw new OrekitException(OrekitMessages.TOO_SMALL_SCALE_FOR_PARAMETER,
  99.                                       name, scale);
  100.         }
  101.         this.name           = name;
  102.         this.referenceValue = referenceValue;
  103.         this.scale          = scale;
  104.         this.minValue       = minValue;
  105.         this.maxValue       = maxValue;
  106.         this.referenceDate  = null;
  107.         this.value          = referenceValue;
  108.         this.selected       = false;
  109.         this.observers      = new ArrayList<ParameterObserver>();
  110.     }


  111.     /** Add an observer for this driver.
  112.      * <p>
  113.      * The observer {@link ParameterObserver#valueChanged(double, ParameterDriver)
  114.      * valueChanged} method is called once automatically when the
  115.      * observer is added, and then called at each value change.
  116.      * </p>
  117.      * @param observer observer to add
  118.      * @exception OrekitException if the observer triggers one
  119.      * while being updated
  120.      */
  121.     public void addObserver(final ParameterObserver observer)
  122.         throws OrekitException {
  123.         observers.add(observer);
  124.         observer.valueChanged(getValue(), this);
  125.     }

  126.     /** Change the name of this parameter driver.
  127.      * @param name new name
  128.      */
  129.     public void setName(final String name) {
  130.         final String previousName = this.name;
  131.         this.name = name;
  132.         for (final ParameterObserver observer : observers) {
  133.             observer.nameChanged(previousName, this);
  134.         }
  135.     }

  136.     /** Get name.
  137.      * @return name
  138.      */
  139.     public String getName() {
  140.         return name;
  141.     }

  142.     /** Get reference parameter value.
  143.      * @return reference parameter value
  144.      */
  145.     public double getReferenceValue() {
  146.         return referenceValue;
  147.     }

  148.     /** Get minimum parameter value.
  149.      * @return minimum parameter value
  150.      */
  151.     public double getMinValue() {
  152.         return minValue;
  153.     }

  154.     /** Get maximum parameter value.
  155.      * @return maximum parameter value
  156.      */
  157.     public double getMaxValue() {
  158.         return maxValue;
  159.     }

  160.     /** Get scale.
  161.      * @return scale
  162.      */
  163.     public double getScale() {
  164.         return scale;
  165.     }

  166.     /** Get normalized value.
  167.      * <p>
  168.      * The normalized value is a non-dimensional value
  169.      * suitable for use as part of a vector in an optimization
  170.      * process. It is computed as {@code (current - reference)/scale}.
  171.      * </p>
  172.      * @return normalized value
  173.      */
  174.     public double getNormalizedValue() {
  175.         return (value - referenceValue) / scale;
  176.     }

  177.     /** Set normalized value.
  178.      * <p>
  179.      * The normalized value is a non-dimensional value
  180.      * suitable for use as part of a vector in an optimization
  181.      * process. It is computed as {@code (current - reference)/scale}.
  182.      * </p>
  183.      * @param normalized value
  184.      * @exception OrekitException if an observer throws one
  185.      */
  186.     public void setNormalizedValue(final double normalized) throws OrekitException {
  187.         setValue(referenceValue + scale * normalized);
  188.     }

  189.     /** Get current reference date.
  190.      * @return current reference date (null if it was never set)
  191.      * @since 9.0
  192.      */
  193.     public AbsoluteDate getReferenceDate() {
  194.         return referenceDate;
  195.     }

  196.     /** Set reference date.
  197.      * @param newReferenceDate new reference date
  198.      * @since 9.0
  199.      */
  200.     public void setReferenceDate(final AbsoluteDate newReferenceDate) {
  201.         final AbsoluteDate previousReferenceDate = getReferenceDate();
  202.         referenceDate = newReferenceDate;
  203.         for (final ParameterObserver observer : observers) {
  204.             observer.referenceDateChanged(previousReferenceDate, this);
  205.         }
  206.     }

  207.     /** Get current parameter value.
  208.      * @return current parameter value
  209.      */
  210.     public double getValue() {
  211.         return value;
  212.     }

  213.     /** Set parameter value.
  214.      * <p>
  215.      * If {@code newValue} is below {@link #getMinValue()}, it will
  216.      * be silently to {@link #getMinValue()}. If {@code newValue} is
  217.      * above {@link #getMaxValue()}, it will be silently to {@link
  218.      * #getMaxValue()}.
  219.      * </p>
  220.      * @param newValue new value
  221.      * @exception OrekitException if an observer throws one
  222.      */
  223.     public void setValue(final double newValue) throws OrekitException {
  224.         final double previousValue = getValue();
  225.         value = FastMath.max(minValue, FastMath.min(maxValue, newValue));
  226.         for (final ParameterObserver observer : observers) {
  227.             observer.valueChanged(previousValue, this);
  228.         }
  229.     }

  230.     /** Configure a parameter selection status.
  231.      * <p>
  232.      * Selection is used for estimated parameters in orbit determination,
  233.      * or to compute the Jacobian matrix in partial derivatives computation.
  234.      * </p>
  235.      * @param selected if true the parameter is selected,
  236.      * otherwise it will be fixed
  237.      */
  238.     public void setSelected(final boolean selected) {
  239.         final boolean previousSelection = isSelected();
  240.         this.selected = selected;
  241.         for (final ParameterObserver observer : observers) {
  242.             observer.selectionChanged(previousSelection, this);
  243.         }
  244.     }

  245.     /** Check if parameter is selected.
  246.      * <p>
  247.      * Selection is used for estimated parameters in orbit determination,
  248.      * or to compute the Jacobian matrix in partial derivatives computation.
  249.      * </p>
  250.      * @return true if parameter is selected, false if it is not
  251.      */
  252.     public boolean isSelected() {
  253.         return selected;
  254.     }

  255.     /** Get a text representation of the parameter.
  256.      * @return text representation of the parameter, in the form name = value.
  257.      */
  258.     public String toString() {
  259.         return name + " = " + value;
  260.     }

  261. }