/// <summary> /// Gets the number of vehicles available in the household for the given timespan /// </summary> /// <param name="h"></param> /// <param name="span"></param> /// <param name="vehicleType"></param> /// <param name="includeAuxTripChains"></param> /// <returns></returns> public static int NumberOfVehicleAvailable(this ITashaHousehold h, TashaTimeSpan span, IVehicleType vehicleType, bool includeAuxTripChains) { Dictionary <TashaTimeSpan, int> availabilities = h.FindVehicleAvailabilites(vehicleType, includeAuxTripChains); var vehicles = h.Vehicles; int available = 0; for (int i = 0; i < vehicles.Length; i++) { if (vehicles[i].VehicleType == vehicleType) { available++; } } foreach (var a in availabilities) { if ((a.Key.Start < span.End) && (a.Key.End > span.Start)) { // this is strictly less than since we want the min of the vehicles if (a.Value < available) { available = a.Value; } } } return(available); }
public static bool VehicleAvailableInTimeSpan(Dictionary <TashaTimeSpan, int> availabilites, TashaTimeSpan span, int available) { foreach (var a in availabilites) { if ((a.Key.Start < span.End) && (a.Key.End > span.Start)) { if (a.Value < available) { available = a.Value; } } } return(available > 0); }
public static int NumberOfVehicleAvailable(List <ITripChain> tripChains, int numVehicles, IVehicleType vehicleType, TashaTimeSpan span) { Dictionary <TashaTimeSpan, int> availabilities = FindVehicleAvailabilites(tripChains, numVehicles); int available = numVehicles; foreach (var a in availabilities) { if ((a.Key.Start < span.End) && (a.Key.End > span.Start)) { if (a.Value < available) { available = a.Value; } } } return(available); }
public static Dictionary <TashaTimeSpan, int> FindVehicleAvailabilites(List <ITripChain> tc, int numVehicles) { int vehiclesAvailable = numVehicles; Dictionary <TashaTimeSpan, int> availabilities = new Dictionary <TashaTimeSpan, int>(); if (tc.Count == 0) { availabilities.Add(new TashaTimeSpan(TashaRuntime.StartOfDay, TashaRuntime.EndOfDay), vehiclesAvailable); return(availabilities); } List <Pair <Time, int> > TripStartAndEndTimes = new List <Pair <Time, int> >(); foreach (var tripChain in tc) { Pair <Time, int> startTime = new Pair <Time, int>(tripChain.StartTime, -1); Pair <Time, int> endTime = new Pair <Time, int>(tripChain.EndTime, 1); TripStartAndEndTimes.Add(startTime); TripStartAndEndTimes.Add(endTime); } TripStartAndEndTimes.Sort(delegate(Pair <Time, int> p1, Pair <Time, int> p2) { var first = p1.First; var second = p2.First; if (second < first) { return(1); } if (second > first) { return(-1); } return(0); }); for (int i = -1; i < TripStartAndEndTimes.Count; i++) { TashaTimeSpan span; //from last trip to end of day if (i == TripStartAndEndTimes.Count - 1) { span = new TashaTimeSpan(TripStartAndEndTimes[i].First, Time.EndOfDay); vehiclesAvailable += TripStartAndEndTimes[i].Second; if (availabilities.ContainsKey(span)) { availabilities[span] += availabilities[span]; } else { availabilities.Add(span, vehiclesAvailable); } } else if (i == -1) //from start of day to first trip { span = new TashaTimeSpan(Time.StartOfDay, TripStartAndEndTimes[i + 1].First); if (availabilities.ContainsKey(span)) { availabilities[span] += availabilities[span]; } else { availabilities.Add(span, vehiclesAvailable); } } else //trips in between { vehiclesAvailable += TripStartAndEndTimes[i].Second; span = new TashaTimeSpan(TripStartAndEndTimes[i].First, TripStartAndEndTimes[i + 1].First); if (availabilities.ContainsKey(span)) { availabilities[span] += availabilities[span]; } else { availabilities.Add(span, vehiclesAvailable); } } } return(availabilities); }