-
Notifications
You must be signed in to change notification settings - Fork 700
/
weighted_best.go
79 lines (66 loc) · 1.71 KB
/
weighted_best.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package sampler
import (
"errors"
"math"
"time"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
safemath "github.com/ava-labs/avalanchego/utils/math"
)
var (
errNoValidWeightedSamplers = errors.New("no valid weighted samplers found")
_ Weighted = (*weightedBest)(nil)
)
// Sampling is performed by using another implementation of the Weighted
// interface.
//
// Initialization attempts to find the best sampling algorithm given the dataset
// by performing a benchmark of the provided implementations.
type weightedBest struct {
Weighted
samplers []Weighted
benchmarkIterations int
clock mockable.Clock
}
func (s *weightedBest) Initialize(weights []uint64) error {
totalWeight := uint64(0)
for _, weight := range weights {
newWeight, err := safemath.Add64(totalWeight, weight)
if err != nil {
return err
}
totalWeight = newWeight
}
samples := []uint64(nil)
if totalWeight > 0 {
samples = make([]uint64, s.benchmarkIterations)
for i := range samples {
samples[i] = globalRNG.Uint64Inclusive(totalWeight - 1)
}
}
s.Weighted = nil
bestDuration := time.Duration(math.MaxInt64)
samplerLoop:
for _, sampler := range s.samplers {
if err := sampler.Initialize(weights); err != nil {
continue
}
start := s.clock.Time()
for _, sample := range samples {
if _, err := sampler.Sample(sample); err != nil {
continue samplerLoop
}
}
end := s.clock.Time()
duration := end.Sub(start)
if duration < bestDuration {
bestDuration = duration
s.Weighted = sampler
}
}
if s.Weighted == nil {
return errNoValidWeightedSamplers
}
return nil
}