package org.opentrafficsim.demo.ntm.trafficdemand; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.NavigableMap; import org.djunits.value.vdouble.scalar.Duration; import org.djunits.value.vdouble.scalar.Time; import org.opentrafficsim.demo.ntm.NTMNode; /** *

* Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License. *

* $LastChangedDate$, @version $Revision$, by $Author$, * initial version 12 Sep 2014
* @author Alexander Verbraeck * @author Hans van Lint * @author Peter Knoppers * @author Guus Tamminga * @author Yufei Yuan * @param */ public class TripDemand { /** Information on trips: number, shortest path etc.. */ private Map> tripInfo; /** Starting time. */ private Time startTime; /** Time period covered by this demand. */ private Duration timeSpan; /** */ public TripDemand() { } /** * @param tripInfo Map<String,Map<String,TripInformation>>; information for all non-empty OD-pairs */ public TripDemand(final Map> tripInfo) { this.tripInfo = tripInfo; } /** * Compresses the trip demand from detailed areas to larger areas * @param tripDemand TripDemand<TripInfoTimeDynamic>; comprising the original demand * @param centroids Map<String,NTMNode>; the detailed areas * @param mapSmallAreaToBigArea LinkedHashMap<NTMNode,NTMNode>; provides the key from small to big areas (type Node!!) * @return TripDemand<TripInfoTimeDynamic>; the compressed trip demand */ public static TripDemand compressTripDemand(TripDemand tripDemand, Map centroids, LinkedHashMap mapSmallAreaToBigArea) { TripDemand compressedTripDemand = new TripDemand(); compressedTripDemand.tripInfo = new LinkedHashMap>(); compressedTripDemand.startTime = tripDemand.getStartTime(); compressedTripDemand.timeSpan = tripDemand.getTimeSpan(); int notFound = 0; // loop through all detailed nodes/areas for (NTMNode node : centroids.values()) { if (mapSmallAreaToBigArea.get(node) != null) { Map tripDemandRow; Map bigTripDemandRow = null; // create or retrieve the (partly filled) compressed data if (mapSmallAreaToBigArea.get(node).getId() != null) { bigTripDemandRow = compressedTripDemand.getTripDemandOriginToAllDestinations(mapSmallAreaToBigArea.get(node).getId()); } else // if not found we keep the old centroid { bigTripDemandRow = compressedTripDemand.getTripDemandOriginToAllDestinations(node.getId()); } if (bigTripDemandRow == null) { bigTripDemandRow = new LinkedHashMap(); } // retrieve the detailled trips tripDemandRow = tripDemand.getTripDemandOriginToAllDestinations(node.getId()); // get all destinations if (tripDemandRow != null) { for (Map.Entry entry : tripDemandRow.entrySet()) { String idSmall = entry.getKey(); // mapSmallAreaToBigArea.get(node); NTMNode destination = centroids.get(idSmall); TripInfoTimeDynamic tripInfo = tripDemandRow.get(idSmall); if (destination != null) { if (mapSmallAreaToBigArea.get(destination) == null) { System.out.println("null mapping"); } else if (mapSmallAreaToBigArea.get(destination).getId() != null) { // System.out.println("node " + destination.getId() + "bigNode ID " // + mapSmallAreaToBigArea.get(destination).getId()); TripInfoTimeDynamic bigTripInfo = bigTripDemandRow.get(mapSmallAreaToBigArea.get(destination).getId()); if (bigTripInfo == null) { bigTripInfo = new TripInfoTimeDynamic(0, null); } bigTripInfo.addNumberOfTrips(tripInfo.getNumberOfTrips()); bigTripInfo.setDepartureTimeProfile(tripInfo.getDepartureTimeProfile()); bigTripDemandRow.put(mapSmallAreaToBigArea.get(destination).getId(), bigTripInfo); } else { System.out.println("node bigNode not found?"); } } else { notFound++; System.out.println("destination not found? " + notFound); } } } if (mapSmallAreaToBigArea.get(node).getId() != null) { compressedTripDemand.tripInfo.put(mapSmallAreaToBigArea.get(node).getId(), bigTripDemandRow); } else // if not found we keep the old centroid { compressedTripDemand.tripInfo.put(node.getId(), bigTripDemandRow); } } } // then compress the rows return compressedTripDemand; } /** * @param origin String; * @param destination * @return mapDestinations a hashmap with destination as key and tripInfo as values */ public final Map getTripDemandOriginToAllDestinations(final String origin) { Map> demand = this.getTripInfo(); Map mapDestinations = demand.get(origin); return mapDestinations; } /** * @param thisDemand TripDemand<TripInfoTimeDynamic>; * @param currentTime Time; * @param timeStepDurationNTM Duration; * @param origin * @param destination * @return mapDestinations a hashmap with destination as key and tripInfo as values */ public static final double getTotalNumberOfTripsFromOrigin(TripDemand thisDemand, String originID, Time currentTime, final Duration timeStepDurationNTM) { Map> demand = thisDemand.getTripInfo(); Map mapDestinations = demand.get(originID); double rowTotal = 0.0; if (mapDestinations != null) { for (Entry tripInfo : mapDestinations.entrySet()) { double trips = getTotalNumberOfTripsFromOriginToDestinationByTimeStep(thisDemand, originID, tripInfo.getKey(), currentTime, timeStepDurationNTM); rowTotal += trips; } } return rowTotal; } public static final double getTotalNumberOfTripsFromOriginToDestinationByTimeStep( TripDemand thisDemand, String originID, String destination, Time currentTime, final Duration timeStepDurationNTM) { Map> demand = thisDemand.getTripInfo(); Map mapDestinations = demand.get(originID); double cellTotal = 0.0; TripInfoTimeDynamic tripInfo = mapDestinations.get(destination); NavigableMap curve = tripInfo.getDepartureTimeProfile().getDepartureTimeCurve(); Object ceilingKey = curve.floorKey(currentTime); if (ceilingKey == null) { System.out.println("TripDemand 186: Strange not within TimeSpan " + ceilingKey); } FractionOfTripDemandByTimeSegment segment = curve.get(ceilingKey); double share = 0; if (segment.getDuration().getSI() > 0) { share = segment.getShareOfDemand() * timeStepDurationNTM.getSI() / segment.getDuration().getSI(); } else { System.out.println("segment should not be zero"); } cellTotal = tripInfo.getNumberOfTrips() * share; return cellTotal; } /** * @param origin String; * @param destination String; * @return tripInfo by OD pair */ public final TripInformation getTripDemandOriginToDestination(final String origin, final String destination) { TripInformation info = null; Map> tripInfoAll = this.getTripInfo(); Map map = tripInfoAll.get(origin); if (map != null) { if (destination != null) { if (map.get(destination) == null) { System.out.println("null!!!"); } else { info = map.get(destination); } } } return info; } /** * @param origin String; * @param destination String; * @param tripInfo TripInformation; * @param tripInfoAll Map<String,Map<String,TripInformation>>; * @return */ public final Map> setTripDemandOriginToDestination(final String origin, final String destination, TripInformation tripInfo, Map> tripInfoAll) { Map map = tripInfoAll.get(origin); if (map == null) { map = new LinkedHashMap(); } map.put(destination, tripInfo); return tripInfoAll; } /** * @return startTime. */ public final Time getStartTime() { return this.startTime; } /** * @param startTime Time; set startTime. */ public final void setStartTime(final Time startTime) { this.startTime = startTime; } /** * @return tripDemand */ public final Map> getTripInfo() { return this.tripInfo; } /** * @return timeSpan. */ public final Duration getTimeSpan() { return this.timeSpan; } /** * @param timeSpan Duration; set timeSpan. */ public final void setTimeSpan(final Duration timeSpan) { this.timeSpan = timeSpan; } /** * @param tripInfo Map<String,Map<String,TripInformation>>; sets tripInfo */ public final void setTripInfo(final Map> tripInfo) { this.tripInfo = tripInfo; } }