예제 #1
0
 private float[] GetRowTotalsFromResource(IResource resource)
 {
     float[] totalByRow;
     if (resource.CheckResourceType <SparseArray <float> >())
     {
         totalByRow = resource.AquireResource <SparseArray <float> >().GetFlatData();
     }
     else
     {
         var matrix = resource.AquireResource <SparseTwinIndex <float> >().GetFlatData();
         totalByRow = new float[matrix.Length];
         if (VectorHelper.IsHardwareAccelerated)
         {
             for (int i = 0; i < totalByRow.Length; i++)
             {
                 totalByRow[i] = VectorHelper.Sum(matrix[i], 0, matrix[i].Length);
             }
         }
         else
         {
             for (int i = 0; i < totalByRow.Length; i++)
             {
                 var total = 0.0f;
                 for (int j = 0; j < matrix[i].Length; j++)
                 {
                     total += matrix[i][j];
                 }
                 totalByRow[i] = total;
             }
         }
     }
     return(totalByRow);
 }
예제 #2
0
 /// <summary>
 /// Get the log of employment per zone.
 /// </summary>
 /// <returns>Log of Employment by zone</returns>
 private float[] GetEmploymentData()
 {
     if (EmploymentHasLogApplied)
     {
         return(Employment.AquireResource <SparseArray <float> >().GetFlatData().Select(x => x).ToArray());
     }
     else
     {
         return(Employment.AquireResource <SparseArray <float> >().GetFlatData().Select(x => (float)Math.Log(x + 1)).ToArray());
     }
 }
예제 #3
0
        public void IterationStarting(int iterationNumber, int maxIterations)
        {
            var zoneSystem = Root.ZoneSystem;

            ZoneArray = zoneSystem.ZoneArray;
            // We do this here instead of the RuntimeValidation so that we don't run into issues with estimation
            for (int i = 0; i < TimePeriodConstants.Length; i++)
            {
                TimePeriodConstants[i].BuildMatrix();
            }
            ZonalDensityForActivitiesArray = ZonalDensityForActivities.AquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
            ZonalDensityForHomeArray       = ZonalDensityForHome.AquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
            for (int i = 0; i < ZonalDensityForActivitiesArray.Length; i++)
            {
                ZonalDensityForActivitiesArray[i] *= ToActivityDensityFactor;
                ZonalDensityForHomeArray[i]       *= ToHomeDensityFactor;
            }

            ProfessionalCost     = ConvertCostFactor(ProfessionalCostFactor, ProfessionalTimeFactor);
            GeneralCost          = ConvertCostFactor(GeneralCostFactor, GeneralTimeFactor);
            SalesCost            = ConvertCostFactor(SalesCostFactor, SalesTimeFactor);
            ManufacturingCost    = ConvertCostFactor(ManufacturingCostFactor, ManufacturingTimeFactor);
            StudentCost          = ConvertCostFactor(StudentCostFactor, StudentTimeFactor);
            NonWorkerStudentCost = ConvertCostFactor(NonWorkerStudentCostFactor, NonWorkerStudentTimeFactor);
        }
예제 #4
0
        public void Start()
        {
            SparseArray <float> data;

            data = Data.AquireResource <SparseArray <float> >();
            TMG.Functions.SaveData.SaveVector(data, OutputTo.GetFilePath());
        }
