Skip to content

Commit

Permalink
renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
koniksedy committed Nov 20, 2024
1 parent 3c1a8d2 commit cec6307
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
5 changes: 3 additions & 2 deletions include/mata/nfa/builder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/nfa/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(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]");
Expand All @@ -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> state_dis(0, num_of_states - 1);
std::uniform_int_distribution<Symbol> symbol_dis(0, static_cast<Symbol>(alphabet_size - 1));
std::uniform_int_distribution<State> state_rand_dis(0, num_of_states - 1);
std::uniform_int_distribution<Symbol> symbol_rand_dis(0, static_cast<Symbol>(alphabet_size - 1));

// Create final states
const size_t num_of_final_states{ static_cast<size_t>(std::round(static_cast<float>(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<size_t>(std::round(static_cast<float>(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;
}
}

Expand Down
18 changes: 9 additions & 9 deletions tests/nfa/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<size_t>(std::round(final_state_density * static_cast<float>(num_of_states))));
Expand All @@ -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);
Expand All @@ -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<size_t>(std::round(final_state_density * static_cast<float>(num_of_states))));
Expand All @@ -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);
Expand All @@ -225,7 +225,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") {
transition_density = static_cast<float>(-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") {
Expand All @@ -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") {
Expand All @@ -243,7 +243,7 @@ TEST_CASE("Create Tabakov-Vardi NFA") {
transition_density = 0.5;
final_state_density = static_cast<float>(-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") {
Expand All @@ -252,6 +252,6 @@ TEST_CASE("Create Tabakov-Vardi NFA") {
transition_density = 0.5;
final_state_density = static_cast<float>(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);
}
}

0 comments on commit cec6307

Please sign in to comment.