private static void sampleTest() { double[] samples = new double[] { 0.1, 0.2, 0.4 }; String srcDir = Path.Combine(baseTrjDir, "20121201"); String targetDir = Path.Combine(baseQueryResultDir, "samples"); String queryFile = "20121208"; double dev = 400; if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } // load files for (int i = 0; i < samples.Length; ++i) { var trjDict = loadMultiTrj(srcDir, samples[i]); // build index MBR mbr = new MBR(115.6, 39.5, 117.3, 40.6); GridMultiTrj trjIndex = new GridMultiTrj(trjDict, 150, mbr); // handle queries String queryFileName = Path.Combine(baseQueryDir, queryFile + ".qry"); String targetFileName = Path.Combine(targetDir, String.Format("d{0}_q{1}_dev{2:#.}.csv", Path.GetFileNameWithoutExtension(srcDir), queryFile, dev)); Console.WriteLine("Src:{0}, Query:{1}", Path.GetFileNameWithoutExtension(srcDir), queryFile); handleQuery(trjDict, trjIndex, queryFileName, targetFileName, dev); } }
private static void handleQuery(Dictionary <long, Trajectory> trjDict, GridMultiTrj index, string queryFile, string targetFileName, double maxDist) { // read queries String[] queryLines = File.ReadAllLines(queryFile); int queryCount = queryLines.Length; // sampling int threadCount = 24, partSize = (int)Math.Ceiling(queryCount * 1.0 / threadCount); StringBuilder[] sbs = new StringBuilder[threadCount]; int processedCount = 0; for (int i = 0; i < threadCount; ++i) { sbs[i] = new StringBuilder(); } ParallelOptions op = new ParallelOptions(); op.MaxDegreeOfParallelism = threadCount; int sampleRate = 20; Parallel.For(0, threadCount, op, i => { for (int j = i * partSize; j < Math.Min((i + 1) * partSize, queryCount); ++j) { Interlocked.Increment(ref processedCount); if (processedCount % 1000 == 0) { Console.WriteLine("querying ... {0}%", processedCount * 100.0 / queryCount); } if (j % sampleRate != 0) { continue; } try { sbs[i].AppendLine(doSingleQuery(trjDict, index, queryLines[j], maxDist)); } catch (Exception e) { logger.Info(e.Message); logger.Info(String.Format("{0},{1}", trjDict, queryLines[j])); } } }); StreamWriter sw = new StreamWriter(targetFileName); for (int i = 0; i < threadCount; ++i) { sw.Write(sbs[i].ToString()); } sw.Close(); }
private static void dataQueryTest(String srcDir, String targetDir, String[] queryFiles, double sampleRate, double maxDist) { // load files var trjDict = loadMultiTrj(srcDir, sampleRate); // build index MBR mbr = new MBR(115.6, 39.5, 117.3, 40.6); GridMultiTrj trjIndex = new GridMultiTrj(trjDict, 150, mbr); // handle queries for (int i = 0; i < queryFiles.Length; ++i) { String queryFileName = Path.Combine(baseQueryDir, queryFiles[i] + ".qry"); String targetFileName = Path.Combine(targetDir, String.Format("d{0}_q{1}_dev{2:#.}.csv", Path.GetFileNameWithoutExtension(srcDir), queryFiles[i], maxDist)); Console.WriteLine("Src:{0}, Query:{1}", Path.GetFileNameWithoutExtension(srcDir), queryFiles[i]); handleQuery(trjDict, trjIndex, queryFileName, targetFileName, maxDist); } }
private static void busQueryTest() { String srcDir = Path.Combine(baseTrjDir, "20121201"); String queryFileName = Path.Combine(baseQueryDir, "bus_query.csv"); String targetDir = Path.Combine(baseQueryResultDir, "bus_query"); double maxDist = 800; //double sampleRate = 0.05; foreach (var sampleRate in new double[] { 0.05, 0.1 }) { var trjDict = loadMultiTrj(srcDir, sampleRate); // build index MBR mbr = new MBR(115.6, 39.5, 117.3, 40.6); GridMultiTrj trjIndex = new GridMultiTrj(trjDict, 150, mbr); // handle queries { String targetFileName = Path.Combine(targetDir, String.Format("d{0}_bus_dev{1:#.}_sample{2:0.##}.csv", Path.GetFileNameWithoutExtension(srcDir), maxDist, sampleRate)); handleQuery(trjDict, trjIndex, queryFileName, targetFileName, maxDist); } } }
private static String doSingleQuery(Dictionary <long, Trajectory> trjDict, GridMultiTrj index, String queryLine, double maxDist = 50) { String[] fields = queryLine.Split(','); // build query GeoPoint start = new GeoPoint(double.Parse(fields[3]), double.Parse(fields[4])); GeoPoint end = new GeoPoint(double.Parse(fields[5]), double.Parse(fields[6])); //start = CNTransform.WGS2MGS(start); //end = CNTransform.WGS2MGS(end); var startRect = start.GetMBR(maxDist).ToRectangle(); var endRect = end.GetMBR(maxDist).ToRectangle(); var startCands = index.RangeQuery(start, maxDist); var endCands = index.RangeQuery(end, maxDist); Dictionary <long, List <long> > startDict = new Dictionary <long, List <long> >(), endDict = new Dictionary <long, List <long> >(); foreach (var cand in startCands) { List <long> list = null; if (!startDict.TryGetValue(cand >> 16, out list)) { list = new List <long>(); startDict[cand >> 16] = list; } list.Add(cand); } foreach (var cand in endCands) { List <long> list = null; if (!endDict.TryGetValue(cand >> 16, out list)) { list = new List <long>(); endDict[cand >> 16] = list; } list.Add(cand); } // get distance Dictionary <long, Tuple <double, double> > distances = new Dictionary <long, Tuple <double, double> >(); HashSet <long> cands = new HashSet <long>(startDict.Keys); cands.IntersectWith(endDict.Keys); double minStartDist = 9999, minEndDist = 9999; double minDist = double.MaxValue; int validCandCount = 0; foreach (var cand in cands) { double startDist = 9999, endDist = 9999; var startList = startDict[cand]; var endList = endDict[cand]; var trj = trjDict[cand]; foreach (var val in startList) { int idx = (int)(val & 0xFFFF); MotionVector cur = trj[idx], next = trj[idx + 1]; double tmpDist = Polyline.DistFrom(cur.point, next.point, start); startDist = Math.Min(tmpDist, startDist); } foreach (var val in endList) { int idx = (int)(val & 0xFFFF); MotionVector cur = trj[idx], next = trj[idx + 1]; double tmpDist = Polyline.DistFrom(cur.point, next.point, end); endDist = Math.Min(tmpDist, endDist); } if (startDist <= maxDist && endDist <= maxDist) { ++validCandCount; } double tmpAvgDist = Math.Sqrt(startDist * startDist + endDist * endDist); if (tmpAvgDist < minDist) { minDist = tmpAvgDist; minStartDist = startDist; minEndDist = endDist; } } return(String.Format("{0},{1:0.0},{2:0.0},{3}", queryLine, minStartDist, minEndDist, validCandCount)); }