-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from StephenButtolph/sampling
added new sampling algos and optimized initializations
- Loading branch information
Showing
14 changed files
with
285 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// (c) 2019-2020, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package sampler | ||
|
||
import ( | ||
"errors" | ||
"math" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/ava-labs/gecko/utils/timer" | ||
) | ||
|
||
var ( | ||
errNoValidUniformSamplers = errors.New("no valid uniform samplers found") | ||
) | ||
|
||
func init() { rand.Seed(time.Now().UnixNano()) } | ||
|
||
// uniformBest implements the Uniform interface. | ||
// | ||
// Sampling is performed by using another implementation of the Uniform | ||
// interface. | ||
// | ||
// Initialization attempts to find the best sampling algorithm given the dataset | ||
// by performing a benchmark of the provided implementations. | ||
type uniformBest struct { | ||
Uniform | ||
samplers []Uniform | ||
maxSampleSize int | ||
benchmarkIterations int | ||
clock timer.Clock | ||
} | ||
|
||
// NewBestUniform returns a new sampler | ||
func NewBestUniform(expectedSampleSize int) Uniform { | ||
return &uniformBest{ | ||
samplers: []Uniform{ | ||
&uniformReplacer{}, | ||
&uniformResample{}, | ||
}, | ||
maxSampleSize: expectedSampleSize, | ||
benchmarkIterations: 100, | ||
} | ||
} | ||
|
||
func (s *uniformBest) Initialize(length uint64) error { | ||
s.Uniform = nil | ||
bestDuration := time.Duration(math.MaxInt64) | ||
|
||
sampleSize := s.maxSampleSize | ||
if length < uint64(sampleSize) { | ||
sampleSize = int(length) | ||
} | ||
|
||
samplerLoop: | ||
for _, sampler := range s.samplers { | ||
if err := sampler.Initialize(length); err != nil { | ||
continue | ||
} | ||
|
||
start := s.clock.Time() | ||
for i := 0; i < s.benchmarkIterations; i++ { | ||
if _, err := sampler.Sample(sampleSize); err != nil { | ||
continue samplerLoop | ||
} | ||
} | ||
end := s.clock.Time() | ||
duration := end.Sub(start) | ||
if duration < bestDuration { | ||
bestDuration = duration | ||
s.Uniform = sampler | ||
} | ||
} | ||
|
||
if s.Uniform == nil { | ||
return errNoValidUniformSamplers | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// (c) 2019-2020, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package sampler | ||
|
||
import ( | ||
"math" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func init() { rand.Seed(time.Now().UnixNano()) } | ||
|
||
// uniformResample allows for sampling over a uniform distribution without | ||
// replacement. | ||
// | ||
// Sampling is performed by sampling with replacement and resampling if a | ||
// duplicate is sampled. | ||
// | ||
// Initialization takes O(1) time. | ||
// | ||
// Sampling is performed in O(count) time and O(count) space. | ||
type uniformResample struct { | ||
length uint64 | ||
} | ||
|
||
func (s *uniformResample) Initialize(length uint64) error { | ||
if length > math.MaxInt64 { | ||
return errOutOfRange | ||
} | ||
s.length = length | ||
return nil | ||
} | ||
|
||
func (s *uniformResample) Sample(count int) ([]uint64, error) { | ||
if count < 0 || s.length < uint64(count) { | ||
return nil, errOutOfRange | ||
} | ||
|
||
drawn := make(map[uint64]struct{}, count) | ||
results := make([]uint64, count) | ||
for i := 0; i < count; i++ { | ||
draw := uint64(rand.Int63n(int64(s.length))) | ||
if _, ok := drawn[draw]; ok { | ||
i-- | ||
continue | ||
} | ||
drawn[draw] = struct{}{} | ||
|
||
results[i] = draw | ||
} | ||
|
||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.