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.util.FastMath;
  26. import org.hipparchus.util.Precision;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.time.AbsoluteDate;


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

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

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

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

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

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

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

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

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

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

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


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

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

  139.     /** Replace an observer.
  140.      * @param oldObserver observer to replace
  141.      * @param newObserver new observer to use
  142.      * @since 10.1
  143.      */
  144.     public void replaceObserver(final ParameterObserver oldObserver, final ParameterObserver newObserver) {
  145.         for (int i = 0; i < observers.size(); ++i) {
  146.             if (observers.get(i) == oldObserver) {
  147.                 observers.set(i, newObserver);
  148.             }
  149.         }
  150.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  302.     /** Set parameter value.
  303.      * <p>
  304.      * If {@code newValue} is below {@link #getMinValue()}, it will
  305.      * be silently set to {@link #getMinValue()}. If {@code newValue} is
  306.      * above {@link #getMaxValue()}, it will be silently set to {@link
  307.      * #getMaxValue()}.
  308.      * </p>
  309.      * @param newValue new value
  310.      */
  311.     public void setValue(final double newValue) {
  312.         final double previousValue = getValue();
  313.         value = FastMath.max(minValue, FastMath.min(maxValue, newValue));
  314.         for (final ParameterObserver observer : observers) {
  315.             observer.valueChanged(previousValue, this);
  316.         }
  317.     }

  318.     /** Configure a parameter selection status.
  319.      * <p>
  320.      * Selection is used for estimated parameters in orbit determination,
  321.      * or to compute the Jacobian matrix in partial derivatives computation.
  322.      * </p>
  323.      * @param selected if true the parameter is selected,
  324.      * otherwise it will be fixed
  325.      */
  326.     public void setSelected(final boolean selected) {
  327.         final boolean previousSelection = isSelected();
  328.         this.selected = selected;
  329.         for (final ParameterObserver observer : observers) {
  330.             observer.selectionChanged(previousSelection, this);
  331.         }
  332.     }

  333.     /** Check if parameter is selected.
  334.      * <p>
  335.      * Selection is used for estimated parameters in orbit determination,
  336.      * or to compute the Jacobian matrix in partial derivatives computation.
  337.      * </p>
  338.      * @return true if parameter is selected, false if it is not
  339.      */
  340.     public boolean isSelected() {
  341.         return selected;
  342.     }

  343.     /** Get a text representation of the parameter.
  344.      * @return text representation of the parameter, in the form name = value.
  345.      */
  346.     public String toString() {
  347.         return name + " = " + value;
  348.     }

  349. }