diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java index 717142ff257dbe4a79f392f4d3aeafe890d08335..7aba38695f9f8479f0aa01f470f77e771f25638f 100644 --- a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java +++ b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java @@ -153,7 +153,6 @@ public abstract class Isochrone { * @param output the output in which the computed nodes and links are stored. * @return the computed output (stored in the output given by the parameter) */ - // TODO: Add some more tests for this method. If isochrones get too small (2points for 15min isochrone from FUB) nothing fails ;-( public <T extends AbstractOutput> T compute(final T output) { output.beforeCalculation(); qStartNodes.initialize(output); @@ -268,14 +267,15 @@ public abstract class Isochrone { final Set<Node> resultCollection = new HashSet<>(); final Node sourceNode = nConnection.getSourceNode(); - for (final Entry<Node, Link> e : nConnection.getContinuousTargets().entrySet()) { + for (final Entry<Node, Collection<Link>> e : nConnection.getContinuousTargets().entrySet()) { final Node adjacentNode = e.getKey(); - final Link link = e.getValue(); - final double newDistance = sourceNode.getDistance() + link.getLength() / qWalkingSpeed; - - if (newDistance <= qDuration && newDistance < adjacentNode.getDistance()) { - adjacentNode.setDistance(newDistance); - resultCollection.add(adjacentNode); + final Collection<Link> linkCollection = e.getValue(); + for (final Link link : linkCollection) { + final double newDistance = sourceNode.getDistance() + link.getLength() / qWalkingSpeed; + if (newDistance <= qDuration && newDistance < adjacentNode.getDistance()) { + adjacentNode.setDistance(newDistance); + resultCollection.add(adjacentNode); + } } } @@ -296,10 +296,13 @@ public abstract class Isochrone { return Collections.emptyList(); } + // FIXME + // Here: 13times (correct) final Set<Node> resultCollection = new HashSet<>(); - final Map<Node, Double> newDistances = getAdjNodeCost(nConnection); - for (final Entry<Node, Double> e : newDistances.entrySet()) { - final Node adjacentNode = e.getKey(); + final Map<Integer, Double> newDistances = getAdjNodeCost(nConnection); + for (final Entry<Integer, Double> e : newDistances.entrySet()) { + // Here; 3times (why?) + final Node adjacentNode = nConnection.getDiscreteTargetNode(e.getKey()); final double newDistance = e.getValue(); if (newDistance <= qDuration && newDistance < adjacentNode.getDistance()) { adjacentNode.setDistance(newDistance); @@ -326,7 +329,7 @@ public abstract class Isochrone { * @param nConnection information about to where start the calculation, where to travel to and which routes are considered * @return the cost of traveling from the node to adjNode */ - private Map<Node, Double> getAdjNodeCost(final NodeConnection nConnection) { + private Map<Integer, Double> getAdjNodeCost(final NodeConnection nConnection) { return database.getAdjNodeCost(nConnection, dateCodes, qFromTime, qToTime); } diff --git a/src/main/java/it/unibz/inf/isochrone/db/Database.java b/src/main/java/it/unibz/inf/isochrone/db/Database.java index f66cf7937299fa850b74697a1ea4376d2c953a55..efd2e3c8e188cb7767f2e2ee2c2ddc10cd389229 100644 --- a/src/main/java/it/unibz/inf/isochrone/db/Database.java +++ b/src/main/java/it/unibz/inf/isochrone/db/Database.java @@ -224,10 +224,9 @@ public class Database { * @param toTime the latest time for which we want to calculate the departure time * @return the cost of for traveling from the start node to the end node */ - public Map<Node, Double> getAdjNodeCost(final NodeConnection nConnection, final Collection<Integer> dateCodes, final long fromTime, final long toTime) { - final Map<Node, Double> resultMap = new HashMap<>(); + public Map<Integer, Double> getAdjNodeCost(final NodeConnection nConnection, final Collection<Integer> dateCodes, final long fromTime, final long toTime) { + final Map<Integer, Double> resultMap = new HashMap<>(); final Node node = nConnection.getSourceNode(); - double minDistance = Double.POSITIVE_INFINITY; ResultSet rs = null; try { @@ -236,14 +235,20 @@ public class Database { while (rs.next()) { final long departureTime = rs.getLong("TIME_D"); final long arrivalTime = rs.getLong("TIME_A"); - final short routeId = rs.getShort("ROUTE_ID"); - final short adjNodeId = rs.getShort("SOURCE"); + final int routeId = rs.getInt("ROUTE_ID"); + final int adjNodeId = rs.getInt("SOURCE"); final Node adjNode = nConnection.getDiscreteTargetNode(adjNodeId); adjNode.setDepartureTime(routeId, departureTime); node.setArrivalTime(routeId, arrivalTime); + + Double minDistance = resultMap.get(adjNodeId); + if (minDistance == null) { + minDistance = Double.POSITIVE_INFINITY; + } + final double distance = toTime - departureTime; if (distance < minDistance) { - minDistance = distance; + resultMap.put(adjNodeId, distance); adjNode.setCheapestReachedRouteId(routeId); } } @@ -252,14 +257,20 @@ public class Database { while (rs.next()) { final long departureTime = rs.getLong("TIME_D"); final long arrivalTime = rs.getLong("TIME_A"); - final short routeId = rs.getShort("ROUTE_ID"); - final short adjNodeId = rs.getShort("TARGET"); + final int routeId = rs.getInt("ROUTE_ID"); + final int adjNodeId = rs.getInt("TARGET"); final Node adjNode = nConnection.getDiscreteTargetNode(adjNodeId); node.setDepartureTime(routeId, departureTime); adjNode.setArrivalTime(routeId, arrivalTime); + + Double minDistance = resultMap.get(adjNodeId); + if (minDistance == null) { + minDistance = Double.POSITIVE_INFINITY; + } + final double distance = arrivalTime > 0 ? arrivalTime - fromTime : Double.POSITIVE_INFINITY; if (distance < minDistance) { - minDistance = distance; + resultMap.put(adjNodeId, distance); adjNode.setCheapestReachedRouteId(routeId); } } @@ -414,12 +425,7 @@ public class Database { final int targetId = rs.getInt("TARGET"); final short degree = rs.getShort("NODE_DEGREE"); final double length = rs.getDouble("LENGTH"); - if (isIncoming) { - addNode(sourceId, degree, nodes); - } else { - addNode(targetId, degree, nodes); - } - + addNode((isIncoming) ? sourceId : targetId, degree, nodes); link = new Link(linkId, sourceId, targetId, length); } } catch (final SQLException e) {