Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding simulation reduction rules #487

Draft
wants to merge 1 commit into
base: devel
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/nfa/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <algorithm>
#include <list>
#include <string>
#include <unordered_set>

// MATA headers
Expand Down Expand Up @@ -54,10 +55,17 @@ namespace {
return lts_for_simulation.compute_simulation();
}

Nfa reduce_size_by_simulation(const Nfa& aut, StateRenaming &state_renaming) {
Nfa reduce_size_by_simulation(const Nfa& aut, StateRenaming &state_renaming, const std::string& simulation_direction) {
Nfa result;
Nfa aut_m;

if (simulation_direction == "backward")
aut_m = revert(aut);
else
aut_m = aut;

const auto sim_relation = algorithms::compute_relation(
aut, ParameterMap{{ "relation", "simulation"}, { "direction", "forward"}});
aut_m, ParameterMap{{ "relation", "simulation"}, { "direction", "forward"}});

auto sim_relation_symmetric = sim_relation;
sim_relation_symmetric.restrict_to_symmetric();
Expand All @@ -66,7 +74,7 @@ namespace {
std::vector<size_t> quot_proj;
sim_relation_symmetric.get_quotient_projection(quot_proj);

const size_t num_of_states = aut.num_of_states();
const size_t num_of_states = aut_m.num_of_states();

// map each state q of aut to the state of the reduced automaton representing the simulation class of q
for (State q = 0; q < num_of_states; ++q) {
Expand All @@ -83,12 +91,12 @@ namespace {
for (State q = 0; q < num_of_states; ++q) {
const State q_class_state = state_renaming.at(q);

if (aut.initial[q]) { // if a symmetric class contains initial state, then the whole class should be initial
if (aut_m.initial[q]) { // if a symmetric class contains initial state, then the whole class should be initial
result.initial.insert(q_class_state);
}

if (quot_proj[q] == q) { // we process only transitions starting from the representative state, this is enough for simulation
for (const auto &q_trans : aut.delta.state_post(q)) {
for (const auto &q_trans : aut_m.delta.state_post(q)) {
const StateSet representatives_of_states_to = [&]{
StateSet state_set;
for (auto s : q_trans.targets) {
Expand Down Expand Up @@ -117,12 +125,15 @@ namespace {
result.delta.mutable_state_post(q_class_state).insert(SymbolPost(q_trans.symbol, representatives_class_states));
}

if (aut.final[q]) { // if q is final, then all states in its class are final => we make q_class_state final
if (aut_m.final[q]) { // if q is final, then all states in its class are final => we make q_class_state final
result.final.insert(q_class_state);
}
}
}

if (simulation_direction == "backward")
result = revert(result);

return result;
}

Expand Down Expand Up @@ -1147,7 +1158,8 @@ Nfa mata::nfa::reduce(const Nfa &aut, StateRenaming *state_renaming, const Param
std::unordered_map<State,State> reduced_state_map;
const std::string& algorithm = params.at("algorithm");
if ("simulation" == algorithm) {
result = reduce_size_by_simulation(aut, reduced_state_map);
const std::string& simulation_direction = params.at("direction");
result = reduce_size_by_simulation(aut, reduced_state_map, simulation_direction);
}
else if ("residual" == algorithm) {
// reduce type either 'after' or 'with' creation of residual automaton
Expand Down
Loading