Example #1
0
def bruteForceNearestNeighbor(S, y):
    best, nn = float("inf"), None
    for x in S:
        if dist(x, y) < best:
            best = dist(x, y)
            nn = x
    return nn
Example #2
0
def bruteForceNearestNeighbor(S,y):
    best, nn = float("inf"), None
    for x in S:
        if dist(x,y) < best:
            best = dist(x,y)
            nn = x
    return nn
Example #3
0
def bruteForceNearestNeighbor(S, y):
    best, nn = float("inf"), None
    for x in S:
        if dist(x, y) < best:
            best = dist(x, y)
            nn = x
    return nn


print "Testing correctness..."
for y in xrange(100):
    y = random.getrandbits(d)
    x1 = nearestNeighbor(D, r, y)
    x2 = bruteForceNearestNeighbor(S, y)
    if dist(y, x2) <= r:
        if x1 == None: print(bin(y), None, bin(x2))
        elif dist(y, x1) != dist(y, x2):
            print(bin(y), bin(x1), bin(x2))

print "Testing speed..."
start = time.clock()
for y in xrange(repetitions):
    y = random.getrandbits(d)
    x1 = bruteForceNearestNeighbor(S, y)
print "Brute force:", int(repetitions /
                          (time.clock() - start)), "queries per second"

start = time.clock()
for y in xrange(repetitions):
    y = random.getrandbits(d)
Example #4
0
D = buildDataStructure(S,r)

def bruteForceNearestNeighbor(S,y):
    best, nn = float("inf"), None
    for x in S:
        if dist(x,y) < best:
            best = dist(x,y)
            nn = x
    return nn

print "Testing correctness..."
for y in xrange(100):
    y = random.getrandbits(d)
    x1 = nearestNeighbor(D,r,y)
    x2 = bruteForceNearestNeighbor(S,y)
    if dist(y,x2) <= r:
        if x1==None: print (bin(y),None,bin(x2))
        elif dist(y,x1)!=dist(y,x2):
            print (bin(y),bin(x1),bin(x2))

print "Testing speed..."
start = time.clock()
for y in xrange(repetitions):
    y = random.getrandbits(d)
    x1 = bruteForceNearestNeighbor(S,y)
print "Brute force:",int(repetitions/(time.clock()-start)),"queries per second"

start = time.clock()
for y in xrange(repetitions):
    y = random.getrandbits(d)
    x1 = nearestNeighbor(D,r,y)