From cec63073257c6bce0f008beb1deff2b52d6c6cc4 Mon Sep 17 00:00:00 2001 From: koniksedy Date: Wed, 20 Nov 2024 17:22:55 +0100 Subject: [PATCH] renaming --- include/mata/nfa/builder.hh | 5 +++-- src/nfa/builder.cc | 20 ++++++++++---------- tests/nfa/builder.cc | 18 +++++++++--------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/include/mata/nfa/builder.hh b/include/mata/nfa/builder.hh index 0ebf1f912..48ec23ebf 100644 --- a/include/mata/nfa/builder.hh +++ b/include/mata/nfa/builder.hh @@ -51,11 +51,12 @@ Nfa create_sigma_star_nfa(Alphabet* alphabet = new OnTheFlyAlphabet{}); * @param num_of_states Number of states in the automaton. * @param alphabet_size Size of the alphabet. * @param transition_density If the density is 1, the automaton will have @p num_of_states transition for each symbol. - * Source and target states are chosen randomly. + * Source and target states are chosen randomly. The value must be in range [0, num_of_states]. * @param final_state_density Density of final states in the automaton. If the density is 1, every state will be final. + * The value must be in range [0, 1]. * */ -Nfa create_tabakov_vardi_nfa(const size_t num_of_states, const size_t alphabet_size, const float transition_density, const float final_state_density); +Nfa create_random_nfa_tabakov_vardi(const size_t num_of_states, const size_t alphabet_size, const float transition_density, const float final_state_density); /** Loads an automaton from Parsed object */ // TODO this function should the same thing as the one taking IntermediateAut or be deleted diff --git a/src/nfa/builder.cc b/src/nfa/builder.cc index 598302b69..481125ab9 100644 --- a/src/nfa/builder.cc +++ b/src/nfa/builder.cc @@ -219,7 +219,7 @@ Nfa builder::create_sigma_star_nfa(mata::Alphabet* alphabet) { return nfa; } -Nfa builder::create_tabakov_vardi_nfa(const size_t num_of_states, const size_t alphabet_size, const float transition_density, const float final_state_density) { +Nfa builder::create_random_nfa_tabakov_vardi(const size_t num_of_states, const size_t alphabet_size, const float transition_density, const float final_state_density) { if (transition_density < 0 || static_cast(transition_density) > num_of_states) { // Maximum of num_of_states^2 unique transitions for one symbol can be created. throw std::runtime_error("Transition density must be in range [0, num_of_states]"); @@ -234,27 +234,27 @@ Nfa builder::create_tabakov_vardi_nfa(const size_t num_of_states, const size_t a // Initialize the random number generator std::random_device rd; // Seed for the random number engine std::mt19937 gen(rd()); // Mersenne Twister engine - std::uniform_int_distribution state_dis(0, num_of_states - 1); - std::uniform_int_distribution symbol_dis(0, static_cast(alphabet_size - 1)); + std::uniform_int_distribution state_rand_dis(0, num_of_states - 1); + std::uniform_int_distribution symbol_rand_dis(0, static_cast(alphabet_size - 1)); // Create final states const size_t num_of_final_states{ static_cast(std::round(static_cast(num_of_states) * final_state_density)) }; while (nfa.final.size() < num_of_final_states) { - nfa.final.insert(state_dis(gen)); + nfa.final.insert(state_rand_dis(gen)); } // Create transitions const size_t num_of_transitions_per_symbol{ static_cast(std::round(static_cast(num_of_states) * transition_density)) }; - for (Symbol symbol{ 0 }; symbol < alphabet_size; symbol++) { + for (Symbol symbol{ 0 }; symbol < alphabet_size; ++symbol) { size_t num_of_added_transitions = 0; while (num_of_added_transitions < num_of_transitions_per_symbol) { - const State src_state{ state_dis(gen) }; - const State tgt_state{ state_dis(gen) }; - if (nfa.delta.contains(src_state, symbol, tgt_state)) { + const State source{ state_rand_dis(gen) }; + const State target{ state_rand_dis(gen) }; + if (nfa.delta.contains(source, symbol, target)) { continue; } - nfa.delta.add(src_state, symbol, tgt_state); - num_of_added_transitions++; + nfa.delta.add(source, symbol, target); + ++num_of_added_transitions; } } diff --git a/tests/nfa/builder.cc b/tests/nfa/builder.cc index b75bb1770..d539b4f55 100644 --- a/tests/nfa/builder.cc +++ b/tests/nfa/builder.cc @@ -155,7 +155,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 0; final_state_density = 0; - Nfa nfa = mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density); + Nfa nfa = mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density); CHECK(nfa.num_of_states() == 1); CHECK(nfa.initial.size() == 1); CHECK(nfa.final.size() == 1); @@ -168,7 +168,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 0.5; final_state_density = 0.5; - Nfa nfa = mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density); + Nfa nfa = mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density); CHECK(nfa.num_of_states() == num_of_states); CHECK(nfa.initial.size() == 1); CHECK(nfa.final.size() == static_cast(std::round(final_state_density * static_cast(num_of_states)))); @@ -182,7 +182,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 0.5; final_state_density = 1; - Nfa nfa = mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density); + Nfa nfa = mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density); CHECK(nfa.num_of_states() == num_of_states); CHECK(nfa.initial.size() == 1); CHECK(nfa.final.size() == num_of_states); @@ -196,7 +196,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 10; final_state_density = 0.5; - Nfa nfa = mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density); + Nfa nfa = mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density); CHECK(nfa.num_of_states() == num_of_states); CHECK(nfa.initial.size() == 1); CHECK(nfa.final.size() == static_cast(std::round(final_state_density * static_cast(num_of_states)))); @@ -210,7 +210,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 5; final_state_density = 1; - Nfa nfa = mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density); + Nfa nfa = mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density); CHECK(nfa.num_of_states() == num_of_states); CHECK(nfa.initial.size() == 1); CHECK(nfa.final.size() == num_of_states); @@ -225,7 +225,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = static_cast(-0.1); final_state_density = 0.5; - CHECK_THROWS_AS(mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); + CHECK_THROWS_AS(mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); } SECTION("Throw runtime_error. transition_density > num_of_states") { @@ -234,7 +234,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 11; final_state_density = 0.5; - CHECK_THROWS_AS(mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); + CHECK_THROWS_AS(mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); } SECTION("Throw runtime_error. final_state_density < 0") { @@ -243,7 +243,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 0.5; final_state_density = static_cast(-0.1); - CHECK_THROWS_AS(mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); + CHECK_THROWS_AS(mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); } SECTION("Throw runtime_error. final_state_density > 1") { @@ -252,6 +252,6 @@ TEST_CASE("Create Tabakov-Vardi NFA") { transition_density = 0.5; final_state_density = static_cast(1.1); - CHECK_THROWS_AS(mata::nfa::builder::create_tabakov_vardi_nfa(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); + CHECK_THROWS_AS(mata::nfa::builder::create_random_nfa_tabakov_vardi(num_of_states, alphabet_size, transition_density, final_state_density), std::runtime_error); } }