ParameterDriver.java

  1. /* Copyright 2002-2020 CS GROUP
  2.  * Licensed to CS GROUP (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 java.util.Map;

  23. import org.hipparchus.analysis.differentiation.DSFactory;
  24. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  25. import org.hipparchus.analysis.differentiation.Gradient;
  26. import org.hipparchus.util.FastMath;
  27. import org.hipparchus.util.Precision;
  28. import org.orekit.errors.OrekitException;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.time.AbsoluteDate;


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

  59.     /** Name of the parameter. */
  60.     private String name;

  61.     /** Reference value. */
  62.     private double referenceValue;

  63.     /** Scaling factor. */
  64.     private double scale;

  65.     /** Minimum value. */
  66.     private double minValue;

  67.     /** Maximum value. */
  68.     private double maxValue;

  69.     /** Reference date.
  70.      * @since 9.0
  71.      */
  72.     private AbsoluteDate referenceDate;

  73.     /** Current value. */
  74.     private double value;

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

  82.     /** Observers observing this driver. */
  83.     private final List<ParameterObserver> observers;

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


  115.     /** Add an observer for this driver.
  116.      * <p>
  117.      * The observer {@link ParameterObserver#valueChanged(double, ParameterDriver)
  118.      * valueChanged} method is called once automatically when the
  119.      * observer is added, and then called at each value change.
  120.      * </p>
  121.      * @param observer observer to add
  122.           * while being updated
  123.      */
  124.     public void addObserver(final ParameterObserver observer) {
  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.     /** Replace an observer.
  141.      * @param oldObserver observer to replace
  142.      * @param newObserver new observer to use
  143.      * @since 10.1
  144.      */
  145.     public void replaceObserver(final ParameterObserver oldObserver, final ParameterObserver newObserver) {
  146.         for (int i = 0; i < observers.size(); ++i) {
  147.             if (observers.get(i) == oldObserver) {
  148.                 observers.set(i, newObserver);
  149.             }
  150.         }
  151.     }

  152.     /** Get the observers for this driver.
  153.      * @return an unmodifiable view of the observers for this driver
  154.      * @since 9.1
  155.      */
  156.     public List<ParameterObserver> getObservers() {
  157.         return Collections.unmodifiableList(observers);
  158.     }

  159.     /** Change the name of this parameter driver.
  160.      * @param name new name
  161.      */
  162.     public void setName(final String name) {
  163.         final String previousName = this.name;
  164.         this.name = name;
  165.         for (final ParameterObserver observer : observers) {
  166.             observer.nameChanged(previousName, this);
  167.         }
  168.     }

  169.     /** Get name.
  170.      * @return name
  171.      */
  172.     public String getName() {
  173.         return name;
  174.     }

  175.     /** Get reference parameter value.
  176.      * @return reference parameter value
  177.      */
  178.     public double getReferenceValue() {
  179.         return referenceValue;
  180.     }

  181.     /** Set reference parameter value.
  182.      * @since 9.3
  183.      * @param referenceValue the reference value to set.
  184.      */
  185.     public void setReferenceValue(final double referenceValue) {
  186.         final double previousReferenceValue = this.referenceValue;
  187.         this.referenceValue = referenceValue;
  188.         for (final ParameterObserver observer : observers) {
  189.             observer.referenceValueChanged(previousReferenceValue, this);
  190.         }
  191.     }

  192.     /** Get minimum parameter value.
  193.      * @return minimum parameter value
  194.      */
  195.     public double getMinValue() {
  196.         return minValue;
  197.     }

  198.     /** Set minimum parameter value.
  199.      * @since 9.3
  200.      * @param minValue the minimum value to set.
  201.      */
  202.     public void setMinValue(final double minValue) {
  203.         final double previousMinValue = this.minValue;
  204.         this.minValue = minValue;
  205.         for (final ParameterObserver observer : observers) {
  206.             observer.minValueChanged(previousMinValue, this);
  207.         }
  208.         // Check if current value is not out of min/max range
  209.         setValue(value);
  210.     }

  211.     /** Get maximum parameter value.
  212.      * @return maximum parameter value
  213.      */
  214.     public double getMaxValue() {
  215.         return maxValue;
  216.     }

  217.     /** Set maximum parameter value.
  218.      * @since 9.3
  219.      * @param maxValue the maximum value to set.
  220.      */
  221.     public void setMaxValue(final double maxValue) {
  222.         final double previousMaxValue = this.maxValue;
  223.         this.maxValue = maxValue;
  224.         for (final ParameterObserver observer : observers) {
  225.             observer.maxValueChanged(previousMaxValue, this);
  226.         }
  227.         // Check if current value is not out of min/max range
  228.         setValue(value);
  229.     }

  230.     /** Get scale.
  231.      * @return scale
  232.      */
  233.     public double getScale() {
  234.         return scale;
  235.     }

  236.     /** Set scale.
  237.      * @since 9.3
  238.      * @param scale the scale to set.
  239.      */
  240.     public void setScale(final double scale) {
  241.         final double previousScale = this.scale;
  242.         this.scale = scale;
  243.         for (final ParameterObserver observer : observers) {
  244.             observer.scaleChanged(previousScale, this);
  245.         }
  246.     }

  247.     /** Get normalized value.
  248.      * <p>
  249.      * The normalized value is a non-dimensional value
  250.      * suitable for use as part of a vector in an optimization
  251.      * process. It is computed as {@code (current - reference)/scale}.
  252.      * </p>
  253.      * @return normalized value
  254.      */
  255.     public double getNormalizedValue() {
  256.         return (value - referenceValue) / scale;
  257.     }

  258.     /** Set normalized value.
  259.      * <p>
  260.      * The normalized value is a non-dimensional value
  261.      * suitable for use as part of a vector in an optimization
  262.      * process. It is computed as {@code (current - reference)/scale}.
  263.      * </p>
  264.      * @param normalized value
  265.      */
  266.     public void setNormalizedValue(final double normalized) {
  267.         setValue(referenceValue + scale * normalized);
  268.     }

  269.     /** Get current reference date.
  270.      * @return current reference date (null if it was never set)
  271.      * @since 9.0
  272.      */
  273.     public AbsoluteDate getReferenceDate() {
  274.         return referenceDate;
  275.     }

  276.     /** Set reference date.
  277.      * @param newReferenceDate new reference date
  278.      * @since 9.0
  279.      */
  280.     public void setReferenceDate(final AbsoluteDate newReferenceDate) {
  281.         final AbsoluteDate previousReferenceDate = getReferenceDate();
  282.         referenceDate = newReferenceDate;
  283.         for (final ParameterObserver observer : observers) {
  284.             observer.referenceDateChanged(previousReferenceDate, this);
  285.         }
  286.     }

  287.     /** Get current parameter value.
  288.      * @return current parameter value
  289.      */
  290.     public double getValue() {
  291.         return value;
  292.     }

  293.     /** Get the value as a derivative structure.
  294.      * @param factory factory for the derivatives
  295.      * @param indices indices of the differentiation parameters in derivatives computations
  296.      * @return value with derivatives
  297.      * @since 9.3
  298.      * @deprecated as of 10.2, replaced by {@link #getValue(int, Map)}
  299.      */
  300.     @Deprecated
  301.     public DerivativeStructure getValue(final DSFactory factory, final Map<String, Integer> indices) {
  302.         final Integer index = indices.get(name);
  303.         return (index == null) ? factory.constant(value) : factory.variable(index, value);
  304.     }

  305.     /** Get the value as a gradient.
  306.      * @param freeParameters total number of free parameters in the gradient
  307.      * @param indices indices of the differentiation parameters in derivatives computations
  308.      * @return value with derivatives
  309.      * @since 10.2
  310.      */
  311.     public Gradient getValue(final int freeParameters, final Map<String, Integer> indices) {
  312.         final Integer index = indices.get(name);
  313.         return (index == null) ? Gradient.constant(freeParameters, value) : Gradient.variable(freeParameters, index, value);
  314.     }

  315.     /** Set parameter value.
  316.      * <p>
  317.      * If {@code newValue} is below {@link #getMinValue()}, it will
  318.      * be silently set to {@link #getMinValue()}. If {@code newValue} is
  319.      * above {@link #getMaxValue()}, it will be silently set to {@link
  320.      * #getMaxValue()}.
  321.      * </p>
  322.      * @param newValue new value
  323.      */
  324.     public void setValue(final double newValue) {
  325.         final double previousValue = getValue();
  326.         value = FastMath.max(minValue, FastMath.min(maxValue, newValue));
  327.         for (final ParameterObserver observer : observers) {
  328.             observer.valueChanged(previousValue, this);
  329.         }
  330.     }

  331.     /** Configure a parameter selection status.
  332.      * <p>
  333.      * Selection is used for estimated parameters in orbit determination,
  334.      * or to compute the Jacobian matrix in partial derivatives computation.
  335.      * </p>
  336.      * @param selected if true the parameter is selected,
  337.      * otherwise it will be fixed
  338.      */
  339.     public void setSelected(final boolean selected) {
  340.         final boolean previousSelection = isSelected();
  341.         this.selected = selected;
  342.         for (final ParameterObserver observer : observers) {
  343.             observer.selectionChanged(previousSelection, this);
  344.         }
  345.     }

  346.     /** Check if parameter is selected.
  347.      * <p>
  348.      * Selection is used for estimated parameters in orbit determination,
  349.      * or to compute the Jacobian matrix in partial derivatives computation.
  350.      * </p>
  351.      * @return true if parameter is selected, false if it is not
  352.      */
  353.     public boolean isSelected() {
  354.         return selected;
  355.     }

  356.     /** Get a text representation of the parameter.
  357.      * @return text representation of the parameter, in the form name = value.
  358.      */
  359.     public String toString() {
  360.         return name + " = " + value;
  361.     }

  362. }