annotate multiprobe.h @ 757:ee612b7bd922 mkc_lsh_update

added queryFromData(), passes equivalence test with queryFromKey
author mas01mc
date Fri, 26 Nov 2010 08:05:48 +0000
parents 9bd13c7819ae
children
rev   line source
mas01mc@754 1 /*
mas01mc@754 2 * MultiProbe C++ class
mas01mc@754 3 *
mas01mc@754 4 * Given a vector of LSH boundary distances for a query,
mas01mc@754 5 * perform lookup by probing nearby hash-function locations
mas01mc@754 6 *
mas01mc@754 7 * Implementation using C++ STL
mas01mc@754 8 *
mas01mc@754 9 * Reference:
mas01mc@754 10 * Qin Lv, William Josephson, Zhe Wang, Moses Charikar and Kai Li,
mas01mc@754 11 * "Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity
mas01mc@754 12 * Search", Proc. Intl. Conf. VLDB, 2007
mas01mc@754 13 *
mas01mc@754 14 *
mas01mc@754 15 * Copyright (C) 2009 Michael Casey, Dartmouth College, All Rights Reserved
mas01mc@754 16 * License: GNU Public License 2.0
mas01mc@754 17 *
mas01mc@754 18 */
mas01mc@754 19
mas01mc@754 20 #ifndef __LSH_MULTIPROBE_
mas01mc@754 21 #define __LSH_MULTIPROBE_
mas01mc@754 22
mas01mc@754 23 #include <functional>
mas01mc@754 24 #include <queue>
mas01mc@754 25 #include <vector>
mas01mc@754 26 #include <set>
mas01mc@754 27 #include <algorithm>
mas01mc@754 28 #include <iostream>
mas01mc@754 29
mas01mc@754 30 using namespace std;
mas01mc@754 31
mas01mc@754 32 typedef set<int > perturbation_set ;
mas01mc@754 33
mas01mc@754 34 typedef class MinHeapElement{
mas01mc@754 35 public:
mas01mc@754 36 perturbation_set perturbs;
mas01mc@754 37 float score;
mas01mc@754 38 MinHeapElement(perturbation_set a, float s);
mas01mc@754 39 virtual ~MinHeapElement();
mas01mc@754 40 } min_heap_element;
mas01mc@754 41
mas01mc@754 42 typedef priority_queue<min_heap_element,
mas01mc@754 43 vector<min_heap_element>,
mas01mc@754 44 greater<min_heap_element>
mas01mc@754 45 > min_heap_of_perturbation_set ;
mas01mc@754 46
mas01mc@754 47 typedef pair<float, pair<int, int> > sorted_distance_functions ;
mas01mc@754 48
mas01mc@754 49 class MultiProbe{
mas01mc@754 50 protected:
mas01mc@754 51 min_heap_of_perturbation_set* minHeap;
mas01mc@754 52 min_heap_of_perturbation_set* outSets;
mas01mc@754 53 vector<sorted_distance_functions>* distFuns;
mas01mc@754 54 unsigned numHashBoundaries;
mas01mc@754 55
mas01mc@754 56 // data structure initialization and cleanup
mas01mc@754 57 void initialize();
mas01mc@754 58 void cleanup();
mas01mc@754 59
mas01mc@754 60 // perturbation set operations
mas01mc@754 61 perturbation_set& shift(perturbation_set&);
mas01mc@754 62 perturbation_set& expand(perturbation_set&);
mas01mc@754 63 float score(perturbation_set&);
mas01mc@754 64 bool valid(perturbation_set&);
mas01mc@754 65 void makeSortedDistFuns(vector<float> &);
mas01mc@754 66 void makeSortedDistFuns(float* x, unsigned N);
mas01mc@754 67
mas01mc@754 68 // perturbation set generation algorithm
mas01mc@754 69 void algorithm1(unsigned T);
mas01mc@754 70
mas01mc@754 71 public:
mas01mc@754 72 MultiProbe();
mas01mc@754 73 ~MultiProbe();
mas01mc@754 74
mas01mc@754 75 // generation of perturbation sets
mas01mc@754 76 void generatePerturbationSets(vector<float>& vectorOfBounaryDistances, unsigned numSetsToGenerate);
mas01mc@754 77 void generatePerturbationSets(float* arrayOfBoundaryDistances, unsigned numDistances, unsigned numSetsToGenerate);
mas01mc@754 78 perturbation_set getNextPerturbationSet();
mas01mc@754 79 void dump(perturbation_set);
mas01mc@754 80 size_t size(); // Number of perturbation sets are in the output queue
mas01mc@754 81 bool empty(); // predicate for empty MultiProbe set
mas01mc@754 82 int getIndex(perturbation_set::iterator it); // return index of hash function for given set entry
mas01mc@754 83 int getBoundary(perturbation_set::iterator it); // return boundary {-1,+1} for given set entry
mas01mc@754 84 };
mas01mc@754 85
mas01mc@754 86
mas01mc@754 87 /* NOTES:
mas01mc@754 88
mas01mc@754 89 Reference:
mas01mc@754 90 Qin Lv, William Josephson, Zhe Wang, Moses Charikar and Kai Li,
mas01mc@754 91 "Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity
mas01mc@754 92 Search", Proc. Intl. Conf. VLDB, 2007
mas01mc@754 93
mas01mc@754 94 i = 1..M (number of hash functions used)
mas01mc@754 95 f_i(q) = a_i * q + b_i // projection to real line
mas01mc@754 96 h_i(q) = floor( ( a_i * q + b_i ) / w ) // hash slot
mas01mc@754 97 delta \in {-1, +1}
mas01mc@754 98 x_i( delta ) = distance of query to left or right boundary
mas01mc@754 99 Delta = [delta_1 delta_2 ... delta_M]
mas01mc@754 100 score(Delta) = sum_{i=1}_M x_i(delta_i).^2
mas01mc@754 101
mas01mc@754 102 z = sort(x(delta_i), increasing) // i = 1..2M delta={+1,-1}
mas01mc@754 103 p_j = (i, delta) if z_j == x_i(delta)
mas01mc@754 104
mas01mc@754 105 A_k is index into p_j
mas01mc@754 106
mas01mc@754 107 Multi-probe algorithm (after Lv et al. 2007)
mas01mc@754 108 ---------------------------------------------
mas01mc@754 109
mas01mc@754 110 A0 = {1}
mas01mc@754 111 minHeap.insert(A0, score(A0))
mas01mc@754 112
mas01mc@754 113 for i = 1 to T do // T = number of perturbation sets
mas01mc@754 114 repeat
mas01mc@754 115 Ai = minHeap.extractMin()
mas01mc@754 116 As = shift(Ai)
mas01mc@754 117 minHeap.insert(As, score(As))
mas01mc@754 118 Ae = expand(Ai)
mas01mc@754 119 minHeap.insert(Ae, score(Ae))
mas01mc@754 120 until valid(Ai)
mas01mc@754 121 output Ai
mas01mc@754 122 end for
mas01mc@754 123
mas01mc@754 124 */
mas01mc@754 125
mas01mc@754 126 #endif // __LSH_MULTIPROBE_