Add random generators of Boolean, SERE, and PSL formula.

* src/ltlvisit/randomltl.cc, src/ltlvisit/randomltl.hh:
(random_boolean, random_sere, random_psl): Add new classes.
* src/ltltest/randltl.cc: Add options to support the above.
Nore: the -p option was renamed to -pL for consistency, but
it is still understood.
This commit is contained in:
Alexandre Duret-Lutz 2011-02-13 22:38:29 +01:00
parent cc889a7f66
commit cce6dd34f8
3 changed files with 477 additions and 70 deletions

View file

@ -35,7 +35,7 @@ namespace spot
{
namespace
{
formula*
static formula*
ap_builder(const random_formula* rl, int n)
{
assert(n == 1);
@ -45,7 +45,7 @@ namespace spot
return (*i)->clone();
}
formula*
static formula*
true_builder(const random_formula*, int n)
{
assert(n == 1);
@ -53,7 +53,15 @@ namespace spot
return constant::true_instance();
}
formula*
static formula*
boolform_builder(const random_formula* rl, int n)
{
assert(n >= 1);
const random_sere* rs = static_cast<const random_sere*>(rl);
return rs->rb.generate(n);
}
static formula*
false_builder(const random_formula*, int n)
{
assert(n == 1);
@ -61,16 +69,32 @@ namespace spot
return constant::false_instance();
}
static formula*
eword_builder(const random_formula*, int n)
{
assert(n == 1);
(void) n;
return constant::empty_word_instance();
}
template <unop::type Op>
formula*
static formula*
unop_builder(const random_formula* rl, int n)
{
assert(n >= 2);
return unop::instance(Op, rl->generate(n - 1));
}
static formula*
closure_builder(const random_formula* rl, int n)
{
assert(n >= 2);
const random_psl* rp = static_cast<const random_psl*>(rl);
return unop::instance(unop::Closure, rp->rs.generate(n - 1));
}
template <binop::type Op>
formula*
static formula*
binop_builder(const random_formula* rl, int n)
{
assert(n >= 3);
@ -79,8 +103,49 @@ namespace spot
return binop::instance(Op, rl->generate(l), rl->generate(n - l));
}
template <binop::type Op>
static formula*
binop_SERELTL_builder(const random_formula* rl, int n)
{
assert(n >= 3);
--n;
const random_psl* rp = static_cast<const random_psl*>(rl);
int l = rrand(1, n - 1);
return binop::instance(Op, rp->rs.generate(l), rl->generate(n - l));
}
template <bunop::type Op>
static formula*
bunop_unbounded_builder(const random_formula* rl, int n)
{
assert(n >= 2);
return bunop::instance(Op, rl->generate(n - 1));
}
template <bunop::type Op>
static formula*
bunop_bounded_builder(const random_formula* rl, int n)
{
assert(n >= 2);
int min = rrand(0, 3);
int max = rrand(min, 4);
return bunop::instance(Op, rl->generate(n - 1), min, max);
}
template <bunop::type Op>
static formula*
bunop_bool_bounded_builder(const random_formula* rl, int n)
{
assert(n >= 2);
int min = rrand(0, 3);
int max = rrand(min, 4);
const random_sere* rp = static_cast<const random_sere*>(rl);
return bunop::instance(Op, rp->rb.generate(n - 1), min, max);
}
template <multop::type Op>
formula*
static formula*
multop_builder(const random_formula* rl, int n)
{
assert(n >= 3);
@ -109,15 +174,24 @@ namespace spot
for (unsigned i = 0; i < proba_size_; ++i)
{
if (proba_[i].min_n == 1)
total_1_ += proba_[i].proba;
{
total_1_ += proba_[i].proba;
if (proba_ + i >= proba_2_)
total_2_ += proba_[i].proba;;
if (proba_ + i >= proba_2_or_more_)
total_2_and_more_ += proba_[i].proba;
}
else if (proba_[i].min_n == 2)
total_2_ += proba_[i].proba;
{
total_2_ += proba_[i].proba;
if (proba_ + i >= proba_2_or_more_)
total_2_and_more_ += proba_[i].proba;
}
else if (proba_[i].min_n > 2)
total_2_and_more_ += proba_[i].proba;
else
assert(!"unexpected max_n");
}
total_2_and_more_ += total_2_;
assert(total_1_ != 0.0);
assert(total_2_ != 0.0);
assert(total_2_and_more_ != 0.0);
@ -127,45 +201,34 @@ namespace spot
random_formula::generate(int n) const
{
assert(n > 0);
double r = drand();
op_proba* p;
if (n == 1)
{
double r = drand() * total_1_;
op_proba* p = proba_;
double s = p->proba;
while (s < r)
{
++p;
s += p->proba;
}
assert(p->min_n == 1);
return p->build(this, n);
r *= total_1_;
p = proba_;
}
else if (n == 2)
{
double r = drand() * total_2_;
op_proba* p = proba_2_;
double s = p->proba;
while (s < r)
{
++p;
s += p->proba;
}
assert(p->min_n == 2);
return p->build(this, n);
r *= total_2_;
p = proba_2_;
}
else
{
double r = drand() * total_2_and_more_;
op_proba* p = proba_2_;
double s = p->proba;
while (s < r)
{
++p;
s += p->proba;
}
assert(p->min_n >= 2);
return p->build(this, n);
r *= total_2_and_more_;
p = proba_2_or_more_;
}
double s = p->proba;
while (s < r)
{
++p;
s += p->proba;
}
return p->build(this, n);
}
const char*
@ -211,15 +274,55 @@ namespace spot
return os;
}
// LTL formulae
// SEREs
random_sere::random_sere(const atomic_prop_set* ap)
: random_formula(11, ap), rb(ap)
{
proba_[0].setup("eword", 1, eword_builder);
proba_2_ = proba_ + 1;
proba_2_or_more_ = proba_ + 1;
proba_[1].setup("boolform", 1, boolform_builder);
proba_[2].setup("star", 2, bunop_unbounded_builder<bunop::Star>);
proba_[3].setup("star_b", 2, bunop_bounded_builder<bunop::Star>);
proba_[4].setup("equal_b", 2, bunop_bool_bounded_builder<bunop::Equal>);
proba_[5].setup("goto_b", 2, bunop_bool_bounded_builder<bunop::Goto>);
proba_[6].setup("and", 3, multop_builder<multop::And>);
proba_[7].setup("andNLM", 3, multop_builder<multop::AndNLM>);
proba_[8].setup("or", 3, multop_builder<multop::Or>);
proba_[9].setup("concat", 3, multop_builder<multop::Concat>);
proba_[10].setup("fusion", 3, multop_builder<multop::Fusion>);
random_ltl::random_ltl(const atomic_prop_set* ap)
: random_formula(16, ap)
update_sums();
}
// Boolean formulae
random_boolean::random_boolean(const atomic_prop_set* ap)
: random_formula(9, ap)
{
proba_[0].setup("ap", 1, ap_builder);
proba_[0].proba = ap_->size();
proba_[1].setup("false", 1, false_builder);
proba_[2].setup("true", 1, true_builder);
proba_2_ = proba_ + 3;
proba_2_or_more_ = proba_2_ = proba_ + 3;
proba_[3].setup("not", 2, unop_builder<unop::Not>);
proba_[4].setup("equiv", 3, binop_builder<binop::Equiv>);
proba_[5].setup("implies", 3, binop_builder<binop::Implies>);
proba_[6].setup("xor", 3, binop_builder<binop::Xor>);
proba_[7].setup("and", 3, multop_builder<multop::And>);
proba_[8].setup("or", 3, multop_builder<multop::Or>);
update_sums();
}
// LTL formulae
void
random_ltl::setup_proba_()
{
proba_[0].setup("ap", 1, ap_builder);
proba_[0].proba = ap_->size();
proba_[1].setup("false", 1, false_builder);
proba_[2].setup("true", 1, true_builder);
proba_2_or_more_ = proba_2_ = proba_ + 3;
proba_[3].setup("not", 2, unop_builder<unop::Not>);
proba_[4].setup("F", 2, unop_builder<unop::F>);
proba_[5].setup("G", 2, unop_builder<unop::G>);
@ -233,10 +336,36 @@ namespace spot
proba_[13].setup("M", 3, binop_builder<binop::M>);
proba_[14].setup("and", 3, multop_builder<multop::And>);
proba_[15].setup("or", 3, multop_builder<multop::Or>);
}
proba_[0].proba = ap_->size();
random_ltl::random_ltl(const atomic_prop_set* ap)
: random_formula(16, ap)
{
setup_proba_();
update_sums();
}
random_ltl::random_ltl(int size, const atomic_prop_set* ap)
: random_formula(size, ap)
{
setup_proba_();
// No call to update_sums(), this functions is always
// called by the random_psl constructor.
}
// PSL
random_psl::random_psl(const atomic_prop_set* ap)
: random_ltl(19, ap), rs(ap)
{
// FIXME: This looks very fragile.
memmove(proba_ + 8, proba_ + 7,
((proba_ + 16) - (proba_ + 7)) * sizeof(*proba_));
proba_[7].setup("Closure", 2, closure_builder);
proba_[17].setup("EConcat", 3, binop_SERELTL_builder<binop::EConcat>);
proba_[18].setup("UConcat", 3, binop_SERELTL_builder<binop::UConcat>);
update_sums();
}
} // ltl
} // spot

