Ejemplo n.º 1
0
Archivo: Taxi.cs Proyecto: lunaxi7/XTMF
        /// <summary>
        /// Calculates the V for this mode
        /// </summary>
        /// <param name="trip">The trip</param>
        /// <returns>The V</returns>
        public double CalculateV(ITrip trip)
        {
            double v = 0.0;

            if (UseIntrazonalRegression && trip.OriginalZone == trip.DestinationZone)
            {
                v += IntrazonalConstantWeight + trip.OriginalZone.InternalDistance * IntrazonalDistanceWeight;
            }
            else
            {
                v += CTaxi;
                v += Time * TravelTime(trip.OriginalZone, trip.DestinationZone, trip.TripStartTime).ToMinutes();
                v += FareCost * CalculateFare(trip.OriginalZone, trip.DestinationZone, trip.ActivityStartTime);
            }
            if (Common.GetTimePeriod(trip.ActivityStartTime) == TravelTimePeriod.Offpeak)
            {
                v += OffPeakTrip;
            }
            if (TransportTerminal.Contains(trip.OriginalZone.ZoneNumber) | TransportTerminal.Contains(trip.DestinationZone.ZoneNumber))
            {
                v += FromToTransportTerminal;
            }
            if (trip.Purpose == Activity.Market | trip.Purpose == Activity.JointMarket)
            {
                v += DpurpShopDrive;
            }
            else if (trip.Purpose == Activity.IndividualOther | trip.Purpose == Activity.JointOther)
            {
                v += DpurpOthDrive;
            }
            return(v);
        }
Ejemplo n.º 2
0
        public void Execute(ITashaHousehold household, int iteration)
        {
            var homeZone = _zones.GetFlatIndex(household.HomeZone.ZoneNumber);

            foreach (var person in household.Persons)
            {
                int emp = GetEmp(person);
                int occ = GetOcc(person);
                if (ValidPerson(emp, occ))
                {
                    var workerCategory = GetWorkerCategory(person, household);
                    var empZone        = person.EmploymentZone;
                    if (empZone == null)
                    {
                        continue;
                    }
                    var   flatEmpZone     = _zones.GetFlatIndex(empZone.ZoneNumber);
                    float expansionFactor = person.ExpansionFactor;
                    _zonalEmployment[emp][occ][flatEmpZone] += expansionFactor;
                    if (!ExternalPDs.Contains(empZone.PlanningDistrict))
                    {
                        _zonalResidence[emp][occ][homeZone] += expansionFactor;
                    }
                    _zonalWorkerCategories[emp][occ][homeZone][flatEmpZone][workerCategory] += expansionFactor;
                }
            }
        }
