private void LoadStudentDist() { List<StudentDist> studentData = new List<StudentDist>(); using ( CommentedCsvReader reader = new CommentedCsvReader( this.StudentDistributionFile.GetFileName( Root.InputBaseDirectory ) ) ) { float[] data = new float[4]; while ( reader.NextLine() ) { for ( int i = 0; i < data.Length && i < reader.NumberOfCurrentCells; i++ ) { reader.Get( out data[i], i ); } studentData.Add( new StudentDist() { Zone = (int)data[0], AgeCat = (int)data[1], EmploymentStatus = (int)data[2], Chance = data[3] } ); } } studentData.Sort( new Comparison<StudentDist>( delegate(StudentDist first, StudentDist second) { if ( first.Zone > second.Zone ) { return 1; } else if ( first.Zone == second.Zone ) { if ( first.AgeCat > second.AgeCat ) { return 1; } else if ( first.AgeCat == second.AgeCat ) { if ( first.EmploymentStatus > second.EmploymentStatus ) { return 1; } else if ( first.EmploymentStatus == second.EmploymentStatus ) { return 0; } } } return -1; } ) ); // Employment is now sorted Zone,Age,EmploymentStatus this.SchoolRates = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<SparseTwinIndex<float>>(); Range currentRange = new Range(); var studentDataLength = studentData.Count; int[] firstIndex; int[] secondIndex; float[] d; int numberOfElements; for ( int i = 1; i < studentDataLength; i++ ) { if ( studentData[i].Zone == studentData[i - 1].Zone ) { currentRange.Stop = i; } else { numberOfElements = currentRange.Stop - currentRange.Start + 1; firstIndex = new int[numberOfElements]; secondIndex = new int[numberOfElements]; d = new float[numberOfElements]; for ( int j = 0; j < numberOfElements; j++ ) { var data = studentData[currentRange.Start + j]; firstIndex[j] = data.AgeCat; secondIndex[j] = data.EmploymentStatus; d[j] = data.Chance; } foreach ( var z in this.PDZoneMap[studentData[i - 1].Zone] ) { this.SchoolRates[z] = SparseTwinIndex<float>.CreateTwinIndex( firstIndex, secondIndex, d ); } currentRange.Start = i; } } numberOfElements = currentRange.Stop - currentRange.Start + 1; firstIndex = new int[numberOfElements]; secondIndex = new int[numberOfElements]; d = new float[numberOfElements]; for ( int j = 0; j < numberOfElements; j++ ) { firstIndex[j] = studentData[currentRange.Start + j].AgeCat; secondIndex[j] = studentData[currentRange.Start + j].EmploymentStatus; d[j] = studentData[currentRange.Start + j].Chance; } foreach ( var z in this.PDZoneMap[studentData[studentDataLength - 1].Zone] ) { this.SchoolRates[z] = SparseTwinIndex<float>.CreateTwinIndex( firstIndex, secondIndex, d ); } }
private void LoadWorkerCarDistribution() { this.WorkerVehicleRates = this.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 } } } ); if ( !this.WorkerVehicleRateFile.ContainsFileName() ) { return; } using ( CommentedCsvReader reader = new CommentedCsvReader( this.WorkerVehicleRateFile.GetFileName( Root.InputBaseDirectory ) ) ) { int pd; int driversLic; int occ; float chanceZero; float chanceOne; float chanceTwo; while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells >= 6 ) //Only read if the number of columns in the row matches. { reader.Get( out pd, 0 ); reader.Get( out driversLic, 1 ); reader.Get( out occ, 2 ); reader.Get( out chanceZero, 3 ); reader.Get( out chanceOne, 4 ); reader.Get( out chanceTwo, 5 ); foreach ( var zone in this.PDZoneMap[pd] ) { var zoneData = this.WorkerVehicleRates[zone]; if ( zoneData == null ) { zoneData = SparseTriIndex<float>.CreateSimilarArray( DriversLicense, this.OccupationCategories, NumberOfVehicles ); this.WorkerVehicleRates[zone] = zoneData; } zoneData[driversLic, occ, 0] = chanceZero; zoneData[driversLic, occ, 1] = chanceOne; zoneData[driversLic, occ, 2] = chanceTwo; } } } } }
private void LoadJobTypeDisribution() { this.JobTypeRates = SparseTwinIndex<float>.CreateSimilarArray( this.Root.ZoneSystem.ZoneArray, this.EmploymentStatus ); var employmentIndexes = this.EmploymentStatus.ValidIndexies().ToArray(); using ( CommentedCsvReader reader = new CommentedCsvReader( this.JobEmploymentRateFile.GetFileName( Root.InputBaseDirectory ) ) ) { int pd; float fulltime; float parttime; while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells >= 3 ) { reader.Get( out pd, 0 ); reader.Get( out fulltime, 1 ); reader.Get( out parttime, 2 ); foreach ( var zone in this.PDZoneMap[pd] ) { this.JobTypeRates[zone, employmentIndexes[1]] = fulltime; this.JobTypeRates[zone, employmentIndexes[2]] = parttime; } } } } }
private void LoadOccupationDist() { List<OccupationDist> occupation = new List<OccupationDist>(); if ( this.SaveDataIntoZones ) { foreach ( var zone in this.Root.ZoneSystem.ZoneArray.ValidIndexies() ) { var z = this.Root.ZoneSystem.ZoneArray[zone]; z.WorkGeneral = 0; z.WorkManufacturing = 0; z.WorkProfessional = 0; z.WorkRetail = 0; } } using ( CommentedCsvReader reader = new CommentedCsvReader( this.OccupationDistributionFile.GetFileName( Root.InputBaseDirectory ) ) ) { float[] data = new float[7]; while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells < 7 ) { continue; } for ( int i = 0; i < data.Length && i < reader.NumberOfCurrentCells; i++ ) { reader.Get( out data[i], i ); } occupation.Add( new OccupationDist() { AgeCat = (int)data[1], Zone = (int)data[0], EmploymentStatus = (int)data[2], Professional = data[3], General = data[4], Sales = data[5], Manufacturing = data[6] } ); } } occupation.Sort( new Comparison<OccupationDist>( delegate(OccupationDist first, OccupationDist second) { if ( first.Zone > second.Zone ) { return 1; } else if ( first.Zone == second.Zone ) { if ( first.AgeCat > second.AgeCat ) { return 1; } else if ( first.AgeCat == second.AgeCat ) { if ( first.EmploymentStatus > second.EmploymentStatus ) { return 1; } else if ( first.EmploymentStatus == second.EmploymentStatus ) { return 0; } } } return -1; } ) ); this.OccupationRates = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<SparseTriIndex<float>>(); Range currentRange = new Range(); var employmentLength = occupation.Count; int[] firstIndex; int[] secondIndex; int[] thirdIndex; float[] d; int numberOfElements; for ( int i = 1; i < employmentLength; i++ ) { if ( occupation[i].Zone == occupation[i - 1].Zone ) { currentRange.Stop = i; } else { numberOfElements = currentRange.Stop - currentRange.Start + 1; firstIndex = new int[numberOfElements * 5]; secondIndex = new int[numberOfElements * 5]; thirdIndex = new int[numberOfElements * 5]; d = new float[numberOfElements * 5]; for ( int j = 0; j < numberOfElements; j++ ) { for ( int k = 0; k < 5; k++ ) { firstIndex[j * 5 + k] = occupation[currentRange.Start + j].AgeCat; secondIndex[j * 5 + k] = occupation[currentRange.Start + j].EmploymentStatus; thirdIndex[j * 5 + k] = k; } d[j * 5 + 1] = occupation[currentRange.Start + j].Professional; d[j * 5 + 2] = occupation[currentRange.Start + j].General; d[j * 5 + 3] = occupation[currentRange.Start + j].Sales; d[j * 5 + 4] = occupation[currentRange.Start + j].Manufacturing; } if ( this.OccupationByPD ) { foreach ( var z in this.PDZoneMap[occupation[i - 1].Zone] ) { this.OccupationRates[z] = SparseTriIndex<float>.CreateSparseTriIndex( firstIndex, secondIndex, thirdIndex, d ); } } else { this.OccupationRates[occupation[i - 1].Zone] = SparseTriIndex<float>.CreateSparseTriIndex( firstIndex, secondIndex, thirdIndex, d ); } currentRange.Start = i; } } numberOfElements = currentRange.Stop - currentRange.Start + 1; firstIndex = new int[numberOfElements * 5]; secondIndex = new int[numberOfElements * 5]; thirdIndex = new int[numberOfElements * 5]; d = new float[numberOfElements * 5]; for ( int j = 0; j < numberOfElements; j++ ) { for ( int k = 0; k < 5; k++ ) { firstIndex[j * 5 + k] = occupation[currentRange.Start + j].AgeCat; secondIndex[j * 5 + k] = occupation[currentRange.Start + j].EmploymentStatus; thirdIndex[j * 5 + k] = k; } d[j * 5 + 1] = occupation[currentRange.Start + j].Professional; d[j * 5 + 2] = occupation[currentRange.Start + j].General; d[j * 5 + 3] = occupation[currentRange.Start + j].Sales; d[j * 5 + 4] = occupation[currentRange.Start + j].Manufacturing; } if ( this.OccupationByPD ) { foreach ( var z in this.PDZoneMap[occupation[employmentLength - 1].Zone] ) { this.OccupationRates[z] = SparseTriIndex<float>.CreateSparseTriIndex( firstIndex, secondIndex, thirdIndex, d ); } } else { this.OccupationRates[occupation[employmentLength - 1].Zone] = SparseTriIndex<float>.CreateSparseTriIndex( firstIndex, secondIndex, thirdIndex, d ); } }
private void LoadEmploymentDist() { List<EmploymentDist> employment = new List<EmploymentDist>(); using ( CommentedCsvReader reader = new CommentedCsvReader( this.EmploymentDistributionFile.GetFileName( Root.InputBaseDirectory ) ) ) { float[] data = new float[5]; while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells < 5 ) { continue; } for ( int i = 0; i < data.Length && i < reader.NumberOfCurrentCells; i++ ) { reader.Get( out data[i], i ); } employment.Add( new EmploymentDist() { AgeCat = (int)data[1], Zone = (int)data[0], NonWork = data[2], FullTime = data[3], PartTime = data[4] } ); } } employment.Sort( new Comparison<EmploymentDist>( delegate(EmploymentDist first, EmploymentDist second) { if ( first.Zone > second.Zone ) { return 1; } else if ( first.Zone == second.Zone ) { if ( first.AgeCat > second.AgeCat ) { return 1; } else if ( first.AgeCat == second.AgeCat ) { return 0; } } return -1; } ) ); this.EmploymentStatusRates = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<SparseTwinIndex<float>>(); Range currentRange = new Range(); var employmentLength = employment.Count; int[] firstIndex; int[] secondIndex; float[] d; int numberOfElements; for ( int i = 1; i < employmentLength; i++ ) { if ( employment[i].Zone == employment[i - 1].Zone ) { currentRange.Stop = i; } else { numberOfElements = currentRange.Stop - currentRange.Start + 1; firstIndex = new int[numberOfElements * 3]; secondIndex = new int[numberOfElements * 3]; d = new float[numberOfElements * 3]; for ( int j = 0; j < numberOfElements; j++ ) { var ageCat = employment[currentRange.Start + j].AgeCat; for ( int k = 0; k < 3; k++ ) { firstIndex[j * 3 + k] = ageCat; secondIndex[j * 3 + k] = k; } d[j * 3] = employment[currentRange.Start + j].NonWork; d[j * 3 + 1] = employment[currentRange.Start + j].FullTime; d[j * 3 + 2] = employment[currentRange.Start + j].PartTime; } foreach ( var z in this.PDZoneMap[employment[i - 1].Zone] ) { this.EmploymentStatusRates[z] = SparseTwinIndex<float>.CreateTwinIndex( firstIndex, secondIndex, d ); } currentRange.Start = i; } } numberOfElements = currentRange.Stop - currentRange.Start + 1; firstIndex = new int[numberOfElements * 3]; secondIndex = new int[numberOfElements * 3]; d = new float[numberOfElements * 3]; for ( int j = 0; j < numberOfElements; j++ ) { for ( int k = 0; k < 3; k++ ) { firstIndex[j * 3 + k] = employment[currentRange.Start + j].AgeCat; secondIndex[j * 3 + k] = k; } d[j * 3] = employment[currentRange.Start + j].NonWork; d[j * 3 + 1] = employment[currentRange.Start + j].FullTime; d[j * 3 + 2] = employment[currentRange.Start + j].PartTime; } foreach ( var z in this.PDZoneMap[employment[employmentLength - 1].Zone] ) { this.EmploymentStatusRates[z] = SparseTwinIndex<float>.CreateTwinIndex( firstIndex, secondIndex, d ); } }
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; } } } } }
private void LoadAgeDist() { List<AgeDist> ageDistributions = new List<AgeDist>(); var ageCategories = AgeCategories.Count; using ( CommentedCsvReader reader = new CommentedCsvReader( this.AgeDistributionFile.GetFileName( Root.InputBaseDirectory ) ) ) { while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells >= ageCategories + 1 ) { int zone; float[] ageD = new float[ageCategories]; reader.Get( out zone, 0 ); for ( int i = 1; i < reader.NumberOfCurrentCells; i++ ) { reader.Get( out ageD[i - 1], i ); } ageDistributions.Add( new AgeDist() { Zone = zone, Percentages = ageD } ); } } } int numberOfSetZones = 0; foreach ( var ageDist in ageDistributions ) { List<int> pd; if ( !this.PDZoneMap.TryGetValue( ageDist.Zone, out pd ) ) { throw new XTMFRuntimeException( "In " + this.Name + " we were unable to find a planning district for the zone number '" + ageDist.Zone + "' while loading the age distribution." ); } numberOfSetZones += pd.Count; } var elements = ageDistributions.Count; var records = elements * ageCategories; var first = new int[numberOfSetZones * ageCategories]; var second = new int[numberOfSetZones * ageCategories]; var d = new float[numberOfSetZones * ageCategories]; var validAgeCategory = AgeCategories.ValidIndexies().ToArray(); int soFar = 0; for ( int i = 0; i < elements; i++ ) { var zones = this.PDZoneMap[ageDistributions[i].Zone]; foreach ( var zone in zones ) { for ( int j = 0; j < ageCategories; j++ ) { first[soFar] = zone; second[soFar] = validAgeCategory[j]; d[soFar] = ageDistributions[i].Percentages[j]; soFar++; } } } this.AgeRates = SparseTwinIndex<float>.CreateTwinIndex( first, second, d ); }
private void LoadDriversLicenseDistribution() { this.DriversLicenseRates = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<SparseTwinIndex<float>>(); var employmentIndexes = this.EmploymentStatus.ValidIndexies().ToArray(); if ( !this.DriversLicenseRateFile.ContainsFileName() ) { return; } using ( CommentedCsvReader reader = new CommentedCsvReader( this.DriversLicenseRateFile.GetFileName( Root.InputBaseDirectory ) ) ) { int pd; int ageCat; int empStat; float chance; while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells >= 4 ) { reader.Get( out pd, 0 ); reader.Get( out ageCat, 1 ); reader.Get( out empStat, 2 ); reader.Get( out chance, 3 ); foreach ( var zone in this.PDZoneMap[pd] ) { var zoneData = this.DriversLicenseRates[zone]; if ( zoneData == null ) { zoneData = SparseTwinIndex<float>.CreateSimilarArray( this.AgeCategories, this.EmploymentStatus ); this.DriversLicenseRates[zone] = zoneData; } zoneData[ageCat, empStat] = chance; } } } } }
public void TestLoad() { using ( var reader = new CommentedCsvReader( "CommentedCSVReaderTest.csv" ) ) { /* string[] truth = new string[] {"Zone", "Age", "EmpStat", "OccGroup", "Value"}; for (int i = 0; i < reader.Headers.Length; i++) { Assert.IsTrue(truth[i] == reader.Headers[i]); }*/ int lines = 0; while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells > 0 ) { Assert.AreEqual<int>( 5, reader.NumberOfCurrentCells ); float val = 0.0f; reader.Get( out val, 5 ); lines++; } } Assert.AreEqual<int>( EXPECTED_NUMBER_OF_LINES, lines ); } using ( var reader = new CommentedCsvReader( "CommentedCSVReaderTestFail.csv" ) ) { for ( int i = 0; i < FAIL_NUMBER_OF_LINES; i++ ) { reader.NextLine(); } bool failed = false; try { reader.NextLine(); } catch ( IOException ) { failed = true; } Assert.IsTrue( failed ); } }
private void LoadPopulation(SparseArray<IZone> zones) { if ( string.IsNullOrWhiteSpace( PopulationFile ) ) return; try { using (CommentedCsvReader reader = new CommentedCsvReader( GetFullPath( PopulationFile ) ) ) { while ( reader.NextLine() ) { var colRead = reader.NumberOfCurrentCells; if ( colRead < 2 ) { continue; } int zoneNumber; int population; reader.Get( out zoneNumber, 0 ); reader.Get( out population, 1 ); var zone = zones[zoneNumber]; if ( zone == null ) { if ( GeneratePDErrors ) { throw new XTMFRuntimeException( "When loading the population we found a distribution for a zone " + zoneNumber + " however that zone does not exist!" ); } } else { zone.Population = population; } } } } catch (IOException) { throw new XTMFRuntimeException( "Please make sure that the file " + GetFullPath( PopulationFile ) + " exists and is not being used by any other program." ); } }
private void LoadPDs(SparseArray<IZone> zones) { if ( string.IsNullOrWhiteSpace( PlanningDistrictFile ) ) return; try { using (CommentedCsvReader reader = new CommentedCsvReader( GetFullPath( PlanningDistrictFile ) ) ) { while ( reader.NextLine() ) { var colRead = reader.NumberOfCurrentCells; if ( colRead < 2 ) { continue; } int zoneNumber; int pd; reader.Get( out zoneNumber, 0 ); reader.Get( out pd, 1 ); var zone = zones[zoneNumber]; if ( zone == null ) { if ( GeneratePDErrors ) { throw new XTMFRuntimeException( "The planning district file contained a zone " + zoneNumber + " however the zone file did not contain this zone." ); } } else { zone.PlanningDistrict = pd; } } } } catch (IOException) { throw new XTMFRuntimeException( "Please make sure that the file " + GetFullPath( PlanningDistrictFile ) + " exists and is not being used by any other program." ); } }
private void LoadAttributes(SparseArray<IZone> zones) { // optional if ( string.IsNullOrWhiteSpace( ZoneAttributesFile ) ) return; try { using (CommentedCsvReader reader = new CommentedCsvReader( GetFullPath( ZoneAttributesFile ) ) ) { while ( reader.NextLine() ) { var colRead = reader.NumberOfCurrentCells; if ( colRead < 5 ) { continue; } int zoneNumber; float parkingCost, parkingCap, intraZoneDistance, area; reader.Get( out zoneNumber, 0 ); reader.Get( out parkingCost, 1 ); reader.Get( out parkingCap, 2 ); reader.Get( out intraZoneDistance, 3 ); reader.Get( out area, 4 ); var zone = zones[zoneNumber]; if ( zone == null ) { if ( GeneratePDErrors ) { throw new XTMFRuntimeException( "The planning district file contained a zone " + zoneNumber + " however the zone file did not contain this zone." ); } } else { zone.ParkingCost = parkingCost; zone.InternalDistance = intraZoneDistance; zone.InternalArea = area; } } } } catch (IOException) { throw new XTMFRuntimeException( "Please make sure that the file " + GetFullPath( ZoneAttributesFile ) + " exists and is not being used by any other program." ); } }
public void Load() { this.Progress = 0; var fileName = this.GetFileName( this.FileName ); if ( !File.Exists( fileName ) ) { throw new XTMFRuntimeException( String.Format( "The file {0} was not found when trying to load the population!", fileName ) ); } SparseArray<IPerson[]> pop = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<IPerson[]>(); SparseArray<Household[]> Households = this.Root.ZoneSystem.ZoneArray.CreateSimilarArray<Household[]>(); var flatHouseholds = Households.GetFlatData(); var flatZones = this.Root.ZoneSystem.ZoneArray.GetFlatData(); Dictionary<int, List<IPerson>> tempPop = new Dictionary<int, List<IPerson>>( flatZones.Length ); Parallel.For( 0, flatZones.Length, delegate(int i) { IZone zone = flatZones[i]; flatHouseholds[i] = new Household[] { new Household() { Cars = 0, Zone = zone }, new Household() { Cars = 1, Zone = zone }, new Household() { Cars = 2, Zone = zone } }; } ); using ( CommentedCsvReader reader = new CommentedCsvReader( fileName ) ) { int i = 0; var baseStream = reader.BaseStream; while ( reader.NextLine() ) { if ( reader.NumberOfCurrentCells > 9 ) { int zone, age, cars, employmentStatus, studentStatus, occupation, driversLicense; float expansionFactor; reader.Get( out zone, 0 ); reader.Get( out age, 1 ); reader.Get( out cars, 2 ); reader.Get( out employmentStatus, 5 ); reader.Get( out studentStatus, 6 ); reader.Get( out occupation, 7 ); reader.Get( out driversLicense, 8 ); reader.Get( out expansionFactor, 9 ); List<IPerson> zoneData; if ( !tempPop.TryGetValue( zone, out zoneData ) ) { zoneData = tempPop[zone] = new List<IPerson>( 10 ); } zoneData.Add( new Person() { Age = age, DriversLicense = driversLicense > 0, EmploymentStatus = employmentStatus, ExpansionFactor = expansionFactor, Household = Households[zone][cars], Occupation = occupation, StudentStatus = studentStatus } ); if ( i >= 4000 ) { i = 0; this.Progress = (float)baseStream.Position / baseStream.Length; } i++; } } this.Progress = 1; } SetupPopulation( pop, tempPop, flatZones ); }