예제 #5
0
        private void StoreProductionData(SparseArray <float> production)
        {
            var age            = AgeCategoryRange[0].Start;
            var mob            = Mobility[0].Start;
            var emp            = EmploymentStatusCategory[0].Start;
            var data           = WorkerData.AquireResource <SparseArray <SparseTriIndex <float> > >();
            var flatData       = data.GetFlatData();
            var test           = flatData[0];
            var flatProduction = production.GetFlatData();

            if (!test.GetFlatIndex(ref emp, ref mob, ref age))
            {
                throw new XTMFRuntimeException("In " + Name + " we were unable to find a place to store our data (" + emp + "," + mob + "," + age + ")");
            }
            int i = 0;

            try
            {
                for (; i < flatProduction.Length; i++)
                {
                    flatData[i].GetFlatData()[emp][mob][age] = flatProduction[i];
                }
            }
            catch
            {
                throw new XTMFRuntimeException("Failed Yo!");
            }
        }
        private float ComputeFitness()
        {
            var fitness = 0.0;
            var auto    = AutoProbabilites.AquireResource <SparseArray <float> >().GetFlatData();

            // we only need the auto probabilities since auto + transit = 1 always.
            for (int i = 0; i < AutoProbabilities.Length; i++)
            {
                var observedTrips = TotalTrips[i];
                var pModel        = auto[i];
                var pTruth        = AutoProbabilities[i];
                if (observedTrips > 0 & pTruth > 0)
                {
                    double cellError;
                    if (pModel > pTruth)
                    {
                        // y - deltaXY <=> 2y-x
                        cellError = pTruth * Math.Log(Math.Min((Math.Max((pTruth + pTruth - pModel), 0)
                                                                + (pTruth * 0.0015)) / (pTruth * 1.0015), 1));
                    }
                    else
                    {
                        cellError = pTruth * Math.Log(Math.Min((pModel + (pTruth * 0.0015)) / (pTruth * 1.0015), 1));
                    }
                    fitness += cellError;
                }
            }
            return((float)fitness);
        }
예제 #7
0
 public void IterationStarting(int iterationNumber, int maxIterations)
 {
     if (!AccessStationChoiceLoaded | UnloadAccessStationModelEachIteration)
     {
         AccessStationModel.Load();
         AccessStationChoiceLoaded = true;
     }
     // We do this here instead of the RuntimeValidation so that we don't run into issues with estimation
     AgeUtilLookup = new float[16];
     for (int i = 0; i < AgeUtilLookup.Length; i++)
     {
         AgeUtilLookup[i] = (float)Math.Log(i + 1, Math.E) * LogOfAgeFactor;
     }
     //build the region constants
     for (int i = 0; i < TimePeriodConstants.Length; i++)
     {
         TimePeriodConstants[i].BuildMatrix();
     }
     ZonalDensityForActivitiesArray = ZonalDensityForActivities.AquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
     ZonalDensityForHomeArray       = ZonalDensityForHome.AquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
     for (int i = 0; i < ZonalDensityForActivitiesArray.Length; i++)
     {
         ZonalDensityForActivitiesArray[i] *= ToActivityDensityFactor;
         ZonalDensityForHomeArray[i]       *= ToHomeDensityFactor;
     }
 }
예제 #8
0
        public void LoadData()
        {
            var resource    = ResourceToMultiply.AquireResource <SparseTwinIndex <float> >();
            var otherData   = resource.GetFlatData();
            var ourResource = resource.CreateSimilarArray <float>();
            var data        = ourResource.GetFlatData();

            if (VectorHelper.IsHardwareAccelerated)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    VectorHelper.Multiply(data[i], 0, otherData[i], 0, Factor, data[i].Length);
                }
            }
            else
            {
                for (int i = 0; i < data.Length; i++)
                {
                    var row      = data[i];
                    var otherRow = otherData[i];
                    for (int j = 0; j < row.Length; j++)
                    {
                        row[j] = otherRow[j] * Factor;
                    }
                }
            }
            Data   = ourResource;
            Loaded = true;
        }
예제 #9
0
파일: SumOD.cs 프로젝트: dianatle/XTMF
 public void LoadData()
 {
     float[][] operateOnMe;
     if (RawDataSource != null)
     {
         RawDataSource.LoadData();
         operateOnMe = RawDataSource.GiveData().GetFlatData();
         RawDataSource.UnloadData();
     }
     else
     {
         operateOnMe = ResourceDataSource.AquireResource <SparseTwinIndex <float> >().GetFlatData();
     }
     if (VectorHelper.IsHardwareAccelerated)
     {
         var sum = 0.0f;
         for (int i = 0; i < operateOnMe.Length; i++)
         {
             sum += VectorHelper.Sum(operateOnMe[i], 0, operateOnMe[i].Length);
         }
         Data = sum;
     }
     else
     {
         Data = operateOnMe.Sum(row => row.Sum());
     }
     Loaded = true;
 }
예제 #10
0
        public void Start()
        {
            string[]     aggregationHeaders;
            List <int>[] zoneToAggregationMap;
            LoadMapping(out aggregationHeaders, out zoneToAggregationMap);
            var data    = AnalysisTarget.AquireResource <SparseTwinIndex <float> >();
            var aggData = AggregationToApply.ApplyAggregation(data.GetFlatData(), zoneToAggregationMap, aggregationHeaders);

            SaveData(aggData, aggregationHeaders);
        }
