Skip to content
Snippets Groups Projects
Commit db374697 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

algo: redesign the use of features from random include

parent 39605372
No related branches found
No related tags found
1 merge request!220Merge jt
......@@ -133,36 +133,30 @@ automaton::NFA < SymbolType, unsigned > RandomAutomatonFactory::LeslieConnectedN
if( n == 0 ) {
return automaton;
} else if( n == 1 ) {
automaton.addTransition( Q[ 0 ], alphabet[ ext::random_devices::semirandom() % alphabet.size( ) ], Q[ 0 ] );
unvisited = 0;
VStates[ 0 ] = true;
} else {
unsigned x = ( ext::random_devices::semirandom() % ( n - 1 ) ) + 1;
automaton.addTransition( Q[ 0 ], alphabet[ ext::random_devices::semirandom() % alphabet.size( ) ], Q[ x ] );
size_t x = std::uniform_int_distribution < size_t > ( 1, n - 1 ) ( ext::random_devices::semirandom );
automaton.addTransition( Q[ 0 ], alphabet[ std::uniform_int_distribution < size_t > ( 0, alphabet.size ( ) - 1 ) ( ext::random_devices::semirandom ) ], Q[ x ] );
unvisited = n - 2;
 
VStates[ 0 ] = true;
VStates[ x ] = true;
}
VStates[ 0 ] = true;
 
while( unvisited != 0 ) {
size_t a = ithAccessibleState ( VStates, ext::random_devices::semirandom() % ( n - unvisited ) ); // select y-th accessible state
size_t b = ithInaccessibleState ( VStates, ext::random_devices::semirandom() % unvisited ); // select z-th inaccessible state
size_t a = ithAccessibleState ( VStates, std::uniform_int_distribution < size_t > ( 0, n - unvisited - 1 ) ( ext::random_devices::semirandom ) ); // select y-th accessible state
size_t b = ithInaccessibleState ( VStates, std::uniform_int_distribution < size_t > ( 0, unvisited - 1 ) ( ext::random_devices::semirandom ) ); // select z-th inaccessible state
 
int c = ext::random_devices::semirandom() % alphabet.size( );
size_t c = std::uniform_int_distribution < size_t > ( 0, alphabet.size ( ) - 1 ) ( ext::random_devices::semirandom );
automaton.addTransition( Q[ a ], alphabet[ c ], Q[ b ] );
 
unvisited -= 1;
VStates[ b ] = true;
}
 
for( const unsigned & q : Q ) {
if( automaton.getTransitionsFromState( q ).empty ( ) && ext::random_devices::semirandom() % 100 < 90 )
automaton.addFinalState( q );
else if( !automaton.getTransitionsFromState( q ).empty ( ) && ext::random_devices::semirandom() % 100 < 10 )
automaton.addFinalState( q );
}
for( const unsigned & q : Q )
if ( ext::uniform_unsigned_event ( automaton.getTransitionsFromState ( q ).empty ( ) ? 90 : 10 ) ( ext::random_devices::semirandom ) )
automaton.addFinalState ( q );
 
