Exemple #1
0
        private SparseTriIndex <float> ConvertResults(float[] results, SparseArray <IZone> zoneSystem)
        {
            SparseTriIndex <float> ret = Data;

            // first create the datastructure
            if (ret == null)
            {
                ret = SparseTriIndex <float> .CreateSimilarArray(new SparseArray <int>(new SparseIndexing()
                {
                    Indexes = new[]
                    { new SparseSet()
                      {
                          BaseLocation = 0,
                          Start = 0,
                          Stop = NumberOfWorkerCategories - 1
                      } }
                }), zoneSystem, zoneSystem);
            }
            // now fill it
            var r             = ret.GetFlatData();
            var numberOfZones = r[0].Length;
            var iterativeRoot = Root as IIterativeModel;

            if (iterativeRoot == null || iterativeRoot.CurrentIteration == 0)
            {
                Parallel.For(0, numberOfZones, i =>
                {
                    for (int workerCategory = 0; workerCategory < r.Length; workerCategory++)
                    {
                        var workerCategoryMatrix = r[workerCategory];
                        var pos = sizeof(float) * ((workerCategoryMatrix.Length * workerCategoryMatrix.Length) * workerCategory + numberOfZones * i);
                        Buffer.BlockCopy(results, pos, workerCategoryMatrix[i], 0, numberOfZones * sizeof(float));
                    }
                });
            }
            else
            {
                Parallel.For(0, numberOfZones, i =>
                {
                    for (int workerCategory = 0; workerCategory < r.Length; workerCategory++)
                    {
                        var workerCategoryMatrix = r[workerCategory];
                        var pos = ((workerCategoryMatrix.Length * workerCategoryMatrix.Length) * workerCategory + numberOfZones * i);
                        VectorHelper.Average(workerCategoryMatrix[i], 0, results, pos, workerCategoryMatrix[i], 0, numberOfZones);
                    }
                });
            }
            return(ret);
        }
