edu.kit.ifv.mobitopp.simulation.publictransport.DijkstraSearch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mobitopp Show documentation
Show all versions of mobitopp Show documentation
mobiTopp (http://mobitopp.ifv.kit.edu/) is an agent-based travel demand model developed at the Institute for transport studies at the Karlsruhe Institute of Technology (http://www.ifv.kit.edu/english/index.php). Publications about mobiTopp can be found on the project site (http://mobitopp.ifv.kit.edu/28.php).
The newest version!
package edu.kit.ifv.mobitopp.simulation.publictransport;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import edu.kit.ifv.mobitopp.routing.GraphFromVisumNetwork;
import edu.kit.ifv.mobitopp.routing.Link;
import edu.kit.ifv.mobitopp.routing.Node;
import edu.kit.ifv.mobitopp.routing.NodeFromVisumNode;
import edu.kit.ifv.mobitopp.routing.Path;
import edu.kit.ifv.mobitopp.routing.TimeAwareForwardDijkstra;
import edu.kit.ifv.mobitopp.routing.TravelTime;
import edu.kit.ifv.mobitopp.routing.TravelTimeFromGraph;
import edu.kit.ifv.mobitopp.routing.VisumLinkFactory;
import edu.kit.ifv.mobitopp.routing.util.SimplePQ;
import edu.kit.ifv.mobitopp.simulation.Location;
import edu.kit.ifv.mobitopp.time.RelativeTime;
import edu.kit.ifv.mobitopp.visum.VisumConnector;
import edu.kit.ifv.mobitopp.visum.VisumNetwork;
import edu.kit.ifv.mobitopp.visum.VisumOrientedLink;
import edu.kit.ifv.mobitopp.visum.VisumTransportSystem;
import edu.kit.ifv.mobitopp.visum.VisumTransportSystemSet;
class DijkstraSearch implements ShortestPathSearch {
private static final float startTime = 0.0f;
private static final Optional unlimited = Optional.empty();
private static final int allTargets = Integer.MAX_VALUE;
private final GraphFromVisumNetwork graph;
private final Optional maximumDuration;
private final int targetsToResolve;
private DijkstraSearch(GraphFromVisumNetwork graph, Optional maximumDuration) {
this(graph, maximumDuration, allTargets);
}
private DijkstraSearch(
GraphFromVisumNetwork graph, Optional maximumDuration, int targetsToResolve) {
super();
this.graph = graph;
this.maximumDuration = maximumDuration;
this.targetsToResolve = targetsToResolve;
}
public static ShortestPathSearch from(
VisumNetwork network, VisumLinkFactory linkFactory, List walking,
int targetsToResolve, Collection targets) {
GraphFromVisumNetwork graph = graphFor(network, linkFactory, walking,
stationNodesFrom(targets));
return new DijkstraSearch(graph, unlimited, targetsToResolve);
}
public static ShortestPathSearch from(
VisumNetwork network, VisumLinkFactory walkLinkFactory, RelativeTime maximumDuration,
List walking, int targetsToResolve, Collection targets) {
GraphFromVisumNetwork graph = graphFor(network, walkLinkFactory, walking, stationNodesFrom(targets));
Optional duration = Optional.of(maximumDuration);
return new DijkstraSearch(graph, duration, targetsToResolve);
}
private static VisumNodeFactory stationNodesFrom(Collection targets) {
return node -> new NodeFromVisumNode(node, targets.contains(node));
}
static ShortestPathSearch from(
VisumNetwork network, VisumLinkFactory linkFactory, List walking) {
GraphFromVisumNetwork graph = graphFor(network, linkFactory, walking, visumNode());
return unlimited(graph);
}
private static ShortestPathSearch unlimited(GraphFromVisumNetwork graph) {
return new DijkstraSearch(graph, unlimited);
}
private static GraphFromVisumNetwork graphFor(
VisumNetwork network, VisumLinkFactory linkFactory, List walking,
VisumNodeFactory nodeFactory) {
return new GraphFromVisumNetwork(network, walkLinkValidation(walking),
walkConnectorValidation(walking), linkFactory, nodeFactory);
}
static ShortestPathSearch from(
VisumNetwork network, VisumLinkFactory linkFactory, RelativeTime maximumDuration,
List walking) {
GraphFromVisumNetwork graph = graphFor(network, linkFactory, walking, visumNode());
Optional duration = Optional.of(maximumDuration);
return new DijkstraSearch(graph, duration);
}
private static VisumNodeFactory visumNode() {
return NodeFromVisumNode::new;
}
private static Function walkLinkValidation(
List walking) {
return link -> containsAtLeastOneOf(walking, link.transportSystems);
}
private static boolean containsAtLeastOneOf(
List walking, VisumTransportSystemSet transportSystems) {
for (VisumTransportSystem transportSystem : walking) {
if (transportSystems.contains(transportSystem)) {
return true;
}
}
return false;
}
private static Function walkConnectorValidation(
List walking) {
return connector -> containsAtLeastOneOf(walking, connector.transportSystems);
}
@Override
public ShortestPathsToStations search(Location location, Collection toTargets) {
Link link = graph.linkFor(location.roadAccessEdgeId);
if (link == null) {
return new CrowFlyDistance(location, targetsToResolve);
}
Node start = link.from();
Map search = searchToTargets(start);
return map(search, toTargets);
}
private Map searchToTargets(Node start) {
return dijkstra().shortestPathToTargets(graph, travelTime(), start, startTime,
targetsToResolve);
}
private Map searchFrom(edu.kit.ifv.mobitopp.network.Node from) {
if (from == null) {
return Collections.emptyMap();
}
Node fromStart = nodeFor(from);
if (fromStart == null) {
return Collections.emptyMap();
}
return search(fromStart);
}
@Override
public ShortestPathsToStations search(edu.kit.ifv.mobitopp.network.Node start, Collection toTargets) {
Map searchInternal = searchFrom(start);
return map(searchInternal, toTargets);
}
private ShortestPathsToStations map(
Map searchInternal, Collection targets) {
HashMap paths = new HashMap<>();
for (edu.kit.ifv.mobitopp.network.Node visumNode : targets) {
Node node = nodeFor(visumNode);
if (node == null || !searchInternal.containsKey(node)) {
continue;
}
Path path = searchInternal.get(node);
paths.put(visumNode, path);
}
return new DijkstraResult(paths);
}
private Node nodeFor(edu.kit.ifv.mobitopp.network.Node visumNode) {
return graph.nodeFor(visumNode.id());
}
private Map search(Node start) {
return dijkstra().shortestPathToAll(graph, travelTime(), start, startTime);
}
private TravelTime travelTime() {
return new TravelTimeFromGraph(graph);
}
private TimeAwareForwardDijkstra dijkstra() {
float latestArrival = startTime + maximumDurationInMinutes();
return new TimeAwareForwardDijkstra(new SimplePQ<>(), latestArrival);
}
private float maximumDurationInMinutes() {
return maximumDuration.map(RelativeTime::toMinutes).orElse(Integer.MAX_VALUE);
}
}