Skip to content

Commit

Permalink
Feature complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Zabuzard committed Nov 3, 2019
1 parent e2cb0a6 commit 2be81a1
Show file tree
Hide file tree
Showing 43 changed files with 1,952 additions and 367 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package de.zabuza.maglev.external.algorithms;

import de.zabuza.maglev.external.graph.Edge;

import java.util.OptionalDouble;

/**
* Interface for Dijkstra modules used by a {@code ModuleDijkstra}. Defines various methods that allow to manipulate how
* the base Dijkstra works, like providing edge costs different to {@link Edge#getCost()}.
*
* @param <N> Type of the nodes
* @param <E> Type of the edges
*
* @author Daniel Tischner {@literal <[email protected]>}
*/
public interface DijkstraModule<N, E extends Edge<N>> {
/**
* Whether or not the given edge should be considered for relaxation. The algorithm will ignore the edge and not
* follow it if this method returns {@code false}.
*
* @param edge The edge in question
* @param pathDestination The destination of the shortest path computation or {@code null} if not present
*
* @return {@code True} if the edge should be considered, {@code false} otherwise
*/
@SuppressWarnings({ "SameReturnValue", "MethodReturnAlwaysConstant", "BooleanMethodNameMustStartWithQuestion" })
default boolean doConsiderEdgeForRelaxation(@SuppressWarnings("unused") final E edge,
@SuppressWarnings("unused") final N pathDestination) {
return true;
}

/**
* Gets an estimate about the shortest path distance from the given node to the destination of the shortest path
* computation.<br>
* <br>
* The estimate must be <i>monotone</i> and <i>admissible</i>.
*
* @param node The node to estimate the distance from
* @param pathDestination The destination to estimate the distance to
*
* @return An estimate about the shortest path distance or empty if the module has no estimate
*/
default OptionalDouble getEstimatedDistance(@SuppressWarnings("unused") final N node,
@SuppressWarnings("unused") final N pathDestination) {
return OptionalDouble.empty();
}

/**
* Provides the cost of a given edge.<br>
* <br>
* The base is the result of {@link Edge#getCost()}. Implementations are allowed to override this method in order to
* modify the cost.
*
* @param edge The edge whose cost to provide
* @param tentativeDistance The current tentative distance when relaxing this edge
*
* @return The cost of the given edge or empty if the module does not provide a cost
*/
default OptionalDouble provideEdgeCost(@SuppressWarnings("unused") final E edge,
@SuppressWarnings("unused") final double tentativeDistance) {
return OptionalDouble.empty();
}

/**
* Whether or not the algorithm should abort computation of the shortest path. The method is called right after the
* given node has been settled.
*
* @param tentativeDistance The tentative distance wrapper of the node that was settled
*
* @return {@code True} if the computation should be aborted, {@code false} if not
*/
default boolean shouldAbort(@SuppressWarnings("unused") final TentativeDistance<N, E> tentativeDistance) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.zabuza.maglev.external.algorithms.shortestpath;
package de.zabuza.maglev.external.algorithms;

import de.zabuza.maglev.external.model.Edge;
import de.zabuza.maglev.external.graph.Edge;

import java.util.Objects;

/**
* POJO class that wraps an edge and cost. Can be used to group a different cost
* than the default cost provided by the edge with the edge.
* Class that wraps an edge and cost. Can be used to group a different cost than the default cost provided by the edge
* with the edge.
*
* @param <N> The type of the node
* @param <E> The type of the edge
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.zabuza.maglev.external.algorithms.shortestpath;
package de.zabuza.maglev.external.algorithms;

/**
* Interface for classes that provide path costs.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.zabuza.maglev.external.algorithms;

import java.util.Collection;

/**
* Interface for classes that provide landmarks. Landmark are special nodes on a graph that can be used for distance
* approximation. A good landmark should be a node that is important in the graph, i.e. is used for many shortest paths
* between nodes.
*
* @param <E> The type of nodes and landmarks
*
* @author Daniel Tischner {@literal <[email protected]>}
*/
@FunctionalInterface
public interface LandmarkProvider<E> {
/**
* Provides the given amount of landmarks. Landmark selection might take a while depending on the size of the graph
* and the amount to generate.
*
* @param amount The amount of landmarks to provide
*
* @return A collection consisting of the given amount of landmarks. If the given amount is more than the underlying
* resource offers, the method should get all the resource offers.
*/
Collection<E> getLandmarks(int amount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.zabuza.maglev.external.algorithms;

/**
* Interface for a metric defined on a given type of objects.
*
* @param <E> The type of objects the metric operates on
*
* @author Daniel Tischner {@literal <[email protected]>}
*/
@FunctionalInterface
public interface Metric<E> {
/**
* Computes the distance between the two given objects accordingly to the implementing metric.
* <p>
* The distance must satisfy the following properties:
* <ul>
* <li><b>non-negativity</b> - must be greater equals {@code 0}</li>
* <li><b>identity-of-indiscernibles</b> - if the distance is {@code 0},
* the elements must be equal, according to their {@link E#equals(Object)}</li>
* <li><b>symmetry</b> - {@code distance(a, b)} must be equals to {@code distance(b, a)}</li>
* <li><b>triangle inequality</b> - {@code distance(a, c)} must be less equals
* {@code distance(x, b) + distance(b, c)}</li>
* </ul>
*
* @param first The first object
* @param second The second object
*
* @return The distance between the two given objects
*/
double distance(E first, E second);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package de.zabuza.maglev.external.algorithms.shortestpath;
package de.zabuza.maglev.external.algorithms;

import de.zabuza.maglev.external.model.Edge;
import de.zabuza.maglev.external.graph.Edge;

/**
* Interface for a path that consists of a source and destination node and edges
* which lead to the destination.
* Interface for a path that consists of a source and destination node and edges which lead to the destination.
*
* @param <N> Type of the node
* @param <E> Type of the edge
Expand All @@ -27,8 +26,7 @@ public interface Path<N, E extends Edge<N>> extends Iterable<EdgeCost<N, E>> {
N getSource();

/**
* Gets the total cost of the path. That is the sum of all edge costs the path
* consists of.
* Gets the total cost of the path. That is the sum of all edge costs the path consists of.
*
* @return The total cost of the path, not negative
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package de.zabuza.maglev.external.algorithms.shortestpath;
package de.zabuza.maglev.external.algorithms;

import de.zabuza.maglev.external.model.Edge;
import de.zabuza.maglev.external.graph.Edge;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
* Interface for algorithms that are able to compute shortest paths from a
* source to a destination.
* Interface for algorithms that are able to compute shortest paths from a source to a destination.
* <p>
* Use {@link ShortestPathComputationBuilder} for convenient construction of instances.
*
* @param <N> Type of node
* @param <E> Type of edge
Expand All @@ -17,47 +18,42 @@
*/
public interface ShortestPathComputation<N, E extends Edge<N>> {
/**
* Computes and returns a collection of all nodes that were visited by the
* algorithm while computing the shortest path from the given sources to the
* given destination. This is known as <i>search space</i> and is primarily
* used for debugging and benchmarking.<br>
* Computes and returns a collection of all nodes that were visited by the algorithm while computing the shortest
* path from the given sources to the given destination. This is known as <i>search space</i> and is primarily used
* for debugging and benchmarking.<br>
* <br>
* The shortest path from multiple sources is the minimal shortest path for
* all source nodes individually.
* The shortest path from multiple sources is the minimal shortest path for all source nodes individually.
*
* @param sources The sources to compute the shortest path from
* @param destination The destination to compute the shortest path to
*
* @return A collection of all visited nodes, the <i>search space</i>
*/
Collection<N> computeSearchSpace(Collection<N> sources, N destination);
Collection<N> searchSpace(Collection<? extends N> sources, N destination);

/**
* Computes and returns a collection of all nodes that were visited by the
* algorithm while computing the shortest path from the given source to the
* given destination. This is known as <i>search space</i> and is primarily
* used for debugging and benchmarking.
* Computes and returns a collection of all nodes that were visited by the algorithm while computing the shortest
* path from the given source to the given destination. This is known as <i>search space</i> and is primarily used
* for debugging and benchmarking.
*
* @param source The source to compute the shortest path from
* @param destination The destination to compute the shortest path to
*
* @return A collection of all visited nodes, the <i>search space</i>
*/
Collection<N> computeSearchSpace(N source, N destination);
Collection<N> searchSpace(N source, N destination);

/**
* Computes the shortest path from the given sources to the given
* destination.<br>
* Computes the shortest path from the given sources to the given destination.<br>
* <br>
* The shortest path from multiple sources is the minimal shortest path for
* all source nodes individually.
* The shortest path from multiple sources is the minimal shortest path for all source nodes individually.
*
* @param sources The sources to compute the shortest path from
* @param destination The destination to compute the shortest path to
*
* @return The shortest path if present, else empty
*/
Optional<Path<N, E>> computeShortestPath(Collection<N> sources, N destination);
Optional<Path<N, E>> shortestPath(Collection<? extends N> sources, N destination);

/**
* Computes the shortest path from the given source to the given destination.
Expand All @@ -67,55 +63,47 @@ public interface ShortestPathComputation<N, E extends Edge<N>> {
*
* @return The shortest path if present, else empty
*/
Optional<Path<N, E>> computeShortestPath(N source, N destination);
Optional<Path<N, E>> shortestPath(N source, N destination);

/**
* Computes the cost of the shortest path from the given sources to the given
* destination.<br>
* Computes the cost of the shortest path from the given sources to the given destination.<br>
* <br>
* The shortest path from multiple sources is the minimal shortest path for
* all source nodes individually.
* The shortest path from multiple sources is the minimal shortest path for all source nodes individually.
*
* @param sources The sources to compute the shortest path from
* @param destination The destination to compute the shortest path to
*
* @return The cost of the shortest path if present, else empty
*/
Optional<Double> computeShortestPathCost(Collection<N> sources, N destination);
Optional<Double> shortestPathCost(Collection<? extends N> sources, N destination);

/**
* Computes the cost of the shortest path from the given source to the given
* destination.
* Computes the cost of the shortest path from the given source to the given destination.
*
* @param source The source to compute the shortest path from
* @param destination The destination to compute the shortest path to
*
* @return The cost of the shortest path if present, else empty
*/
Optional<Double> computeShortestPathCost(N source, N destination);
Optional<Double> shortestPathCost(N source, N destination);

/**
* Computes the costs of all shortest paths from the given sources to all
* other nodes.<br>
* Computes the costs of all shortest paths from the given sources to all other nodes.<br>
* <br>
* The shortest path from multiple sources is the minimal shortest path for
* all source nodes individually.
* The shortest path from multiple sources is the minimal shortest path for all source nodes individually.
*
* @param sources The sources to compute the shortest path from
*
* @return A map which connects destination nodes to the costs of their
* shortest path
* @return A map which connects destination nodes to the costs of their shortest path
*/
Map<N, ? extends HasPathCost> computeShortestPathCostsReachable(Collection<N> sources);
Map<N, ? extends HasPathCost> shortestPathCostsReachable(Collection<? extends N> sources);

/**
* Computes the costs of all shortest paths from the given source to all other
* nodes.
* Computes the costs of all shortest paths from the given source to all other nodes.
*
* @param source The source to compute the shortest path from
*
* @return A map which connects destination nodes to the costs of their
* shortest path
* @return A map which connects destination nodes to the costs of their shortest path
*/
Map<N, ? extends HasPathCost> computeShortestPathCostsReachable(N source);
Map<N, ? extends HasPathCost> shortestPathCostsReachable(N source);
}
Loading

0 comments on commit 2be81a1

Please sign in to comment.