private static SparseTwinIndex <float> CreateData() { var firstIndex = new[] { 1, 1, 2, 2 }; var secondIndex = new[] { 1, 2, 1, 2 }; var data = new[] { 0.25f, 0.75f, 2f, 2f }; return(SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, data)); }
private void LoadAgeDist() { List <AgeDist> ageDistributions = new List <AgeDist>(); var ageCategories = AgeCategories.Count; using (CommentedCsvReader reader = new CommentedCsvReader(AgeDistributionFile.GetFileName(Root.InputBaseDirectory))) { while (reader.NextLine()) { if (reader.NumberOfCurrentCells >= ageCategories + 1) { float[] ageD = new float[ageCategories]; reader.Get(out int 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) { if (!PDZoneMap.TryGetValue(ageDist.Zone, out List <int> pd)) { throw new XTMFRuntimeException(this, "In " + 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 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 = 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++; } } } AgeRates = SparseTwinIndex <float> .CreateTwinIndex(first, second, d); }
public void SparseTwinIndexTest() { var length = 1000000; Random r = new Random(); int[] baseData = new int[length]; int[][] position = GeneratePositions(2, length); for (int i = 0; i < length; i++) { baseData[i] = r.Next(); } var sparseData = SparseTwinIndex <int> .CreateTwinIndex(position[0], position[1], baseData); for (int i = 0; i < length; i++) { if (baseData[i] != sparseData[position[0][i], position[1][i]]) { Assert.Fail($"{baseData[i]} != {sparseData[position[0][i], position[1][i]]}"); } } }
public void SparseTwinIndexTest() { var length = 1000000; Random r = new Random(); int[] BaseData = new int[length]; int[][] Position = GeneratePositions(2, length); for (int i = 0; i < length; i++) { BaseData[i] = r.Next(); } var sparseData = SparseTwinIndex <int> .CreateTwinIndex(Position[0], Position[1], BaseData); for (int i = 0; i < length; i++) { if (BaseData[i] != sparseData[Position[0][i], Position[1][i]]) { Assert.Fail(String.Format("{0} != {1}", BaseData[i], sparseData[Position[0][i], Position[1][i]])); } } }
private SparseTwinIndex <float> CreateSparseData(List <int> zoneNumbers, int sparseLength, List <float>[] dataColumns) { var numberOfZoneEntries = zoneNumbers.Count; var entries = numberOfZoneEntries * sparseLength; var first = new int[entries]; var second = new int[entries]; var data = new float[entries]; int pos = 0; for (int dataEntry = 0; dataEntry < numberOfZoneEntries; dataEntry++) { var zoneNumber = zoneNumbers[dataEntry]; var dataRow = dataColumns[dataEntry]; for (int k = 0; k < sparseLength; k++) { first[pos] = zoneNumber; second[pos] = this.SparseSpace[k]; data[pos] = dataRow[k]; pos++; } } return(SparseTwinIndex <float> .CreateTwinIndex(first, second, data)); }
/// <summary> /// Load the network from a 211 file /// </summary> /// <param name="fileName211"></param> public void LoadNetwork(string fileName211) { Dictionary <int, Node> nodes = new Dictionary <int, Node>(); Dictionary <Pair <int, int>, Link> links = new Dictionary <Pair <int, int>, Link>(); using (StreamReader reader = new StreamReader(fileName211)) { string line = null; // run until we get to the link information while ((line = reader.ReadLine()) != null) { string[] parts = line.Split(SplitCharacters, StringSplitOptions.RemoveEmptyEntries); try { var numberOfParts = parts.Length; if (numberOfParts >= 3 && parts[0].Length > 0) { int offset = -1; if (parts[0][0] == 'a') { offset = 0; } else if (parts[0][0] == 'c') { continue; } Node node = new Node(); if (offset == -1) { node.IsCentroid = false; } else { node.IsCentroid = (parts[0].Length >= 2 && parts[0] == "a*"); } node.Number = int.Parse(parts[1 + offset]); node.X = float.Parse(parts[2 + offset]); node.Y = float.Parse(parts[3 + offset]); if (numberOfParts > 4 + offset) { node.USER1 = int.Parse(parts[4 + offset]); if (numberOfParts > 5 + offset) { node.USER2 = int.Parse(parts[5 + offset]); if (numberOfParts > 6 + offset) { node.NodeType = int.Parse(parts[6 + offset]); node.Modified = false; if (parts.Length > 7 + offset) { node.NodeLabel = parts[7 + offset]; } else { node.NodeLabel = node.Number.ToString(); } } } } nodes.Add(node.Number, node); } } catch { } if (line != null && line.StartsWith("t links")) { break; } } while ((line = reader.ReadLine()) != null) { try { string[] parts = line.Split(SplitCharacters, StringSplitOptions.RemoveEmptyEntries); if (parts.Length > 7) { Link link; link.I = int.Parse(parts[1]); link.J = int.Parse(parts[2]); link.Length = float.Parse(parts[3]); link.Modes = parts[4].ToLower().ToCharArray(); link.LinkType = int.Parse(parts[5]); link.Lanes = float.Parse(parts[6]); link.VDF = float.Parse(parts[7]); // We don't load [8] link.Speed = float.Parse(parts[9]); link.Capacity = float.Parse(parts[10]); link.Modified = false; links.Add(new Pair <int, int>(link.I, link.J), link); } } catch { } } } // Now that we have loaded the data it is time to create the sparse structures var numberOfLinks = links.Count; var first = new int[numberOfLinks]; var second = new int[numberOfLinks]; var data = new Link[numberOfLinks]; int i = 0; foreach (var l in links.Values) { first[i] = l.I; second[i] = l.J; data[i] = l; i++; } if (nodes.Values.Count == 0) { this.Nodes = null; } else { this.Nodes = SparseArray <Node> .CreateSparseArray((n => n.Number), nodes.Values.ToArray()); } if (numberOfLinks == 0) { this.Links = null; } else { this.Links = SparseTwinIndex <Link> .CreateTwinIndex(first, second, data); } }
private void LoadStudentDist() { List <StudentDist> studentData = new List <StudentDist>(); using (CommentedCsvReader reader = new CommentedCsvReader(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(delegate(StudentDist first, StudentDist second) { if (first.Zone > second.Zone) { return(1); } if (first.Zone == second.Zone) { if (first.AgeCat > second.AgeCat) { return(1); } if (first.AgeCat == second.AgeCat) { if (first.EmploymentStatus > second.EmploymentStatus) { return(1); } if (first.EmploymentStatus == second.EmploymentStatus) { return(0); } } } return(-1); }); // Employment is now sorted Zone,Age,EmploymentStatus SchoolRates = Root.ZoneSystem.ZoneArray.CreateSimilarArray <SparseTwinIndex <float> >(); var start = 0; var stop = 0; 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) { stop = i; } else { numberOfElements = stop - start + 1; firstIndex = new int[numberOfElements]; secondIndex = new int[numberOfElements]; d = new float[numberOfElements]; for (int j = 0; j < numberOfElements; j++) { var data = studentData[start + j]; firstIndex[j] = data.AgeCat; secondIndex[j] = data.EmploymentStatus; d[j] = data.Chance; } foreach (var z in PDZoneMap[studentData[i - 1].Zone]) { SchoolRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } start = i; } } numberOfElements = stop - start + 1; firstIndex = new int[numberOfElements]; secondIndex = new int[numberOfElements]; d = new float[numberOfElements]; for (int j = 0; j < numberOfElements; j++) { firstIndex[j] = studentData[start + j].AgeCat; secondIndex[j] = studentData[start + j].EmploymentStatus; d[j] = studentData[start + j].Chance; } foreach (var z in PDZoneMap[studentData[studentDataLength - 1].Zone]) { SchoolRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } }
private void LoadEmploymentDist() { List <EmploymentDist> employment = new List <EmploymentDist>(); using (CommentedCsvReader reader = new CommentedCsvReader(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(delegate(EmploymentDist first, EmploymentDist second) { if (first.Zone > second.Zone) { return(1); } if (first.Zone == second.Zone) { if (first.AgeCat > second.AgeCat) { return(1); } if (first.AgeCat == second.AgeCat) { return(0); } } return(-1); }); EmploymentStatusRates = Root.ZoneSystem.ZoneArray.CreateSimilarArray <SparseTwinIndex <float> >(); int start = 0; int stop = 0; var employmentLength = employment.Count; int[] firstIndex; int[] secondIndex; float[] d; int numberOfElements; for (var i = 1; i < employmentLength; i++) { if (employment[i].Zone == employment[i - 1].Zone) { stop = i; } else { numberOfElements = stop - start + 1; firstIndex = new int[numberOfElements * 3]; secondIndex = new int[numberOfElements * 3]; d = new float[numberOfElements * 3]; for (var j = 0; j < numberOfElements; j++) { var ageCat = employment[start + j].AgeCat; for (int k = 0; k < 3; k++) { firstIndex[j * 3 + k] = ageCat; secondIndex[j * 3 + k] = k; } d[j * 3] = employment[start + j].NonWork; d[j * 3 + 1] = employment[start + j].FullTime; d[j * 3 + 2] = employment[start + j].PartTime; } foreach (var z in PDZoneMap[employment[i - 1].Zone]) { EmploymentStatusRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } start = i; } } numberOfElements = stop - start + 1; firstIndex = new int[numberOfElements * 3]; secondIndex = new int[numberOfElements * 3]; d = new float[numberOfElements * 3]; for (var j = 0; j < numberOfElements; j++) { for (var k = 0; k < 3; k++) { firstIndex[j * 3 + k] = employment[start + j].AgeCat; secondIndex[j * 3 + k] = k; } d[j * 3] = employment[start + j].NonWork; d[j * 3 + 1] = employment[start + j].FullTime; d[j * 3 + 2] = employment[start + j].PartTime; } foreach (var z in PDZoneMap[employment[employmentLength - 1].Zone]) { EmploymentStatusRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } }
private void LoadStudentDist() { List <StudentDist> studentData = new List <StudentDist>(); List <int> zones; using (CsvReader reader = new CsvReader(GetFullPath(StudentDistributionFile))) { int length; float[] data = new float[4]; if (StudentDistributionFileHeader) { // burn the header reader.LoadLine(); } while ((length = reader.LoadLine()) > 2) { for (int i = 0; i < data.Length && i < length; 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(delegate(StudentDist first, StudentDist second) { if (first.Zone > second.Zone) { return(1); } if (first.Zone == second.Zone) { if (first.AgeCat > second.AgeCat) { return(1); } if (first.AgeCat == second.AgeCat) { if (first.EmploymentStatus > second.EmploymentStatus) { return(1); } if (first.EmploymentStatus == second.EmploymentStatus) { return(0); } } } return(-1); }); // Employment is now sorted Zone,Age,EmploymentStatus SchoolRates = Root.ZoneSystem.ZoneArray.CreateSimilarArray <SparseTwinIndex <float> >(); var start = 0; var stop = 0; 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) { stop = i; } else { numberOfElements = stop - start + 1; firstIndex = new int[numberOfElements]; secondIndex = new int[numberOfElements]; d = new float[numberOfElements]; for (int j = 0; j < numberOfElements; j++) { var data = studentData[start + j]; firstIndex[j] = data.AgeCat; secondIndex[j] = data.EmploymentStatus; d[j] = data.Chance; } if (PdZoneMap.TryGetValue(studentData[i - 1].Zone, out zones)) { foreach (var z in zones) { SchoolRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } } start = i; } } numberOfElements = stop - start + 1; firstIndex = new int[numberOfElements]; secondIndex = new int[numberOfElements]; d = new float[numberOfElements]; for (int j = 0; j < numberOfElements; j++) { firstIndex[j] = studentData[start + j].AgeCat; secondIndex[j] = studentData[start + j].EmploymentStatus; d[j] = studentData[start + j].Chance; } if (PdZoneMap.TryGetValue(studentData[studentDataLength - 1].Zone, out zones)) { foreach (var z in zones) { SchoolRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } } }
private void LoadEmploymentDist() { List <int> zones; List <EmploymentDist> employment = new List <EmploymentDist>(); using (CsvReader reader = new CsvReader(GetFullPath(EmploymentDistributionFile))) { int length; float[] data = new float[5]; if (EmploymentDistributionFileHeader) { // burn the header reader.LoadLine(); } while (!reader.EndOfFile) { if ((length = reader.LoadLine()) < 5) { continue; } for (int i = 0; i < data.Length && i < length; 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(delegate(EmploymentDist first, EmploymentDist second) { if (first.Zone > second.Zone) { return(1); } if (first.Zone == second.Zone) { if (first.AgeCat > second.AgeCat) { return(1); } if (first.AgeCat == second.AgeCat) { return(0); } } return(-1); }); EmploymentStatusRates = Root.ZoneSystem.ZoneArray.CreateSimilarArray <SparseTwinIndex <float> >(); var start = 0; var stop = 0; 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) { stop = i; } else { numberOfElements = stop - 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[start + j].AgeCat; for (int k = 0; k < 3; k++) { firstIndex[j * 3 + k] = ageCat; secondIndex[j * 3 + k] = k; } d[j * 3] = employment[start + j].NonWork; d[j * 3 + 1] = employment[start + j].FullTime; d[j * 3 + 2] = employment[start + j].PartTime; } if (PdZoneMap.TryGetValue(employment[i - 1].Zone, out zones)) { foreach (var z in zones) { EmploymentStatusRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } } start = i; } } numberOfElements = stop - 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[start + j].AgeCat; secondIndex[j * 3 + k] = k; } d[j * 3] = employment[start + j].NonWork; d[j * 3 + 1] = employment[start + j].FullTime; d[j * 3 + 2] = employment[start + j].PartTime; } if (PdZoneMap.TryGetValue(employment[employmentLength - 1].Zone, out zones)) { foreach (var z in zones) { EmploymentStatusRates[z] = SparseTwinIndex <float> .CreateTwinIndex(firstIndex, secondIndex, d); } } }
private void LoadAgeDist() { List <AgeDist> ageDistributions = new List <AgeDist>(); var ageCategories = AgeCategories.Count; using (CsvReader reader = new CsvReader(GetFullPath(AgeDistributionFile))) { int length; if (AgeDistributionFileHeader) { // burn the header reader.LoadLine(); } while ((length = reader.LoadLine()) > ageCategories) { float[] ageD = new float[ageCategories]; reader.Get(out int zone, 0); for (int i = 1; i < length; i++) { reader.Get(out ageD[i - 1], i); } ageDistributions.Add(new AgeDist { Zone = zone, Percentages = ageD }); } } int numberOfSetZones = 0; foreach (var ageDist in ageDistributions) { if (PdZoneMap.TryGetValue(ageDist.Zone, out List <int> temp)) { numberOfSetZones += temp.Count; } } var elements = ageDistributions.Count; 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++) { if (PdZoneMap.TryGetValue(ageDistributions[i].Zone, out List <int> zones)) { 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++; } } } } AgeRates = SparseTwinIndex <float> .CreateTwinIndex(first, second, d); }