예제 #11
0
파일: AverageOD.cs 프로젝트: dianatle/XTMF
        private SparseTwinIndex <float> GetMatrix(IResource resource, IDataSource <SparseTwinIndex <float> > dataSource)
        {
            if (resource != null)
            {
                return(resource.AquireResource <SparseTwinIndex <float> >());
            }
            dataSource.LoadData();
            var ret = dataSource.GiveData();

            dataSource.UnloadData();
            return(ret);
        }
예제 #12
0
        public void Start()
        {
            var matrix = ODMatrix.AquireResource <SparseTwinIndex <float> >();

            if (ThirdNormalized)
            {
                TMG.Functions.SaveData.SaveMatrixThirdNormalized(matrix, SaveLocation);
            }
            else
            {
                TMG.Functions.SaveData.SaveMatrix(matrix, SaveLocation);
            }
        }
예제 #13
0
파일: SCurve.cs 프로젝트: dianatle/XTMF
        public void LoadData()
        {
            var original = OriginalData.AquireResource <SparseArray <float> >();
            var oData    = original.GetFlatData();
            var ours     = original.CreateSimilarArray <float>();
            var ourData  = ours.GetFlatData();

            for (int i = 0; i < oData.Length; i++)
            {
                ourData[i] = 1.0f / (1.0f + (float)Math.Exp(Stretch * -(oData[i] + OffsetX)));
            }
            Data   = ours;
            Loaded = true;
        }
예제 #14
0
        public void Load()
        {
            // Get our resources
            ElementarySchoolProbabilities = ElementarySchoolProbabilitiesResource.AquireResource <SparseTwinIndex <float> >();
            HighSchoolProbabilities       = HighschoolProbabilitiesResource.AquireResource <SparseTwinIndex <float> >();
            UniversityProbabilities       = UniversityProbabilitiesResource.AquireResource <SparseTwinIndex <float> >();

            // create replicated versions for our per iteration needs
            CurrentElementarySchoolProbabilities = Replicate(ElementarySchoolProbabilities);
            CurrentHighSchoolProbabilities       = Replicate(HighSchoolProbabilities);
            CurrentUniversityProbabilities       = Replicate(UniversityProbabilities);

            // Gather the zone system for use from the root module.
            Zones = Root.ZoneSystem.ZoneArray;
        }
        public void LoadData()
        {
            var employmentForZone   = EmploymentByZoneForOccEmpStat.AquireResource <SparseArray <float> >().GetFlatData();
            var workAtHomeRates     = WorkAtHomeRateByZoneForOccEmpStat.AquireResource <SparseArray <float> >().GetFlatData();
            var externalWorkerRates = ExternalWorkerRateByZoneForByOccEmpStat.AquireResource <SparseArray <float> >().GetFlatData();
            var data = Root.ZoneSystem.ZoneArray.CreateSimilarArray <float>();
            var flat = data.GetFlatData();

            for (int i = 0; i < flat.Length; i++)
            {
                var postWaHEmployment = employmentForZone[i] * (1.0f - workAtHomeRates[i]);
                flat[i] = postWaHEmployment * (1.0f - externalWorkerRates[i]);
            }
            Data = data;
        }
예제 #16
0
        public void Execute()
        {
            var sparse = ToOutput.AquireResource <SparseArray <float> >();
            var data   = sparse.GetFlatData();

            using (StreamWriter writer = new StreamWriter(OutputFile))
            {
                writer.WriteLine("SparseIndex,Value");
                for (int i = 0; i < data.Length; i++)
                {
                    writer.Write(sparse.GetSparseIndex(i));
                    writer.Write(',');
                    writer.WriteLine(data[i]);
                }
            }
        }
예제 #17
0
 public void Load()
 {
     ZoneSystem = Root.ZoneSystem.ZoneArray;
     Zones      = ZoneSystem.GetFlatData();
     if (SaveWorkerCategory != null)
     {
         WorkerResults = new float[3][];
         for (int i = 0; i < WorkerResults.Length; i++)
         {
             WorkerResults[i] = new float[Zones.Length];
         }
     }
     Probabilities = Linkages.AquireResource <SparseTriIndex <float> >();
     ConvertToProbabilities(Probabilities.GetFlatData());
     Linkages.ReleaseResource();
 }
