public Trip(ITripChain chain, IZone origin, IZone destination, Activity purpose, Time startTime, int householdIterations) { TripChain = chain; TripStartTime = startTime; OriginalZone = origin; DestinationZone = destination; Purpose = purpose; IntermediateZone = null; Passengers = new List<ITashaPerson>(); ModesChosen = new ITashaMode[householdIterations]; }
public ModeChoiceTripChainData(ITripChain tripChain, int numberOfModes, int numberOfVehicleTypes) { var trips = tripChain.Trips; TripChain = tripChain; BestPossibleAssignmentForVehicleType = new PossibleTripChainSolution[numberOfVehicleTypes]; var tripData = new ModeChoiceTripData[trips.Count]; PossibleAssignments = new List<PossibleTripChainSolution>((numberOfModes * trips.Count) >> 1); for(int i = 0; i < tripData.Length; i++) { tripData[i] = new ModeChoiceTripData(numberOfModes); } TripData = tripData; }
internal void PickSolution(ITripChain chain) { if ( TourData == null ) return; var onSolution = TourData.OnSolution; var picked = PickedModes; for ( int i = 0; i < onSolution.Length; i++ ) { if ( onSolution[i] != null ) { onSolution[i]( chain ); } } }
public void GetTripActivities(ITrip trip, ITripChain chain, out char origin, out char destination) { destination = GetActivityChar( trip.Purpose ); List<ITrip> trips = chain.Trips; for ( int i = 0; i < trips.Count; i++ ) { if ( trips[i] == trip ) { if ( i == 0 ) { origin = GetActivityChar( Activity.Home ); } else { origin = GetActivityChar( trips[i - 1].Purpose ); } return; } } origin = '0'; }
public bool Feasible(ITripChain tripChain) { return(true); }
public ITrip CreateTrip(ITripChain chain, IZone originalZone, IZone destinationZone, Activity purpose, Time startTime) { throw new NotImplementedException(); }
private float ComputeError(ITripChain tour) { var observedAccessStation = tour[this.AccessStationTag] as IZone; var result = this.AccessStationChoiceModel.ProduceResult( tour ); float correct = 0.0f; if ( result != null ) { var total = 0.0f; int correctIndex = -1; for ( int i = 0; i < result.First.Length; i++ ) { if ( result.First[i] == null ) break; if ( result.First[i] == observedAccessStation ) correctIndex = i; var value = result.Second[i]; total += result.Second[i]; } if ( total > 0 & correctIndex >= 0 ) { correct = result.Second[correctIndex] / total; } } // we finished another Interlocked.Increment( ref this.Completed ); var error = 1.0f - correct; // use squared error return error * error; }
/// <summary> /// /// </summary> /// <param name="tripChain"></param> /// <returns></returns> public bool Feasible(ITripChain tripChain) { //passenger mode does not handle joint trips return !tripChain.JointTrip; }
internal static ModeSet Make(ITripChain chain) { ModeSet newModeSet; var chainLength = chain.Trips.Count; ConcurrentBag<ModeSet> ourBag; if ( !ModeSetPool.TryGetValue( chainLength, out ourBag ) ) { ModeSetPool[chainLength] = ( ourBag = new ConcurrentBag<ModeSet>() ); return new ModeSet( chain ); } if ( ourBag.TryTake( out newModeSet ) ) { newModeSet.Chain = chain; return newModeSet; } else { return new ModeSet( chain ); } }
private float TimeToNextTrip(ITrip trip, ITripChain chain) { var tchain = trip.TripChain.Trips; for(int i = 0; i < tchain.Count - 1; i++) { if(tchain[i] == trip) { // the number is (1/60)f return (tchain[i + 1].ActivityStartTime - trip.ActivityStartTime).ToMinutes() * 0.01666666666666f; } } return 0f; }
/// <summary> /// This code initializes the modeset for a trip chain /// </summary> /// <param name="chain">The chain to init mode sets for</param> public static void InitModeSets(ITripChain chain) { chain.Attach( "ModeSets", new List<ModeSet>( chain.Trips.Count ) ); }
/// <summary> /// Stores this mode set to the trip chain /// </summary> /// <param name="chain">The chain to attach this set to</param> /// <param name="U"></param> public void Store(ITripChain chain, double U) { List<ModeSet> set = (List<ModeSet>)chain["ModeSets"]; set.Add( new ModeSet( this, U ) ); }
/// <summary> /// Gets an enumeration of the ModeSets for the trip chain /// </summary> /// <param name="chain">The trip chain to look at</param> /// <returns>An enumeration of all of the mode sets for this chain</returns> public static IEnumerable<ModeSet> GetModeSets(ITripChain chain) { return (List<ModeSet>)chain["ModeSets"]; }
/// <summary> /// Checks to see if a whole chain of trips is possible /// </summary> /// <param name="tripChain">The chain to check</param> /// <returns>If it is possible</returns> public bool Feasible(ITripChain tripChain) { int vehicleLeftAt = tripChain.Person.Household.HomeZone.ZoneNumber; var home = vehicleLeftAt; var trips = tripChain.Trips; var length = trips.Count; bool noAutoTrips = true; bool first = false; bool lastMadeWithAuto = false; for(int i = 0; i < length; i++) { var trip = trips[i]; var mode = trip.Mode; if(!mode.NonPersonalVehicle) { if(mode.RequiresVehicle == this.AutoType) { // it is only not feasible if we actually take the mode and we don't have a licence if((trip.OriginalZone.ZoneNumber != vehicleLeftAt)) { return false; } vehicleLeftAt = trip.DestinationZone.ZoneNumber; lastMadeWithAuto = true; noAutoTrips = false; } else { lastMadeWithAuto = false; } } else { lastMadeWithAuto = false; } if(i == 0) { first = lastMadeWithAuto; } } return (noAutoTrips) | ((first) & (lastMadeWithAuto) & (vehicleLeftAt == home)); }
public bool Feasible(ITripChain tripChain) { IZone vehicleLeftAt = tripChain.Person.Household.HomeZone; var home = vehicleLeftAt; var trips = tripChain.Trips; bool noAutoTrips = true; bool first = false; bool lastMadeWithAuto = false; for(int i = 0; i < trips.Count; i++) { var trip = trips[i]; if(trip.Mode.RequiresVehicle == RequiresVehicle) { // it is only not feasible if we actually take the mode and we don't have a license if((trip.OriginalZone != vehicleLeftAt)) { return false; } vehicleLeftAt = trip.DestinationZone; lastMadeWithAuto = true; noAutoTrips = false; } else { lastMadeWithAuto = false; } if(i == 0) { first = lastMadeWithAuto; } } return (noAutoTrips) | ((first) & (lastMadeWithAuto) & (vehicleLeftAt == home)); }
public Pair<IZone[], float[]> ProduceResult(ITripChain data) { ITrip first, second; if(GetTripsFirst(data, out first, out second)) { if(first == null | second == null) return null; TimePeriod firstTimePeriod = GetTimePeriod(first); TimePeriod secondTimePeriod = GetTimePeriod(second); if(firstTimePeriod == null | secondTimePeriod == null) return null; var zoneArray = Root.ZoneSystem.ZoneArray; float[] utilities = new float[AccessZoneIndexes.Length]; var firstOrigin = zoneArray.GetFlatIndex(first.OriginalZone.ZoneNumber) * AccessZoneIndexes.Length; var firstDestination = zoneArray.GetFlatIndex(first.DestinationZone.ZoneNumber) * AccessZoneIndexes.Length; var secondOrigin = zoneArray.GetFlatIndex(second.OriginalZone.ZoneNumber) * AccessZoneIndexes.Length; var secondDestination = zoneArray.GetFlatIndex(second.DestinationZone.ZoneNumber) * AccessZoneIndexes.Length; if(VectorHelper.IsHardwareAccelerated) { VectorHelper.Multiply(utilities, 0, firstTimePeriod.AutoFromOriginToAccessStation, firstOrigin, firstTimePeriod.TransitFromAccessStationToDestination, firstDestination, secondTimePeriod.TransitFromDestinationToAccessStation, secondOrigin, secondTimePeriod.AutoFromAccessStationToDestination, secondDestination, utilities.Length); VectorHelper.ReplaceIfLessThanOrNotFinite(utilities, 0, 0.0f, MinimumStationUtility, utilities.Length); } else { for(int i = 0; i < utilities.Length; i++) { utilities[i] = firstTimePeriod.AutoFromOriginToAccessStation[firstOrigin + i] * firstTimePeriod.TransitFromAccessStationToDestination[firstDestination + i] * secondTimePeriod.TransitFromDestinationToAccessStation[secondOrigin + i] * secondTimePeriod.AutoFromAccessStationToDestination[secondDestination + i]; if(!(utilities[i] >= MinimumStationUtility)) { utilities[i] = 0.0f; } } } return new Pair<IZone[], float[]>(AccessZones, utilities); } return null; }
/// <summary> /// Checking Feasibility of the entire trip chain /// TODO: Switch this to "Feasible", inform James /// </summary> /// <param name="tripChain"></param> /// <returns></returns> public bool Feasible(ITripChain tripChain) { bool AccessOccured = false; var trips = tripChain.Trips; int carLocation = trips[0].OriginalZone.ZoneNumber; //checking if each access has an Egress and car is located at origin of access foreach ( ITrip trip in trips ) { var mode = trip.Mode; if ( mode is TransitEgress )//if mode is an Egress { //there was no access before this Egress if ( !AccessOccured ) { return false; } AccessOccured = false; //cancel out the previous access with this Egress } else if ( mode is TransitAccess ) { // if there was an access before this access if ( AccessOccured | ( trip.OriginalZone.ZoneNumber != carLocation ) ) { return false; } AccessOccured = true; } else if ( mode.RequiresVehicle == this.AutoType ) { carLocation = trip.DestinationZone.ZoneNumber; } } return !AccessOccured; }
private bool GetTripsFirst(ITripChain tc, out ITrip trip1, out ITrip trip2) { var list = tc.Trips; int i = 0; trip1 = null; trip2 = null; for(; i < list.Count; i++) { if(list[i].Mode == OurMode) { trip1 = list[i++]; break; } } for(; i < list.Count; i++) { if(list[i].Mode == OurMode) { trip2 = list[i++]; break; } } // if we get in here and find another trip, then we have more than 2 and is thus invalid for(; i < list.Count; i++) { if(list[i].Mode == OurMode) { return false; } } return trip1 != null & trip2 != null; }
/// <summary> /// Checking feasibility of entire trip chain for a bike /// </summary> /// <param name="tripChain">The Trip Chain to test feasibility on</param> /// <returns>is this trip chain feasible?</returns> public bool Feasible(ITripChain tripChain) { var trips = tripChain.Trips; var lastPlace = trips[0].OriginalZone; var homeZone = lastPlace; bool lastMadeWithBike = true; bool firstWasBike = trips[0].Mode == this; bool anyBike = false; for(int i = 0; i < trips.Count; i++) { var trip = trips[i]; if(trip.Mode == this) { anyBike = true; if(trip.OriginalZone != lastPlace) { return false; } lastPlace = trip.DestinationZone; lastMadeWithBike = true; } else { lastMadeWithBike = false; } } return !anyBike | (firstWasBike & lastPlace == homeZone & lastMadeWithBike); }
internal static void ReleaseModeSets(ITripChain tripChain) { var sets = GetModeSets( tripChain ); if ( sets != null ) { foreach ( var set in sets ) { var length = set.Length; set.Chain = null; ModeSetPool[length].Add( set ); } } }
/// <summary> /// Create a new Mode Set /// </summary> /// <param name="chain">The trip chain this mode is for</param> private ModeSet(ITripChain chain) { this.Chain = chain; this.Length = chain.Trips.Count; this.ChosenMode = new ITashaMode[this.Length]; }
private int LatestAccessStation(ITripChain tripChain, ITrip trip) { int egressStation = -1; //looking for latest access foreach ( ITrip trip2 in tripChain.Trips ) { if ( trip2 == trip ) break; if ( trip2.GetVariable( "subway-access-station" ) != null ) { egressStation = (int)trip2.GetVariable( "subway-access-station" ); } } return egressStation; }
/// <summary> /// /// </summary> /// <param name="tripChain"></param> /// <returns></returns> public bool Feasible(ITripChain tripChain) { bool carIsOut = false; foreach ( var trip in tripChain.Trips ) { if ( trip.Mode is GoAccess ) { if ( carIsOut ) return false; carIsOut = true; } else if ( trip.Mode is GoEgress ) { if ( !carIsOut ) return false; carIsOut = false; } } return !carIsOut; }
public bool GetTranslatedOD(ITripChain chain, ITrip trip, bool access, out IZone origin, out IZone destination) { if(CountAccess ^ (!access)) { origin = trip.OriginalZone; destination = chain[AccessZoneTagName] as IZone; return destination != null; } else { origin = chain[AccessZoneTagName] as IZone; destination = trip.DestinationZone; return origin != null; } }
public bool Feasible(ITripChain tripChain) { return true; }
/// <summary> /// Determines if assigning a ride share mode to this trip chain is feasible. /// Criteria: Someone must be assigned a car on this trip chain (not necessarily the rep). /// </summary> /// <param name="tripChain"></param> /// <returns></returns> public bool Feasible(ITripChain tripChain) { if ( tripChain.Person.Household.Vehicles == null ) return false; return tripChain.Person.Household.Vehicles.Length > 0; }
private float ComputeError(ITripChain[] tours) { return ( from tour in tours.AsParallel() select ComputeError( tour ) ).Sum(); }
/// <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--; } }
private void ProduceConfusionMatrix(ITripChain[] tours) { var zoneSystem = ZoneSystem.ZoneArray; var zones = zoneSystem.GetFlatData(); var results = new float[zones.Length * zones.Length]; for ( int t = 0; t < tours.Length; t++ ) { var tour = tours[t]; var observedAccessStation = tour[this.AccessStationTag] as IZone; var result = this.AccessStationChoiceModel.ProduceResult( tour ); var observedOffset = zoneSystem.GetFlatIndex( observedAccessStation.ZoneNumber ) * zones.Length; if ( result != null ) { if ( Normalize( result ) ) { for ( int i = 0; i < result.First.Length && result.First[i] != null; i++ ) { var value = result.Second[i]; var jIndex = zoneSystem.GetFlatIndex( result.First[i].ZoneNumber ); results[observedOffset + jIndex] += value; } } } } TMG.Functions.SaveData.SaveMatrix( zones, results, ConfusionMatrix ); }
private static bool AreTogether(ITripChain f, ITripChain s) { bool success = true; if ( f.Trips.Count != s.Trips.Count ) return false; f.Trips.CoDo( s.Trips, delegate(ITrip first, ITrip second) { if ( success && !AreTogether( first, second ) ) { success = false; } } ); return success; }
public bool Feasible(ITripChain tripChain) { throw new NotImplementedException(); }
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); } } } }