ParameterDriver.java

  1. /* Copyright 2002-2019 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 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.     /** Get the observers for this driver.
  140.      * @return an unmodifiable view of the observers for this driver
  141.      * @since 9.1
  142.      */
  143.     public List<ParameterObserver> getObservers() {
  144.         return Collections.unmodifiableList(observers);
  145.     }

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

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

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

  168.     /** Set reference parameter value.
  169.      * @since 9.3
  170.      * @param referenceValue the reference value to set.
  171.      */
  172.     public void setReferenceValue(final double referenceValue) {
  173.         final double previousReferenceValue = this.referenceValue;
  174.         this.referenceValue = referenceValue;
  175.         for (final ParameterObserver observer : observers) {
  176.             observer.referenceValueChanged(previousReferenceValue, this);
  177.         }
  178.     }

  179.     /** Get minimum parameter value.
  180.      * @return minimum parameter value
  181.      */
  182.     public double getMinValue() {
  183.         return minValue;
  184.     }

  185.     /** Set minimum parameter value.
  186.      * @since 9.3
  187.      * @param minValue the minimum value to set.
  188.      */
  189.     public void setMinValue(final double minValue) {
  190.         final double previousMinValue = this.minValue;
  191.         this.minValue = minValue;
  192.         for (final ParameterObserver observer : observers) {
  193.             observer.minValueChanged(previousMinValue, this);
  194.         }
  195.         // Check if current value is not out of min/max range
  196.         setValue(value);
  197.     }

  198.     /** Get maximum parameter value.
  199.      * @return maximum parameter value
  200.      */
  201.     public double getMaxValue() {
  202.         return maxValue;
  203.     }

  204.     /** Set maximum parameter value.
  205.      * @since 9.3
  206.      * @param maxValue the maximum value to set.
  207.      */
  208.     public void setMaxValue(final double maxValue) {
  209.         final double previousMaxValue = this.maxValue;
  210.         this.maxValue = maxValue;
  211.         for (final ParameterObserver observer : observers) {
  212.             observer.maxValueChanged(previousMaxValue, this);
  213.         }
  214.         // Check if current value is not out of min/max range
  215.         setValue(value);
  216.     }

  217.     /** Get scale.
  218.      * @return scale
  219.      */
  220.     public double getScale() {
  221.         return scale;
  222.     }

  223.     /** Set scale.
  224.      * @since 9.3
  225.      * @param scale the scale to set.
  226.      */
  227.     public void setScale(final double scale) {
  228.         final double previousScale = this.scale;
  229.         this.scale = scale;
  230.         for (final ParameterObserver observer : observers) {
  231.             observer.scaleChanged(previousScale, this);
  232.         }
  233.     }

  234.     /** Get normalized value.
  235.      * <p>
  236.      * The normalized value is a non-dimensional value
  237.      * suitable for use as part of a vector in an optimization
  238.      * process. It is computed as {@code (current - reference)/scale}.
  239.      * </p>
  240.      * @return normalized value
  241.      */
  242.     public double getNormalizedValue() {
  243.         return (value - referenceValue) / scale;
  244.     }

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

  256.     /** Get current reference date.
  257.      * @return current reference date (null if it was never set)
  258.      * @since 9.0
  259.      */
  260.     public AbsoluteDate getReferenceDate() {
  261.         return referenceDate;
  262.     }

  263.     /** Set reference date.
  264.      * @param newReferenceDate new reference date
  265.      * @since 9.0
  266.      */
  267.     public void setReferenceDate(final AbsoluteDate newReferenceDate) {
  268.         final AbsoluteDate previousReferenceDate = getReferenceDate();
  269.         referenceDate = newReferenceDate;
  270.         for (final ParameterObserver observer : observers) {
  271.             observer.referenceDateChanged(previousReferenceDate, this);
  272.         }
  273.     }

  274.     /** Get current parameter value.
  275.      * @return current parameter value
  276.      */
  277.     public double getValue() {
  278.         return value;
  279.     }

  280.     /** Get the value as a derivative structure.
  281.      * @param factory factory for the derivatives
  282.      * @param indices indices of the differentiation parameters in derivatives computations
  283.      * @return value with derivatives
  284.      * @since 9.3
  285.      */
  286.     public DerivativeStructure getValue(final DSFactory factory, final Map<String, Integer> indices) {
  287.         final Integer index = indices.get(name);
  288.         return (index == null) ? factory.constant(value) : factory.variable(index, value);
  289.     }

  290.     /** Set parameter value.
  291.      * <p>
  292.      * If {@code newValue} is below {@link #getMinValue()}, it will
  293.      * be silently set to {@link #getMinValue()}. If {@code newValue} is
  294.      * above {@link #getMaxValue()}, it will be silently set to {@link
  295.      * #getMaxValue()}.
  296.      * </p>
  297.      * @param newValue new value
  298.      */
  299.     public void setValue(final double newValue) {
  300.         final double previousValue = getValue();
  301.         value = FastMath.max(minValue, FastMath.min(maxValue, newValue));
  302.         for (final ParameterObserver observer : observers) {
  303.             observer.valueChanged(previousValue, this);
  304.         }
  305.     }

  306.     /** Configure a parameter selection status.
  307.      * <p>
  308.      * Selection is used for estimated parameters in orbit determination,
  309.      * or to compute the Jacobian matrix in partial derivatives computation.
  310.      * </p>
  311.      * @param selected if true the parameter is selected,
  312.      * otherwise it will be fixed
  313.      */
  314.     public void setSelected(final boolean selected) {
  315.         final boolean previousSelection = isSelected();
  316.         this.selected = selected;
  317.         for (final ParameterObserver observer : observers) {
  318.             observer.selectionChanged(previousSelection, this);
  319.         }
  320.     }

  321.     /** Check if parameter is selected.
  322.      * <p>
  323.      * Selection is used for estimated parameters in orbit determination,
  324.      * or to compute the Jacobian matrix in partial derivatives computation.
  325.      * </p>
  326.      * @return true if parameter is selected, false if it is not
  327.      */
  328.     public boolean isSelected() {
  329.         return selected;
  330.     }

  331.     /** Get a text representation of the parameter.
  332.      * @return text representation of the parameter, in the form name = value.
  333.      */
  334.     public String toString() {
  335.         return name + " = " + value;
  336.     }

  337. }