From e6f2180219cd398d9e8d1733ed0b77f706fb772d Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Sun, 14 Jul 2019 00:02:48 +0200 Subject: [PATCH] preparing the outer/inner tree structure for the MST approach Change-Id: If0a90cb006ffd3db223bc803383513239ba3c808 --- .../msthmm/graph/BestElementPicker.java | 4 +- .../GroupOutOfWhichToPickTheBestElement.java | 18 ++++- .../graph/InnerGraphSuccessorSupplier.java | 75 +++++++++++++++++++ .../aggregator/msthmm/graph/Tree.java | 4 +- .../aggregator/msthmm/graph/TreeNode.java | 3 +- 5 files changed, 97 insertions(+), 7 deletions(-) create mode 100755 java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/InnerGraphSuccessorSupplier.java diff --git a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/BestElementPicker.java b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/BestElementPicker.java index b80c47d690c..4b0e937f1ec 100755 --- a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/BestElementPicker.java +++ b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/BestElementPicker.java @@ -10,6 +10,6 @@ package com.sap.sailing.windestimation.aggregator.msthmm.graph; * * @param */ -public interface BestElementPicker { - Iterable getBestElements(Tree> tree); +public interface BestElementPicker> { + Iterable getBestElements(Tree> tree); } diff --git a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/GroupOutOfWhichToPickTheBestElement.java b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/GroupOutOfWhichToPickTheBestElement.java index fa16241b221..f5671c6916a 100755 --- a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/GroupOutOfWhichToPickTheBestElement.java +++ b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/GroupOutOfWhichToPickTheBestElement.java @@ -1,12 +1,26 @@ package com.sap.sailing.windestimation.aggregator.msthmm.graph; /** - * A special {@link TreeNode tree node} that + * A special {@link TreeNode tree node} that has a number of elements, each of which representing a node in a graph to + * which the {@link DijsktraShortestPathFinder} can be applied. This "inner graph" has a structure that aligns with the + * path from a leaf in the {@link Tree} up to the {@link Tree#getRoot() root} of that tree. It traverses the + * {@link Tree}'s levels. At each level (at each {@link TreeNode} along the path to the root node) there are a number of + * {@link #getElements() elements}. The inner graph has edges starting at each {@link #getElements() element} and + * pointing to each of the {@link #getParent() parent's} {@link #getElements() elements}. The {@link ElementAdjacencyQualityMetric} + * describes the quality (or "probability") of each such edge.

+ * + * The interesting spots in the overarching {@link Tree} are those objects of this type that have more than one + * {@link #getChildren() child}. For those, two "inner graphs" from a leaf to the root of the overarching {@link Tree} + * meet and then follow the same path up to the root. When it comes to picking the best element in such a group, optimizing + * along each path from the leaf to the root may come to different solutions for this node. Such nodes then need to be + * disambiguated in some way and will violate one of the two "inner graphs'" computed optimum. + * * @author Axel Uhl (D043530) * * @param */ -public interface GroupOutOfWhichToPickTheBestElement extends TreeNode> { +public interface GroupOutOfWhichToPickTheBestElement> +extends TreeNode { /** * Obtains the elements in this group. Exactly one of them has to be selected, based on the element's * {@link ElementWithQuality#getQuality() quality} as well as the edges connecting elements of this type with each diff --git a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/InnerGraphSuccessorSupplier.java b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/InnerGraphSuccessorSupplier.java new file mode 100755 index 00000000000..6ca5a77231f --- /dev/null +++ b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/InnerGraphSuccessorSupplier.java @@ -0,0 +1,75 @@ +package com.sap.sailing.windestimation.aggregator.msthmm.graph; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +import com.sap.sse.common.Util; + +/** + * A "successor supplier" constructing edges of a graph that lies within a {@link Tree}, ascending upwards from the + * {@link Tree}'s leaves to the {@link Tree}'s {@link Tree#getRoot() root} node. The tree is assumed to consist of nodes + * of some subtype of {@link GroupOutOfWhichToPickTheBestElement}, so each {@link TreeNode} has a number of + * {@link GroupOutOfWhichToPickTheBestElement#getElements() elements. Starting at an artificial start node constructed + * for each of the {@link Tree}'s leaves, an edge leads to each of the + * {@link GroupOutOfWhichToPickTheBestElement#getElements() elements} of the leaf node. From each of those, an edge + * leads to each of the {@link GroupOutOfWhichToPickTheBestElement#getElements() elements} of the + * {@link TreeNode#getParent() parent} node in the overarching tree, and so on, up to the {@link Tree}'s + * {@link Tree#getRoot() root}. All {@link GroupOutOfWhichToPickTheBestElement#getElements() elements} of the + * root node get an additional artifical edge to an artificial root node of the "inner graph." + * + * @author Axel Uhl (D043530) + * + * @param + * @param + */ +public class InnerGraphSuccessorSupplier> +implements Function> { + private final Map> successors; + private final T artificialRoot; + private final Map artificialLeaves; + + public InnerGraphSuccessorSupplier(Tree overarchingTree) { + successors = new HashMap<>(); + artificialRoot = null; // TODO generate an artificial root of type T + artificialLeaves = new HashMap<>(); + if (overarchingTree.getRoot() != null) { + // add the edges to the artificial root node + for (final T innerRootElement : overarchingTree.getRoot().getElements()) { + Util.addToValueSet(successors, innerRootElement, artificialRoot); + } + addAllEdges(overarchingTree.getRoot()); + } + } + + private void addAllEdges(G node) { + final Iterable innerChildElements; + if (Util.isEmpty(node.getChildren())) { + // leaf node; add an artificial leaf: + final T artificialLeaf = null; // TODO generate an artificial leaf of type T + artificialLeaves.put(node, artificialLeaf); + innerChildElements = Collections.singleton(artificialLeaf); + } else { + final Set allInnerChildNodes = new HashSet<>(); + for (final GroupOutOfWhichToPickTheBestElement child : node.getChildren()) { + for (final T innerChildElement : child.getElements()) { + allInnerChildNodes.add(innerChildElement); + } + } + innerChildElements = allInnerChildNodes; + } + for (final T innerChildElement : innerChildElements) { + for (final T innerParentElement : node.getElements()) { + Util.addToValueSet(successors, innerChildElement, innerParentElement); + } + } + } + + @Override + public Iterable apply(T t) { + return successors.get(t); + } +} diff --git a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/Tree.java b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/Tree.java index 3118ee90392..42d08f08377 100755 --- a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/Tree.java +++ b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/Tree.java @@ -7,6 +7,6 @@ package com.sap.sailing.windestimation.aggregator.msthmm.graph; * * @param */ -public interface Tree> { - TreeNode getRoot(); +public interface Tree> { + TN getRoot(); } diff --git a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/TreeNode.java b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/TreeNode.java index 1f3f6c581d3..3be032e0d27 100755 --- a/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/TreeNode.java +++ b/java/com.sap.sailing.windestimation/src/com/sap/sailing/windestimation/aggregator/msthmm/graph/TreeNode.java @@ -1,7 +1,8 @@ package com.sap.sailing.windestimation.aggregator.msthmm.graph; /** - * A node in a {@link Tree} + * A node in a {@link Tree}. If it has no (a {@code null}) {@link #getParent() parent}, it is the {@link Tree#getRoot() + * root} of the tree. If it has an empty set of {@link #getChildren() children}, it is called a leaf in the tree. * * @author Axel Uhl (D043530) *