* 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()
{
super();
}
/**
* @param tripInfo Map<String,Map<String,TripInformation>>; information for all non-empty OD-pairs
*/
public TripDemand(final Map> tripInfo)
{
super();
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
*/
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