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

Adds the two families of automata occurring in Hopcrofts paper

This commit is contained in:
Joshua Moerman 2015-07-30 16:33:15 +02:00
parent 01bc580081
commit 415689b04c

View file

@ -11,10 +11,12 @@
using namespace std; using namespace std;
static const char USAGE[] = static const char USAGE[] =
R"(Random Mealy machine generator R"(Random Mealy machine generator
Usage: Usage:
generator random [-mc] <states> <inputs> <outputs> <machines> [<seed>] generator random [-mc] <states> <inputs> <outputs> <machines> [<seed>]
generator hopcroft a <states>
generator hopcroft b <states>
Options: Options:
-h, --help Show this screen -h, --help Show this screen
@ -51,8 +53,8 @@ static mealy generate_random_machine(size_t N, size_t P, size_t Q, mt19937 & gen
return m; return m;
} }
static void print_machine(mealy const & m, size_t count) { static void print_machine(string const & prefix, mealy const & m, size_t count) {
ofstream file("machine_" + to_string(m.graph_size) + "_" + to_string(m.input_size) + "_" ofstream file(prefix + "_" + to_string(m.graph_size) + "_" + to_string(m.input_size) + "_"
+ to_string(m.output_size) + "_" + to_string(count) + ".txt"); + to_string(m.output_size) + "_" + to_string(count) + ".txt");
for (state s = 0; s < m.graph_size; ++s) { for (state s = 0; s < m.graph_size; ++s) {
for (input i = 0; i < m.input_size; ++i) { for (input i = 0; i < m.input_size; ++i) {
@ -64,10 +66,8 @@ static void print_machine(mealy const & m, size_t count) {
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
const auto args = docopt::docopt(USAGE, {argv + 1, argv + argc}, true, __DATE__ __TIME__); const auto args = docopt::docopt(USAGE, {argv + 1, argv + argc}, true, __DATE__ __TIME__);
for (auto const & arg : args) {
std::cout << arg.first << arg.second << std::endl;
}
if (args.at("random").asBool()) {
auto gen = [&] { auto gen = [&] {
if (args.at("<seed>")) { if (args.at("<seed>")) {
auto seed = args.at("<seed>").asLong(); auto seed = args.at("<seed>").asLong();
@ -81,8 +81,8 @@ int main(int argc, char * argv[]) {
size_t constructed = 0; size_t constructed = 0;
while (constructed < number_of_machines) { while (constructed < number_of_machines) {
auto const m auto const m = generate_random_machine(args.at("<states>").asLong(),
= generate_random_machine(args.at("<states>").asLong(), args.at("<inputs>").asLong(), args.at("<inputs>").asLong(),
args.at("<outputs>").asLong(), gen); args.at("<outputs>").asLong(), gen);
if (args.at("--connected").asBool()) { if (args.at("--connected").asBool()) {
@ -96,6 +96,43 @@ int main(int argc, char * argv[]) {
} }
constructed++; constructed++;
print_machine(m, constructed); print_machine("machine", m, constructed);
}
} else if (args.at("hopcroft").asBool() && args.at("a").asBool()) {
mealy m;
m.graph_size = args.at("<states>").asLong();
m.input_size = m.output_size = 2;
m.graph.assign(m.graph_size, vector<mealy::edge>(m.input_size));
for (state s = 0; s < m.graph_size; ++s) {
m.graph[s][0] = mealy::edge(s + 1, 0);
m.graph[s][1] = mealy::edge(s, 0);
}
// "accepting state"
m.graph[m.graph_size - 1][0] = mealy::edge(m.graph_size - 1, 1);
m.graph[m.graph_size - 1][1] = mealy::edge(m.graph_size - 1, 1);
print_machine("hopcroft_a", m, 1);
} else if (args.at("hopcroft").asBool() && args.at("b").asBool()) {
// In the original paper, the machine is not well defined...
// So I don't know what Hopcroft had in mind exactly...
mealy m;
auto n = m.graph_size = args.at("<states>").asLong();
m.input_size = m.output_size = 2;
m.graph.assign(m.graph_size, vector<mealy::edge>(m.input_size));
for (state s = 0; s < n; ++s) {
m.graph[s][0] = mealy::edge(s ? s - 1 : 0, s < n / 2 ? 0 : 1);
if (s < n / 2) {
m.graph[s][1] = mealy::edge(2 * s + 1, 0);
} else {
m.graph[s][1] = mealy::edge(s + (n - s) / 2, 0);
}
}
print_machine("hopcroft_b", m, 1);
} }
} }