5 #include <boost/algorithm/string/erase.hpp> 6 #include <boost/tokenizer.hpp> 26 template <
typename ExpressionSet,
27 typename RandomGenerator = std::default_random_engine>
33 using weight_t =
typename expressionset_t::weight_t;
37 const std::string& param,
56 std::ostringstream
out;
75 using tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
76 using boost::algorithm::erase_all_copy;
77 auto sep = boost::escaped_list_separator<char>(
"#####",
",",
"\"");
78 for (
const auto& arg: tokenizer(param, sep))
80 auto eq = arg.find(
'=');
81 auto op = erase_all_copy(arg.substr(0, eq),
" ");
86 float value = (eq != std::string::npos)
87 ? detail::lexical_cast<float>(arg.substr(eq + 1))
91 else if (op ==
"length")
94 raise(
"random_expression: invalid operator: ", op);
98 [](
const auto&
v){
return v.second; });
117 const std::string& op)
const 120 if (op ==
"!" || op ==
"w.")
147 const std::string& op)
const 153 auto dis = std::uniform_int_distribution<>(1, length - 2);
154 auto num_lhs = dis(
gen_);
172 else if (length == 1)
194 assert(!
"invalid arity");
206 std::unordered_map<std::string, int>
arities_ 241 template <
typename ExpressionSet,
typename RandomGenerator = std::mt19937>
244 const std::string& param,
247 return {rs, param, gen};
259 template <
typename ExpressionSet>
264 return random_exp.random_expression_string();
269 template <
typename ExpressionSet>
270 typename ExpressionSet::value_t
274 return random_exp.random_expression();
282 template <
typename Context,
typename String,
typename Identities>
287 const auto& c = ctx->
as<Context>();
random_expression_impl(const expressionset_t &rs, const std::string ¶m, RandomGenerator &gen)
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
random_weight< weightset_t, RandomGenerator > random_weight_
Random weights generator.
void print_binary_exp_(std::ostream &out, unsigned length, const std::string &op) const
Print binary expression with binary operator.
void print_weight_(std::ostream &out) const
Print random weight.
discrete_chooser< RandomGenerator > chooser_it_
An expressionset can implement several different sets of identities on expressions.
std::ostream & print_random_expression_(std::ostream &out, unsigned length) const
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
void print_unary_exp_(std::ostream &out, unsigned length, const std::string &op) const
Print expression with unary operator.
static identities ids(const driver &d)
Get the identities of the driver.
std::map< std::string, float > operators_
For each operator, its probability.
Class random expression generator.
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.
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.
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.
void print_label_(std::ostream &out) const
Print label.
typename expressionset_t::weight_t weight_t
std::unordered_map< std::string, int > arities_
Number of arguments of each operator.
std::shared_ptr< const node< Context > > expression
expressionset< Context >::value_t random_label(const expressionset< Context > &rs, RandomGenerator &gen=RandomGenerator())
Random label from expressionset: limited to a single label.
std::vector< float > proba_op_
Vector of weights associated with the operators, i.e., the probabilities to pick each operator...
std::string random_expression_string() const
A random expression string (not parsed, so there might be some syntactic sugar such as <+)...
typename expressionset_t::weightset_t weightset_t
Random selector on container, using discrete distribution.
auto make_expressionset(const context< LabelSet, WeightSet > &ctx, rat::identities ids={}) -> expressionset< context< LabelSet, WeightSet >>
Shorthand to expressionset constructor.
expression_t random_expression() const
A random expression (parsed, so there cannot be syntactic sugar such as <+).
Template-less root for contexts.
typename expressionset_t::value_t expression_t
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 <+)...
auto & as()
Downcast to the exact type.
RandomGenerator & gen_
Random generator.
ExpressionSet expressionset_t