17 #ifndef AWALI_ALGOS_RANDOM_HH
18 # define AWALI_ALGOS_RANDOM_HH
29 namespace awali {
namespace sttc {
37 template <
typename RandomGenerator = std::default_random_engine>
40 RandomGenerator& = RandomGenerator())
45 template <
typename... LabelSet,
46 typename RandomGenerator = std::default_random_engine>
47 typename tupleset<LabelSet...>::value_t
49 RandomGenerator& gen = RandomGenerator())
54 template <
typename... LabelSet,
56 typename RandomGenerator = std::default_random_engine>
57 typename tupleset<LabelSet...>::value_t
66 template <
typename LabelSet,
67 typename RandomGenerator = std::default_random_engine>
68 typename LabelSet::value_t
70 RandomGenerator& gen = RandomGenerator())
74 return ls.value(pick(ls.genset()));
77 template <
typename LabelSet,
78 typename RandomGenerator = std::default_random_engine>
81 RandomGenerator& gen = RandomGenerator())
83 std::uniform_int_distribution<> dis(0, 1);
90 template <
typename Context,
91 typename RandomGenerator = std::default_random_engine>
94 RandomGenerator& gen = RandomGenerator())
103 template <
typename Ctx>
106 unsigned num_states,
float density = 0.1,
107 unsigned num_initial = 1,
unsigned num_final = 1)
109 require(0 <= density && density <= 1,
110 "random: density must be in [0,1]");
112 automaton_t res = make_shared_ptr<automaton_t>(ctx);
115 std::random_device rd;
117 if (getenv(
"AWALI_SEED"))
118 seed = std::mt19937::default_seed;
119 std::mt19937 gen(seed);
121 std::vector<state_t>
states;
122 states.reserve(num_states);
124 std::vector<int> state_randomizer;
125 state_randomizer.reserve(num_states);
130 using state_set = std::set<int>;
133 state_set unreachables;
135 for (
unsigned i = 0; i < num_states; ++i)
137 states.push_back(res->add_state());
138 state_randomizer.push_back(i);
141 unreachables.emplace(i);
143 res->set_initial(
states[i]);
148 for (
unsigned i = 0; i < num_final; ++i)
150 std::uniform_int_distribution<> dis(i, num_states - 1);
151 int index = dis(gen);
152 res->set_final(
states[state_randomizer[index]]);
155 std::swap(state_randomizer[index], state_randomizer[i]);
161 std::binomial_distribution<> bin(num_states - 1, density);
166 while (!worklist.empty())
168 auto src =
states[*worklist.begin()];
169 worklist.erase(worklist.begin());
173 unsigned nsucc = 1 + bin(gen);
178 bool saw_unreachable =
false;
179 auto possibilities = num_states;
188 && !unreachables.empty())
191 dst = pick.
pop(unreachables);
192 worklist.insert(dst);
197 std::uniform_int_distribution<> dis(0, possibilities - 1);
198 int index = dis(gen);
201 dst = state_randomizer[index];
205 std::swap(state_randomizer[index],
206 state_randomizer[possibilities]);
208 state_set::iterator j = unreachables.find(dst);
209 if (j != unreachables.end())
211 worklist.insert(dst);
212 unreachables.erase(j);
213 saw_unreachable =
true;
216 res->add_transition(src,
states[dst],
220 return std::move(res);
227 template <
typename Ctx>
231 require(0 < num_states,
"num_states must be > 0");
234 automaton_t res = make_shared_ptr<automaton_t>(ctx);
236 std::random_device rd;
238 if (getenv(
"AWALI_SEED"))
239 seed = std::mt19937::default_seed;
240 std::mt19937 gen(seed);
241 std::uniform_int_distribution<int> distrib(0, num_states - 1);
243 std::vector<state_t>
states;
244 states.reserve(num_states);
246 for (
unsigned i = 0; i < num_states; ++i)
247 states.push_back(res->add_state());
249 for (
unsigned i = 0; i < num_states; ++i)
250 for (
auto l : ctx.labelset()->genset())
252 ctx.weightset()->one());
254 res->set_initial(
states[distrib(gen)]);
255 res->set_final(
states[distrib(gen)]);
Implementation of labels are nullables (letter or empty).
Definition: nullableset.hh:189
typename helper_t::value_t value_t
Definition: nullableset.hh:197
static constexpr ATTRIBUTE_PURE value_t one()
Definition: nullableset.hh:257
const labelset_ptr labelset() const
Definition: nullableset.hh:281
value_t value(Args &&... args) const
Definition: nullableset.hh:288
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:38
empty_t value_t
Definition: oneset.hh:41
static empty_t one()
Definition: oneset.hh:133
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
static constexpr indices_t indices
Definition: tupleset.hh:84
value_t value(const std::tuple< Args... > &args) const
Construct a value.
Definition: tupleset.hh:174
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
mutable_automaton< Ctx > random_deterministic(const Ctx &ctx, unsigned num_states)
Definition: random.hh:229
mutable_automaton< Ctx > random(const Ctx &ctx, unsigned num_states, float density=0.1, unsigned num_initial=1, unsigned num_final=1)
Definition: random.hh:105
struct random_selector< RandomGenerator > make_random_selector(const RandomGenerator &g) ATTRIBUTE_PURE
Definition: random.hh:81
oneset::value_t random_label(const oneset &ls, RandomGenerator &=RandomGenerator())
Definition: random.hh:39
std::string to_string(identities i)
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
Main namespace of Awali.
Definition: ato.hh:22
auto pop(Container &c) -> typename Container::value_type
A randomly selected member of c. Remove it from c.
Definition: random.hh:61
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38