trsl logo
trsl_example1plus.cpp

This example is an "advanced" version of trsl_example1.cpp. Instead of creating the sample iterators within the code body, we hide the creation detail within a population class.

// (C) Copyright Renaud Detry 2007-2011.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <cassert>
#include <functional>
#include <examples/Particle.hpp>
#include <examples/ParticleCollection.hpp>
int main()
{
using namespace trsl::example;
const size_t POPULATION_SIZE = 100;
const size_t SAMPLE_SIZE = 10;
//-----------------------//
// Generate a population //
//-----------------------//
ParticleCollection population;
for (size_t i = 0; i < POPULATION_SIZE; ++i)
{
Particle p(double(rand())/RAND_MAX, // weight
double(rand())/RAND_MAX, // position (x)
double(rand())/RAND_MAX); // position (y)
population.add(p);
}
//----------------------------//
// Sample from the population //
//----------------------------//
ParticleCollection sample;
//-- population contains 100 elements. --//
for (ParticleCollection::const_sample_iterator
si = population.sample_begin(SAMPLE_SIZE),
sb = si,
se = population.sample_end();
si != se; ++si)
{
std::cout << "sample_" << std::distance(sb, si) << "'s weight = " <<
si->getWeight() << std::endl;
Particle p = *si;
p.setWeight(1);
sample.add(p);
// ... or do something else with *si ...
}
//-- sample contains 10 elements. --//
assert(sample.size() == SAMPLE_SIZE);
return 0;
}
is_picked_systematic.hpp
© Copyright 2007-2011 Renaud Detry.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt.)
Revised Wed Jan 8 2020 14:43:31.