Exemple #2
0
        private void LoadJobOccupationDistribution()
        {
            JobOccupationRates = SparseTriIndex <float> .CreateSimilarArray(Root.ZoneSystem.ZoneArray, EmploymentStatus, OccupationCategories);

            var employmentIndexes = EmploymentStatus.ValidIndexies().ToArray();
            var occupationIndexes = OccupationCategories.ValidIndexies().ToArray();

            using (CsvReader reader = new CsvReader(GetFullPath(JobOccupationRateFile)))
            {
                int   pd;
                int   employmentStatus;
                float professional;
                float general;
                float sales;
                float manufacturing;
                if (JobOccupationRateFileHeader)
                {
                    reader.LoadLine();
                }
                while (!reader.EndOfFile)
                {
                    var length = reader.LoadLine();
                    if (length >= 5)
                    {
                        reader.Get(out pd, 0);
                        reader.Get(out employmentStatus, 1);
                        reader.Get(out professional, 2);
                        reader.Get(out general, 3);
                        reader.Get(out sales, 4);
                        reader.Get(out manufacturing, 5);
                        List <int> zones;
                        if (PDZoneMap.TryGetValue(pd, out zones))
                        {
                            foreach (var zone in zones)
                            {
                                this.JobOccupationRates[zone, employmentStatus, occupationIndexes[0]] = 0;
                                this.JobOccupationRates[zone, employmentStatus, occupationIndexes[1]] = professional;
                                this.JobOccupationRates[zone, employmentStatus, occupationIndexes[2]] = general;
                                this.JobOccupationRates[zone, employmentStatus, occupationIndexes[3]] = sales;
                                this.JobOccupationRates[zone, employmentStatus, occupationIndexes[4]] = manufacturing;
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
                /// <summary>
                /// Copy the linkages in case they need to be reset.
                /// </summary>
                /// <param name="linkages"></param>
                private void CopyLinkages(SparseTriIndex <float> linkages)
                {
                    _originalLinkages = linkages.CreateSimilarArray <float>();
                    var flatLinkages = linkages.GetFlatData();
                    var flatOriginal = _originalLinkages.GetFlatData();
                    var numberofPDs  = _planningDistricts.Count;
                    var zones        = _zoneSystem.GetFlatData();

                    _totalEmployment = new float[numberofPDs][][];
                    _assigned        = new float[numberofPDs][][];
                    for (int pd = 0; pd < numberofPDs; pd++)
                    {
                        _totalEmployment[pd] = new float[3][];
                        _assigned[pd]        = new float[3][];
                    }
                    for (int i = 0; i < flatLinkages.Length; i++)
                    {
                        for (int pd = 0; pd < numberofPDs; pd++)
                        {
                            _totalEmployment[pd][i] = new float[flatLinkages[i].Length];
                            _assigned[pd][i]        = new float[flatLinkages[i].Length];
                        }
                        for (int j = 0; j < flatLinkages[i].Length; j++)
                        {
                            Array.Copy(flatLinkages[i][j], flatOriginal[i][j], flatLinkages[i][j].Length);
                        }
                    }
                    for (int type = 0; type < flatLinkages.Length; type++)
                    {
                        for (int i = 0; i < zones.Length; i++)
                        {
                            var iPD     = _planningDistricts.GetFlatIndex(zones[i].PlanningDistrict);
                            var empRow  = _totalEmployment[iPD][type];
                            var linkRow = flatLinkages[type][i];
                            VectorHelper.Add(empRow, 0, empRow, 0, linkRow, 0, empRow.Length);
                        }
                    }
                }
Exemple #4
0
        private void LoadJobOccupationDistribution()
        {
            this.JobOccupationRates = SparseTriIndex <float> .CreateSimilarArray(this.Root.ZoneSystem.ZoneArray, this.EmploymentStatus, this.OccupationCategories);

            var employmentIndexes = this.EmploymentStatus.ValidIndexies().ToArray();
            var occupationIndexes = this.OccupationCategories.ValidIndexies().ToArray();

            using (CommentedCsvReader reader = new CommentedCsvReader(this.JobOccupationRateFile.GetFileName(Root.InputBaseDirectory)))
            {
                int   pd;
                int   employmentStatus;
                float professional;
                float general;
                float sales;
                float manufacturing;

                while (reader.NextLine())
                {
                    if (reader.NumberOfCurrentCells >= 5)
                    {
                        reader.Get(out pd, 0);
                        reader.Get(out employmentStatus, 1);
                        reader.Get(out professional, 2);
                        reader.Get(out general, 3);
                        reader.Get(out sales, 4);
                        reader.Get(out manufacturing, 5);
                        foreach (var zone in this.PDZoneMap[pd])
                        {
                            this.JobOccupationRates[zone, employmentStatus, occupationIndexes[0]] = 0;
                            this.JobOccupationRates[zone, employmentStatus, occupationIndexes[1]] = professional;
                            this.JobOccupationRates[zone, employmentStatus, occupationIndexes[2]] = general;
                            this.JobOccupationRates[zone, employmentStatus, occupationIndexes[3]] = sales;
                            this.JobOccupationRates[zone, employmentStatus, occupationIndexes[4]] = manufacturing;
                        }
                    }
                }
            }
        }
Exemple #5
0
        private void ValidateOccupations()
        {
            var zoneArray         = ZoneSystem.ZoneArray;
            var occZoneDist       = Demographics.OccupationRates.CreateSimilarArray <SparseTriIndex <float> >();
            var validOccupations  = Demographics.OccupationCategories.ValidIndexies().ToArray();
            var occupationsLength = validOccupations.Length;
            var validZones        = zoneArray.ValidIndexies().ToArray();
            var numberOfZones     = validZones.Length;
            var validAges         = Demographics.AgeCategories.ValidIndexies().ToArray();
            var agesLength        = validAges.Length;

            Parallel.For(0, numberOfZones,
                         delegate(int i)
            {
                var zoneNumber = validZones[i];
                var pop        = Population.Population[zoneNumber];
                var popLength  = pop.Length;
                var dist       = SparseTriIndex <float> .CreateSimilarArray(Demographics.AgeCategories, Demographics.EmploymentStatus,
                                                                            Demographics.OccupationCategories);
                var ages = Demographics.AgeCategories;

                for (int p = 0; p < popLength; p++)
                {
                    var person = pop[p];
                    if (person.EmploymentStatus != UnemployedEmploymentStatus)
                    {
                        var personAge = person.Age;
                        for (int a = 0; a < agesLength; a++)
                        {
                            var range = ages[validAges[a]];
                            if (personAge >= range.Start && personAge <= range.Stop)
                            {
                                dist[validAges[a], person.EmploymentStatus, person.Occupation] += person.ExpansionFactor;
                                break;
                            }
                        }
                    }
                }
                occZoneDist[zoneNumber] = dist;
            });
            using (StreamWriter writer = new StreamWriter(OccupationReportFile))
            {
                writer.Write("Zone,Age Category,Employment Status,");
                foreach (var ageCat in Demographics.OccupationCategories.ValidIndexies())
                {
                    writer.Write(Demographics.OccupationCategories[ageCat]);
                    writer.Write(',');
                }
                writer.WriteLine("Population");
                foreach (var employmentStatus in Demographics.EmploymentStatus.ValidIndexies())
                {
                    if (employmentStatus == UnemployedEmploymentStatus)
                    {
                        continue;
                    }
                    foreach (var zone in zoneArray.ValidIndexies())
                    {
                        var baseData = Demographics.OccupationRates[zone];
                        if (zoneArray[zone].Population <= 0)
                        {
                            continue;
                        }
                        var zoneDist = occZoneDist[zone];
                        for (int a = 0; a < agesLength; a++)
                        {
                            var population = zoneArray[zone].Population * Demographics.AgeRates[zone, a] *
                                             Demographics.EmploymentStatusRates[zone][a, employmentStatus];
                            var total = 0f;
                            for (int i = 0; i < occupationsLength; i++)
                            {
                                total += zoneDist[validAges[a], employmentStatus, validOccupations[i]];
                            }

                            for (int o = 0; o < occupationsLength; o++)
                            {
                                float result = (zoneDist[validAges[a], employmentStatus, validOccupations[o]] / total);
                                if (float.IsNaN(result))
                                {
                                    result = 0;
                                }
                                result -= baseData[validAges[a], employmentStatus, validOccupations[o]];
                                if (o == 0)
                                {
                                    writer.Write(zone);
                                    writer.Write(',');
                                    writer.Write(validAges[a]);
                                    writer.Write(',');
                                    writer.Write(employmentStatus);
                                    writer.Write(',');
                                    writer.Write(result * population);
                                }
                                else
                                {
                                    writer.Write(',');
                                    writer.Write(result * population);
                                }
                            }
                            writer.Write(',');
                            writer.WriteLine(population);
                        }
                    }
                }
            }
        }
Exemple #6
0
        private void LoadNonWorkerCarDistribution()
        {
            NonWorkerVehicleRates = Root.ZoneSystem.ZoneArray.CreateSimilarArray <SparseTriIndex <float> >();
            SparseArray <float> NumberOfVehicles =
                new SparseArray <float>(new SparseIndexing()
            {
                Indexes = new SparseSet[] { new SparseSet()
                                            {
                                                Start = 0, Stop = 2
                                            } }
            });
            SparseArray <float> DriversLicense =
                new SparseArray <float>(new SparseIndexing()
            {
                Indexes = new SparseSet[] { new SparseSet()
                                            {
                                                Start = 0, Stop = 1
                                            } }
            });

            using (CsvReader reader = new CsvReader(GetFullPath(NonWorkerVehicleRateFile)))
            {
                int   pd;
                int   driversLic;
                int   ageCat;
                float chanceZero;
                float chanceOne;
                float chanceTwo;
                if (NonWorkerVehicleRateFileHeader)
                {
                    reader.LoadLine();
                }
                while (!reader.EndOfFile)
                {
                    var length = reader.LoadLine();
                    if (length >= 6)
                    {
                        reader.Get(out pd, 0);
                        reader.Get(out driversLic, 1);
                        reader.Get(out ageCat, 2);
                        reader.Get(out chanceZero, 3);
                        reader.Get(out chanceOne, 4);
                        reader.Get(out chanceTwo, 5);
                        List <int> zones;
                        if (PDZoneMap.TryGetValue(pd, out zones))
                        {
                            foreach (var zone in zones)
                            {
                                var zoneData = this.NonWorkerVehicleRates[zone];
                                if (zoneData == null)
                                {
                                    zoneData = SparseTriIndex <float> .CreateSimilarArray(DriversLicense, AgeCategories, NumberOfVehicles);

                                    this.NonWorkerVehicleRates[zone] = zoneData;
                                }
                                zoneData[driversLic, ageCat, 0] = chanceZero;
                                zoneData[driversLic, ageCat, 1] = chanceOne;
                                zoneData[driversLic, ageCat, 2] = chanceTwo;
                            }
                        }
                    }
                }
            }
        }