Vcsn  2.3
Be Rational
random-label.hh
Go to the documentation of this file.
1 #pragma once
2 
6 #include <vcsn/misc/irange.hh>
7 #include <vcsn/misc/raise.hh>
8 #include <vcsn/misc/random.hh>
9 #include <vcsn/misc/set.hh>
10 
11 namespace vcsn
12 {
14  template <typename RandomGenerator = std::default_random_engine>
15  typename oneset::value_t
16  random_label(const oneset& ls,
17  RandomGenerator& = RandomGenerator())
18  {
19  return ls.one();
20  }
21 
22 
24  template <typename... LabelSet,
25  typename RandomGenerator = std::default_random_engine>
26  typename tupleset<LabelSet...>::value_t
28  RandomGenerator& gen = RandomGenerator())
29  {
30  return random_label_(ls, gen, ls.indices);
31  }
32 
33 
35  template <typename... LabelSet,
36  size_t... I,
37  typename RandomGenerator = std::default_random_engine>
38  typename tupleset<LabelSet...>::value_t
40  RandomGenerator& gen,
42  {
43  // No need to check for the emptiness here: it will be checked in
44  // each sub-labelset.
45  return ls.tuple(random_label(ls.template set<I>(), gen)...);
46  }
47 
48 
50  template <typename GenSet,
51  typename RandomGenerator = std::default_random_engine>
52  typename wordset<GenSet>::value_t
54  RandomGenerator& gen = RandomGenerator())
55  {
56  require(!ls.generators().empty(),
57  "random_label: the alphabet needs at least 1 letter");
58  auto dis = std::uniform_int_distribution<>(0, 5);
59  auto res_label = ls.one();
60  auto pick = make_random_selector(gen);
61  for (auto _: detail::irange(dis(gen)))
62  res_label = ls.mul(res_label, ls.value(pick(ls.generators())));
63  return res_label;
64  }
65 
66 
68  template <typename LabelSet,
69  typename RandomGenerator = std::default_random_engine>
70  typename LabelSet::value_t
71  random_label(const LabelSet& ls,
72  RandomGenerator& gen = RandomGenerator())
73  {
74  require(!ls.generators().empty(),
75  "random_label: the alphabet needs at least 1 letter");
76  // Pick a member of a container following a uniform distribution.
77  auto pick = make_random_selector(gen);
78  return ls.value(pick(ls.generators()));
79  }
80 
81 
83  template <typename LabelSet,
84  typename RandomGenerator = std::default_random_engine>
85  typename nullableset<LabelSet>::value_t
87  RandomGenerator& gen = RandomGenerator())
88  {
89  // FIXME: the proportion should be controllable.
90  auto dis = std::bernoulli_distribution(0.5);
91  if (dis(gen) || ls.generators().empty())
92  return ls.one();
93  else
94  return ls.value(random_label(*ls.labelset(), gen));
95  }
96 
97 
99  template <typename Context,
100  typename RandomGenerator = std::default_random_engine>
101  typename expressionset<Context>::value_t
103  RandomGenerator& gen = RandomGenerator())
104  {
105  return rs.atom(random_label(*rs.labelset(), gen));
106  }
107 }
value_t value(Args &&...args) const
Definition: nullableset.hh:299
auto mul(Args &&...args) const -> decltype(this->genset() -> mul(std::forward< Args >(args)...))
bool value_t
Definition: oneset.hh:20
static value_t one()
Definition: oneset.hh:102
value_t value(Args &&...args) const
Value constructor.
Definition: wordset.hh:83
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
Implementation of labels are nullables (letter or empty).
Definition: fwd.hh:14
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:16
static ATTRIBUTE_PURE constexpr value_t one()
Definition: nullableset.hh:239
auto rs
Definition: lift.hh:152
Definition: a-star.hh:8
const genset_t & generators() const
static value_t one()
Definition: wordset.hh:176
tupleset< LabelSet... >::value_t random_label_(const tupleset< LabelSet... > &ls, RandomGenerator &gen, detail::index_sequence< I... >)
Implementation detail for random label from tupleset.
Definition: random-label.hh:39
weightset_mixin< detail::tupleset_impl< LabelSets... >> tupleset
Definition: fwd.hh:27
oneset::value_t random_label(const oneset &ls, RandomGenerator &=RandomGenerator())
Random label from oneset.
Definition: random-label.hh:16
Implementation of labels are words.
Definition: fwd.hh:31
genset_t generators() const
The generators. Meaningful for labelsets only.
Definition: nullableset.hh:265
const labelset_ptr labelset() const
Definition: nullableset.hh:292
random_selector< RandomGenerator > make_random_selector(RandomGenerator &g)
Definition: random.hh:88
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
auto irange(Integer last)
Generate an integer range.
Definition: irange.hh:11