ParameterDriver.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.utils;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.List;

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


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

  55.     /** Name of the parameter. */
  56.     private String name;

  57.     /** Reference value. */
  58.     private final double referenceValue;

  59.     /** Scaling factor. */
  60.     private final double scale;

  61.     /** Minimum value. */
  62.     private final double minValue;

  63.     /** Maximum value. */
  64.     private final double maxValue;

  65.     /** Reference date.
  66.      * @since 9.0
  67.      */
  68.     private AbsoluteDate referenceDate;

  69.     /** Current value. */
  70.     private double value;

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

  78.     /** Observers observing this driver. */
  79.     private final List<ParameterObserver> observers;

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


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

  128.     /** Remove an observer.
  129.      * @param observer observer to remove
  130.      * @since 9.1
  131.      */
  132.     public void removeObserver(final ParameterObserver observer) {
  133.         for (final Iterator<ParameterObserver> iterator = observers.iterator(); iterator.hasNext();) {
  134.             if (iterator.next() == observer) {
  135.                 iterator.remove();
  136.                 return;
  137.             }
  138.         }
  139.     }

  140.     /** Get the observers for this driver.
  141.      * @return an unmodifiable view of the observers for this driver
  142.      * @since 9.1
  143.      */
  144.     public List<ParameterObserver> getObservers() {
  145.         return Collections.unmodifiableList(observers);
  146.     }

  147.     /** Change the name of this parameter driver.
  148.      * @param name new name
  149.      */
  150.     public void setName(final String name) {
  151.         final String previousName = this.name;
  152.         this.name = name;
  153.         for (final ParameterObserver observer : observers) {
  154.             observer.nameChanged(previousName, this);
  155.         }
  156.     }

  157.     /** Get name.
  158.      * @return name
  159.      */
  160.     public String getName() {
  161.         return name;
  162.     }

  163.     /** Get reference parameter value.
  164.      * @return reference parameter value
  165.      */
  166.     public double getReferenceValue() {
  167.         return referenceValue;
  168.     }

  169.     /** Get minimum parameter value.
  170.      * @return minimum parameter value
  171.      */
  172.     public double getMinValue() {
  173.         return minValue;
  174.     }

  175.     /** Get maximum parameter value.
  176.      * @return maximum parameter value
  177.      */
  178.     public double getMaxValue() {
  179.         return maxValue;
  180.     }

  181.     /** Get scale.
  182.      * @return scale
  183.      */
  184.     public double getScale() {
  185.         return scale;
  186.     }

  187.     /** Get normalized value.
  188.      * <p>
  189.      * The normalized value is a non-dimensional value
  190.      * suitable for use as part of a vector in an optimization
  191.      * process. It is computed as {@code (current - reference)/scale}.
  192.      * </p>
  193.      * @return normalized value
  194.      */
  195.     public double getNormalizedValue() {
  196.         return (value - referenceValue) / scale;
  197.     }

  198.     /** Set normalized value.
  199.      * <p>
  200.      * The normalized value is a non-dimensional value
  201.      * suitable for use as part of a vector in an optimization
  202.      * process. It is computed as {@code (current - reference)/scale}.
  203.      * </p>
  204.      * @param normalized value
  205.      * @exception OrekitException if an observer throws one
  206.      */
  207.     public void setNormalizedValue(final double normalized) throws OrekitException {
  208.         setValue(referenceValue + scale * normalized);
  209.     }

  210.     /** Get current reference date.
  211.      * @return current reference date (null if it was never set)
  212.      * @since 9.0
  213.      */
  214.     public AbsoluteDate getReferenceDate() {
  215.         return referenceDate;
  216.     }

  217.     /** Set reference date.
  218.      * @param newReferenceDate new reference date
  219.      * @since 9.0
  220.      */
  221.     public void setReferenceDate(final AbsoluteDate newReferenceDate) {
  222.         final AbsoluteDate previousReferenceDate = getReferenceDate();
  223.         referenceDate = newReferenceDate;
  224.         for (final ParameterObserver observer : observers) {
  225.             observer.referenceDateChanged(previousReferenceDate, this);
  226.         }
  227.     }

  228.     /** Get current parameter value.
  229.      * @return current parameter value
  230.      */
  231.     public double getValue() {
  232.         return value;
  233.     }

  234.     /** Set parameter value.
  235.      * <p>
  236.      * If {@code newValue} is below {@link #getMinValue()}, it will
  237.      * be silently set to {@link #getMinValue()}. If {@code newValue} is
  238.      * above {@link #getMaxValue()}, it will be silently set to {@link
  239.      * #getMaxValue()}.
  240.      * </p>
  241.      * @param newValue new value
  242.      * @exception OrekitException if an observer throws one
  243.      */
  244.     public void setValue(final double newValue) throws OrekitException {
  245.         final double previousValue = getValue();
  246.         value = FastMath.max(minValue, FastMath.min(maxValue, newValue));
  247.         for (final ParameterObserver observer : observers) {
  248.             observer.valueChanged(previousValue, this);
  249.         }
  250.     }

  251.     /** Configure a parameter selection status.
  252.      * <p>
  253.      * Selection is used for estimated parameters in orbit determination,
  254.      * or to compute the Jacobian matrix in partial derivatives computation.
  255.      * </p>
  256.      * @param selected if true the parameter is selected,
  257.      * otherwise it will be fixed
  258.      */
  259.     public void setSelected(final boolean selected) {
  260.         final boolean previousSelection = isSelected();
  261.         this.selected = selected;
  262.         for (final ParameterObserver observer : observers) {
  263.             observer.selectionChanged(previousSelection, this);
  264.         }
  265.     }

  266.     /** Check if parameter is selected.
  267.      * <p>
  268.      * Selection is used for estimated parameters in orbit determination,
  269.      * or to compute the Jacobian matrix in partial derivatives computation.
  270.      * </p>
  271.      * @return true if parameter is selected, false if it is not
  272.      */
  273.     public boolean isSelected() {
  274.         return selected;
  275.     }

  276.     /** Get a text representation of the parameter.
  277.      * @return text representation of the parameter, in the form name = value.
  278.      */
  279.     public String toString() {
  280.         return name + " = " + value;
  281.     }

  282. }