View file

@ -45,7 +45,7 @@ namespace spot
~random_formula()
{
delete proba_;
delete[] proba_;
}
/// Return the set of atomic proposition used to build formulae.
@ -69,9 +69,6 @@ namespace spot
/// \brief Update the priorities used to generate the formulae.
///
/// The initial priorities are defined in each sub class as follows.
///
/// These priorities can be altered using this function.
/// \a options should be comma-separated list of KEY=VALUE
/// assignments, using keys from the above list.
/// For instance <code>"xor=0, F=3"</code> will prevent \c xor
@ -96,6 +93,7 @@ namespace spot
double total_1_;
op_proba* proba_2_;
double total_2_;
op_proba* proba_2_or_more_;
double total_2_and_more_;
const atomic_prop_set* ap_;
};
@ -104,10 +102,10 @@ namespace spot
/// \brief Generate random LTL formulae.
/// \ingroup ltl_io
///
/// This class recursively construct LTL formulae of a given size.
/// The formulae will use the use atomic propositions from the
/// set of proposition passed to the constructor, in addition to the
/// constant and all LTL operators supported by Spot.
/// This class recursively constructs LTL formulae of a given
/// size. The formulae will use the use atomic propositions from
/// the set of propositions passed to the constructor, in addition
/// to the constant and all LTL operators supported by Spot.
///
/// By default each operator has equal chance to be selected.
/// Also, each atomic proposition has as much chance as each
@ -148,7 +146,155 @@ namespace spot
///
/// These priorities can be changed use the parse_options method.
random_ltl(const atomic_prop_set* ap);
protected:
void setup_proba_();
random_ltl(int size, const atomic_prop_set* ap);
};
/// \brief Generate random Boolean formulae.
/// \ingroup ltl_io
///
/// This class recursively constructs Boolean formulae of a given size.
/// The formulae will use the use atomic propositions from the
/// set of propositions passed to the constructor, in addition to the
/// constant and all Boolean operators supported by Spot.
///
/// By default each operator has equal chance to be selected.
class random_boolean: public random_formula
{
public:
/// Create a random Boolean formula generator using atomic
/// propositions from \a ap.
///
/// The default priorities are defined as follows:
///
/// \verbatim
/// ap n
/// false 1
/// true 1
/// not 1
/// equiv 1
/// implies 1
/// xor 1
/// and 1
/// or 1
/// \endverbatim
///
/// Where \c n is the number of atomic propositions in the
/// set passed to the constructor.
///
/// This means that each operator has equal chance to be
/// selected. Also, each atomic proposition has as much chance
/// as each constant (i.e., true and false) to be picked.
///
/// These priorities can be changed use the parse_options method.
random_boolean(const atomic_prop_set* ap);
};
/// \brief Generate random SERE.
/// \ingroup ltl_io
///
/// This class recursively constructs SERE of a given size.
/// The formulae will use the use atomic propositions from the
/// set of propositions passed to the constructor, in addition to the
/// constant and all SERE operators supported by Spot.
///
/// By default each operator has equal chance to be selected.
class random_sere: public random_formula
{
public:
/// Create a random SERE genere using atomic propositions from \a ap.
///
/// The default priorities are defined as follows:
///
/// \verbatim
/// eword 1
/// boolform 1
/// star 1
/// star_b 1
/// equal_b 1
/// goto_b 1
/// and 1
/// andNLM 1
/// or 1
/// concat 1
/// fusion 1
/// \endverbatim
///
/// Where "boolfrom" designates a Boolean formula generated
/// by random_boolean.
///
/// These priorities can be changed use the parse_options method.
///
/// In addition, you can set the properties of the Boolean
/// formula generator used to build Boolean subformulae using
/// the parse_options method of the \c rb attribute.
random_sere(const atomic_prop_set* ap);
random_boolean rb;
};
/// \brief Generate random PSL formulae.
/// \ingroup ltl_io
///
/// This class recursively constructs PSL formulae of a given size.
/// The formulae will use the use atomic propositions from the
/// set of propositions passed to the constructor, in addition to the
/// constant and all PSL operators supported by Spot.
class random_psl: public random_ltl
{
public:
/// Create a random PSL generator using atomic propositions from \a ap.
///
/// PSL formulae are built by combining LTL operators, plus
/// three operators (EConcat, UConcat, Closure) taking a SERE
/// as parameter.
///
/// The default priorities are defined as follows:
///
/// \verbatim
/// ap n
/// false 1
/// true 1
/// not 1
/// F 1
/// G 1
/// X 1
/// Closure 1
/// equiv 1
/// implies 1
/// xor 1
/// R 1
/// U 1
/// W 1
/// M 1
/// and 1
/// or 1
/// EConcat 1
/// UConcat 1
/// \endverbatim
///
/// Where \c n is the number of atomic propositions in the
/// set passed to the constructor.
///
/// This means that each operator has equal chance to be
/// selected. Also, each atomic proposition has as much chance
/// as each constant (i.e., true and false) to be picked.
///
/// These priorities can be changed use the parse_options method.
///
/// In addition, you can set the properties of the SERE generator
/// used to build SERE subformulae using the parse_options method
/// of the \c rs attribute.
random_psl(const atomic_prop_set* ap);
/// The SERE generator used to generate SERE subformulae.
random_sere rs;
};
}
}