preparing the outer/inner tree structure for the MST approach

Change-Id: If0a90cb006ffd3db223bc803383513239ba3c808
This commit is contained in:
Axel Uhl committed 2019-07-14 00:02:48 +02:00
1 parent bdfe4aaa4d
commit e6f2180219
5 files changed
+97 -7

No files matched your search

@@ -10,6 +10,6 @@ package com.sap.sailing.windestimation.aggregator.msthmm.graph;
*
* @param <T>
*/
public interface BestElementPicker<T extends ElementWithQuality> {
Iterable<T> getBestElements(Tree<GroupOutOfWhichToPickTheBestElement<T>> tree);
public interface BestElementPicker<T extends ElementWithQuality, G extends GroupOutOfWhichToPickTheBestElement<T, G>> {
Iterable<T> getBestElements(Tree<GroupOutOfWhichToPickTheBestElement<T, G>> tree);
}
@@ -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.<p>
*
* 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 <T>
*/
public interface GroupOutOfWhichToPickTheBestElement<T extends ElementWithQuality> extends TreeNode<GroupOutOfWhichToPickTheBestElement<T>> {
public interface GroupOutOfWhichToPickTheBestElement<T extends ElementWithQuality, G extends GroupOutOfWhichToPickTheBestElement<T, G>>
extends TreeNode<G> {
/**
* 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
@@ -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 <T>
* @param <G>
*/
public class InnerGraphSuccessorSupplier<T extends ElementWithQuality, G extends GroupOutOfWhichToPickTheBestElement<T, G>>
implements Function<T, Iterable<T>> {
private final Map<T, Set<T>> successors;
private final T artificialRoot;
private final Map<G, T> artificialLeaves;
public InnerGraphSuccessorSupplier(Tree<G> 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<T> 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<T> allInnerChildNodes = new HashSet<>();
for (final GroupOutOfWhichToPickTheBestElement<T, G> 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<T> apply(T t) {
return successors.get(t);
}
}
@@ -7,6 +7,6 @@ package com.sap.sailing.windestimation.aggregator.msthmm.graph;
*
* @param <TN>
*/
public interface Tree<TN extends TreeNode<TN>> {
TreeNode<TN> getRoot();
public interface Tree<TN extends TreeNode<? extends TN>> {
TN getRoot();
}
@@ -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 <em>leaf</em> in the tree.
*
* @author Axel Uhl (D043530)
*