Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::BaseReservoirSampling Class Reference

Resevoir sampling is based on the 2005 paper "Weighted Random Sampling" by Efraimidis and Spirakis. More...

#include <duckdb.hpp>

Collaboration diagram for duckdb::BaseReservoirSampling:

Public Member Functions

 BaseReservoirSampling (int64_t seed)
void InitializeReservoirWeights (idx_t cur_size, idx_t sample_size)
void SetNextEntry ()
void ReplaceElementWithIndex (idx_t entry_index, double with_weight, bool pop=true)
void ReplaceElement (double with_weight=-1)
void UpdateMinWeightThreshold ()
void FillWeights (SelectionVector &sel, idx_t &sel_size)
 Go from the naive sampling to the reservoir sampling Naive samping will not collect weights, but when we serialize we need to serialize weights again.
unique_ptr< BaseReservoirSampling > Copy ()
void Serialize (Serializer &serializer) const

Static Public Member Functions

static unique_ptr< BaseReservoirSampling > Deserialize (Deserializer &deserializer)

Public Attributes

ReservoirRNG random
 The random generator.
idx_t next_index_to_sample
 The next element to sample.
double min_weight_threshold
 The reservoir threshold of the current min entry.
idx_t min_weighted_entry_index
 The reservoir index of the current min entry.
idx_t num_entries_to_skip_b4_next_sample
 The current count towards next index (i.e. we will replace an entry in next_index - current_count tuples) The number of entries "seen" before choosing one that will go in our reservoir sample.
idx_t num_entries_seen_total
 when collecting a sample in parallel, we want to know how many values each thread has seen so we can collect the samples from the thread local states in a uniform manner
std::priority_queue< std::pair< double, idx_t > > reservoir_weights
 Priority queue of [random element, index] for each of the elements in the sample.

Friends

class BlockingSample

Detailed Description

Resevoir sampling is based on the 2005 paper "Weighted Random Sampling" by Efraimidis and Spirakis.

Constructor & Destructor Documentation

◆ BaseReservoirSampling() [1/2]

