public CostCounter ExactSearch(double[] ts, out IndexFileDist bsf) { CostCounter meas = new CostCounter(0, 0); IntervalHeap <IndexEntryDist> pq = new IntervalHeap <IndexEntryDist>(NumIndexEntries); // approx search TermEntry approx = ApproximateSearch(ts); bsf = Index <DATAFORMAT> .MinFileEucDist(ts, approx.FileName); meas.IO++; meas.distance += approx.NumTimeSeries; // initalize pq with IndexEntries at root node foreach (IndexEntry e in index.Values) { pq.Add(new IndexEntryDist(e, Sax.MinDistPAAToiSAX( Sax.SaxStrToSaxVals(e.SaxWord), options.SaxOpts, ts))); } while (!pq.IsEmpty) { IndexEntryDist minInfo = pq.DeleteMin(); IndexEntry minEntry = minInfo.entry; if (minInfo.dist >= bsf.distance) { break; } if (minEntry is TermEntry) { IndexFileDist posMin = Index <DATAFORMAT> .MinFileEucDist(ts, ((TermEntry)minEntry).FileName); meas.IO++; meas.distance += minEntry.NumTimeSeries; // update bsf if (posMin.distance < bsf.distance) { bsf = posMin; } } else if (minEntry is SplitEntry <DATAFORMAT> ) { SplitEntry <DATAFORMAT> sEntry = minEntry as SplitEntry <DATAFORMAT>; foreach (IndexEntry e in sEntry.GetIndexEntries()) { pq.Add(new IndexEntryDist(e, Sax.MinDistPAAToiSAX( Sax.SaxStrToSaxVals(e.SaxWord), sEntry.Options.SaxOpts, ts))); } } } return(meas); }
public static IndexFileDist MinFileEucDist(double[] ts, string file) { ushort[] val; double[] dd; byte[] temp; int pos = 0; int length; IndexFileDist?best = null; int lineNum = 1; int wlen = Globals.SaxBaseCard; using (FileStream sr = new FileStream(file, FileMode.Open, FileAccess.Read)) { BinaryReader r = new BinaryReader(sr); pos = 0; length = (int)r.BaseStream.Length; val = new ushort[wlen]; dd = new double[ts.Length]; while (pos < length) { temp = r.ReadBytes(SaxData.ByteLength(typeof(DATAFORMAT))); SaxData tmp = SaxData.Parse <DATAFORMAT>(temp); pos = pos + SaxData.ByteLength(typeof(DATAFORMAT)); double[] fileTs = Util.NormalizationHandler(tmp.dl.GetTimeSeries()); // repo.ReturnData(Util.IndexFlineToDataLocation(line))); double dist = Util.EuclideanDistance(fileTs, ts); IndexFileDist retEntry = new IndexFileDist(file, lineNum, dist); if (best == null) { best = retEntry; } else { if (best.Value.distance > dist) { best = retEntry; } } lineNum++; } r.Close(); sr.Close(); } return(best.Value); }
/// <summary> /// Builds an index with randomly generated time series /// </summary> public static void BaseIndex() { DateTime startTime = DateTime.Now; // index construction Index <RawDataFormat> si = new Index <RawDataFormat>(0, new IndexOptions("root")); DataLoader dl = new GeneratedRawDataLoader(si, Globals.TimeSeriesLength, NUM_TIMESERIES, SEED); InsertTimeSeries(dl); Console.WriteLine(); Console.WriteLine("Sequential Disk Accesses: " + DiskCost.seqcost); Console.WriteLine("Random Disk Accesses: " + DiskCost.rancost); Console.WriteLine("Read Disk Accesses: " + DiskCost.readcost); Console.WriteLine("Saved cost in buffer: " + DiskCost.savedcost); Console.WriteLine(); Index <RawDataFormat> .Save(Globals.IndexRootDir, si); Index <RawDataFormat> si2 = Index <RawDataFormat> .Load(Globals.IndexRootDir); DateTime endConstructionTime = DateTime.Now; Console.WriteLine("Index Construction Time: {0}", endConstructionTime - startTime); // generate some test queries const int NUM_QUERIES = 10; List <double[]> queries = new List <double[]>(NUM_QUERIES); for (int i = 0; i < NUM_QUERIES; i++) { queries.Add(Util.RandomWalk(Globals.TimeSeriesLength)); } // full sequential scan Console.WriteLine("Performing full sequential scan."); Console.WriteLine("--------------------------------"); List <IndexFileDist[]> nnInfo = si.KNearestNeighborSequentialScan(10, queries); Console.WriteLine(); // query results Console.WriteLine("Performing exact and approximate search."); Console.WriteLine("----------------------------------------"); int counter = 0; for (int i = 0; i < NUM_QUERIES; i++) { IndexFileDist exactResult; si.ExactSearch(queries[i], out exactResult); IndexFileDist approxResult = Index <RawDataFormat> .MinFileEucDist(queries[i], si.ApproximateSearch(queries[i]).FileName); Assert.IsTrue(exactResult == nnInfo[i][0]); if (approxResult == exactResult) { counter++; Console.WriteLine(approxResult); } } Console.WriteLine("{0} approximate results == exact results.", counter); Console.WriteLine(); }