Commit f3fa9ab7 authored by Jean-Baptiste Mouret's avatar Jean-Baptiste Mouret
Browse files

make the new grand API compatible with int

parent 108bc1f4
......@@ -18,11 +18,11 @@ namespace limbo {
template <typename StateFunction, typename AggregatorFunction, typename Opt>
void operator()(const StateFunction& seval, const AggregatorFunction&, Opt& opt) const
{
auto rgen = tools::make_rgen(0.0, 1.0);
tools::rgen_double_t rgen(0.0, 1.0);
for (int i = 0; i < Params::init_randomsampling::samples(); i++) {
Eigen::VectorXd new_sample(StateFunction::dim_in);
for (size_t j = 0; j < StateFunction::dim_in; j++)
new_sample[j] = tools::rand_double(rgen);
new_sample[j] = rgen.rand();
opt.add_new_sample(new_sample, seval(new_sample));
}
}
......
......@@ -18,10 +18,11 @@ namespace limbo {
template <typename StateFunction, typename AggregatorFunction, typename Opt>
void operator()(const StateFunction& seval, const AggregatorFunction&, Opt& opt) const
{
tools::rgen_int_t rgen(0, Params::init_randomsamplinggrid::bins());
for (int i = 0; i < Params::init_randomsamplinggrid::samples(); i++) {
Eigen::VectorXd new_sample(StateFunction::dim_in);
for (size_t i = 0; i < StateFunction::dim_in; i++)
new_sample[i] = int(((double)(Params::init_randomsamplinggrid::bins() + 1) * rand()) / (RAND_MAX + 1.0)) / double(Params::init_randomsamplinggrid::bins());
new_sample[i] = rgen.rand() / double(Params::init_randomsamplinggrid::bins());
opt.add_new_sample(new_sample, seval(new_sample));
}
}
......
......@@ -48,22 +48,25 @@
namespace limbo {
namespace tools {
// usage :
// auto rgen = make_rgen();
// double r = rand_double(rgen);
using rand_gen_t = std::mt19937;
using uniform_dist_t = std::uniform_real_distribution<double>;
using rdist_t = std::pair<rand_gen_t, uniform_dist_t>;
inline rdist_t make_rgen(double min, double max)
{
std::random_device rd;
return std::make_pair(std::mt19937(rd()), std::uniform_real_distribution<double>(min, max));
}
inline double rand_double(rdist_t& rgen)
{
rand_gen_t& gen = std::get<0>(rgen);
uniform_dist_t& dist = std::get<1>(rgen);
return dist(gen);
}
// rgen_double_t(0.0, 1.0);
// double r = rgen.rand();
template<typename D>
class RandomGenerator {
public:
using result_type = typename D::result_type;
RandomGenerator(result_type min, result_type max) :
_dist(min, max), _rgen(std::random_device()()) {}
result_type rand() { return _dist(_rgen); }
private:
D _dist;
std::mt19937 _rgen;
};
using rdist_double_t = std::uniform_real_distribution<double>;
using rdist_int_t = std::uniform_int_distribution<int>;
using rgen_double_t = RandomGenerator<rdist_double_t>;
using rgen_int_t = RandomGenerator<rdist_int_t>;
// get sign of number
template <typename T>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment