public override IResult SearchKNN(object q, int K, IResult res) { var n = this.Vertices.Count; const int window = 2; var prev = double.MaxValue; var curr = 0.0; var inserted = new HashSet<int> (); var candidates = new Result (Math.Max(K, 32)); // var candidates = new Result (K+K); while (prev > curr) { prev = res.CoveringRadius; for (int i = 0; i < window; ++i) { var next = this.rand.Next (this.Vertices.Count); if (inserted.Add (next)) { var _res = new Result (res.K); var d = this.DB.Dist (q, this.DB [next]); candidates.Push (next, d); res.Push (next, d); this.InternalSearch (q, candidates, inserted, _res); foreach (var p in _res) { res.Push (p.ObjID, p.Dist); } } } curr = res.CoveringRadius; } return res; }
protected override void SearchKNNNode(Node node, object q, IResult res) { // res.Push (node.objID, dist); var D = new double[node.Children.Count]; var closer_child = node.Children[0]; var closer_dist = this.DB.Dist(q, this.DB[closer_child.objID]); res.Push(closer_child.objID, closer_dist); D[0] = closer_dist; for (int i = 1; i < D.Length; ++i) { var child = node.Children[i]; D[i] = this.DB.Dist(q, this.DB[child.objID]); res.Push(child.objID, D[i]); if (D[i] < closer_dist) { closer_dist = D[i]; closer_child = child; } } if (closer_child.Children.Count > 0) { this.SearchKNNNode (closer_child, q, res); } // for (int i = 0; i < D.Length; ++i) { // if (D[i] <= closer_dist + 2 * res.CoveringRadius) { // this.SearchKNNNode(D[i], node.Children[i], q, res); // } // } }
public override IResult SearchKNN(object q, int K, IResult final_result) { var state = new SearchState (); var beam = this.FirstBeam (q, final_result, state); var beamsize = beam.Count; double prevcov = 0; int count_ties = 0; for (int i = 0; i < this.RepeatSearch; ++i) { prevcov = final_result.CoveringRadius; var _beam = new Result (beamsize); //if (this.Vertices.Count == this.DB.Count) // Console.WriteLine ("=== Iteration {0}/{1}, res-count: {2}, res-cov: {3}", i, this.RepeatSearch, final_result.Count, final_result.CoveringRadius); foreach (var pair in beam) { foreach(var neighbor_docID in this.Vertices [pair.docid]) { // Console.WriteLine ("=== B i: {0}, docID: {1}, parent: {2}, beamsize: {3}", i, neighbor_docID, pair, beam.Count); if (state.evaluated.Add(neighbor_docID)) { var d = this.DB.Dist (q, this.DB [neighbor_docID]); final_result.Push (neighbor_docID, d); _beam.Push (neighbor_docID, d); } } } if (final_result.CoveringRadius == prevcov) { if (count_ties == 1) { // we stop after two ties break; } ++count_ties; } else { count_ties = 0; } beam = _beam; } return final_result; }
public override IResult SearchKNN(object q, int K, IResult res) { var window = 2; if (this.Vertices.Count > 16) { window = 8; } // if (this.Vertices.Count > 10000) { // Console.WriteLine ("STARTING SEARCH"); // } var prev = double.MaxValue; var curr = 0.0; var inserted = new HashSet<int> (); var candidates = new Result (this.Vertices.Count); while (prev > curr) { prev = res.CoveringRadius; for (int i = 0; i < window; ++i) { var next = this.rand.Next (this.Vertices.Count); if (inserted.Add (next)) { var d = this.DB.Dist (q, this.DB [next]); candidates.Push (next, d); res.Push (next, d); this.InternalSearch (q, candidates, inserted, res); } } curr = res.CoveringRadius; } return res; }
public override IResult SearchKNN(object q, int K, IResult res) { int m = this.nodes.Length; // ~10% of the pivots will be seen var distances = new double[m]; var sortedNodes = new Node[m]; for (int i = 0; i < m; ++i) { sortedNodes [i] = this.nodes [i]; var objID = this.nodes [i].objID; distances [i] = this.DB.Dist (q, this.DB [objID]); res.Push (objID, distances [i]); } this.internal_numdists += m; Array.Sort (distances, sortedNodes); foreach (var objID in sortedNodes[0].IterateRange(res, distances[0])) { bool discarded = false; var rad = res.CoveringRadius; for (int i = 1; i < m; ++i) { var node = sortedNodes [i]; if (node.Discarded (objID, rad, distances [i])) { discarded = true; break; } } if (discarded) { continue; } var d = this.DB.Dist(q, this.DB[objID]); res.Push (objID, d); } return res; }
public override IResult SearchKNN(object q, int K, IResult final_result) { var window = 2; if (this.Vertices.Count > 16) { window = 4; } var prev = double.MaxValue; var curr = 0.0; var inserted = new HashSet<int> (); var expanded = new HashSet<int> (); while (prev > curr) { prev = final_result.CoveringRadius; for (int i = 0; i < window; ++i) { var res = new Result (K); var next = this.rand.Next (this.Vertices.Count); if (expanded.Add(next)) { this.GreedySearch (q, next, expanded, res); foreach (var p in res) { if (inserted.Add(p.ObjID)) { final_result.Push(p.ObjID, p.Dist); } } } } curr = final_result.CoveringRadius; } return final_result; }
protected override Result FirstBeam(object q, HashSet<int> evaluated, IResult res) { int samplesize = 0; // the real value can bee really complex, Monte Carlo methods // just use the larger sustainable value, however we are interested // on keep a fixed upper bound if (this.Vertices.Count > 4) { if (this.Vertices.Count < 10000) { samplesize = (int) Math.Sqrt (this.Vertices.Count) * 2; } else { samplesize = 1000; } } // int samplesize = Math.Min (2, this.Vertices.Count); int beamsize = Math.Min (this.BeamSize, this.Vertices.Count); var beam = new Result (beamsize); // initializing the first beam for (int i = 0; i < samplesize; ++i) { var docID = this.Vertices.GetRandom().Key; if (evaluated.Add (docID)) { var d = this.DB.Dist (q, this.DB [docID]); beam.Push (docID, d); res.Push(docID, d); } } return beam; }
//, Result C) protected void GreedySearchGlobalMinima(int parent, object q, IResult res) { var rs = this.SEQ.Unravel (parent); var children_count = rs.Count1; var closer_dist = double.MaxValue; var closer_objID = -1; for (int rank = 1; rank <= children_count; ++rank) { var objID = rs.Select1(rank); var dist = this.DB.Dist(q, this.DB[objID]); res.Push (objID, dist); if (dist < closer_dist) { closer_dist = dist; closer_objID = objID; } //if (C != null) C.Push (objID, dist); } if (closer_objID >= 0) { this.GreedySearchGlobalMinima (closer_objID, q, res); //, C); } // for (int childID = 0; childID < children_count; ++childID) { // var child_objID = C[childID]; // var child_dist = D[childID]; // var radius = res.CoveringRadius; // //Console.WriteLine ("---- cov: {0}", this.COV[child_objID]); // if (child_dist <= radius + this.GetCOV(child_objID) && child_dist <= closer_dist + radius + radius) { // this.SearchKNNNode(child_objID, q, res); // } // } }
/// <summary> /// KNN Search /// </summary> public override IResult SearchKNN(object q, int k, IResult R) { int L = this.DB.Count; for (int docid = 0; docid < L; docid++) { double d = this.DB.Dist (q, this.DB[docid]); R.Push (docid, d); } return R; }
public override IResult SearchKNN(object q, int K, IResult res) { var n = this.DB.Count; for (int i = 0; i < this.SampleSize; ++i) { var objID = this.rand.Next (0, n); var d = this.DB.Dist (q, this.DB [objID]); res.Push (objID, d); } return res; }
public override IResult SearchKNN(object q, int K, IResult res) { if (this.root == null) { return res; } var dist_root = this.DB.Dist(q, this.DB[this.root.objID]); res.Push(this.root.objID, dist_root); if (this.root.Children.Count > 0) { this.SearchKNNNode (this.root, q, res); } return res; }
protected void ApproxSearchKNNNode(Node node, object q, IResult res) { // res.Push (node.objID, dist); var D = new double[node.Children.Count]; var closer_child = node.Children[0]; var closer_dist = this.DB.Dist(q, this.DB[closer_child.objID]); res.Push(closer_child.objID, closer_dist); D[0] = closer_dist; for (int i = 1; i < D.Length; ++i) { var child = node.Children[i]; D[i] = this.DB.Dist(q, this.DB[child.objID]); res.Push(child.objID, D[i]); if (D[i] < closer_dist) { closer_dist = D[i]; closer_child = child; } } if (closer_child.Children.Count > 0) { this.ApproxSearchKNNNode (closer_child, q, res); } }
protected void ExpandNode(object q, IResult res, int startID, SearchState state, int level) { var nodeS = this.Vertices [startID]; foreach (var nodeID in nodeS) { if (state.visited.Add(nodeID)) { if (level > 0) { this.ExpandNode (q, res, nodeID, state, level - 1); } } if (state.evaluated.Add (nodeID)) { var d = this.DB.Dist (q, this.DB [nodeID]); res.Push(nodeID, d); } } }
public override IResult SearchKNN(object q, int K, IResult res) { var visited = new HashSet<int> (); var evaluated = new HashSet<int> (); for (int i = 0; i < this.RepeatSearch; ++i) { var objID = this.rand.Next (this.Vertices.Count); while (visited.Add (objID)) { if (evaluated.Add (objID)) { var d = this.DB.Dist (this.DB [objID], q); res.Push (objID, d); } this.GreedySearch(q, res, visited, evaluated, objID); } } return res; }
protected override Result FirstBeam(object q, IResult final_result, SearchState state) { int samplesize = Math.Min (1024, this.Vertices.Count); //WARNING fixing the parameter beamsize for construction step for testing purposes int beamsize = Math.Min (this.BeamSize, this.Vertices.Count); var beam = new Result (beamsize); // initializing the first beam for (int i = 0; i < samplesize; ++i) { var docID = this.rand.Next(this.Vertices.Count); if (state.evaluated.Add (docID)) { var d = this.DB.Dist (q, this.DB [docID]); beam.Push (docID, d); final_result.Push(docID, d); } } return beam; }
public void InternalSearch(object q, Result candidates, HashSet<int> inserted, IResult res) { do { var start = candidates.PopFirst (); if (start.Dist > res.CoveringRadius) { break; } var adjList = this.Vertices[start.ObjID]; foreach (var item in adjList) { if (inserted.Add(item.ObjID)) { // true iff it wasn't evaluated var d = this.DB.Dist (this.DB [item.ObjID], q); candidates.Push (item.ObjID, d); res.Push(item.ObjID, d); } } } while (candidates.Count > 0); }
void GreedySearch(object q, IResult res, HashSet<int> visited, HashSet<int> evaluated, int startID) { var minDist = double.MaxValue; var minItem = 0; do { // Console.WriteLine ("XXXXXX======= SEARCH startID: {0}, count: {1}, res-count: {2}", startID, this.Vertices.Count, res.Count); foreach (var objID in this.Vertices[startID]) { if (evaluated.Add (objID)) { var d = this.DB.Dist (this.DB [objID], q); res.Push (objID, d); if (minDist > d) { minDist = d; minItem = objID; } } } startID = minItem; } while (visited.Add (startID)); }
public override IResult SearchKNN(object q, int K, IResult res) { // var state = new SearchState (final_result); HashSet<int> evaluated = new HashSet<int> (); var beam = this.FirstBeam (q, evaluated, res); var maxbeamsize = beam.Count; var beamsize = maxbeamsize; double prev; do { prev = res.CoveringRadius; var _beamsize = Math.Min (beamsize, beam.Count); // var _beam = new Result(_beamsize); var _beam = new Result(_beamsize); for (int j = 0; j < _beamsize; ++j) { var item = beam.PopFirst (); foreach (var neighbor_docID in this.Vertices [item.ObjID]) { if (evaluated.Add (neighbor_docID)) { var d = this.DB.Dist (q, this.DB [neighbor_docID]); res.Push (neighbor_docID, d); _beam.Push(neighbor_docID, d); // beam.Push (neighbor_docID, d); // this can include distant items at any step } } } beam = _beam; // foreach (var p in _beam) { // beam.Push(p.ObjID, p.Dist); // } if (beamsize > 32) { beamsize = beamsize / 2; } if (prev == res.CoveringRadius) { beamsize = beamsize / 2; } } while (beamsize > 1); // } while (prev > curr); // } while (beamsize <= maxbeamsize); return res; }
public override int SearchKNN(MetricDB db, object q, int K, IResult res, short[] A, short current_rank_A) { int abs_pos = 0; int count_dist = 0; foreach (var piv in this.Pivs) { var pivOBJ = db [piv.objID]; var dqp = db.Dist (q, pivOBJ); res.Push (piv.objID, dqp); ++count_dist; // checking near ball radius if (dqp <= piv.last_near + res.CoveringRadius * this.ApproxFactor) { for (int j = 0; j < piv.num_near; ++j, ++abs_pos) { var item = this.Items [abs_pos]; // checking covering pivot if (Math.Abs (item.Dist - dqp) <= res.CoveringRadius) { ++A [item.ObjID]; } } } else { abs_pos += piv.num_near; } // checking external radius if (dqp + res.CoveringRadius * this.ApproxFactor >= piv.first_far) { for (int j = 0; j < piv.num_far; ++j, ++abs_pos) { var item = this.Items [abs_pos]; // checking covering pivot if (Math.Abs (item.Dist - dqp) <= res.CoveringRadius) { ++A [item.ObjID]; } } } else { abs_pos += piv.num_far; } if (dqp + res.CoveringRadius*this.ApproxFactor <= piv.last_near || piv.first_far <= dqp - res.CoveringRadius*this.ApproxFactor) { break; } } return count_dist; }
public override IResult SearchKNN(object q, int K, IResult final_result) { var state = new SearchState (); var beam = this.FirstBeam (q, final_result, state); var beamsize = beam.Count; double prevcov = 0; int count_ties = 0; var neighborhood = new List<int> (32); for (int i = 0; i < this.RepeatSearch; ++i) { prevcov = final_result.CoveringRadius; var _beam = new Result (beamsize); foreach (var pair in beam) { neighborhood.Clear (); // neighborhood.Add (pair.docid); foreach (var neighbor_docID in this.Vertices [pair.docid]) { this.ExpandNeighborhood (q, neighbor_docID, neighborhood, state, 8); } foreach (var neighbor_docID in neighborhood) { var d = this.DB.Dist (q, this.DB [neighbor_docID]); final_result.Push (neighbor_docID, d); _beam.Push (neighbor_docID, d); } } if (final_result.CoveringRadius == prevcov) { if (count_ties == 1) { // we stop after two ties break; } ++count_ties; } else { count_ties = 0; } beam = _beam; } return final_result; }
public override IResult SearchKNN(object q, int K, IResult final_result) { var knr = Math.Min (this.RepeatSearch, this.Vertices.Count); var knrseq = new Result (knr); var n = this.Vertices.Count; int ss = Math.Min (this.SampleSize, this.Vertices.Count); // Get the HSP!!! bool[] subdb=new bool[this.DB.Count]; for (int i = 0; i < ss; ++i) { var objID = this.rand.Next (0, n); subdb[objID]=true; //knrseq.Push (objID, d); } // Get the hsp-neigbors of q IList<int> hsp_neighbors= HSP.ComputeEdges(q,this.DB,subdb); foreach (int id in hsp_neighbors) { var d = this.DB.Dist (q, this.DB [id]); Console.WriteLine("id:{0} d:{1}",id,d); knrseq.Push(id,d); } Console.WriteLine( knrseq.Count); // End getting the HSP var res_array = new Result[knrseq.Count]; int I = 0; // on a parallel implementation these two hashsets must be set to null var state = new SearchState (); // visited = evaluated = null; foreach (var p in knrseq) { var res = new Result (K); //Console.WriteLine ("** starting GreedySearch"); this.GreedySearch(q, res, p.docid, state); res_array [I] = res; ++I; } var inserted = new HashSet<int> (); foreach (var res in res_array) { if (res == null) { break; } foreach (var p in res) { if (inserted.Add(p.docid)) { final_result.Push(p.docid, p.dist); } } } return final_result; }
public override IResult SearchKNN(object q, int knn, IResult res) { var R = this.IDX.SearchKNN (q, knn, new Result (res.K, res.Ceiling)); var P = (this.IDX.DB as SampleSpace); foreach (var p in R) { // res.Push(P.PERM.Inverse(p.docid), p.dist); res.Push(P.SAMPLE[p.docid], p.dist); } return res; }
public override IResult SearchKNN(object q, int K, IResult res) { Action<int> on_intersection = delegate(int item) { var dist = this.DB.Dist (q, this.DB [item]); res.Push (item, dist); }; var cache = new Dictionary<int, double>(this.LC_LIST[0].NODES.Count); return this.PartialSearchKNN (q, res, this.LC_LIST.Count, cache, on_intersection); }
// Sequential S; protected virtual void SearchKNNNode(Node node, object q, IResult res) { // res.Push (node.objID, dist); var D = new double[node.Children.Count]; var closer_child = node.Children[0]; var closer_dist = this.DB.Dist(q, this.DB[closer_child.objID]); D[0] = closer_dist; for (int i = 1; i < D.Length; ++i) { var child = node.Children[i]; D[i] = this.DB.Dist(q, this.DB[child.objID]); if (D[i] < closer_dist) { closer_dist = D[i]; closer_child = child; } } for (int i = 0; i < D.Length; ++i) { var child = node.Children[i]; res.Push(child.objID, D[i]); var radius = res.CoveringRadius; if (child.Children.Count > 0 && D[i] <= radius + child.cov && D[i] <= closer_dist + radius + radius) { this.SearchKNNNode(child, q, res); } } }
public override IResult SearchKNN(object q, int K, IResult res) { int m = this.pivs.Count; var D = new double[m]; for (int pivID = 0; pivID < m; ++pivID) { var objID = this.pivs [pivID]; var piv = this.DB [objID]; var d = this.DB.Dist(q, piv); D[pivID] = d; res.Push (objID, d); } this.internal_numdists += m; int n = this.DB.Count; for (int objID = 0; objID < n; ++objID) { if (this.ispivot [objID] == 1) { continue; } var rad = res.CoveringRadius; var review = true; foreach (var p in this.assocpivots[objID]) { var dqp = D [p.ObjID]; var dup = p.Dist; if (Math.Abs (dup - dqp) > rad) { review = false; break; } } if (review) { var d = this.DB.Dist (q, this.DB [objID]); res.Push (objID, d); } } return res; }
public override IResult SearchKNN(object q, int K, IResult res) { if (this.root == null) { return res; } /*var rand = new Random(); double min = double.MaxValue; for (int i = 0; i < 128; ++i) { ++this.internal_numdists; var obj = this.DB[ rand.Next(0, this.DB.Count) ]; min = Math.Min(this.DB.Dist(q, obj), min); } this.SearchRangeNode (this.DB.Dist(q, this.DB[this.root.objID]), this.root, q, min, res); return res; */ var dist = this.DB.Dist(q, this.DB[this.root.objID]); res.Push (this.root.objID, dist); // Console.WriteLine ("CHILDREN-count: {0}", this.root.Children.Count); if (this.root.Children.Count > 0 && dist <= res.CoveringRadius + this.root.cov) { this.SearchKNNNode (this.root, q, res); } // Console.WriteLine ("\n ROOT: {0}", this.root.objID); // if (this.S == null) { // this.S = new Sequential (); // S.Build (this.DB); // } // var R = S.SearchKNN (q, K); // if (R.First.docid != res.First.docid) { // Console.WriteLine ("XXXXX seq {0} != sat {1} XXXXXX", R, res); // } return res; }
public override IResult SearchKNN(object q, int K, IResult res) { var m = this.PIVS.Count; var n = this.DB.Count; var _PIVS = (this.PIVS as SampleSpace).SAMPLE; var P = new float[ m ]; var A = new HashSet<int>(); for (int piv_id = 0; piv_id < m; ++piv_id) { var dqp = this.DB.Dist (q, this.PIVS [piv_id]); ++this.internal_numdists; var i = _PIVS[piv_id]; A.Add(i); P[piv_id] = (float)dqp; res.Push (i, dqp); } for (int i = 0; i < n; ++i) { if (A.Contains(i)) { continue; } bool check_object = true; for (int piv_id = 0; piv_id < m; ++piv_id) { var dqp = P[piv_id]; var seq = this.DIST[piv_id]; var sym = seq[i]; var stddev = this.STDDEV[piv_id]; var mean = this.MEAN[piv_id]; var lower = this.Discretize(Math.Abs (dqp - res.CoveringRadius), stddev, mean); var upper = this.Discretize(dqp + res.CoveringRadius, stddev, mean); if (sym < lower || upper < sym ) { check_object = false; break; } } if (check_object) { res.Push(i, this.DB.Dist(q, this.DB[i])); } } return res; }
public void SearchKNN(object q, IResult res, MetricDB db) { var d = db.Dist (db [this.refID], q); res.Push (this.refID, d); if (this.left != null && d - res.CoveringRadius <= this.median) { this.left.SearchKNN (q, res, db); } if (this.right != null && this.median <= d + res.CoveringRadius) { this.right.SearchKNN (q, res, db); } }
public override IResult SearchKNN(object q, int K, IResult res) { var l = this.forest.Length; var n = this.DB.Count; float[] Linf = new float[n]; byte[] A = new byte[n]; var D_trees = new double[this.forest.Length][]; for (int rowID = 0; rowID < this.forest.Length; ++rowID) { var pivs = this.forest[rowID].Pivs; var D = D_trees[rowID] = new double[pivs.Length]; for (int pivID = 0; pivID < pivs.Length; ++pivID) { var objID = pivs[pivID].objID; D[pivID] = this.DB.Dist(q, this.DB[objID]); res.Push (objID, D[pivID]); Linf[objID] = float.MaxValue; } this.internal_numdists += pivs.Length; } for (short treeID = 0; treeID < l; ++treeID) { var t = this.forest[treeID]; t.SearchKNN(this.DB, q, K, res, A, treeID, Linf, D_trees[treeID]); } for (int docID = 0; docID < n; ++docID) { if (A[docID] == l && Linf[docID] <= res.CoveringRadius) { var d = this.DB.Dist(q, this.DB[docID]); res.Push(docID, d); } } return res; }
public override IResult SearchKNN(object q, int K, IResult res) { int maxC = this.MaxCandidates; if (maxC < 0) { maxC = this.DB.Count; } var cand = this.InternalIndex.SearchKNN(q, maxC); foreach (var p in cand) { var d = this.DB.Dist(this.DB[p.docid], q); res.Push (p.docid, d); } return res; }