5 #include <boost/algorithm/string/erase.hpp> 6 #include <boost/tokenizer.hpp> 19 template <
typename ExpressionSet>
29 template <
typename ExpressionSet,
30 typename RandomGenerator = std::default_random_engine>
36 using weight_t =
typename expressionset_t::weight_t;
40 const std::string& param,
51 const format& fmt = {})
const 60 std::ostringstream
out;
79 using tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
80 using boost::algorithm::erase_all_copy;
82 = boost::escaped_list_separator<char>(
"#####",
",",
"\"");
83 for (
const auto& arg: tokenizer(param, sep))
85 const auto eq = arg.find(
'=');
86 const auto op = erase_all_copy(arg.substr(0, eq),
" ");
94 const float value = (eq != std::string::npos)
95 ? detail::lexical_cast<float>(arg.substr(eq + 1))
100 if ((op !=
"|" && op !=
"@")
104 else if (op ==
"length")
107 raise(
"random_expression: invalid operator: ", op);
111 [](
const auto&
v){
return v.second; });
117 auto res = std::string{};
138 const auto& ls = *
es_.labelset();
145 const std::string& op,
const format& fmt)
const 148 if (op ==
"!" || op ==
"w.")
175 const std::string& op,
const format& fmt)
const 181 auto dis = std::uniform_int_distribution<>(1, length - 2);
182 const auto num_lhs = dis(
gen_);
195 template <
typename ExpSet = expressionset_t>
198 const std::string& op,
const format& fmt)
const 199 -> std::enable_if_t<context_t_of<ExpSet>::is_lat,
203 const auto len = length /
es_.labelset()->size();
204 const auto param =
make_param_(std::max(
size_t{1}, len));
206 const auto* sep =
"";
207 es_.as_tupleset().map([&out, ¶m, &sep](
const auto& subes)
221 template <
typename ExpSet = expressionset_t>
224 const std::string& op,
const format& fmt)
const 225 -> std::enable_if_t<!context_t_of<ExpSet>::is_lat,
229 raise(
es_,
"random_expression: invalid use of '|'");
242 else if (length == 1)
267 assert(!
"invalid arity");
279 const std::unordered_map<std::string, int>
arities_ 320 template <
typename ExpressionSet,
typename RandomGenerator = std::mt19937>
323 const std::string& param,
326 return {es, param, gen};
337 template <
typename ExpressionSet>
342 return random_exp.random_expression_string();
347 template <
typename ExpressionSet>
348 typename ExpressionSet::value_t
352 return random_exp.random_expression();
360 template <
typename Context,
typename String,
typename Identities>
365 const auto& c = ctx->
as<Context>();
void print_binary_exp_(std::ostream &out, unsigned length, const std::string &op, const format &fmt) const
Print binary expression with binary operator.
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
std::shared_ptr< const node< Context > > expression
format for_labels() const
A copy of this format, but to print labels.
random_expression_impl< ExpressionSet, RandomGenerator > make_random_expression_impl(const ExpressionSet &es, const std::string ¶m, RandomGenerator &gen=make_random_engine())
Convenience constructor.
std::string make_param_(unsigned length) const
A string that specifies the current parameters.
typename expressionset_t::weight_t weight_t
RandomGenerator & gen_
Random generator.
Random selector on container, using discrete distribution.
void print_label_(std::ostream &out, const format &fmt) const
Print label.
std::string random_expression_string() const
A random expression string (not parsed, so there might be some syntactic sugar such as <+)...
random_weight< weightset_t, RandomGenerator > random_weight_
Random weights generator.
std::mt19937 & make_random_engine()
Generate a unique random device.
auto conv(const ValueSet &vs, const std::string &str, Args &&... args) -> decltype(vs.conv(std::declval< std::istream &>(), std::forward< Args >(args)...))
Parse str via vs.conv.
std::ostream & print_random_expression_(std::ostream &out, unsigned length, const format &fmt) const
discrete_chooser< RandomGenerator > choose_
Random selection in containers.
An expressionset can implement several different sets of identities on expressions.
auto transform(const Container &c, Fun &&fun)
Map a unary function on a container of values, and return the vector the results. ...
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
An input/output format for valuesets.
Random expression generator.
void print_unary_exp_(std::ostream &out, unsigned length, const std::string &op, const format &fmt) const
Print expression with unary operator.
std::map< std::string, float > operators_
For each operator, its probability.
Template-less root for contexts.
std::string random_expression_string(const ExpressionSet &es, const std::string ¶m)
Generate a random expression string.
expression_t random_expression() const
A random expression (parsed, so there cannot be syntactic sugar such as <+).
ExpressionSet expressionset_t
bool delimit() const
Whether we should delimit: (1, 2) instead of 1, 2.
auto & as()
Downcast to the exact type.
std::ostream & print_random_expression(std::ostream &out, const format &fmt={}) const
Print a random expression string (not parsed, so there might be some syntactic sugar such as <+)...
auto print_tuple_exp_(std::ostream &out, unsigned length, const std::string &op, const format &fmt) const -> std::enable_if_t< context_t_of< ExpSet >::is_lat, void >
Print a tuple operator.
void print_weight_(std::ostream &out, const format &fmt) const
Print random weight.
random_expression_impl(const expressionset_t &es, const std::string ¶m, RandomGenerator &gen)
std::string to_string(identities i)
Wrapper around operator<<.
typename expressionset_t::weightset_t weightset_t
auto make_expressionset(const context< LabelSet, WeightSet > &ctx, rat::identities ids={}) -> expressionset< context< LabelSet, WeightSet >>
Shorthand to expressionset constructor.
auto print_tuple_exp_(std::ostream &out, unsigned length, const std::string &op, const format &fmt) const -> std::enable_if_t<!context_t_of< ExpSet >::is_lat, void >
Print a tuple operator.
typename expressionset_t::value_t expression_t
std::string random_weight_params_
Random weights generator parameters.
std::vector< float > proba_op_
Vector of weights associated with the operators, i.e., the probabilities to pick each operator...
format for_weights() const
A copy of this format, but to print weights.
static identities ids(const driver &d)
Get the identities of the driver.
const std::unordered_map< std::string, int > arities_
Number of arguments of each operator.
void parse_param_(const std::string ¶m)
expressionset< Context >::value_t random_label(const expressionset< Context > &rs, RandomGenerator &gen=RandomGenerator())
Random label from expressionset: limited to a single label.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.