예제 #18
0
        public void LoadData()
        {
            // Get totals by row
            var totalToNormalizeTo = GetRowTotalsFromResource(DataToNormalizeTo);
            var inputMatrix        = DataToNormalize.AquireResource <SparseTwinIndex <float> >();
            var ourMatrix          = inputMatrix.GetFlatData();
            var ourTotalByRow      = GetRowTotalsFromResource(DataToNormalize);

            // create inverse
            if (VectorHelper.IsHardwareAccelerated)
            {
                VectorHelper.Divide(ourTotalByRow, 0, totalToNormalizeTo, 0, ourTotalByRow, 0, ourTotalByRow.Length);
            }
            else
            {
                for (int i = 0; i < ourTotalByRow.Length; i++)
                {
                    ourTotalByRow[i] = totalToNormalizeTo[i] / ourTotalByRow[i];
                }
            }
            // apply inverse
            var data     = inputMatrix.CreateSimilarArray <float>();
            var flatData = data.GetFlatData();

            for (int i = 0; i < ourTotalByRow.Length; i++)
            {
                // if it is infinity or NAN, that means that we had zero elements
                // thusly we can just leave the matrix alone to its default value of zero.
                if (!(float.IsInfinity(ourTotalByRow[i]) || float.IsNaN(ourTotalByRow[i])))
                {
                    if (VectorHelper.IsHardwareAccelerated)
                    {
                        VectorHelper.Multiply(flatData[i], 0, ourMatrix[i], 0, ourTotalByRow[i], flatData[i].Length);
                    }
                    else
                    {
                        var dataRow = flatData[i];
                        for (int j = 0; j < ourMatrix[i].Length; j++)
                        {
                            dataRow[j] = ourMatrix[i][j] * ourTotalByRow[i];
                        }
                    }
                }
            }
            Data   = data;
            Loaded = true;
        }
예제 #19
0
        public void Start()
        {
            var zoneSystem        = Root.ZoneSystem.ZoneArray;
            var zones             = zoneSystem.GetFlatData();
            var NIApop            = NIAData.AquireResource <SparseArray <float> >().GetFlatData();
            var employmentByZone  = EmploymentData.AquireResource <SparseArray <float> >().GetFlatData();
            var AutoTimes         = AutoTimeMatrix.AquireResource <SparseTwinIndex <float> >().GetFlatData();
            var TransitIVTT       = TransitIVTTMatrix.AquireResource <SparseTwinIndex <float> >().GetFlatData();
            var TotalTransitTimes = TotalTransitTimeMatrix.AquireResource <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();
            }
        }
예제 #20
0
        public IEnumerable <ODData <float> > Read()
        {
            var            firstSparse = First.AquireResource <SparseTwinIndex <float> >();
            var            first       = firstSparse.GetFlatData();
            var            second      = Second.AquireResource <SparseTwinIndex <float> >().GetFlatData();
            ODData <float> point       = new ODData <float>();

            for (int i = 0; i < first.Length; i++)
            {
                point.O = firstSparse.GetSparseIndex(i);
                for (int j = 0; j < first[i].Length; j++)
                {
                    point.D    = firstSparse.GetSparseIndex(i, j);
                    point.Data = first[i][j] - second[i][j];
                    yield return(point);
                }
            }
        }
예제 #21
0
        public void IterationStarting(int iterationNumber, int maxIterations)
        {
            if (iterationNumber == 0)
            {
                StationIndexLookup = null;
            }
            if (!AccessStationChoiceLoaded | UnloadAccessStationModelEachIteration)
            {
                AccessStationModel.Load();
                AccessStationChoiceLoaded = true;
            }
            // We do this here instead of the RuntimeValidation so that we don't run into issues with estimation
            if (AgeUtilLookup == null)
            {
                AgeUtilLookup = new float[16];
            }
            for (int i = 0; i < AgeUtilLookup.Length; i++)
            {
                AgeUtilLookup[i] = (float)Math.Log(i + 1, Math.E) * LogOfAgeFactor;
            }
            //build the region constants
            for (int i = 0; i < TimePeriodConstants.Length; i++)
            {
                TimePeriodConstants[i].BuildMatrix();
            }


            ProfessionalCost     = ConvertCostFactor(ProfessionalCostFactor, ProfessionalTimeFactor);
            GeneralCost          = ConvertCostFactor(GeneralCostFactor, GeneralTimeFactor);
            SalesCost            = ConvertCostFactor(SalesCostFactor, SalesTimeFactor);
            ManufacturingCost    = ConvertCostFactor(ManufacturingCostFactor, ManufacturingTimeFactor);
            StudentCost          = ConvertCostFactor(StudentCostFactor, StudentTimeFactor);
            NonWorkerStudentCost = ConvertCostFactor(NonWorkerStudentCostFactor, NonWorkerStudentTimeFactor);


            ZonalDensityForActivitiesArray = ZonalDensityForActivities.AquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
            ZonalDensityForHomeArray       = ZonalDensityForHome.AquireResource <SparseArray <float> >().GetFlatData().Clone() as float[];
            for (int i = 0; i < ZonalDensityForActivitiesArray.Length; i++)
            {
                ZonalDensityForActivitiesArray[i] *= ToActivityDensityFactor;
                ZonalDensityForHomeArray[i]       *= ToHomeDensityFactor;
            }
        }
예제 #22
0
        public void LoadData()
        {
            var zoneArray  = Root.ZoneSystem.ZoneArray;
            var zones      = zoneArray.GetFlatData();
            var firstRate  = FirstRateToApply.AquireResource <SparseTwinIndex <float> >().GetFlatData();
            var secondRate = SecondRateToApply.AquireResource <SparseTwinIndex <float> >().GetFlatData();
            SparseTwinIndex <float> data;

            data = zoneArray.CreateSquareTwinArray <float>();
            var flatData     = data.GetFlatData();
            var dereferenced = AdditionalRates.Select(a => a.AquireResource <SparseTwinIndex <float> >().GetFlatData()).ToArray();

            if (VectorHelper.IsHardwareAccelerated)
            {
                for (int i = 0; i < flatData.Length; i++)
                {
                    VectorHelper.Add(flatData[i], 0, firstRate[i], 0, secondRate[i], 0, flatData[i].Length);
                    for (int j = 0; j < dereferenced.Length; j++)
                    {
                        VectorHelper.Add(flatData[i], 0, flatData[i], 0, dereferenced[j][i], 0, flatData[i].Length);
                    }
                }
            }
            else
            {
                for (int i = 0; i < flatData.Length; i++)
                {
                    for (int k = 0; k < flatData[i].Length; k++)
                    {
                        flatData[i][k] = firstRate[i][k] + secondRate[i][k];
                    }
                    for (int j = 0; j < dereferenced.Length; j++)
                    {
                        for (int k = 0; k < flatData[i].Length; k++)
                        {
                            flatData[i][k] = flatData[i][k] + dereferenced[j][i][k];
                        }
                    }
                }
            }
            Data = data;
        }
예제 #23
0
        public void LoadData()
        {
            var resource    = ResourceToMultiply.AquireResource <SparseArray <float> >();
            var otherData   = resource.GetFlatData();
            var ourResource = resource.CreateSimilarArray <float>();
            var data        = ourResource.GetFlatData();

            if (TMG.Functions.VectorHelper.IsHardwareAccelerated)
            {
                TMG.Functions.VectorHelper.Multiply(data, 0, otherData, 0, Factor, data.Length);
            }
            else
            {
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = otherData[i] * Factor;
                }
            }
            Data   = ourResource;
            Loaded = true;
        }
예제 #24
0
        public void IncludeTally(float[][] currentTally)
        {
            var modeProbability      = ModeProbabilityArray.AquireResource <SparseArray <float> >().GetFlatData();
            var distribution         = DistributionProbabilityArray.AquireResource <SparseArray <float> >().GetFlatData();
            var airportZone          = Root.ZoneSystem.ZoneArray.GetFlatIndex(PearsonZone);
            var effectiveEmplainings = Emplainings * (1.0f / PassengersPerTrip);
            var effectiveDeplainings = Deplainings * (1.0f / PassengersPerTrip);

            for (int i = 0; i < distribution.Length; i++)
            {
                if (distribution[i] > 0)
                {
                    var probability = modeProbability[i] * distribution[i];
                    if (float.IsInfinity(probability) | float.IsNaN(probability))
                    {
                        throw new XTMFRuntimeException("In '" + Name + "' when trying to produce the probability of a zone being selected we found an invalid probability!\r\n"
                                                       + "at index = " + i + " the modeProbability was " + modeProbability[i] + " and the distribution was " + distribution[i]);
                    }
                    currentTally[i][airportZone] += probability * effectiveEmplainings;
                    currentTally[airportZone][i] += probability * effectiveDeplainings;
                }
            }
        }
