From 098f0e9bff37cdd2f0c85a688895e6ebcc250b0f Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Tue, 7 Apr 2015 16:17:28 +0200 Subject: [PATCH] Added restriction to reachable sub machine --- lib/reachability.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++ lib/reachability.hpp | 7 ++++++ src/main.cpp | 7 +++--- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 lib/reachability.cpp create mode 100644 lib/reachability.hpp diff --git a/lib/reachability.cpp b/lib/reachability.cpp new file mode 100644 index 0000000..474463c --- /dev/null +++ b/lib/reachability.cpp @@ -0,0 +1,56 @@ +#include "reachability.hpp" +#include "mealy.hpp" + +#include +#include +#include + +using namespace std; + +mealy reachable_submachine(const mealy& in, state start) { + using state_out = state; + state_out max_state = 0; + map new_state; + vector visited(in.graph_size, false); + + mealy out; + + queue work; + work.push(start); + while (!work.empty()) { + state s = work.front(); + work.pop(); + + if (visited[s]) continue; + visited[s] = true; + + if (!new_state.count(s)) new_state[s] = max_state++; + state_out s2 = new_state[s]; + + for (input i = 0; i < in.input_size; ++i) { + const auto ret = apply(in, s, i); + const output o = ret.output; + const state t = ret.to; + + if (!new_state.count(t)) new_state[t] = max_state++; + state_out t2 = new_state[t]; + + if (out.graph.size() < max_state) out.graph.resize(max_state); + if (out.graph[s2].size() < in.input_size) out.graph[s2].resize(in.input_size); + out.graph[s2][i] = {t2, o}; + + if (!visited[t]) work.push(t); + } + } + + out.graph_size = max_state; + out.input_size = in.input_size; + out.output_size = in.output_size; + + if(out.graph_size == 0) throw runtime_error("Empty state set"); + if(out.input_size == 0) throw runtime_error("Empty input set"); + if(out.output_size == 0) throw runtime_error("Empty output set"); + if(!is_complete(out)) throw runtime_error("Partial machine"); + + return out; +} diff --git a/lib/reachability.hpp b/lib/reachability.hpp new file mode 100644 index 0000000..5c5a3d0 --- /dev/null +++ b/lib/reachability.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include "types.hpp" + +struct mealy; + +mealy reachable_submachine(const mealy& in, state start); diff --git a/src/main.cpp b/src/main.cpp index 428997f..e436ab8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -25,8 +26,8 @@ int main(int argc, char *argv[]) try { const auto k_max = stoul(argv[2]); const string mode = argv[3]; - const bool streaming = mode == "stream"; - const bool random_part = streaming; + const bool streaming = mode == "stream" || mode == "stop"; + const bool random_part = streaming && mode != "stop"; const bool statistics = mode == "stats"; const bool use_distinguishing_sequence = true; @@ -44,7 +45,7 @@ int main(int argc, char *argv[]) try { } }(); - const auto & machine = machine_and_translation.first; + const auto & machine = reachable_submachine(move(machine_and_translation.first), 0); const auto & translation = machine_and_translation.second; auto all_pair_seperating_sequences_fut = async([&]{