if( automaton.getFinalStates( ).empty ( ) ) {
if( n == 1 ) {
......@@ -179,9 +173,9 @@ automaton::NFA < SymbolType, unsigned > RandomAutomatonFactory::LeslieConnectedN
 
double mnn100 = 100.0 / alphabet.size( ) / n / n;
while( automaton.getTransitions ( ).size ( ) * mnn100 < density ) {
int y = ext::random_devices::semirandom() % n;
int z = ext::random_devices::semirandom() % n;
int a = ext::random_devices::semirandom() % alphabet.size();
size_t y = std::uniform_int_distribution < size_t > ( 0, n - 1 ) ( ext::random_devices::semirandom );
size_t z = std::uniform_int_distribution < size_t > ( 0, n - 1 ) ( ext::random_devices::semirandom );
size_t a = std::uniform_int_distribution < size_t > ( 0, alphabet.size ( ) - 1 ) ( ext::random_devices::semirandom );
 
automaton.addTransition( Q[ y ], alphabet [ a ], Q[ z ] );
}
......
......@@ -140,13 +140,9 @@ automaton::NFTA < SymbolType, unsigned > RandomTreeAutomatonFactory::LeslieConne
 
if( n == 0 ) {
return automaton;
} else if( n == 1 ) {
automaton.addTransition( nullaryAlphabet[ ext::random_devices::semirandom() % nullaryAlphabet.size( ) ], ext::vector < unsigned > { }, Q[ 0 ] );
VStates[ 0 ] = true;
} else {
unsigned x = ext::random_devices::semirandom() % n;
automaton.addTransition( nullaryAlphabet[ ext::random_devices::semirandom() % nullaryAlphabet.size( ) ], ext::vector < unsigned > { }, Q[ x ] );
size_t x = std::uniform_int_distribution < size_t > ( 0, n - 1 ) ( ext::random_devices::semirandom );
automaton.addTransition( nullaryAlphabet[ std::uniform_int_distribution < size_t > ( 0, nullaryAlphabet.size ( ) - 1 ) ( ext::random_devices::semirandom ) ], ext::vector < unsigned > { }, Q[ x ] );
 
VStates[ x ] = true;
}
......@@ -154,14 +150,14 @@ automaton::NFTA < SymbolType, unsigned > RandomTreeAutomatonFactory::LeslieConne
size_t unvisited = n - 1;
 
while( unvisited != 0 ) {
int c = ext::random_devices::semirandom() % alphabet.size( );
size_t c = std::uniform_int_distribution < size_t > ( 0, alphabet.size ( ) - 1 ) ( ext::random_devices::semirandom );
ext::vector < unsigned > from;
while ( from.size ( ) < alphabet [ c ].getRank ( ) ) {
size_t a = ithAccessibleState ( VStates, ext::random_devices::semirandom() % ( n - unvisited ) ); // select y-th accessible state
size_t a = ithAccessibleState ( VStates, std::uniform_int_distribution < size_t > ( 0, n - unvisited - 1 ) ( ext::random_devices::semirandom ) ); // select y-th accessible state
from.push_back ( Q[ a ] );
}
 
size_t b = ithInaccessibleState ( VStates, ext::random_devices::semirandom() % unvisited ); // select z-th inaccessible state
size_t b = ithInaccessibleState ( VStates, std::uniform_int_distribution < size_t > ( 0, unvisited - 1 ) ( ext::random_devices::semirandom ) ); // select z-th inaccessible state
 
automaton.addTransition( alphabet[ c ], std::move ( from ), Q[ b ] );
 
......@@ -169,12 +165,9 @@ automaton::NFTA < SymbolType, unsigned > RandomTreeAutomatonFactory::LeslieConne
VStates[ b ] = true;
}
 
for( const unsigned & q : Q ) {
if( automaton.getTransitionsFromState( q ).empty ( ) && ext::random_devices::semirandom() % 100 < 90 )
automaton.addFinalState( q );
else if( !automaton.getTransitionsFromState( q ).empty ( ) && ext::random_devices::semirandom() % 100 < 10 )
automaton.addFinalState( q );
}
for( const unsigned & q : Q )
if ( ext::uniform_unsigned_event ( automaton.getTransitionsFromState ( q ).empty ( ) ? 90 : 10 ) ( ext::random_devices::semirandom ) )
automaton.addFinalState ( q );
 
if( automaton.getFinalStates( ).empty ( ) ) {
if( n == 1 ) {
......@@ -195,14 +188,15 @@ automaton::NFTA < SymbolType, unsigned > RandomTreeAutomatonFactory::LeslieConne
 
double mnn100 = 100.0 / alphabet.size( ) / accSourceStates / n; //FIXME check the computation of density
while( automaton.getTransitions ( ).size ( ) * mnn100 < density ) {
int a = ext::random_devices::semirandom() % alphabet.size( );
size_t a = std::uniform_int_distribution < size_t > ( 0, alphabet.size ( ) - 1 ) ( ext::random_devices::semirandom );
ext::vector < unsigned > from;
while ( from.size ( ) < alphabet [ a ].getRank ( ) ) {
int y = ext::random_devices::semirandom() % n;
size_t y = std::uniform_int_distribution < size_t > ( 0, n - 1 ) ( ext::random_devices::semirandom );
from.push_back ( Q[ y ] );
}
 
int z = ext::random_devices::semirandom() % n;
size_t z = std::uniform_int_distribution < size_t > ( 0, n - 1 ) ( ext::random_devices::semirandom );
 
automaton.addTransition( alphabet [ a ], std::move ( from ), Q[ z ] );
}
......
......@@ -72,24 +72,27 @@ grammar::CFG < TerminalSymbolType, NonterminalSymbolType > RandomGrammarFactory:
grammar.setTerminalAlphabet({terminals.begin(), terminals.end()});
grammar.setNonterminalAlphabet({nonterminals.begin(), nonterminals.end()});
 
if(ext::random_devices::semirandom() % 2)
if ( ext::uniform_unsigned_event ( 50 ) ( ext::random_devices::semirandom ) )
grammar.addRule(grammar.getInitialSymbol(), {});
 
int rules = 0;
while(rules < terminals.size() * nonterminals.size() * density / 100) {
const NonterminalSymbolType& lhs = nonterminals[ext::random_devices::semirandom() % nonterminals.size()];
size_t nonterminal = std::uniform_int_distribution < size_t > ( 0, nonterminals.size ( ) - 1 ) ( ext::random_devices::semirandom );
const NonterminalSymbolType & lhs = nonterminals [ nonterminal ];
 
int rhsSize = ext::random_devices::semirandom() % 5;
ext::vector<ext::variant<TerminalSymbolType, NonterminalSymbolType>> rhs;
size_t rhsSize = std::uniform_int_distribution < size_t > ( 0, 4 ) ( ext::random_devices::semirandom );
ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rhs;
int nonterminalsOnRHS = 0;
for(int i = 0; i < rhsSize; i++) {
if(ext::random_devices::semirandom() % (nonterminals.size() + terminals.size()) < nonterminals.size()) {
rhs.push_back(ext::variant < TerminalSymbolType, NonterminalSymbolType > ( nonterminals[ext::random_devices::semirandom() % nonterminals.size()]));
for ( size_t i = 0; i < rhsSize; i++) {
size_t symbol = std::uniform_int_distribution < size_t > ( 0, nonterminals.size ( ) + terminals.size ( ) - 1 ) ( ext::random_devices::semirandom );
if ( symbol < nonterminals.size ( ) ) {
rhs.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( nonterminals [ symbol ] ) );
nonterminalsOnRHS++;
} else {
rhs.push_back(ext::variant < TerminalSymbolType, NonterminalSymbolType > ( terminals[ext::random_devices::semirandom() % terminals.size()]));
rhs.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( terminals [ symbol - nonterminals.size ( ) ] ) );
}
}
rules += nonterminalsOnRHS;
grammar.addRule(lhs, rhs);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment