5 #include <boost/algorithm/string/erase.hpp>
6 #include <boost/tokenizer.hpp>
27 template <
typename ExpressionSet,
28 typename RandomGenerator = std::default_random_engine>
34 using weight_t =
typename expressionset_t::weight_t;
38 const std::string& param,
57 std::ostringstream
out;
76 using tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
77 auto sep = boost::escaped_list_separator<char>(
"#####",
",",
"\"");
78 auto tok = tokenizer(param, sep);
79 for (
auto it = tok.begin(); it != tok.end(); ++it)
81 auto tok_arg = std::string{*it};
82 auto eq = tok_arg.find_first_of(
'=');
83 auto op = tok_arg.substr(0, eq);
84 boost::algorithm::erase_all(op,
" ");
90 if (eq != std::string::npos)
94 else if (op ==
"length")
97 raise(
"random_expression: invalid operator: ", op);
101 [](
const auto&
v){
return v.second; });
120 const std::string& op)
const
123 if (op ==
"!" || op ==
"w.")
150 const std::string& op)
const
156 auto dis = std::uniform_int_distribution<>(1, length - 2);
157 auto num_lhs = dis(
gen_);
175 else if (length == 1)
197 assert(!
"invalid arity");
209 std::unordered_map<std::string, int>
arities_
244 template <
typename ExpressionSet,
typename RandomGenerator = std::mt19937>
245 random_expression_impl<ExpressionSet, RandomGenerator>
247 const std::string& param,
250 return {
rs, param, gen};
262 template <
typename ExpressionSet>
267 return random_exp.random_expression_string();
272 template <
typename ExpressionSet>
273 typename ExpressionSet::value_t
277 return random_exp.random_expression();
285 template <
typename Context,
typename String,
typename Identities>
290 const auto& c = ctx->
as<Context>();
std::ostream & print_random_expression_(std::ostream &out, unsigned length) const
expression random_expression(const context &ctx, const std::string ¶m, identities ids)
Bridge.
std::ostream & print_random_expression(std::ostream &out) const
Print a random expression string (not parsed, so there might be some syntactic sugar such as <+)...
T lexical_cast(const std::string &str)
String to templated type T conversion.
typename expressionset_t::weightset_t weightset_t
static identities ids(const driver &d)
Get the identities of the driver.
auto make_expressionset(const context< LabelSet, WeightSet > &ctx, rat::identities ids={}) -> expressionset< context< LabelSet, WeightSet >>
Shorthand to expressionset constructor.
void print_label_(std::ostream &out) const
Print label.
ExpressionSet::value_t random_expression(const ExpressionSet &rs, const std::string ¶m)
Generate a random expression.
void print_binary_exp_(std::ostream &out, unsigned length, const std::string &op) const
Print binary expression with binary operator.
void parse_param_(const std::string ¶m)
FIXME: maybe use something similar to Boost.ProgramOptions or getargs.
std::mt19937 & make_random_engine()
Generate a unique random device.
std::unordered_map< std::string, int > arities_
Number of arguments of each operator.
auto & as()
Downcast to the exact type.
std::vector< float > proba_op_
Vector of weights associated with the operators, i.e., the probabilities to pick each operator...
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
auto transform(const Container &c, Fun &&fun)
Map a unary function on a container of values, and return the vector the results. ...
random_expression_impl< ExpressionSet, RandomGenerator > make_random_expression_impl(const ExpressionSet &rs, const std::string ¶m, RandomGenerator &gen=make_random_engine())
Convenience constructor.
std::string random_expression_string() const
A random expression string (not parsed, so there might be some syntactic sugar such as <+)...
Template-less root for contexts.
random_expression_impl(const expressionset_t &rs, const std::string ¶m, RandomGenerator &gen)
typename expressionset_t::value_t expression_t
void print_unary_exp_(std::ostream &out, unsigned length, const std::string &op) const
Print expression with unary operator.
void print_weight_(std::ostream &out) const
Print random weight.
Class random expression generator.
ExpressionSet expressionset_t
random_weight< weightset_t, RandomGenerator > random_weight_
Random weights generator.
std::map< std::string, float > operators_
For each operator, its probability.
discrete_chooser< RandomGenerator > chooser_it_
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.
value_impl< detail::expression_tag > expression
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
expressionset< Context >::value_t random_label(const expressionset< Context > &rs, RandomGenerator &gen=RandomGenerator())
Random label from expressionset: limited to a single label.
RandomGenerator & gen_
Random generator.
std::string random_expression_string(const ExpressionSet &rs, const std::string ¶m)
Generate a random expression string.
typename expressionset_t::weight_t weight_t
Random selector on container, using discrete distribution.
expression_t random_expression() const
A random expression (parsed, so there cannot be syntactic sugar such as <+).