예제 #25
0
파일: ZoneODMask.cs 프로젝트: dianatle/XTMF
        private SparseTwinIndex <float> GetToMaskData()
        {
            SparseTwinIndex <float> toMask;

            if (BaseDataDataSource != null)
            {
                var needToLoad = !BaseDataDataSource.Loaded;
                if (needToLoad)
                {
                    BaseDataDataSource.LoadData();
                }
                toMask = BaseDataDataSource.GiveData();
                if (needToLoad)
                {
                    BaseDataDataSource.UnloadData();
                }
            }
            else
            {
                toMask = BaseDataResource.AquireResource <SparseTwinIndex <float> >();
            }

            return(toMask);
        }
예제 #26
0
            public override float[][] ApplyAggregation(float[][] data, List <int>[] zoneToHeaderMap, string[] headers)
            {
                var ret       = BuildData(headers);
                var weightSum = BuildData(headers);
                var weights   = WeightedValuesByZone.AquireResource <SparseTwinIndex <float> >().GetFlatData();

                // build totals
                for (int i = 0; i < zoneToHeaderMap.Length; i++)
                {
                    var iReferencedZones = zoneToHeaderMap[i];
                    if (iReferencedZones != null)
                    {
                        for (int j = 0; j < zoneToHeaderMap.Length; j++)
                        {
                            var jReferencedZones = zoneToHeaderMap[j];
                            if (jReferencedZones != null)
                            {
                                for (int o = 0; o < iReferencedZones.Count; o++)
                                {
                                    for (int d = 0; d < jReferencedZones.Count; d++)
                                    {
                                        ret[iReferencedZones[o]][jReferencedZones[d]]       += data[i][j] * weights[i][j];
                                        weightSum[iReferencedZones[o]][jReferencedZones[d]] += weights[i][j];
                                    }
                                }
                            }
                        }
                    }
                }
                // average
                for (int i = 0; i < ret.Length; i++)
                {
                    for (int j = 0; j < ret[i].Length; j++)
                    {
                        if (weightSum[i][j] > 0)
                        {
                            ret[i][j] /= weightSum[i][j];
                        }
                        else
                        {
                            var totalTime = 0.0f;
                            var indexes   = 0;
                            // if there are no trips coming to this agg OD cell we need to generate something still.
                            for (int o = 0; o < zoneToHeaderMap.Length; o++)
                            {
                                if (zoneToHeaderMap[o] != null && zoneToHeaderMap[o].Contains(i))
                                {
                                    for (int d = 0; d < zoneToHeaderMap.Length; d++)
                                    {
                                        if (zoneToHeaderMap[d] != null && zoneToHeaderMap[d].Contains(j))
                                        {
                                            totalTime += data[o][d];
                                            indexes++;
                                        }
                                    }
                                }
                            }
                            ret[i][j] = totalTime / (float)indexes;
                        }
                    }
                }
                return(ret);
            }
예제 #27
0
 public float ReturnData()
 {
     return(SummationOfResult.AquireResource <float>());
 }
