lundi 17 avril 2017

Discrete sampling from a set of structures based on a member value

I got a set of structures of the type

struct S {
    double x;
    double y;
    double weight;
};

Now I want to use a discrete_distribution to repeatedly sample from these structures based on their weight values, using something like the following snippet:

std::random_device rd;
std::mt19937 gen(rd());
std::discrete_distribution<> GetDistribution();
std::map<int, int> m;
for(int n=0; n<10000; ++n) {
    ++m[d(gen)];
}

Here is my first approach in pseudocode:

listOfStructures = GetListOfAllStructures()
listOfWeights = CreateListOfAllWeights()
for each iteration:
    sampledWeight = DiscretelySampleFrom(listOfWeights)
    for each structure in listOfStructures:
        if structure.weight equals sampledWeight:
           sampledStructures.add(structure)
    sampledStructure = UniformlySampleFrom(sampledStructures)
    DoSomethingWith(sampledStructure)

Writing it down like that, it all seems a bit inefficient. So here are my two questions:

  1. Is there a better approach to do that?
  2. If there isn't, how can this approach be implemented most efficiently in space and time?



Aucun commentaire:

Enregistrer un commentaire