trsl logo
trsl_example2.cpp
// (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)
//#define TRSL_USE_BSD_BETTER_RANDOM_GENERATOR
//#define TRSL_USE_SYSTEMATIC_INTUITIVE_ALGORITHM
#include <boost/random/uniform_real.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <vector>
#include <iostream>
#include <numeric> // accumulate
// In this example, population elements are floats, and an element's
// weight is the element itself. The weight accessor is:
struct wac
{
double operator()(float const& e) const
{
return e;
}
};
int main()
{
//-----------------------//
// Generate a population //
//-----------------------//
#define C_ARRAY_ITERATOR_SAMPLING
//#define STD_VECTOR_ITERATOR_SAMPLING
#if defined(C_ARRAY_ITERATOR_SAMPLING)
float population[] = { 0, 1, 4, 3, 5, 8, 2 };
const size_t POPULATION_SIZE = sizeof(population)/sizeof(float);
const size_t SAMPLE_SIZE = 3;
typedef float* population_iterator;
population_iterator populationIteratorBegin(population),
populationIteratorEnd(population + POPULATION_SIZE);
#elif defined(STD_VECTOR_ITERATOR_SAMPLING)
boost::mt19937 rng;
boost::uniform_real<float> dist(0, 100);
boost::variate_generator<
boost::mt19937&,
boost::uniform_real<float>
> uni(rng, dist);
const size_t POPULATION_SIZE = 100;
std::vector<float> population(POPULATION_SIZE);
std::generate(population.begin(), population.end(), uni);
const size_t SAMPLE_SIZE = 10;
typedef std::vector<float>::iterator population_iterator;
population_iterator populationIteratorBegin = population.begin(),
populationIteratorEnd = population.end();
#else
# error Please choose an example.
#endif
std::cout << "Population ("
<< std::distance(populationIteratorBegin,
populationIteratorEnd)
<< " elements):" << std::endl;
std::copy(populationIteratorBegin,
populationIteratorEnd,
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
{
//----------------------------//
// Sample from the population //
//----------------------------//
float,
double,
wac
> is_picked;
<is_picked, population_iterator> sample_iterator;
is_picked predicate(SAMPLE_SIZE,
std::accumulate(populationIteratorBegin,
populationIteratorEnd,
float(0)),
wac());
sample_iterator sampleIteratorBegin(predicate,
populationIteratorBegin,
populationIteratorEnd);
sample_iterator sampleIteratorEnd(predicate,
populationIteratorEnd,
populationIteratorEnd);
std::cout << "Sample of " << SAMPLE_SIZE << " elements:" << std::endl;
std::copy(sampleIteratorBegin,
sampleIteratorEnd,
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
//-------------------------------------------------------------------//
// Sample from the population (robust to patterns in the population) //
//-------------------------------------------------------------------//
float,
double,
wac
> is_picked;
<is_picked, population_iterator> sample_iterator;
is_picked predicate(SAMPLE_SIZE,
std::accumulate(populationIteratorBegin,
populationIteratorEnd,
float(0)),
wac());
sample_iterator sampleIteratorBegin(predicate,
populationIteratorBegin,
populationIteratorEnd);
sample_iterator sampleIteratorEnd = sampleIteratorBegin.end();
std::cout << "Probabilistic sample of " << SAMPLE_SIZE << " elements:" << std::endl;
std::copy(sampleIteratorBegin,
sampleIteratorEnd,
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
//-------------------------------------//
// Get a permutation of the population //
//-------------------------------------//
<population_iterator> permutation_iterator;
{
permutation_iterator pi =
trsl::random_permutation_iterator(populationIteratorBegin,
populationIteratorEnd);
std::cout << "Permutation:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
permutation_iterator pi =
trsl::random_permutation_iterator(populationIteratorBegin,
populationIteratorEnd,
SAMPLE_SIZE);
std::cout << "Permutation of a subset of " << SAMPLE_SIZE
<< " elements:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
}
{
//---------------------//
// Sort the population //
//---------------------//
<population_iterator> permutation_iterator;
{
permutation_iterator pi =
trsl::sort_iterator(populationIteratorBegin,
populationIteratorEnd);
std::cout << "Sorted population:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
{
permutation_iterator pi =
trsl::sort_iterator(populationIteratorBegin,
populationIteratorEnd,
std::less<float>(), SAMPLE_SIZE);
std::cout << "Sorted " << SAMPLE_SIZE
<< " smallest elements:" << std::endl;
std::copy(pi,
pi.end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
}
return 0;
}
trsl::sort_iterator
reorder_iterator< ElementIterator > sort_iterator(ElementIterator first, ElementIterator last, ElementComparator comp, unsigned permutationSize)
Constructs a reorder_iterator that will iterate through the first permutationSize elements of a sorte...
Definition: sort_iterator.hpp:76
ppfilter_iterator.hpp
random_permutation_iterator.hpp
sort_iterator.hpp
trsl::is_picked_systematic
Functor to use with persistent_filter_iterator for systematic sampling of a range.
Definition: is_picked_systematic.hpp:77
trsl::ppfilter_iterator
Random permutation, persistent filter iterator.
Definition: ppfilter_iterator.hpp:21
is_picked_systematic.hpp
trsl::reorder_iterator
Provides an iterator over a permutation of a range.
Definition: reorder_iterator.hpp:31
trsl::random_permutation_iterator
reorder_iterator< ElementIterator > random_permutation_iterator(ElementIterator first, ElementIterator last, unsigned permutationSize)
Constructs a reorder_iterator that will iterate through a random subset of size permutationSize of a ...
Definition: random_permutation_iterator.hpp:40
trsl::persistent_filter_iterator
Adaptation of boost::filter_iterator to allow an element to be selected multiple times.
Definition: persistent_filter_iterator.hpp:30
© 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.