예제 #28
0
        public void Execute(int iterationNumber, int totalIterations)
        {
            if (Writer == null)
            {
                Writer = new StreamWriter(ReportFile);
                switch (AnalysisToRun)
                {
                case AnalysisType.Average:
                    Writer.Write("Iteration,Average");
                    break;

                case AnalysisType.Max:
                    Writer.Write("Iteration,Max");
                    break;
                }
                Writer.WriteLine(SumFirst ? ",SumOfFirst" : "");
            }
            var   first  = FirstMatrix.AquireResource <SparseTwinIndex <float> >().GetFlatData();
            var   second = SecondMatrix.AquireResource <SparseTwinIndex <float> >().GetFlatData();
            float value  = 0.0f;

            switch (AnalysisToRun)
            {
            case AnalysisType.Average:
                value = GetAverage(first, second);
                break;

            case AnalysisType.Max:
                value = GetMax(first, second);
                break;
            }
            Writer.Write(iterationNumber + 1);
            Writer.Write(',');
            Writer.Write(value);
            if (SumFirst)
            {
                var sum = 0.0f;
                for (int i = 0; i < first.Length; i++)
                {
                    if (VectorHelper.IsHardwareAccelerated)
                    {
                        sum += VectorHelper.Sum(first[i], 0, first[i].Length);
                    }
                    else
                    {
                        for (int j = 0; j < first[i].Length; j++)
                        {
                            sum += first[i][j];
                        }
                    }
                }
                Writer.Write(',');
                Writer.Write(sum);
            }
            Writer.WriteLine();
            // if this is the last iteration dispose
            if (iterationNumber >= totalIterations - 1)
            {
                Writer.Dispose();
                Writer = null;
            }
        }
예제 #29
0
        public void LoadData()
        {
            var zoneArray = Root.ZoneSystem.ZoneArray;
            var zones     = zoneArray.GetFlatData();
            var data      = LocalData;

            if (data == null)
            {
                LocalData = data = new float[zones.Length * zones.Length * NumberOfWorkerCategories];
            }
            var distances = Root.ZoneSystem.Distances.GetFlatData();
            var pds       = PlanningDistricts;

            if (pds == null)
            {
                PlanningDistricts = pds = zones.Select((zone) => zone.PlanningDistrict).ToArray();
            }
            if (KeepLocalData)
            {
                CreateHighPerformanceLookup(zoneArray);
            }
            float[] workerSplits             = LoadWorkerCategories(zones, zoneArray);
            SparseTwinIndex <float> kFactors = null;

            if (KFactors != null)
            {
                kFactors = KFactors.AquireResource <SparseTwinIndex <float> >();
                Parallel.For(0, zones.Length, new ParallelOptions()
                {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                }, (int i) =>
                {
                    var distanceRow = distances[i];
                    var iPD         = pds[i];
                    for (int k = 0; k < NumberOfWorkerCategories; k++)
                    {
                        int offset = k * zones.Length * zones.Length + i * zones.Length;
                        for (int j = 0; j < zones.Length; j++)
                        {
                            // use distance in km
                            data[offset + j] = kFactors[iPD, pds[j]] * CalculateUtilityToE(iPD, pds[j], i, j, k, distanceRow[j] * 0.001f);
                        }
                    }
                });
                KFactors.ReleaseResource();
            }
            else
            {
                Parallel.For(0, zones.Length, new ParallelOptions()
                {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                }, (int i) =>
                {
                    var distanceRow = distances[i];
                    var iPD         = pds[i];
                    for (int k = 0; k < NumberOfWorkerCategories; k++)
                    {
                        int offset = k * zones.Length * zones.Length + i * zones.Length;
                        for (int j = 0; j < zones.Length; j++)
                        {
                            // use distance in km
                            data[offset + j] = CalculateUtilityToE(iPD, pds[j], i, j, k, distanceRow[j] * 0.001f);
                        }
                    }
                });
            }

            SparseArray <float> employmentSeekers = EmployedPopulationResidenceByZone.AquireResource <SparseArray <float> >();
            var jobs    = CreateNormalizedJobs(employmentSeekers, JobsByZone.AquireResource <SparseArray <float> >().GetFlatData());
            var results = TMG.Functions.GravityModel3D.ProduceFlows(MaxIterations, Epsilon,
                                                                    CreateWorkersByCategory(employmentSeekers, workerSplits),
                                                                    jobs, data,
                                                                    NumberOfWorkerCategories, zones.Length);

            Data = ConvertResults(results, zoneArray);
            if (!KeepLocalData)
            {
                LocalData         = null;
                PlanningDistricts = null;
                WorkerCategories  = null;
            }
            Loaded = true;
        }
예제 #30
0
        public void Start()
        {
            var matrix = new EmmeMatrix(Root.ZoneSystem.ZoneArray, MatrixToSave.AquireResource <SparseTwinIndex <float> >().GetFlatData());

            matrix.Save(OutputFile, true);
        }