duckdb::BaseReservoirSampling::BaseReservoirSampling ( int64_t seed)
explicit
208018 : random(seed) {
208024}
double min_weight_threshold
The reservoir threshold of the current min entry.
Definition duckdb.hpp:47781
idx_t next_index_to_sample
The next element to sample.
Definition duckdb.hpp:47779
idx_t min_weighted_entry_index
The reservoir index of the current min entry.
Definition duckdb.hpp:47783
idx_t num_entries_seen_total
when collecting a sample in parallel, we want to know how many values each thread has seen so we can ...
Definition duckdb.hpp:47789
idx_t num_entries_to_skip_b4_next_sample
The current count towards next index (i.e. we will replace an entry in next_index - current_count tup...
Definition duckdb.hpp:47786
ReservoirRNG random
The random generator.
Definition duckdb.hpp:47776

◆ BaseReservoirSampling() [2/2]

duckdb::BaseReservoirSampling::BaseReservoirSampling ( )
208026 : BaseReservoirSampling(1) {
208027}

Member Function Documentation

◆ InitializeReservoirWeights()

void duckdb::BaseReservoirSampling::InitializeReservoirWeights ( idx_t cur_size,
idx_t sample_size )

1: The first m items of V are inserted into R first we need to check if the reservoir already has "m" elements

  1. For each item vi ∈ R: Calculate a key ki = random(0, 1) we then define the threshold to enter the reservoir T_w as the minimum key of R we use a priority queue to extract the minimum key in O(1) time
  1. For each item vi ∈ R: Calculate a key ki = random(0, 1) we then define the threshold to enter the reservoir T_w as the minimum key of R we use a priority queue to extract the minimum key in O(1) time
208040 {
208046 if (cur_size == sample_size) {
208050 for (idx_t i = 0; i < sample_size; i++) {
208051 double k_i = random.NextRandom();
208052 reservoir_weights.emplace(-k_i, i);
208053 }
208054 SetNextEntry();
208055 }
208056}
void SetNextEntry()
Definition duckdb.cpp:208058
std::priority_queue< std::pair< double, idx_t > > reservoir_weights
Priority queue of [random element, index] for each of the elements in the sample.
Definition duckdb.hpp:47791
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ SetNextEntry()

void duckdb::BaseReservoirSampling::SetNextEntry ( )
  1. Let r = random(0, 1) and Xw = log(r) / log(T_w)
  1. From the current item vc skip items until item vi , such that:
  2. wc +wc+1 +···+wi−1 < Xw <= wc +wc+1 +···+wi−1 +wi since all our weights are 1 (uniform sampling), we can just determine the amount of elements to skip
208058 {
208059 D_ASSERT(!reservoir_weights.empty());
208061 auto &min_key = reservoir_weights.top();
208062 double t_w = -min_key.first;
208063 double r = random.NextRandom32();
208064 double x_w = log(r) / log(t_w);
208068 min_weight_threshold = t_w;
208069 min_weighted_entry_index = min_key.second;
208070 if (x_w >= 1.0 && std::isfinite(x_w)) {
208071 next_index_to_sample = idx_t(ceil(x_w));
208072 } else {
208074 }
208076}
void log(InputArray src, OutputArray dst)
Here is the caller graph for this function:

◆ ReplaceElementWithIndex()

void duckdb::BaseReservoirSampling::ReplaceElementWithIndex ( idx_t entry_index,
double with_weight,
bool pop = true )

now we insert the new weight into the reservoir

we update the min entry with the new min entry in the reservoir

208078 {
208079 if (pop) {
208080 reservoir_weights.pop();
208081 }
208082 double r2 = with_weight;
208084 reservoir_weights.emplace(-r2, entry_index);
208086 SetNextEntry();
208087}
Here is the call graph for this function:

◆ ReplaceElement()

void duckdb::BaseReservoirSampling::ReplaceElement ( double with_weight = -1)

replace the entry in the reservoir pop the minimum entry

now update the reservoir

  1. Let tw = Tw i , r2 = random(tw,1) and vi’s key: ki = (r2)1/wi
  2. The new threshold Tw is the new minimum key of R we generate a random number between (min_weight_threshold, 1)

if we are merging two reservoir samples use the weight passed

now we insert the new weight into the reservoir

we update the min entry with the new min entry in the reservoir

208089 {
208092 reservoir_weights.pop();
208097 double r2 = random.NextRandom(min_weight_threshold, 1);
208098
208100 if (with_weight >= 0) {
208101 r2 = with_weight;
208102 }
208106 SetNextEntry();
208107}
Here is the call graph for this function:

◆ UpdateMinWeightThreshold()

void duckdb::BaseReservoirSampling::UpdateMinWeightThreshold ( )
208109 {
208110 if (!reservoir_weights.empty()) {
208113 return;
208114 }
208116}

◆ FillWeights()

void duckdb::BaseReservoirSampling::FillWeights ( SelectionVector & sel,
idx_t & sel_size )

Go from the naive sampling to the reservoir sampling Naive samping will not collect weights, but when we serialize we need to serialize weights again.

208136 {
208137 if (!reservoir_weights.empty()) {
208138 return;
208139 }
208140 D_ASSERT(reservoir_weights.empty());
208141 auto weights = GenerateTopKFromUniform(random, num_entries_seen_total, sel_size);
208142 std::shuffle(weights.begin(), weights.end(), random);
208143 for (idx_t i = 0; i < sel_size; i++) {
208144 // clamp to smallest positive normal to avoid degenerate 0-weight entries
208145 // (floating-point underflow in GenerateTopKFromUniform for large k)
208146 auto weight = MaxValue(weights[i], std::numeric_limits<double>::min());
208147 reservoir_weights.emplace(-weight, i);
208148 }
208149 D_ASSERT(reservoir_weights.size() <= sel_size);
208150 SetNextEntry();
208151}
Here is the call graph for this function:

◆ Copy()

unique_ptr< BaseReservoirSampling > duckdb::BaseReservoirSampling::Copy ( )
208029 {
208030 auto ret = make_uniq<BaseReservoirSampling>(1);
208031 ret->reservoir_weights = reservoir_weights;
208032 ret->next_index_to_sample = next_index_to_sample;
208033 ret->min_weight_threshold = min_weight_threshold;
208034 ret->min_weighted_entry_index = min_weighted_entry_index;
208035 ret->num_entries_to_skip_b4_next_sample = num_entries_to_skip_b4_next_sample;
208036 ret->num_entries_seen_total = num_entries_seen_total;
208037 return ret;
208038}

◆ Serialize()

void duckdb::BaseReservoirSampling::Serialize ( Serializer & serializer) const
417244 {
417245 serializer.WritePropertyWithDefault<idx_t>(100, "next_index_to_sample", next_index_to_sample);
417246 serializer.WriteProperty<double>(101, "min_weight_threshold", min_weight_threshold);
417247 serializer.WritePropertyWithDefault<idx_t>(102, "min_weighted_entry_index", min_weighted_entry_index);
417248 serializer.WritePropertyWithDefault<idx_t>(103, "num_entries_to_skip_b4_next_sample", num_entries_to_skip_b4_next_sample);
417249 serializer.WritePropertyWithDefault<idx_t>(104, "num_entries_seen_total", num_entries_seen_total);
417250 serializer.WritePropertyWithDefault<std::priority_queue<std::pair<double, idx_t>>>(105, "reservoir_weights", reservoir_weights);
417251}

◆ Deserialize()

unique_ptr< BaseReservoirSampling > duckdb::BaseReservoirSampling::Deserialize ( Deserializer & deserializer)
static
417253 {
417254 auto result = duckdb::unique_ptr<BaseReservoirSampling>(new BaseReservoirSampling());
417255 deserializer.ReadPropertyWithDefault<idx_t>(100, "next_index_to_sample", result->next_index_to_sample);
417256 deserializer.ReadProperty<double>(101, "min_weight_threshold", result->min_weight_threshold);
417257 deserializer.ReadPropertyWithDefault<idx_t>(102, "min_weighted_entry_index", result->min_weighted_entry_index);
417258 deserializer.ReadPropertyWithDefault<idx_t>(103, "num_entries_to_skip_b4_next_sample", result->num_entries_to_skip_b4_next_sample);
417259 deserializer.ReadPropertyWithDefault<idx_t>(104, "num_entries_seen_total", result->num_entries_seen_total);
417260 deserializer.ReadPropertyWithDefault<std::priority_queue<std::pair<double, idx_t>>>(105, "reservoir_weights", result->reservoir_weights);
417261 return result;
417262}

The documentation for this class was generated from the following files: