InterSensorsOptimizationProblemBuilder.java

  1. /* Copyright 2013-2025 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()) {
  117.                     break;
  118.                 }

  119.                 // Get LOS distance
  120.                 final Double losDistance  = reference.getLosDistance(i);

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

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

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

  133.     /** {@inheritDoc} */
  134.     @Override
  135.     protected MultivariateJacobianFunction createFunction() {

  136.         // model function
  137.         final MultivariateJacobianFunction model = point -> {

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

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

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

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

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

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

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

  160.                     final SensorPixel spA = mapping.getKey();
  161.                     final SensorPixel spB = mapping.getValue();

  162.                     final LineSensor lineSensorB = ruggedB.getLineSensor(reference.getSensorNameB());
  163.                     final LineSensor lineSensorA = ruggedA.getLineSensor(reference.getSensorNameA());

  164.                     final AbsoluteDate dateA = lineSensorA.getDate(spA.getLineNumber());
  165.                     final AbsoluteDate dateB = lineSensorB.getDate(spB.getLineNumber());

  166.                     final double pixelA = spA.getPixelNumber();
  167.                     final double pixelB = spB.getPixelNumber();

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

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

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

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

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

  186.                     l += 2; // pass to the next evaluation
  187.                 }
  188.             }

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

  192.         return model;
  193.     }


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

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