InterSensorsOptimizationProblemBuilder.java

  1. /* Copyright 2013-2022 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.rugged.adjustment;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.LinkedHashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.hipparchus.analysis.differentiation.Gradient;
  26. import org.hipparchus.linear.Array2DRowRealMatrix;
  27. import org.hipparchus.linear.ArrayRealVector;
  28. import org.hipparchus.linear.RealMatrix;
  29. import org.hipparchus.linear.RealVector;
  30. import org.hipparchus.optim.ConvergenceChecker;
  31. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresBuilder;
  32. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem;
  33. import org.hipparchus.optim.nonlinear.vector.leastsquares.MultivariateJacobianFunction;
  34. import org.hipparchus.optim.nonlinear.vector.leastsquares.ParameterValidator;
  35. import org.hipparchus.util.Pair;
  36. import org.orekit.rugged.adjustment.measurements.Observables;
  37. import org.orekit.rugged.adjustment.measurements.SensorToSensorMapping;
  38. import org.orekit.rugged.api.Rugged;
  39. import org.orekit.rugged.errors.RuggedException;
  40. import org.orekit.rugged.errors.RuggedMessages;
  41. import org.orekit.rugged.linesensor.LineSensor;
  42. import org.orekit.rugged.linesensor.SensorPixel;
  43. import org.orekit.rugged.utils.SpacecraftToObservedBody;
  44. import org.orekit.time.AbsoluteDate;
  45. import org.orekit.utils.ParameterDriver;

  46. /** Constructs the optimization problem for a list of tie points.
  47.  * @author Guylaine Prat
  48.  * @author Lucie Labat Allee
  49.  * @author Jonathan Guinet
  50.  * @author Luc Maisonobe
  51.  * @since 2.0
  52.  */
  53. public class InterSensorsOptimizationProblemBuilder extends OptimizationProblemBuilder {

  54.     /** Key for target. */
  55.     private static final String TARGET = "Target";

  56.     /** Key for weight. */
  57.     private static final String WEIGHT = "Weight";

  58.     /** List of rugged instance to refine.*/
  59.     private Map<String, Rugged> ruggedMap;

  60.     /** Sensor to ground mapping to generate target tab for optimization.*/
  61.     private List<SensorToSensorMapping> sensorToSensorMappings;

  62.     /** Targets and weights of optimization problem. */
  63.     private HashMap<String, double[] > targetAndWeight;

  64.     /** Constructor.
  65.      * @param sensors list of sensors to refine
  66.      * @param measurements set of observables
  67.      * @param ruggedList names of rugged to refine
  68.      */
  69.     public InterSensorsOptimizationProblemBuilder(final List<LineSensor> sensors,
  70.                                                   final Observables measurements, final Collection<Rugged> ruggedList) {

  71.         super(sensors, measurements);
  72.         this.ruggedMap = new LinkedHashMap<String, Rugged>();
  73.         for (final Rugged rugged : ruggedList) {
  74.             this.ruggedMap.put(rugged.getName(), rugged);
  75.         }
  76.         this.initMapping();
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     protected void initMapping() {

  81.         this.sensorToSensorMappings = new ArrayList<>();

  82.         for (final String ruggedNameA : this.ruggedMap.keySet()) {
  83.             for (final String ruggedNameB : this.ruggedMap.keySet()) {

  84.                 for (final LineSensor sensorA : this.getSensors()) {
  85.                     for (final LineSensor sensorB : this.getSensors()) {

  86.                         final String sensorNameA = sensorA.getName();
  87.                         final String sensorNameB = sensorB.getName();
  88.                         final SensorToSensorMapping mapping = this.getMeasurements().getInterMapping(ruggedNameA, sensorNameA, ruggedNameB, sensorNameB);

  89.                         if (mapping != null) {
  90.                             this.sensorToSensorMappings.add(mapping);
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     protected void createTargetAndWeight() {

  100.         int n = 0;
  101.         for (final SensorToSensorMapping reference : this.sensorToSensorMappings) {
  102.             n += reference.getMapping().size();
  103.         }
  104.         if (n == 0) {
  105.             throw new RuggedException(RuggedMessages.NO_REFERENCE_MAPPINGS);
  106.         }

  107.         n = 2 * n;

  108.         final double[] target = new double[n];
  109.         final double[] weight = new double[n];

  110.         int k = 0;
  111.         for (final SensorToSensorMapping reference : this.sensorToSensorMappings) {

  112.             // Get central body constraint weight
  113.             final double bodyConstraintWeight = reference.getBodyConstraintWeight();

  114.             int i = 0;
  115.             for (Iterator<Map.Entry<SensorPixel, SensorPixel>> gtIt = reference.getMapping().iterator(); gtIt.hasNext(); i++) {

  116.                 if (i == reference.getMapping().size()) break;

  117.                 // Get LOS distance
  118.                 final Double losDistance  = reference.getLosDistance(i);

  119.                 weight[k] = 1.0 - bodyConstraintWeight;
  120.                 target[k++] = losDistance.doubleValue();

  121.                 // Get central body distance (constraint)
  122.                 final Double bodyDistance  = reference.getBodyDistance(i);
  123.                 weight[k] = bodyConstraintWeight;
  124.                 target[k++] = bodyDistance.doubleValue();
  125.             }
  126.         }

  127.         this.targetAndWeight = new HashMap<String, double[]>();
  128.         this.targetAndWeight.put(TARGET, target);
  129.         this.targetAndWeight.put(WEIGHT, weight);
  130.     }

  131.     /** {@inheritDoc} */
  132.     @Override
  133.     protected MultivariateJacobianFunction createFunction() {

  134.         // model function
  135.         final MultivariateJacobianFunction model = point -> {

  136.             // set the current parameters values
  137.             int i = 0;
  138.             for (final ParameterDriver driver : this.getDrivers()) {
  139.                 driver.setNormalizedValue(point.getEntry(i++));
  140.             }

  141.             final double[] target = this.targetAndWeight.get(TARGET);

  142.             // compute distance and its partial derivatives
  143.             final RealVector value = new ArrayRealVector(target.length);
  144.             final RealMatrix jacobian = new Array2DRowRealMatrix(target.length, this.getNbParams());

  145.             int l = 0;
  146.             for (final SensorToSensorMapping reference : this.sensorToSensorMappings) {

  147.                 final String ruggedNameA = reference.getRuggedNameA();
  148.                 final String ruggedNameB = reference.getRuggedNameB();
  149.                 final Rugged ruggedA = this.ruggedMap.get(ruggedNameA);
  150.                 if (ruggedA == null) {
  151.                     throw new RuggedException(RuggedMessages.INVALID_RUGGED_NAME);
  152.                 }

  153.                 final Rugged ruggedB = this.ruggedMap.get(ruggedNameB);
  154.                 if (ruggedB == null) {
  155.                     throw new RuggedException(RuggedMessages.INVALID_RUGGED_NAME);
  156.                 }

  157.                 for (final Map.Entry<SensorPixel, SensorPixel> mapping : reference.getMapping()) {

  158.                     final SensorPixel spA = mapping.getKey();
  159.                     final SensorPixel spB = mapping.getValue();

  160.                     final LineSensor lineSensorB = ruggedB.getLineSensor(reference.getSensorNameB());
  161.                     final LineSensor lineSensorA = ruggedA.getLineSensor(reference.getSensorNameA());

  162.                     final AbsoluteDate dateA = lineSensorA.getDate(spA.getLineNumber());
  163.                     final AbsoluteDate dateB = lineSensorB.getDate(spB.getLineNumber());

  164.                     final double pixelA = spA.getPixelNumber();
  165.                     final double pixelB = spB.getPixelNumber();

  166.                     final SpacecraftToObservedBody scToBodyA = ruggedA.getScToBody();

  167.                     final Gradient[] ilResult =
  168.                             ruggedB.distanceBetweenLOSderivatives(lineSensorA, dateA, pixelA, scToBodyA,
  169.                                     lineSensorB, dateB, pixelB, this.getGenerator());

  170.                     // extract the value
  171.                     value.setEntry(l, ilResult[0].getValue());
  172.                     value.setEntry(l + 1, ilResult[1].getValue());

  173.                     // extract the Jacobian
  174.                     final int[] orders = new int[this.getNbParams()];
  175.                     int m = 0;

  176.                     for (final ParameterDriver driver : this.getDrivers()) {
  177.                         final double scale = driver.getScale();
  178.                         orders[m] = 1;
  179.                         jacobian.setEntry(l, m, ilResult[0].getPartialDerivative(orders) * scale);
  180.                         jacobian.setEntry(l + 1, m, ilResult[1].getPartialDerivative(orders) * scale);
  181.                         orders[m] = 0;
  182.                         m++;
  183.                     }

  184.                     l += 2; // pass to the next evaluation
  185.                 }
  186.             }

  187.             // distance result with Jacobian for all reference points
  188.             return new Pair<RealVector, RealMatrix>(value, jacobian);
  189.         };

  190.         return model;
  191.     }


  192.     /** Least square problem builder.
  193.      * @param maxEvaluations maxIterations and evaluations
  194.      * @param convergenceThreshold parameter convergence threshold
  195.      * @return the least square problem
  196.      */
  197.     @Override
  198.     public final LeastSquaresProblem build(final int maxEvaluations, final double convergenceThreshold) {

  199.         this.createTargetAndWeight();
  200.         final double[] target = this.targetAndWeight.get(TARGET);
  201.         final double[] start = this.createStartTab();
  202.         final ParameterValidator validator = this.createParameterValidator();
  203.         final ConvergenceChecker<LeastSquaresProblem.Evaluation> checker = this.createChecker(convergenceThreshold);
  204.         final MultivariateJacobianFunction model = this.createFunction();
  205.         return new LeastSquaresBuilder()
  206.                         .lazyEvaluation(false).maxIterations(maxEvaluations)
  207.                         .maxEvaluations(maxEvaluations).weight(null).start(start)
  208.                         .target(target).parameterValidator(validator).checker(checker)
  209.                         .model(model).build();
  210.     }
  211. }