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 SparseSet[] { new SparseSet() { BaseLocation = 0, Start = 0, Stop = NumberOfWorkerCategories - 1 } } }), zoneSystem, zoneSystem); } // now fill it var r = ret.GetFlatData(); var numberOfZones = r[0].Length; Parallel.For(0, numberOfZones, (int 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)); } }); return(ret); }
private void Save(SparseTriIndex <float> data) { var layerIndex = data.ValidIndexes().ToArray(); System.Threading.Tasks.Parallel.For(0, layerIndex.Length, (int i) => { Save(layerIndex[i], data.GetFlatData()[i], data); }); }
internal IZone ProduceResult(Random random, ITashaHousehold household) { var type = ClassifyHousehold(household); var homeZoneIndex = ZoneSystem.GetFlatIndex(household.HomeZone.ZoneNumber); var row = Probabilities.GetFlatData()[type][homeZoneIndex]; var pop = (float)random.NextDouble(); return(Zones[FindFirstClosestIndex(pop, row)]); }
private int PickAZoneToSelect(float pop, ITashaHousehold household, ITashaPerson person, float expansionFactor) { var type = ClassifyHousehold(household, person); var homeZoneIndex = _zoneSystem.GetFlatIndex(household.HomeZone.ZoneNumber); var row = _linkages.GetFlatData()[type][homeZoneIndex]; var pdIndex = _planningDistricts.GetFlatIndex(household.HomeZone.PlanningDistrict); float[] assigned = _assigned[pdIndex][type]; float[] emp = _totalEmployment[pdIndex][type]; var totalLinkages = GetTotalLinkages(row, assigned, emp); if (totalLinkages <= 0.0f) { Array.Copy(_originalLinkages.GetFlatData()[type][homeZoneIndex], row, row.Length); totalLinkages = VectorHelper.Sum(row, 0, row.Length); if (totalLinkages <= 0.0f) { throw new XTMFRuntimeException(this, $"A person living at zone {household.HomeZone.ZoneNumber} with worker category" + $" {type + 1} tried to find an employment zone. There was no aggregate data for any workers of this class however. Please" + $" update your worker categories and zonal residence files for this scenario!\r\n" + $"HHLD#: {household.HouseholdId}"); } } // total linkages is greater than zero pop *= totalLinkages; float acc = 0.0f; int index = 0; for (; index < row.Length; index++) { var ratio = emp[index] > 0 ? (assigned[index] / emp[index]) : 1f; acc += row[index] * Math.Max((1.0f - ratio), 0f); if (pop < acc) { break; } } // make sure it is bounded in case of rounding errors if (index == row.Length) { for (index = row.Length - 1; index >= 0; index--) { if (row[index] > 0) { break; } } // make sure we didn't run all the way back past the start of the array if (index == -1) { throw new XTMFRuntimeException(this, $"After already checking that there was an available job, none were found!"); } } _assigned[pdIndex][type][index] += expansionFactor; return(index); }
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(); }
public void Load() { _zoneSystem = Root.ZoneSystem.ZoneArray; _planningDistricts = ZoneSystemHelper.CreatePdArray <float>(_zoneSystem); Zones = _zoneSystem.GetFlatData(); if (SaveWorkerCategory != null) { WorkerResults = new float[3][]; for (int i = 0; i < WorkerResults.Length; i++) { WorkerResults[i] = new float[Zones.Length]; } } _linkages = Linkages.AcquireResource <SparseTriIndex <float> >(); CopyLinkages(_linkages); SaveLinkagesToFile(_linkages.GetFlatData()); Linkages.ReleaseResource(); }
internal IZone ProduceResult(Random random, ITashaHousehold household) { var type = ClassifyHousehold(household); var homeZoneIndex = ZoneSystem.GetFlatIndex(household.HomeZone.ZoneNumber); var row = Probabilities.GetFlatData()[type][homeZoneIndex]; var pop = (float)random.NextDouble(); var index = FindFirstClosestIndex(pop, row); // Detect if there is no actual data in this row and we are drawing from it anyways if (index == 0 && row[index] <= 0.0f) { // check to make sure that there is no probability in the row. if (VectorHelper.Sum(row, 0, row.Length) <= 0.0f) { throw new XTMFRuntimeException(this, $"A person living at zone {household.HomeZone.ZoneNumber} with worker category" + $" {type + 1} tried to find an employment zone. There was no aggregate data for any workers of this class however. Please" + $" update your worker categories and zonal residence files for this scenario!\r\n" + $"HHLD#: {household.HouseholdId}"); } } return(Zones[index]); }
/// <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); } } }