Ejemplo n.º 3
0
        private bool CheckPDContained(IZone origin, IZone destination)
        {
            var containsOrigin      = ValidPlanningDistricts.Contains(origin.PlanningDistrict);
            var containsDestination = ValidPlanningDistricts.Contains(destination.PlanningDistrict);

            if (OriginDestinationPDSame)
            {
                if (origin.PlanningDistrict != destination.PlanningDistrict)
                {
                    return(false);
                }
                return(containsOrigin);
            }

            if (ExclusiveOrPD)
            {
                return(containsOrigin ^ containsDestination);
            }
            if (ContainsOnePD)
            {
                return(containsOrigin | containsDestination);
            }
            if (ContainsBothPD)
            {
                return(containsOrigin & containsDestination);
            }
            return(!(containsOrigin | containsDestination));
        }
 public void HouseholdComplete(ITashaHousehold household, bool success)
 {
     if (WriteThisIteration)
     {
         if (SelectedHouseholdZones.Contains(household.HomeZone.ZoneNumber))
         {
             var householdNumber = household.HouseholdId;
             lock (this)
             {
                 int personNumber = 0;
                 foreach (var person in household.Persons)
                 {
                     personNumber++;
                     var preferedNonVehicle = (int[])person[AttachmentName];
                     var empZone            = person.EmploymentZone;
                     var employmentZone     = empZone == null ? 0 : empZone.ZoneNumber;
                     if (SelectedEmploymentZones.Contains(employmentZone))
                     {
                         var tripNumber      = 1;
                         var tripChainNumber = 1;
                         foreach (var tripChain in person.TripChains)
                         {
                             foreach (var trip in tripChain.Trips)
                             {
                                 SaveTrip(trip, householdNumber, personNumber, tripChainNumber, tripNumber++, preferedNonVehicle[tripChainNumber - 1]);
                             }
                             tripChainNumber++;
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
            private void CalculateUtilities(RangeSet stationRanges, RangeSet spatialZones, float[] capacity, int[] closestStation)
            {
                INetworkData       autoNetwork    = GetNetwork(AutoNetworkName);
                ITripComponentData transitNetwork = GetNetwork(TransitNetworkName) as ITripComponentData;

                EnsureNetworks(autoNetwork, transitNetwork);
                var zoneArray = Root.ZoneSystem.ZoneArray;

                IZone[] zones              = zoneArray.GetFlatData();
                int[]   stationZones       = GetStationZones(stationRanges, capacity, zones);
                var     flatCapacityFactor = CapacityFactor.GetFlatData();

                if (AutoFromOriginToAccessStation == null || TransitFromAccessStationToDestination.Length != stationZones.Length * zones.Length)
                {
                    TransitFromAccessStationToDestination = new float[stationZones.Length * zones.Length];
                    AutoFromOriginToAccessStation         = new float[stationZones.Length * zones.Length];
                    TransitFromDestinationToAccessStation = new float[stationZones.Length * zones.Length];
                    AutoFromAccessStationToDestination    = new float[stationZones.Length * zones.Length];
                }
                // compute the toAccess utilities
                Parallel.For(0, zones.Length, (int originIndex) =>
                {
                    var zoneNumber = zones[originIndex].ZoneNumber;
                    if (spatialZones.Contains(zoneNumber))
                    {
                        for (int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor      = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to access station this will include more factors
                            AutoFromOriginToAccessStation[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, originIndex, accessIndex)
                                                                                                                   + (Capacity * capacity[accessIndex]
                                                                                                                      + ParkingCost * zones[accessIndex].ParkingCost
                                                                                                                      + (closestStation[originIndex] == accessIndex ? ClosestStationFactor : 0))) * factor;
                            // calculate egress' from access station
                            AutoFromAccessStationToDestination[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, accessIndex, originIndex)) * factor;
                        }
                    }
                });

                // compute the toDesinstination utilities
                Parallel.For(0, zones.Length, (int destIndex) =>
                {
                    var zoneNumber = zones[destIndex].ZoneNumber;
                    if (spatialZones.Contains(zoneNumber))
                    {
                        for (int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor      = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to destination
                            TransitFromAccessStationToDestination[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, accessIndex, destIndex)) * factor;
                            // calculate egress' to access station
                            TransitFromDestinationToAccessStation[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, destIndex, accessIndex)) * factor;
                        }
                    }
                });
            }
Ejemplo n.º 6
0
 private static bool IsContained(IZone zone, RangeSet validRanges, bool contained, bool useZoneRegion, bool useZonePlanningDistrict)
 {
     if (useZoneRegion)
     {
         return((!contained) ^ validRanges.Contains(zone.RegionNumber));
     }
     if (useZonePlanningDistrict)
     {
         return((!contained) ^ validRanges.Contains(zone.PlanningDistrict));
     }
     return((!contained) ^ validRanges.Contains(zone.ZoneNumber));
 }
Ejemplo n.º 7
0
 public void Initialize(int[] regionNumber, float[] originalZoneDensities, float[] forecastZoneDensities)
 {
     InitialZoneClassification  = originalZoneDensities.Select((d, i) => Regions.Contains(regionNumber[i]) ? ClassifyZone(d) : -1).ToArray();
     ForecastZoneClassification = forecastZoneDensities.Select((d, i) => Regions.Contains(regionNumber[i]) ? ClassifyZone(d) : -1).ToArray();
     DensityPool       = new List <ExpandedHousehold> [DensityBins.Count];
     PoolToGlobalIndex = new List <int> [DensityBins.Count];
     for (int i = 0; i < DensityPool.Length; i++)
     {
         DensityPool[i]       = new List <ExpandedHousehold>();
         PoolToGlobalIndex[i] = new List <int>();
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Get the current probabilities for a given person's age
 /// </summary>
 /// <param name="age">The age of the person to lookup for.</param>
 /// <returns>The probability distribution for the age.</returns>
 private SparseTwinIndex <float> GetDataForAge(int age)
 {
     if (ElementryRange.Contains(age))
     {
         return(CurrentElementarySchoolProbabilities);
     }
     if (HighschoolRange.Contains(age))
     {
         return(CurrentHighSchoolProbabilities);
     }
     return(CurrentUniversityProbabilities);
 }
            public void HouseholdIterationComplete(ITashaHousehold household)
            {
                var persons = household.Persons;

                foreach (var person in persons)
                {
                    var tripChains = person.TripChains;
                    if (tripChains.Count > 0)
                    {
                        var personalModeChoices = OperatingOn[person];
                        var expansionFactor     = person.ExpansionFactor;
                        foreach (var tripChain in tripChains)
                        {
                            foreach (var trip in tripChain.Trips)
                            {
                                // if the trip is within the catchment area record it
                                if (ContainedZones.Contains(trip.OriginalZone.ZoneNumber))
                                {
                                    var startTime = trip.TripStartTime;
                                    if (startTime >= StartTime && startTime < EndTime)
                                    {
                                        personalModeChoices[Root.Modes[trip.Mode]] += expansionFactor;
                                    }
                                }
                            }
                        }
                    }
                }
            }
                private int ClassifyHousehold(ITashaHousehold household)
                {
                    int category;
                    int zoneNumber = household.HomeZone.ZoneNumber;

                    if (TaxibotServedHouseholds.Contains(zoneNumber))
                    {
                        category = 2;
                    }
                    else
                    {
                        var numberOfLicenses = 0;
                        var numberOfVehicles = household.Vehicles.Length;
                        if (numberOfVehicles > 0)
                        {
                            var persons = household.Persons;
                            for (int i = 0; i < persons.Length; i++)
                            {
                                if (persons[i].Licence)
                                {
                                    numberOfLicenses++;
                                }
                            }
                        }
                        category = numberOfLicenses == 0 ? 0 : (numberOfVehicles < numberOfLicenses ? 1 : 2);
                    }
                    if (SaveWorkerCategory != null)
                    {
                        RecordHouseholdCategory(category, zoneNumber, household.ExpansionFactor);
                    }
                    return(category);
                }
 public void HouseholdIterationComplete(ITashaHousehold household, int hhldIteration, int totalHouseholdIterations)
 {
     if (WriteThisIteration)
     {
         if (SelectedHouseholdZones.Contains(household.HomeZone.ZoneNumber))
         {
             var householdNumber = household.HouseholdId;
             lock (this)
             {
                 int personNumber = 0;
                 foreach (var person in household.Persons)
                 {
                     personNumber++;
                     var empZone        = person.EmploymentZone;
                     var employmentZone = empZone == null ? 0 : empZone.ZoneNumber;
                     if (SelectedEmploymentZones.Contains(employmentZone))
                     {
                         var tripNumber = 1;
                         foreach (var tripChain in person.TripChains)
                         {
                             foreach (var trip in tripChain.Trips)
                             {
                                 SaveTrip(trip, householdNumber, personNumber, tripNumber);
                                 tripNumber++;
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 12
0
        private void RemoveBadDestinationOrigins(Dictionary <int, int> badHouseholds, IDbCommand command)
        {
            Progress = 0;
            float total;

            command.CommandText = "SELECT COUNT(*) FROM [@TripsTable];";
            AddParameters(command, "@TripsTable", TripsTable, DbType.String);
            total = (int)command.ExecuteScalar();

            command.CommandText = "SELECT [@TripsTable].[@HhldColumn], [@TripsTable].[@OriginColumn], [@TripsTable].[@DesinationColumn] FROM [@TripsTable];";
            AddParameters(command, @"HhldColumn", HhldColumn, DbType.String);
            AddParameters(command, @"OriginColumn", OriginColumn, DbType.String);
            AddParameters(command, @"DesinationColumn", DesinationColumn, DbType.String);
            using (var reader = command.ExecuteReader())
            {
                int current = 0;
                while (reader.Read())
                {
                    var hhld = reader.GetInt32(0);
                    int orig = Read32(1, reader);
                    int dest = Read32(2, reader);

                    if (!(ZoneSystem.ZoneArray.ContainsIndex(orig) && ZoneSystem.ZoneArray.ContainsIndex(dest)) ||
                        (ExcludeExternalTrips & ExternalRanges.Contains(dest)))
                    {
                        AddBadHousehold(badHouseholds, hhld);
                    }
                    Progress = current++ / total;
                }
            }
            command.Parameters.Clear();
        }
Ejemplo n.º 13
0
        private void BuildData(IZone[] zones)
        {
            var temp = new int[zones.Length];

            Parallel.For(0, temp.Length, (int i) =>
            {
                var origin          = zones[i];
                int bestIndex       = 0;
                double bestDistance = double.MaxValue;
                for (int j = 0; j < zones.Length; j++)
                {
                    if (AccessStationZones.Contains(zones[j].ZoneNumber))
                    {
                        double dist;
                        if ((dist = Distance(origin, zones[j])) < bestDistance)
                        {
                            bestIndex    = j;
                            bestDistance = dist;
                        }
                    }
                }
                temp[i] = bestIndex;
            });
            ClosestStationIndex = temp;
        }
Ejemplo n.º 14
0
 public void HouseholdComplete(ITashaHousehold household, bool success)
 {
     if (WriteThisIteration)
     {
         if (SelectedHouseholdZones.Contains(household.HomeZone.ZoneNumber))
         {
             var householdNumber = household.HouseholdId;
             lock (this)
             {
                 int personNumber = 0;
                 foreach (var person in household.Persons)
                 {
                     personNumber++;
                     var empZone        = person.EmploymentZone;
                     var employmentZone = empZone == null ? 0 : empZone.ZoneNumber;
                     if (SelectedEmploymentZones.Contains(employmentZone))
                     {
                         foreach (var tripChain in person.TripChains)
                         {
                             var tripNumber         = 1;
                             var numberOfWorkTrips  = tripChain.Trips.Count(trip => trip.Purpose == Activity.PrimaryWork || trip.Purpose == Activity.SecondaryWork || trip.Purpose == Activity.WorkBasedBusiness);
                             var numberOfTripInTour = tripChain.Trips.Count;
                             foreach (var trip in tripChain.Trips)
                             {
                                 SaveTrip(trip, householdNumber, personNumber, tripNumber, numberOfWorkTrips, numberOfTripInTour);
                                 tripNumber++;
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 15
0
        public void Start()
        {
            var zoneSystem        = Root.ZoneSystem.ZoneArray;
            var zones             = zoneSystem.GetFlatData();
            var niApop            = NIAData.AcquireResource <SparseArray <float> >().GetFlatData();
            var employmentByZone  = EmploymentData.AcquireResource <SparseArray <float> >().GetFlatData();
            var autoTimes         = AutoTimeMatrix.AcquireResource <SparseTwinIndex <float> >().GetFlatData();
            var transitIVTT       = TransitIVTTMatrix.AcquireResource <SparseTwinIndex <float> >().GetFlatData();
            var totalTransitTimes = TotalTransitTimeMatrix.AcquireResource <SparseTwinIndex <float> >().GetFlatData();

            float[] zonePopulation = (from z in Root.ZoneSystem.ZoneArray.GetFlatData()
                                      select(float) z.Population).ToArray();

            float analyzedpopulationSum = (from z in Root.ZoneSystem.ZoneArray.GetFlatData()
                                           where PopZoneRange.Contains(z.ZoneNumber)
                                           select z.Population).Sum();

            float employmentSum = (from z in Root.ZoneSystem.ZoneArray.GetFlatData()
                                   where EmpZoneRange.Contains(z.ZoneNumber)
                                   select employmentByZone[zoneSystem.GetFlatIndex(z.ZoneNumber)]).Sum();

            float niAsum            = niApop.Sum();
            var   normalDenominator = 1.0f / (analyzedpopulationSum * employmentSum);
            var   niaDenominator    = 1.0f / (niAsum * employmentSum);

            using (StreamWriter writer = new StreamWriter(ResultsFile))
            {
                CalculateAccessibility(zones, employmentByZone, autoTimes, transitIVTT, totalTransitTimes, zonePopulation, false);
                writer.WriteLine("Analyzed Population Accessibility");
                WriteToFile(normalDenominator, writer);
                writer.WriteLine();

                AutoAccessibilityResults.Clear();
                TransitIVTTAccessibilityResults.Clear();
                TransitAccessibilityResults.Clear();

                CalculateAccessibility(zones, employmentByZone, autoTimes, transitIVTT, totalTransitTimes, niApop, true);
                writer.WriteLine("NIA Zone Accessibility");
                WriteToFile(niaDenominator, writer);

                AutoAccessibilityResults.Clear();
                TransitIVTTAccessibilityResults.Clear();
                TransitAccessibilityResults.Clear();
            }
        }
Ejemplo n.º 16
0
        public void Execute(ITashaHousehold household, int iteration)
        {
            // only run on the last iteration
            if (iteration == Root.TotalIterations - 1)
            {
                lock (this)
                {
                    float amountToAddPerTrip;

                    foreach (var person in household.Persons)
                    {
                        if (ExpandedTrips)
                        {
                            amountToAddPerTrip = person.ExpansionFactor;
                        }
                        else
                        {
                            amountToAddPerTrip = 1;
                        }

                        if (person.Age < MinAge)
                        {
                            continue;
                        }
                        foreach (var tripChain in person.TripChains)
                        {
                            foreach (var trip in tripChain.Trips)
                            {
                                IZone originalZone    = trip.OriginalZone;
                                IZone destinationZone = trip.DestinationZone;
                                if (OriginZones.Contains(originalZone.ZoneNumber) && DestinationZones.Contains(destinationZone.ZoneNumber))
                                {
                                    var tripStartTime = trip.TripStartTime;
                                    if (tripStartTime >= StartTime && tripStartTime < EndTime)
                                    {
                                        if (SaveOD)
                                        {
                                            var dictionary = ODPurposeDictionary[originalZone.ZoneNumber, destinationZone.ZoneNumber];
                                            if (dictionary == null)
                                            {
                                                ODPurposeDictionary[originalZone.ZoneNumber, destinationZone.ZoneNumber] = dictionary = new Dictionary <Activity, float>();
                                            }
                                            AddTripToDictionary(dictionary, amountToAddPerTrip, trip);
                                        }
                                        else
                                        {
                                            AddTripToDictionary(PurposeDictionary, amountToAddPerTrip, trip);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Call this to see if the given origin/destination should be accepted.
        /// </summary>
        /// <param name="origin">The zone the trip starts at</param>
        /// <param name="destination">The zone the trip ends at</param>
        /// <returns>If the zone pair should have the utility applied</returns>
        protected virtual bool IsContained(IZone origin, IZone destination)
        {
            if (this.InvalidRegions.Count > 0 &&
                (this.InvalidRegions.Contains(origin.RegionNumber) || this.InvalidRegions.Contains(destination.RegionNumber)))
            {
                return(false);
            }

            var containsOrigin      = ValidRegions.Contains(origin.RegionNumber);
            var containsDestination = ValidRegions.Contains(destination.RegionNumber);

            if (this.OriginDestinationSame)
            {
                if (origin.RegionNumber != destination.RegionNumber)
                {
                    return(false);
                }
                return(containsOrigin);
            }

            if (OriginDestinationDifferent && origin.RegionNumber == destination.RegionNumber)
            {
                return(false);
            }

            switch (this.CurrentContainsType)
            {
            case ContainsType.ExclusiveOr:
                return(containsOrigin ^ containsDestination);

            case ContainsType.One:
                return(containsOrigin | containsDestination);

            case ContainsType.Both:
                return(containsOrigin & containsDestination);

            case ContainsType.None:
                return(!(containsOrigin | containsDestination));

            default:
                throw new XTMFRuntimeException("Unknown Contains Type!");
            }
        }
Ejemplo n.º 18
0
        private bool[] CreateMask(RangeSet range)
        {
            var zones = Root.ZoneSystem.ZoneArray.GetFlatData();

            bool[] ret = new bool[zones.Length];
            for (int i = 0; i < zones.Length; i++)
            {
                ret[i] = range.Contains(zones[i].ZoneNumber);
            }
            return(ret);
        }
Ejemplo n.º 19
0
 private int GetGradeTier(int age)
 {
     if (ElementaryAges.Contains(age))
     {
         return(0);
     }
     else if (HighschoolAges.Contains(age))
     {
         return(1);
     }
     return(2);
 }
Ejemplo n.º 20
0
        private List <int> GetTargetedZones(IZone[] flatZones, bool[] flatData)
        {
            List <int> targetedZones = new List <int>(flatData.Length);

            for (int i = 0; i < flatZones.Length; i++)
            {
                if (TargetedZones.Contains(flatZones[i].ZoneNumber))
                {
                    targetedZones.Add(i);
                }
            }
            return(targetedZones);
        }
Ejemplo n.º 21
0
        internal static int[] GetStationZones(RangeSet stationRanges, float[] capacity, IZone[] zones)
        {
            List <int> validStationIndexes = new List <int>();

            for (int i = 0; i < zones.Length; i++)
            {
                if (capacity[i] > 0 && stationRanges.Contains(zones[i].ZoneNumber))
                {
                    validStationIndexes.Add(i);
                }
            }
            return(validStationIndexes.ToArray());
        }
Ejemplo n.º 22
0
        public bool IsContained(IPerson person)
        {
            var age = person.Age;

            // Convert the age into an age category
            if (!TryGetAgeCat(age, out age))
            {
                return(false);
            }
            int mobilityCategory;
            var cars = person.Household.Cars;

            mobilityCategory = cars + (person.DriversLicense ? 3 : 0);
            return((EmploymentStatusCategory.Contains(person.EmploymentStatus)) & (OccupationCategory.Contains(person.Occupation))
                   & (AgeCategoryRange.Contains(age)) & (Mobility.Contains(mobilityCategory)));
        }
Ejemplo n.º 23
0
        public void Execute(ITashaHousehold household, int iteration)
        {
            var householdZone = household.HomeZone.ZoneNumber;
            var persons       = household.Persons;

            lock (this)
            {
                for (int i = 0; i < persons.Length; i++)
                {
                    var schoolZone = persons[i].SchoolZone;
                    if (schoolZone != null && Ages.Contains(persons[i].Age))
                    {
                        ToSave.Add(new Assignment(householdZone, schoolZone.ZoneNumber, persons[i].ExpansionFactor));
                    }
                }
            }
        }
Ejemplo n.º 24
0
        public void LoadData()
        {
            var zoneSystem = Root.ZoneSystem;

            if (!zoneSystem.Loaded)
            {
                zoneSystem.LoadData();
            }
            var zones    = zoneSystem.ZoneArray.GetFlatData();
            var data     = zoneSystem.ZoneArray.CreateSimilarArray <float>();
            var flatData = data.GetFlatData();

            for (int i = 0; i < flatData.Length; i++)
            {
                flatData[i] = Range.Contains(zones[i].ZoneNumber) ? IfTrue : IfFalse;
            }
            Data = data;
        }
Ejemplo n.º 25
0
        public void Execute(ITashaHousehold household, int iteration)
        {
            // only run on the last iteration
            var homeZoneIndex = Root.ZoneSystem.ZoneArray.GetFlatIndex(household.HomeZone.ZoneNumber);

            if (iteration == Root.TotalIterations - 1)
            {
                foreach (var person in household.Persons)
                {
                    float amountToAddPerTrip = person.ExpansionFactor;
                    if (person.Age >= MinAge)
                    {
                        foreach (var tripChain in person.TripChains)
                        {
                            foreach (var trip in tripChain.Trips)
                            {
                                IZone originalZone    = trip.OriginalZone;
                                IZone destinationZone = trip.DestinationZone;
                                if (OriginZones.Contains(originalZone.ZoneNumber) && DestinationZones.Contains(destinationZone.ZoneNumber))
                                {
                                    var tripStartTime = TripStartTime ? trip.TripStartTime : trip.ActivityStartTime;
                                    if (trip.Mode != null)
                                    {
                                        if (tripStartTime >= StartTime && tripStartTime < EndTime)
                                        {
                                            lock (this)
                                            {
                                                AddTripToDictionary(PurposeDictionary, amountToAddPerTrip, trip, homeZoneIndex);
                                                AddToSummary(trip, SummaryTripCount, amountToAddPerTrip);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 26
0
        private int GetTimePeriod(Time tTime)
        {
            int iTime = tTime.Hours * 100 + tTime.Minutes;

            if (iTime > 2800)
            {
                throw new XTMFRuntimeException(this, "Cannot have a time of more than 28:00!");
            }

            if (MorningTimePeriod.Contains(iTime))
            {
                return(0);
            }
            if (MiddayTimePeriod.Contains(iTime))
            {
                return(1);
            }
            if (AfternoonTimePeriod.Contains(iTime))
            {
                return(2);
            }
            return(3);
        }
Ejemplo n.º 27
0
        /// <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)
                {
                    var oZone = trip.OriginalZone;
                    if (oZone != lastPlace)
                    {
                        // See if this could be a bike-share trip
                        if (BikeShareZones.Contains(oZone.ZoneNumber) && BikeShareZones.Contains(trip.DestinationZone.ZoneNumber))
                        {
                            lastMadeWithBike = false;
                            continue;
                        }
                        return(false);
                    }
                    lastPlace        = trip.DestinationZone;
                    lastMadeWithBike = true;
                    anyBike          = true;
                }
                else
                {
                    lastMadeWithBike = false;
                }
            }
            return(!anyBike | (firstWasBike & lastPlace == homeZone & lastMadeWithBike));
        }
Ejemplo n.º 28
0
 private bool IsExternal(IZone employmentZone)
 {
     return(employmentZone != null && ExternalZones.Contains(employmentZone.ZoneNumber));
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Test to see if the origin and destinations pass the test to be recorded
 /// </summary>
 /// <param name="originZoneNumber">The sparse zone number for the origin of the trip</param>
 /// <param name="destinationZoneNumber">The sparse zone number for the destination of the trip</param>
 /// <returns>True if it passes the spatial test, false otherwise.</returns>
 private bool TestZones(int originZoneNumber, int destinationZoneNumber)
 {
     return(And ? OriginZones.Contains(originZoneNumber) && DestinationZones.Contains(destinationZoneNumber)
         : OriginZones.Contains(originZoneNumber) || DestinationZones.Contains(destinationZoneNumber));
 }
Ejemplo n.º 30
0
        public void IncludeTally(float[][] currentTally)
        {
            var zoneSystem = Root.ZoneSystem.ZoneArray;
            var zones      = zoneSystem.GetFlatData();

            BuildData(zones);
            var tripChains     = BaseYearTrips.AcquireResource <List <ITripChain> >();
            var basePopulation = BaseYearPopulation.AcquireResource <SparseArray <float> >().GetFlatData();
            var ratio          = new float[zones.Length];

            for (int i = 0; i < ratio.Length; i++)
            {
                ratio[i] = zones[i].Population / basePopulation[i];
                if (float.IsInfinity(ratio[i]) | float.IsNaN(ratio[i]))
                {
                    ratio[i] = 1;
                }
            }
            // Use the current tally if we don't care to save the results.
            // Otherwise we should create a replica so we can save those results then
            // recombine them at the end
            var tallyToUse = currentTally;

            if (SaveResults != null)
            {
                tallyToUse = new float[currentTally.Length][];
                for (int i = 0; i < tallyToUse.Length; i++)
                {
                    tallyToUse[i] = new float[currentTally[i].Length];
                }
            }
            for (int i = 0; i < tripChains.Count; i++)
            {
                var chain = tripChains[i];
                if ((chain.StartTime >= StartTime) | (chain.EndTime < EndTime))
                {
                    var person          = chain.Person;
                    var homeZone        = zoneSystem.GetFlatIndex(person.Household.HomeZone.ZoneNumber);
                    var expansionFactor = person.ExpansionFactor * ratio[homeZone];
                    foreach (var trip in chain.Trips)
                    {
                        var tripStart = trip.TripStartTime;
                        if ((tripStart >= StartTime) & (tripStart < EndTime))
                        {
                            var originZone           = trip.OriginalZone.ZoneNumber;
                            var destinationZone      = trip.DestinationZone.ZoneNumber;
                            var originZoneIndex      = zoneSystem.GetFlatIndex(originZone);
                            var destinationZoneIndex = zoneSystem.GetFlatIndex(destinationZone);
                            if (ImplementExternalTransit && ExternalTransit.Contains(trip.Mode))
                            {
                                var originExternal      = ExternalZones.Contains(originZone);
                                var destinationExternal = ExternalZones.Contains(destinationZone);
                                if (originExternal && destinationExternal)
                                {
                                    // if the transit trip is external ignore it since we don't model the service
                                    continue;
                                }
                                else
                                {
                                    if (RecordAccess)
                                    {
                                        // if we are recording the auto side of the trip
                                        if (originExternal)
                                        {
                                            tallyToUse[originZoneIndex][ClosestStationIndex[destinationZoneIndex]] += expansionFactor;
                                        }
                                        else if (destinationExternal)
                                        {
                                            tallyToUse[ClosestStationIndex[originZoneIndex]][destinationZoneIndex] += expansionFactor;
                                        }
                                        else
                                        {
                                            tallyToUse[originZoneIndex][destinationZoneIndex] += expansionFactor;
                                        }
                                    }
                                    else
                                    {
                                        // if we are recording the transit side of the trip
                                        if (originExternal)
                                        {
                                            tallyToUse[ClosestStationIndex[originZoneIndex]][destinationZoneIndex] += expansionFactor;
                                        }
                                        else if (destinationExternal)
                                        {
                                            tallyToUse[originZoneIndex][ClosestStationIndex[destinationZoneIndex]] += expansionFactor;
                                        }
                                        else
                                        {
                                            tallyToUse[originZoneIndex][destinationZoneIndex] += expansionFactor;
                                        }
                                    }
                                }
                            }
                            else if (trip.Mode == PrimaryMode)
                            {
                                tallyToUse[originZoneIndex][destinationZoneIndex] += expansionFactor;
                            }
                        }
                    }
                }
            }
            if (SaveResults != null)
            {
                // save the results then combine them into the current tally
                SaveData.SaveMatrix(zoneSystem.GetFlatData(), tallyToUse, SaveResults);
                // now that the data is saved we need to recombine the data
                for (int i = 0; i < tallyToUse.Length; i++)
                {
                    VectorHelper.Add(currentTally[i], 0, currentTally[i], 0, tallyToUse[i], 0, tallyToUse[i].Length);
                }
            }
        }
Ejemplo n.º 31
0
 private static bool IsContained(IZone zone, RangeSet validRanges, bool contained, bool useZoneRegion, bool useZonePlanningDistrict)
 {
     if ( useZoneRegion )
     {
         return ( !contained ) ^ validRanges.Contains( zone.RegionNumber );
     }
     else if ( useZonePlanningDistrict )
     {
         return ( !contained ) ^ validRanges.Contains( zone.PlanningDistrict );
     }
     else
     {
         return ( !contained ) ^ validRanges.Contains( zone.ZoneNumber );
     }
 }
Ejemplo n.º 32
0
            private void CalculateUtilities(RangeSet stationRanges, RangeSet spatialZones, float[] capacity, int[] closestStation)
            {
                INetworkData autoNetwork = GetNetwork(AutoNetworkName);
                ITripComponentData transitNetwork = GetNetwork(TransitNetworkName) as ITripComponentData;
                EnsureNetworks(autoNetwork, transitNetwork);
                var zoneArray = Root.ZoneSystem.ZoneArray;
                IZone[] zones = zoneArray.GetFlatData();
                int[] stationZones = GetStationZones(stationRanges, capacity, zones);
                var flatCapacityFactor = CapacityFactor.GetFlatData();
                if(AutoFromOriginToAccessStation == null || TransitFromAccessStationToDestination.Length != stationZones.Length * zones.Length)
                {
                    TransitFromAccessStationToDestination = new float[stationZones.Length * zones.Length];
                    AutoFromOriginToAccessStation = new float[stationZones.Length * zones.Length];
                    TransitFromDestinationToAccessStation = new float[stationZones.Length * zones.Length];
                    AutoFromAccessStationToDestination = new float[stationZones.Length * zones.Length];
                }
                // compute the toAccess utilities
                Parallel.For(0, zones.Length, (int originIndex) =>
                {
                    var zoneNumber = zones[originIndex].ZoneNumber;
                    if(spatialZones.Contains(zoneNumber))
                    {
                        for(int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to access station this will include more factors
                            AutoFromOriginToAccessStation[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, originIndex, accessIndex)
                                + (Capacity * capacity[accessIndex]
                                + ParkingCost * zones[accessIndex].ParkingCost
                                + (closestStation[originIndex] == accessIndex ? ClosestStationFactor : 0))) * factor;
                            // calculate egress' from access station
                            AutoFromAccessStationToDestination[originIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(autoNetwork, accessIndex, originIndex)) * factor;
                        }
                    }
                });

                // compute the toDesinstination utilities
                Parallel.For(0, zones.Length, (int destIndex) =>
                {
                    var zoneNumber = zones[destIndex].ZoneNumber;
                    if(spatialZones.Contains(zoneNumber))
                    {
                        for(int i = 0; i < stationZones.Length; i++)
                        {
                            var accessIndex = stationZones[i];
                            var factor = (float)Math.Pow(flatCapacityFactor[accessIndex], CapacityFactorExp);
                            // calculate access' to destination
                            TransitFromAccessStationToDestination[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, accessIndex, destIndex)) * factor;
                            // calculate egress' to access station
                            TransitFromDestinationToAccessStation[destIndex * stationZones.Length + i] = (float)Math.Exp(ComputeUtility(transitNetwork, destIndex, accessIndex)) * factor;
                        }
                    }
                });
            }
Ejemplo n.º 33
0
 internal static int[] GetStationZones(RangeSet stationRanges, float[] capacity, IZone[] zones)
 {
     List<int> validStationIndexes = new List<int>();
     for (int i = 0; i < zones.Length; i++)
     {
         if (capacity[i] > 0 && stationRanges.Contains(zones[i].ZoneNumber))
         {
             validStationIndexes.Add(i);
         }
     }
     return validStationIndexes.ToArray();
 }