#include <examples/Particle.hpp>
#include <vector>
#include <iostream>
#include <numeric>
#include <cassert>
using namespace trsl::example;
is_picked, std::vector<Particle>::const_iterator
> sample_iterator;
int main()
{
const size_t POPULATION_SIZE = 100;
const size_t SAMPLE_SIZE = 10;
std::vector<Particle> population;
double totalWeight = 0;
for (size_t i = 0; i < POPULATION_SIZE; ++i)
{
Particle p(double(rand())/RAND_MAX,
double(rand())/RAND_MAX,
double(rand())/RAND_MAX);
totalWeight += p.getWeight();
population.push_back(p);
}
for (std::vector<Particle>::iterator i = population.begin();
i != population.end(); ++i)
i->setWeight(i->getWeight()/totalWeight);
std::vector<Particle> const& const_pop = population;
std::vector<Particle> sample;
is_picked predicate(SAMPLE_SIZE, 1.0, &Particle::getWeight);
std::cout << "Mean weight: " << 1.0/POPULATION_SIZE << std::endl;
for (sample_iterator
sb = sample_iterator(predicate, const_pop.begin(), const_pop.end()),
si = sb,
se = sample_iterator(predicate, const_pop.end(), const_pop.end());
si != se; ++si)
{
std::cout << "sample_" << std::distance(sb, si) << "'s weight = " <<
si->getWeight() << std::endl;
Particle p = *si;
p.setWeight(1);
sample.push_back(p);
}
assert(sample.size() == SAMPLE_SIZE);
return 0;
}