/// <summary> /// Generates all feasible sets of modes for the trip chain /// </summary> /// <param name="chain">The chain to operate on</param> public static void GenerateModeSets(this ITripChain chain) { //initiates the mode set ModeSet.InitModeSets(chain); ModeData[] data = new ModeData[chain.Trips.Count]; // Generate the random terms var trips = chain.Trips; for (int i = 0; i < data.Length; i++) { data[i] = ModeData.Get(trips[i]); if (data[i] != null) { data[i].GenerateError(); } } ModeSet set = ModeSet.Make(chain); // launch the recursive version to explore all sets GenerateModeSets(chain, data, set); //clear temp var 'mode' that was used in generate mode set algo foreach (var trip in chain.Trips) { trip.Mode = null; } }
/// <summary> /// Calculates and stores the best trip chain for /// each type of vehicle (and NPV) /// </summary> /// <param name="chain">The chain to calculate</param> public static void SelectBestPerVehicleType(this ITripChain chain) { ModeSet[] best = chain["BestForVehicle"] as ModeSet[]; var sets = ModeSet.GetModeSets(chain); if (best == null) { best = new ModeSet[TashaRuntime.VehicleTypes.Count + 1]; chain.Attach("BestForVehicle", best); } for (int i = 0; i < best.Length; i++) { best[i] = null; } foreach (var set in sets) { IVehicleType type = null; foreach (var mode in set.ChosenMode) { if (mode.RequiresVehicle != null) { type = mode.RequiresVehicle; break; } } int index = TashaRuntime.VehicleTypes.IndexOf(type); best[index + 1] = (best[index + 1] == null || best[index + 1].U < set.U) ? set : best[index + 1]; } }
public UnAssignedModeSet(ITripChain chain, ModeSet mSet, IVehicleType vehicleType) : this() { Chain = chain; Set = mSet; VehicleType = vehicleType; }
/// <summary> /// Are their less vehicles than the amount of vehicles needed by the trips in the household at one point /// in the day? /// /// Using Marzullo's algorithm /// /// </summary> /// <param name="tripChains">The trip chains of the household</param> /// <param name="numVehicles">The number of this vehicle type the household has available</param> /// <param name="bestForVehicle"></param> /// <returns></returns> private static bool Conflict(List <ITripChain> tripChains, int numVehicles, int bestForVehicle) { List <Pair <Time, int> > tripIntervals = new List <Pair <Time, int> >(tripChains.Count() * 2); foreach (ITripChain tripChain in tripChains) { //add start time to list ModeSet[] sets = (ModeSet[])tripChain["BestForVehicle"]; ModeSet set = sets[bestForVehicle]; Time travelTime = set.ChosenMode[0].TravelTime(tripChain.Trips[0].OriginalZone, tripChain.Trips[0].DestinationZone, tripChain.Trips[0].ActivityStartTime); tripIntervals.Add(new Pair <Time, int>(tripChain.Trips[0].ActivityStartTime - travelTime, 1)); //add end time to list tripIntervals.Add(new Pair <Time, int>(tripChain.EndTime, -1)); } //sort based on times tripIntervals.Sort(delegate(Pair <Time, int> p1, Pair <Time, int> p2) { var first = p1.First; var second = p2.First; if (first < second) { return(-1); } /*else if(first > second) * { * return 1; * }*/ return(1);//0; }); return(MaxConflicts(tripIntervals) > numVehicles); }
internal static ModeSet Make(ModeSet set, double newU) { ModeSet newModeSet; var chainLength = set.Length; ConcurrentBag <ModeSet> ourBag; if (!ModeSetPool.TryGetValue(chainLength, out ourBag)) { ModeSetPool[chainLength] = (ourBag = new ConcurrentBag <ModeSet>()); return(new ModeSet(set, newU)); } if (ourBag.TryTake(out newModeSet)) { for (int i = 0; i < chainLength; i++) { newModeSet.ChosenMode[i] = set.ChosenMode[i]; } newModeSet.U = newU; newModeSet.Chain = set.Chain; return(newModeSet); } else { return(new ModeSet(set, newU) { Chain = set.Chain }); } }
private ModeSet(ModeSet CopyMe, double U) { this.Chain = CopyMe.Chain; this.Length = CopyMe.Length; this.ChosenMode = new ITashaMode[this.Length]; Array.Copy(CopyMe.ChosenMode, this.ChosenMode, CopyMe.Length); this.U = U; }
private ModeSet(ModeSet copyMe, double u) { Chain = copyMe.Chain; Length = copyMe.Length; ChosenMode = new ITashaMode[Length]; Array.Copy(copyMe.ChosenMode, ChosenMode, copyMe.Length); U = u; }
private ModeSet(ModeSet CopyMe, double U) { this.Chain = CopyMe.Chain; this.Length = CopyMe.Length; this.ChosenMode = new ITashaMode[this.Length]; this.ChosenMode.CoApply( CopyMe.ChosenMode, (n, k) => k ); this.U = U; }
private ModeSet(ModeSet CopyMe, double U) { this.Chain = CopyMe.Chain; this.Length = CopyMe.Length; this.ChosenMode = new ITashaMode[this.Length]; this.ChosenMode.CoApply(CopyMe.ChosenMode, (n, k) => k); this.U = U; }
/// <summary> /// Finds the best non-personal vehicle mode set for a tour, returns false otherwise /// </summary> /// <param name="tour"></param> /// <param name="bestSet"></param> /// <param name="U"></param> /// <returns></returns> private static bool BestNonVehicleModeSetForTour(List <ITripChain> tour, out IList <ITashaMode> bestSet, out double U) { bestSet = null; U = Double.MinValue; ITripChain firstTripChain = tour[0]; IList <ModeSet> firstModeSet = (IList <ModeSet>)firstTripChain["ModeSets"]; List <List <ModeSet> > ModeSets = new List <List <ModeSet> >(); foreach (var chain in tour) { ModeSets.Add(new List <ModeSet>(ModeSet.GetModeSets(chain))); } Dictionary <ModeSet, double> setAndU = new Dictionary <ModeSet, double>(); foreach (var set in ModeSets[0]) { double curU = set.U; bool existsInAllSets = true; for (int i = 1; i < ModeSets.Count; i++) { bool exists = false; foreach (var nextSet in ModeSets[i]) { if (SameChosenModes(set, nextSet)) { exists = true; curU += nextSet.U; break; } } if (!exists) { existsInAllSets = false; break; } } if (existsInAllSets) { setAndU.Add(set, curU); } } if (setAndU.Count == 0) { return(false); } foreach (var element in setAndU) { IList <ITashaMode> chosen = element.Key.ChosenMode; double u = element.Value; if (u > U) { bestSet = chosen; U = u; } } return(true); }
private void ReleaseModeSets(ITashaHousehold h) { foreach (var person in h.Persons) { foreach (var tripChain in person.TripChains) { ModeSet.ReleaseModeSets(tripChain); } } }
/// <summary> /// Goes through the data and regenerates the random components for utility /// </summary> /// <param name="household"></param> public static void UpdateUtilities(this ITashaHousehold household) { foreach (var p in household.Persons) { foreach (var chain in p.TripChains) { foreach (var trip in chain.Trips) { ModeData.Get(trip).GenerateError(); } foreach (var modeSet in ModeSet.GetModeSets(chain)) { modeSet.RecalculateU(); } chain.SelectBestPerVehicleType(); } } }
/// <summary> /// Assigns the best mode to each trip in the trip chain for use in pass 3. (Conflict Vers.) /// </summary> /// <param name="household">Household to work on</param> /// <param name="bestAssignment">The current (after pass 2) best vehicle assignment</param> private void AssignBestMode(ITashaHousehold household, IVehicleType[] bestAssignment) { int i = 0; foreach (var person in household.Persons) { foreach (var tripChain in person.TripChains) { ModeSet[] sets = (ModeSet[])tripChain["BestForVehicle"]; ModeSet set = sets[this.VehicleTypes.IndexOf(bestAssignment[i++]) + 1]; if (set != null) { var numberOfTrips = tripChain.Trips.Count; for (int j = 0; j < numberOfTrips; j++) { tripChain.Trips[j].Mode = set.ChosenMode[j]; } } } } }
private static bool SameChosenModes(ModeSet A, ModeSet B) { var aLength = A.ChosenMode.Length; if (aLength != B.ChosenMode.Length) { return(false); } for (int i = 0; i < aLength; i++) { if (A.ChosenMode[i].NonPersonalVehicle == false) { return(false); } if (A.ChosenMode[i] != B.ChosenMode[i]) { return(false); } } return(true); }
/// <summary> /// Generates all feasible sets of modes for the trip chain /// </summary> /// <param name="chain">The chain to operate on</param> /// <param name="Data">The ModeData for each trip</param> /// <param name="set">The mode set we are building</param> /// <param name="level">How deep in the recursion we are</param> /// <param name=Fo"U">What is the total Utility value calculated</param> private static void GenerateModeSets(ITripChain chain, ModeData[] Data, ModeSet set) { var modes = TashaRuntime.AllModes; var numberOfModes = modes.Count - TashaRuntime.SharedModes.Count; var topLevel = Data.Length - 1; int level = 0; double U = 0; int mode = 0; List<ModeSet> possibleTripChains = ModeSet.GetModeSets( chain ) as List<ModeSet>; Stack<int> previousMode = new Stack<int>( 10 ); Stack<double> previousU = new Stack<double>( 10 ); int chainLength = chain.Trips.Count; var trips = chain.Trips; ITrip currentTrip = trips[0]; while ( level != -1 ) { for ( ; mode < numberOfModes; mode++ ) { // For each feasible mode var currentData = Data[level]; if ( currentData.Feasible[mode] ) { // find the total utility double newU = U + currentData.V[mode] + currentData.Error[mode]; // store the mode into our set and chain set.ChosenMode[level] = currentTrip.Mode = modes[mode]; // if we are at the end, store the set if ( level >= topLevel ) { bool feasible = true; // make sure this chain is allowed for ( int j = 0; j < numberOfModes; j++ ) { // if this doesn't work don't save it if ( !modes[j].Feasible( chain ) ) { feasible = false; break; } } if ( feasible ) { possibleTripChains.Add( ModeSet.Make( set, newU ) ); } } else { // otherwise go to the next trip level++; previousU.Push( U ); U = newU; currentTrip = trips[level]; previousMode.Push( mode ); mode = -1; continue; } } } if ( previousMode.Count > 0 ) { mode = previousMode.Pop() + 1; U = previousU.Pop(); currentTrip = trips[level - 1]; } level--; } }
internal static ModeSet Make(ModeSet set, double newU) { ModeSet newModeSet; var chainLength = set.Length; ConcurrentBag<ModeSet> ourBag; if ( !ModeSetPool.TryGetValue( chainLength, out ourBag ) ) { ModeSetPool[chainLength] = ( ourBag = new ConcurrentBag<ModeSet>() ); return new ModeSet( set, newU ); } if ( ourBag.TryTake( out newModeSet ) ) { for ( int i = 0; i < chainLength; i++ ) { newModeSet.ChosenMode[i] = set.ChosenMode[i]; } newModeSet.U = newU; newModeSet.Chain = set.Chain; return newModeSet; } else { return new ModeSet( set, newU ) { Chain = set.Chain }; } }
/// <summary> /// Generates all feasible sets of modes for the trip chain /// </summary> /// <param name="chain">The chain to operate on</param> /// <param name="data">The ModeData for each trip</param> /// <param name="set">The mode set we are building</param> private static void GenerateModeSets(ITripChain chain, ModeData[] data, ModeSet set) { var modes = TashaRuntime.AllModes; var numberOfModes = modes.Count - TashaRuntime.SharedModes.Count; var topLevel = data.Length - 1; int level = 0; double utility = 0; int mode = 0; List <ModeSet> possibleTripChains = ModeSet.GetModeSets(chain) as List <ModeSet>; Stack <int> previousMode = new Stack <int>(10); Stack <double> previousU = new Stack <double>(10); var trips = chain.Trips; ITrip currentTrip = trips[0]; while (level != -1) { for ( ; mode < numberOfModes; mode++) { // For each feasible mode var currentData = data[level]; if (currentData.Feasible[mode]) { // find the total utility double newU = utility + currentData.V[mode] + currentData.Error[mode]; // store the mode into our set and chain set.ChosenMode[level] = currentTrip.Mode = modes[mode]; // if we are at the end, store the set if (level >= topLevel) { bool feasible = true; // make sure this chain is allowed for (int j = 0; j < numberOfModes; j++) { // if this doesn't work don't save it if (!modes[j].Feasible(chain)) { feasible = false; break; } } if (feasible) { possibleTripChains?.Add(ModeSet.Make(set, newU)); } } else { // otherwise go to the next trip level++; previousU.Push(utility); utility = newU; currentTrip = trips[level]; previousMode.Push(mode); mode = -1; } } } if (previousMode.Count > 0) { mode = previousMode.Pop() + 1; utility = previousU.Pop(); currentTrip = trips[level - 1]; } level--; } }
public static void FindPossibleAssignments(List <Pair <IVehicleType[], double> > possibleAssignments, List <ITripChain> chains, List <IVehicle> vehicles, int position, List <UnAssignedModeSet> currentChains, IVehicleType[] currentAssignment, double currentU) { ModeSet[] sets = (ModeSet[])chains[position]["BestForVehicle"]; if (sets == null) { return; } var numberOfVehicleTypes = ModeChoice.VehicleTypes.Count + 1; for (int j = 0; j < numberOfVehicleTypes; j++) //take into account NPV { ModeSet set = sets[j]; //if this vehicle cannot be used for this set skip it since we are already considering NPV if ((set != null) && (set.ChosenMode != null)) { //a Personal vehicle mode if (j > 0) { UnAssignedModeSet newUms = new UnAssignedModeSet(chains[position], set, set.ChosenMode[0].RequiresVehicle); List <UnAssignedModeSet> allChainsWithSameVehicle = currentChains.FindAll(n => n.VehicleType == newUms.VehicleType); List <ITripChain> sameVehicleChains = new List <ITripChain>(); foreach (var ums in allChainsWithSameVehicle) { sameVehicleChains.Add(ums.GetChainWithAssignedModes()); } int count = vehicles.Count(n => n.VehicleType.Equals(newUms.VehicleType)); var availabilities = HouseholdExtender.FindVehicleAvailabilites(sameVehicleChains, count); foreach (var ums in allChainsWithSameVehicle) { ums.ResetChain(); } ITripChain curChain = newUms.GetChainWithAssignedModes(); var span = new TashaTimeSpan(curChain.StartTime, curChain.EndTime); newUms.ResetChain(); if (HouseholdExtender.VehicleAvailableInTimeSpan(availabilities, span, count)) { currentAssignment[position] = newUms.VehicleType; currentChains.Add(newUms); } else { continue; } } double newU = currentU + set.U; if (position + 1 == chains.Count) { //end of the line IVehicleType[] nextAssignment = new IVehicleType[chains.Count]; currentAssignment.CopyTo(nextAssignment, 0); possibleAssignments.Add(new Pair <IVehicleType[], double>(nextAssignment, newU)); } else { IVehicleType[] nextAssignment = new IVehicleType[chains.Count]; currentAssignment.CopyTo(nextAssignment, 0); FindPossibleAssignments(possibleAssignments, chains, vehicles, position + 1, new List <UnAssignedModeSet>(currentChains), nextAssignment, newU); } } } }
/// <summary> /// Calculates and stores the best trip chain for /// each type of vehicle (and NPV) /// </summary> /// <param name="chain">The chain to calculate</param> public static void SelectBestPerVehicleType(this ITripChain chain) { ModeSet[] best = chain["BestForVehicle"] as ModeSet[]; var sets = ModeSet.GetModeSets( chain ); if ( best == null ) { best = new ModeSet[TashaRuntime.VehicleTypes.Count + 1]; chain.Attach( "BestForVehicle", best ); } for ( int i = 0; i < best.Length; i++ ) { best[i] = null; } foreach ( var set in sets ) { IVehicleType type = null; foreach ( var mode in set.ChosenMode ) { if ( mode.RequiresVehicle != null ) { type = mode.RequiresVehicle; break; } } int index = TashaRuntime.VehicleTypes.IndexOf( type ); best[index + 1] = ( best[index + 1] == null || best[index + 1].U < set.U ) ? set : best[index + 1]; } }
private static bool sameChosenModes(ModeSet A, ModeSet B) { var aLength = A.ChosenMode.Length; if ( aLength != B.ChosenMode.Length ) { return false; } for ( int i = 0; i < aLength; i++ ) { if ( A.ChosenMode[i].NonPersonalVehicle == false ) return false; if ( A.ChosenMode[i] != B.ChosenMode[i] ) return false; } return true; }