Skip to content

Commit

Permalink
up knn test
Browse files Browse the repository at this point in the history
  • Loading branch information
exAClior committed Aug 15, 2024
1 parent 5e6cecd commit ee6f618
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/TinyHNSW.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Distances, Graphs
using Random

export NaiveHeurestic, NNHeurestic, HNSW, insert!
export select_neighbors, search_layer
export select_neighbors, search_layer, knn_search

include("hnsw.jl")
include("graph.jl")
Expand Down
24 changes: 24 additions & 0 deletions test/hnsw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ using Test, TinyHNSW, TinyHNSW.Distances, TinyHNSW.Graphs, TinyHNSW.Random

using TinyHNSW: assignl, enterpoint

@testset "Find KNN" begin
d = 10
xmax = 10
db = [tuple(2 * xmax * rand(d) .- xmax...) for _ in 1:10]
M = 16
M_max = 5
mL = 1 / log(M)
efConstruction = 100
method = NaiveHeurestic()
dist = Euclidean()
hnsw = HNSW(db, M, M_max, efConstruction, mL, method, dist)

target = tuple(2*xmax*rand(d) .- xmax...)

k = 2
knn_idx = knn_search(hnsw, target, k, efConstruction)

knn_res = sort(db[knn_idx], by=x->dist(target, x))

true_res = sort(db, by=x->dist(target, x))[1:k]

@test length(intersect(knn_res, true_res)) == k
end

@testset "Constructor" begin
M = 3
M_max = 6
Expand Down

0 comments on commit ee6f618

Please sign in to comment.