edu.kit.ifv.mobitopp.routing.GraphFromVisumNetwork 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.routing;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import edu.kit.ifv.mobitopp.simulation.publictransport.VisumNodeFactory;
import edu.kit.ifv.mobitopp.visum.VisumConnector;
import edu.kit.ifv.mobitopp.visum.VisumLink;
import edu.kit.ifv.mobitopp.visum.VisumNode;
import edu.kit.ifv.mobitopp.visum.VisumOrientedLink;
import edu.kit.ifv.mobitopp.visum.VisumRoadNetwork;
import edu.kit.ifv.mobitopp.visum.VisumTransportSystem;
import edu.kit.ifv.mobitopp.visum.VisumZone;
public class GraphFromVisumNetwork implements Graph {
final protected Map nodes;
final public Map zones;
final public Map links;
final public List connectors;
protected Node[] allNodes;
public GraphFromVisumNetwork(VisumRoadNetwork visum) {
this(visum, mivLinkValidation(carSystemFrom(visum)),
mivConnectorValidation(carSystemFrom(visum)), LinkFromVisumLink::new,
NodeFromVisumNode::new, NodeFromVisumZone::new);
}
GraphFromVisumNetwork(VisumRoadNetwork visum, VisumZoneNodeFactory zoneFactory) {
this(visum, mivLinkValidation(carSystemFrom(visum)),
mivConnectorValidation(carSystemFrom(visum)), LinkFromVisumLink::new,
NodeFromVisumNode::new, zoneFactory);
}
public GraphFromVisumNetwork(
VisumRoadNetwork network, Function linkValidation,
Function connectorValidation, VisumLinkFactory linkFactory,
VisumNodeFactory nodeFactory) {
this(network, linkValidation, connectorValidation, linkFactory, nodeFactory,
NodeFromVisumZone::new);
}
protected static VisumTransportSystem carSystemFrom(VisumRoadNetwork visum) {
return visum.getTransportSystem("P");
}
public GraphFromVisumNetwork(
VisumRoadNetwork network, Function linkValidation,
Function connectorValidation, VisumLinkFactory linkFactory,
VisumNodeFactory nodeFactory, VisumZoneNodeFactory zoneFactory) {
Map tmpNodes = makeNodes(network.nodes, nodeFactory);
this.links = makeValidLinks(network.links.links, tmpNodes, linkValidation, linkFactory);
this.nodes = nodesFromLinks(this.links);
this.zones = makeZones(network.zones, zoneFactory);
this.connectors = makeValidConnectors(network.connectors, this.nodes, this.zones,
connectorValidation);
ArrayList tmp = new ArrayList();
tmp.addAll(nodes.values());
tmp.addAll(zones.values());
allNodes = tmp.toArray(new Node[0]);
}
private static Function mivLinkValidation(
VisumTransportSystem carSystem) {
return link -> link.transportSystems.contains(carSystem) && link.attributes.capacityCar > 0.0f;
}
static Function mivConnectorValidation(
VisumTransportSystem carSystem) {
return connector -> connector.transportSystems.contains(carSystem);
}
protected static Map makeNodes(Map nodes, VisumNodeFactory factory) {
HashMap data = new HashMap();
for (VisumNode node: nodes.values()) {
data.put(new Integer(node.id()),factory.create(node));
}
return data;
}
protected static Map nodesFromLinks(Map links) {
HashMap data = new HashMap();
for (Link link : links.values()) {
data.put(Integer.valueOf(link.from().id()),link.from());
data.put(Integer.valueOf(link.to().id()),link.to());
}
return data;
}
protected static Map makeZones(Map zones, VisumZoneNodeFactory zoneFactory) {
HashMap data = new HashMap();
for (VisumZone zone: zones.values()) {
data.put(zone.id, zoneFactory.create(zone));
}
return data;
}
protected static Map makeValidLinks(
Map links,
Map nodes,
Function linkValidation,
VisumLinkFactory linkFactory
) {
Map data = new HashMap<>();
Map> inLinks = new HashMap>();
Map> outLinks = new HashMap>();
for (VisumLink link: links.values()) {
VisumOrientedLink link1 = link.linkA;
VisumOrientedLink link2 = link.linkB;
if (linkValidation.apply(link1)) {
Link mivLink = makeValidLink(link1, nodes, inLinks, outLinks, linkFactory);
data.put(mivLink.id(), mivLink);
}
if (linkValidation.apply(link2)) {
Link mivLink = makeValidLink(link2, nodes, inLinks, outLinks, linkFactory);
data.put(mivLink.id(), mivLink);
}
}
for (NodeFromVisumNode to : inLinks.keySet()) {
Collection edges = inLinks.get(to);
to.setIncomingLinks(edges);
}
for (NodeFromVisumNode from : outLinks.keySet()) {
Collection edges = outLinks.get(from);
from.setOutgoingLinks(edges);
}
return data;
}
protected static List makeValidConnectors(
Map> connectors,
Map nodes,
Map zones,
Function connectorValidation
) {
ArrayList data = new ArrayList();
Map> inLinks = new HashMap>();
Map> outLinks = new HashMap>();
for (List connectorsByZone: connectors.values()) {
for (VisumConnector connector : connectorsByZone) {
if (connectorValidation.apply(connector)) {
NodeFromVisumNode node = (NodeFromVisumNode) nodes.get(connector.node.id());
NodeFromVisumZone zone = (NodeFromVisumZone) zones.get(connector.zone.id);
if (node != null) {
if (!inLinks.containsKey(node)) {
inLinks.put(node, new ArrayList());
}
if (!outLinks.containsKey(node)) {
outLinks.put(node, new ArrayList());
}
if (!inLinks.containsKey(zone)) {
inLinks.put(zone, new ArrayList());
}
if (!outLinks.containsKey(zone)) {
outLinks.put(zone, new ArrayList());
}
if (connector.direction == VisumConnector.Direction.ORIGIN) {
Link link = new LinkFromVisumConnector(connector, zone, node);
data.add(link);
outLinks.get(zone).add(link);
inLinks.get(node).add(link); // Don't add connector links to street network nodes
} else {
Link link = new LinkFromVisumConnector(connector, node, zone);
data.add(link);
inLinks.get(zone).add(link);
outLinks.get(node).add(link); // Don't add connector links to street network nodes
}
}
}
}
}
for (DefaultNode to : inLinks.keySet()) {
Collection edges = inLinks.get(to);
edges.addAll(convertToLinkCollection(to.incomingEdges()));
to.setIncomingLinks(edges);
}
for (DefaultNode from : outLinks.keySet()) {
Collection edges = outLinks.get(from);
edges.addAll(convertToLinkCollection(from.outgoingEdges()));
from.setOutgoingLinks(edges);
}
return data;
}
protected static Collection convertToLinkCollection(Edge[] edges) {
return Arrays.asList(Arrays.asList(edges).toArray(new Link[0]));
}
protected static Link makeValidLink(
VisumOrientedLink link,
Map nodes,
Map> inLinks,
Map> outLinks,
VisumLinkFactory linkFactory
) {
NodeFromVisumNode from = nodes.get(link.from.id());
NodeFromVisumNode to = nodes.get(link.to.id());
Link edge = linkFactory.create(link, from, to);
if (!inLinks.containsKey(to)) {
inLinks.put(to, new ArrayList());
}
if (!outLinks.containsKey(from)) {
outLinks.put(from, new ArrayList());
}
inLinks.get(to).add(edge);
outLinks.get(from).add(edge);
return edge;
}
public Node getNodeForZone(int zoneId) {
return this.zones.get(zoneId);
}
public Node[] nodes() {
return allNodes;
}
public Edge[] outgoingEdges(Node node) {
assert node instanceof DefaultNode;
return node.outgoingEdges();
}
public Edge[] incomingEdges(Node node) {
assert node instanceof DefaultNode;
return node.incomingEdges();
}
public Node from(Edge edge)
{
assert edge instanceof DefaultLink;
return ((DefaultLink)edge).from();
}
public Node to(Edge edge)
{
assert edge instanceof DefaultLink;
return ((DefaultLink)edge).to();
}
public float cost(Edge edge) {
assert edge instanceof DefaultLink;
return ((DefaultLink)edge).travelTime();
}
public Map zones() {
return this.zones;
}
public List links() {
return new ArrayList<>(links.values());
}
public Node nodeFor(int id) {
return nodes.get(id);
}
public Link linkFor(Integer edgeId) {
if (edgeId == null) {
return null;
}
return links.get(Math.abs(edgeId) + ":1");
}
}