Skip to content

Commit

Permalink
search layer given a cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed Jun 4, 2024
1 parent 3d0a643 commit 1b817ef
Showing 1 changed file with 95 additions and 2 deletions.
97 changes: 95 additions & 2 deletions pkg/hnsw/hnsw_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package hnsw

import "testing"
import (
"reflect"
"testing"
)

/*
var clusterA = []Point{
{0.2, 0.5},
{0.2, 0.7},
Expand All @@ -17,6 +19,7 @@ var clusterA = []Point{
{0.01, 0.3},
}

/*
var clusterB = []Point{
{4.2, 3.5},
{4.2, 4.7},
Expand Down Expand Up @@ -64,4 +67,94 @@ func TestHnsw_SearchLevel(t *testing.T) {
t.Fatalf("expected item id to be %v, got %v", 1, closestItem.id)
}
})

t.Run("cluster a search", func(t *testing.T) {
efc := uint(4)

entryPoint := Point{0, 0}
g := NewHnsw(2, efc, 4, entryPoint)

for idx, point := range clusterA {
pointId := Id(idx + 1)
g.points[pointId] = &point
g.friends[pointId] = NewFriends(0)

distEntryToClusterPoint := EuclidDistance(entryPoint, point)
g.friends[Id(0)].InsertFriendsAtLevel(0, pointId, distEntryToClusterPoint)
g.friends[pointId].InsertFriendsAtLevel(0, Id(0), distEntryToClusterPoint)
}

for idx, pointA := range clusterA {
for jdx, pointB := range clusterA {
if idx == jdx {
continue
}

pointAId := Id(idx + 1)
pointBId := Id(jdx + 1)

distAToB := EuclidDistance(pointA, pointB)
g.friends[pointAId].InsertFriendsAtLevel(0, pointBId, distAToB)
g.friends[pointBId].InsertFriendsAtLevel(0, pointAId, distAToB)
}
}

for kdx := range clusterA {
pointId := Id(kdx + 1)
friends, err := g.friends[pointId].GetFriendsAtLevel(0)
if err != nil {
t.Fatal(err)
}

for friends.Len() > int(efc) {
friends.Pop()
}

if friends.Len() != int(efc) {
t.Fatalf("expected a dynamic list of 4 friends per level per node, got %v", friends.Len())
}
}

qPoint := clusterA[3]
expectedId := Id(4)

expectedPoint := g.points[expectedId]

if !reflect.DeepEqual(qPoint, *expectedPoint) {
t.Fatalf("expected point to be %v, got %v", expectedPoint, qPoint)
}

closestNeighbor, err := g.searchLevel(&qPoint, &Item{
id: 0,
dist: EuclidDistance(entryPoint, qPoint),
}, 1, 0)

if err != nil {
t.Fatal(err)
}

if closestNeighbor.IsEmpty() {
t.Fatalf("expected # of neighbors to return to be 1, got %v", closestNeighbor)
}

closestItem, err := closestNeighbor.PopItem()

if err != nil {
t.Fatal(err)
}

if closestItem.id != expectedId {
t.Fatalf("expected the closest item to be the 3rd point in Cluster a, got %v", closestItem.id)
}

})
}

/*
*/

0 comments on commit 1b817ef

Please sign in to comment.