-
Notifications
You must be signed in to change notification settings - Fork 110
/
nearestNeighbor.go
162 lines (146 loc) · 3.14 KB
/
nearestNeighbor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package motionplan
import (
"context"
"math"
"sort"
"sync"
"go.viam.com/utils"
"go.viam.com/rdk/referenceframe"
)
const neighborsBeforeParallelization = 1000
type neighborManager struct {
nnKeys chan node
neighbors chan *neighbor
nnLock sync.RWMutex
seedPos []referenceframe.Input
ready bool
nCPU int
}
type neighbor struct {
dist float64
node node
}
//nolint:revive
func kNearestNeighbors(planOpts *PlannerOptions, rrtMap map[node]node, target []referenceframe.Input, neighborhoodSize int) []*neighbor {
kNeighbors := neighborhoodSize
if neighborhoodSize > len(rrtMap) {
kNeighbors = len(rrtMap)
}
allCosts := make([]*neighbor, 0)
for node := range rrtMap {
_, dist := planOpts.DistanceFunc(&ConstraintInput{
StartInput: target,
EndInput: node.Q(),
})
allCosts = append(allCosts, &neighbor{dist: dist, node: node})
}
sort.Slice(allCosts, func(i, j int) bool {
return allCosts[i].dist < allCosts[j].dist
})
return allCosts[:kNeighbors]
}
func (nm *neighborManager) nearestNeighbor(
ctx context.Context,
planOpts *PlannerOptions,
seed []referenceframe.Input,
rrtMap map[node]node,
) node {
if len(rrtMap) > neighborsBeforeParallelization && nm.nCPU > 1 {
// If the map is large, calculate distances in parallel
return nm.parallelNearestNeighbor(ctx, planOpts, seed, rrtMap)
}
bestDist := math.Inf(1)
var best node
for k := range rrtMap {
_, dist := planOpts.DistanceFunc(&ConstraintInput{
StartInput: seed,
EndInput: k.Q(),
})
if dist < bestDist {
bestDist = dist
best = k
}
}
return best
}
func (nm *neighborManager) parallelNearestNeighbor(
ctx context.Context,
planOpts *PlannerOptions,
seed []referenceframe.Input,
rrtMap map[node]node,
) node {
nm.ready = false
nm.seedPos = seed
nm.startNNworkers(ctx, planOpts)
defer close(nm.nnKeys)
defer close(nm.neighbors)
for k := range rrtMap {
nm.nnKeys <- k
}
nm.nnLock.Lock()
nm.ready = true
nm.nnLock.Unlock()
var best node
bestDist := math.Inf(1)
returned := 0
for returned < nm.nCPU {
select {
case <-ctx.Done():
return nil
default:
}
select {
case nn := <-nm.neighbors:
returned++
if nn.dist < bestDist {
bestDist = nn.dist
best = nn.node
}
default:
}
}
return best
}
func (nm *neighborManager) startNNworkers(ctx context.Context, planOpts *PlannerOptions) {
nm.neighbors = make(chan *neighbor, nm.nCPU)
nm.nnKeys = make(chan node, nm.nCPU)
for i := 0; i < nm.nCPU; i++ {
utils.PanicCapturingGo(func() {
nm.nnWorker(ctx, planOpts)
})
}
}
func (nm *neighborManager) nnWorker(ctx context.Context, planOpts *PlannerOptions) {
var best node
bestDist := math.Inf(1)
for {
select {
case <-ctx.Done():
return
default:
}
select {
case k := <-nm.nnKeys:
if k != nil {
nm.nnLock.RLock()
_, dist := planOpts.DistanceFunc(&ConstraintInput{
StartInput: nm.seedPos,
EndInput: k.Q(),
})
nm.nnLock.RUnlock()
if dist < bestDist {
bestDist = dist
best = k
}
}
default:
nm.nnLock.RLock()
if nm.ready {
nm.nnLock.RUnlock()
nm.neighbors <- &neighbor{bestDist, best}
return
}
nm.nnLock.RUnlock()
}
}
}