1
Fork 0
mirror of https://github.com/Jaxan/hybrid-ads.git synced 2025-04-27 23:17:44 +02:00

Reintroduces the bug/feature that created longer prefixes

This commit is contained in:
Joshua Moerman 2015-10-29 13:35:30 +01:00
parent 7ea3593588
commit aa801a7946
4 changed files with 45 additions and 14 deletions

View file

@ -4,28 +4,45 @@
#include <algorithm> #include <algorithm>
#include <numeric> #include <numeric>
#include <queue> #include <deque>
#include <random> #include <random>
#include <tuple> #include <tuple>
using namespace std; using namespace std;
namespace {
static size_t clamp_to_size_t(double x, size_t min, size_t max) {
if (x >= max) return max;
if (x <= min) return min;
return x;
}
}
transfer_sequences create_transfer_sequences(transfer_options const & opt, const mealy & machine, transfer_sequences create_transfer_sequences(transfer_options const & opt, const mealy & machine,
state s, uint_fast32_t random_seed) { state s, uint_fast32_t random_seed) {
mt19937 generator(random_seed); mt19937 generator(random_seed);
uniform_int_distribution<double> dist(opt.q_min, opt.q_max);
vector<bool> added(machine.graph_size, false); vector<bool> added(machine.graph_size, false);
vector<word> words(machine.graph_size); vector<word> words(machine.graph_size);
vector<input> all_inputs(machine.input_size); vector<input> all_inputs(machine.input_size);
iota(begin(all_inputs), end(all_inputs), input(0)); iota(begin(all_inputs), end(all_inputs), input(0));
// state deque<state> work(1, s);
queue<state> work;
work.push(s);
added[s] = true; added[s] = true;
while (!work.empty()) { while (!work.empty()) {
const auto u = work.front(); const auto u = [&] {
work.pop(); // get the place in the array to pop a state
const auto sample = dist(generator);
const auto scaled_sample = clamp_to_size_t(floor(work.size() * sample), 0, work.size()-1);
const auto it = work.begin() + scaled_sample;
// get the element
const auto ret = *it;
// pop it
work.erase(it);
// return it
return ret;
}();
// NOTE: we could also shuffle work, but we would need to do this per distance // NOTE: we could also shuffle work, but we would need to do this per distance
// the current shuffle is an approximation of real randomization, but easier to implement. // the current shuffle is an approximation of real randomization, but easier to implement.
@ -34,7 +51,7 @@ transfer_sequences create_transfer_sequences(transfer_options const & opt, const
const auto v = apply(machine, u, i).to; const auto v = apply(machine, u, i).to;
if (added[v]) continue; if (added[v]) continue;
work.push(v); work.push_back(v);
added[v] = true; added[v] = true;
words[v] = words[u]; words[v] = words[u];
words[v].push_back(i); words[v].push_back(i);

View file

@ -8,11 +8,20 @@ struct mealy;
using transfer_sequences = std::vector<word>; using transfer_sequences = std::vector<word>;
struct transfer_options { struct transfer_options {
// range used to sample the work-queue. [0,0] is a bfs (minimal), [1,1] is a dfs (dumb).
// q_min should be smaller than q_max.
double q_min;
double q_max;
// indicates whether to randomly iterate over the alphabet
// and to randomly select in [q_min, q_max] instead of taking the center
bool randomized; bool randomized;
}; };
const transfer_options randomized_transfer_sequences{true}; const transfer_options canonical_transfer_sequences{0.0, 0.0, false};
const transfer_options canonical_transfer_sequences{false}; const transfer_options minimal_transfer_sequences{0.0, 0.0, true};
const transfer_options buggy_transfer_sequences{0.0, 1.0, true};
const transfer_options longest_transfer_sequences{1.0, 1.0, true}; // longest, forming a tree
transfer_sequences create_transfer_sequences(transfer_options const & opt, mealy const & machine, transfer_sequences create_transfer_sequences(transfer_options const & opt, mealy const & machine,
state s, uint_fast32_t random_seed); state s, uint_fast32_t random_seed);

View file

@ -32,6 +32,7 @@ R"(Generate a test suite
--random-ds Choose randomly between the ds method or hsi method --random-ds Choose randomly between the ds method or hsi method
--no-suffix Dont calculate anything smart, just do the random stuff --no-suffix Dont calculate anything smart, just do the random stuff
--suffix-based Only applies in random mode. Chooses suffix first, and not prefix first --suffix-based Only applies in random mode. Chooses suffix first, and not prefix first
--prefix <type> Chooses the kind of prefix: canonical, minimal, buggy, longest
)"; )";
using time_logger = silent_timer; using time_logger = silent_timer;
@ -60,7 +61,7 @@ int main(int argc, char *argv[]) try {
} }
return !args.at("--no-ds").asBool(); return !args.at("--no-ds").asBool();
}(); }();
const bool randomize_prefixes = true; const string prefix_type = args.at("--prefix") ? args.at("--prefix").asString() : "minimal";
const bool randomize_hopcroft = true; const bool randomize_hopcroft = true;
const bool randomize_lee_yannakakis = true; const bool randomize_lee_yannakakis = true;
@ -129,9 +130,13 @@ int main(int argc, char *argv[]) try {
auto transfer_sequences = [&] { auto transfer_sequences = [&] {
time_logger t("determining transfer sequences"); time_logger t("determining transfer sequences");
return create_transfer_sequences(randomize_prefixes ? randomized_transfer_sequences if(prefix_type == "canonical") return create_transfer_sequences(canonical_transfer_sequences, machine, 0, random_seeds[2]);
: canonical_transfer_sequences, if(prefix_type == "minimal") return create_transfer_sequences(minimal_transfer_sequences, machine, 0, random_seeds[2]);
machine, 0, random_seeds[2]); if(prefix_type == "buggy") return create_transfer_sequences(buggy_transfer_sequences, machine, 0, random_seeds[2]);
if(prefix_type == "longest") return create_transfer_sequences(longest_transfer_sequences, machine, 0, random_seeds[2]);
cerr << "Warning: no valid prefix type specified. Assuming minimal.\n";
return create_transfer_sequences(minimal_transfer_sequences, machine, 0, random_seeds[2]);
}(); }();
auto inputs = create_reverse_map(translation.input_indices); auto inputs = create_reverse_map(translation.input_indices);

View file

@ -81,7 +81,7 @@ int main(int argc, char * argv[]) {
auto prefixes_fut = async([&] { auto prefixes_fut = async([&] {
return create_transfer_sequences(args.at("--non-random").asBool() return create_transfer_sequences(args.at("--non-random").asBool()
? canonical_transfer_sequences ? canonical_transfer_sequences
: randomized_transfer_sequences, : minimal_transfer_sequences,
machine, 0, random_seeds[2]); machine, 0, random_seeds[2]);
}); });