43 template <
typename Ctx>
44 mutable_automaton<Ctx>
46 unsigned num_states,
float density = 0.1,
47 unsigned num_initial = 1,
unsigned num_final = 1,
48 boost::optional<unsigned> max_labels = {},
49 float loop_chance = 0.0,
const std::string& weights =
"")
51 require(0 <= density && density <= 1,
52 "random_automaton: density must be in [0,1]");
53 require(0 <= loop_chance && loop_chance <= 1,
54 "random_automaton: loop chance must be in [0,1]");
57 using automaton_t = mutable_automaton<Ctx>;
58 using state_t = state_t_of<automaton_t>;
59 auto res = make_shared_ptr<automaton_t>(
ctx);
63 const auto& ws = *ctx.weightset();
66 const auto& ls = *ctx.labelset();
70 "random_automaton: max number of labels cannot be null");
72 require(*max_labels <= boost::distance(ls.generators()) + ls.has_one(),
73 "random_automaton: max number of labels cannot be greater "
74 "than the number of generators");
78 max_labels = (boost::distance(ls.generators()) + ls.has_one());
80 "random_automaton: empty labelset: ", ls);
82 auto num_labels = std::uniform_int_distribution<>(1, *max_labels);
89 auto states = std::vector<state_t>{};
90 states.reserve(num_states);
92 auto state_randomizer = std::vector<int>{};
93 state_randomizer.reserve(num_states);
98 using state_set = std::set<int>;
101 state_set unreachables;
105 states.emplace_back(
res->new_state());
106 state_randomizer.emplace_back(i);
109 unreachables.emplace(i);
111 res->set_initial(states[i]);
118 auto dis = std::uniform_int_distribution<>(i, num_states - 1);
119 int index = dis(gen);
120 res->set_final(states[state_randomizer[index]]);
123 std::swap(state_randomizer[index], state_randomizer[i]);
129 auto bin = std::binomial_distribution<>(num_states - 1, density);
134 while (!worklist.empty())
136 auto src = states[*worklist.begin()];
137 worklist.erase(worklist.begin());
141 unsigned nsucc = 1 + bin(gen);
146 bool saw_unreachable =
false;
147 auto possibilities = num_states;
156 && !unreachables.empty())
159 dst = pick.pop(unreachables);
160 worklist.insert(dst);
166 = std::uniform_int_distribution<>(0, possibilities - 1);
167 int index = dis(gen);
170 dst = state_randomizer[index];
174 std::swap(state_randomizer[index],
175 state_randomizer[possibilities]);
177 state_set::iterator j = unreachables.find(dst);
178 if (j != unreachables.end())
180 worklist.insert(dst);
181 unreachables.erase(j);
182 saw_unreachable =
true;
185 auto n = num_labels(gen);
187 res->add_transition(src, states[dst],
196 auto dis = std::bernoulli_distribution(loop_chance);
197 for (
auto s :
res->states())
199 res->add_transition(s, s,
213 template <
typename Ctx,
typename NumStates,
typename Density,
214 typename NumInitial,
typename NumFinal,
215 typename MaxLabels,
typename LoopChance,
typename String>
218 unsigned num_states,
float density,
219 unsigned num_initial,
unsigned num_final,
220 boost::optional<unsigned> max_labels,
222 const std::string& weights)
224 const auto& c = ctx->
as<Ctx>();
226 num_initial, num_final,
239 template <
typename Ctx>
240 mutable_automaton<Ctx>
243 require(0 < num_states,
"num_states must be > 0");
247 automaton_t
res = make_shared_ptr<automaton_t>(
ctx);
250 auto dis = std::uniform_int_distribution<int>(0, num_states - 1);
252 auto states = std::vector<state_t>{};
253 states.reserve(num_states);
256 states.emplace_back(res->new_state());
259 for (
auto l : ctx.labelset()->generators())
260 res->add_transition(states[i], states[dis(gen)], l,
261 ctx.weightset()->one());
263 res->set_initial(states[dis(gen)]);
264 res->set_final(states[dis(gen)]);
274 template <
typename Ctx,
typename>
278 const auto& c = ctx->
as<Ctx>();
automaton random_automaton_deterministic(const context &ctx, unsigned num_states)
Bridge.
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
mutable_automaton< Ctx > random_automaton(const Ctx &ctx, unsigned num_states, float density=0.1, unsigned num_initial=1, unsigned num_final=1, boost::optional< unsigned > max_labels={}, float loop_chance=0.0, const std::string &weights="")
Produce a random automaton.
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
WeightSet::value_t random_weight(const WeightSet &ws, const std::string ¶m={})
Generate a random weight.
std::mt19937 & make_random_engine()
Generate a unique random device.
auto & as()
Downcast to the exact type.
auto irange(Integer last)
Generate an integer range.
random_selector< RandomGenerator > make_random_selector(RandomGenerator &g)
Template-less root for contexts.
mutable_automaton< Ctx > random_automaton_deterministic(const Ctx &ctx, unsigned num_states)
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
expressionset< Context >::value_t random_label(const expressionset< Context > &rs, RandomGenerator &gen=RandomGenerator())
Random label from expressionset: limited to a single label.
automaton random_automaton(const context &ctx, unsigned num_states, float density, unsigned num_initial, unsigned num_final, boost::optional< unsigned > max_labels, float loop_chance, const std::string &weights)
Bridge.