From 750a103ea8df4e5d0c8790a40253cce3fc8c8cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tr=C3=A1vn=C3=AD=C4=8Dek?= <jan.travnicek@fit.cvut.cz> Date: Thu, 30 Dec 2021 18:12:54 +0100 Subject: [PATCH] algo: generate trees with minimal alphabet only --- .../src/tree/generate/RandomTreeFactory.cpp | 81 ++++--------------- 1 file changed, 16 insertions(+), 65 deletions(-) diff --git a/alib2algo/src/tree/generate/RandomTreeFactory.cpp b/alib2algo/src/tree/generate/RandomTreeFactory.cpp index 7fed5333c9..e29267b2a3 100644 --- a/alib2algo/src/tree/generate/RandomTreeFactory.cpp +++ b/alib2algo/src/tree/generate/RandomTreeFactory.cpp @@ -337,9 +337,7 @@ UnrankedTree < > RandomUnrankedTreeFactory::generateUnrankedTree ( int depth, in root->generateUnrankedSymbols ( alphabet ); - ext::set < DefaultSymbolType > treeAlphabet ( alphabet.begin ( ), alphabet.end ( ) ); - - UnrankedTree < > tree ( treeAlphabet, root->createUnrankedNode ( ) ); + UnrankedTree < > tree ( root->createUnrankedNode ( ) ); delete root; return tree; } @@ -352,15 +350,10 @@ UnrankedPattern < > RandomUnrankedPatternFactory::generateUnrankedPattern ( int root->generateUnrankedSymbols ( alphabet ); - ext::set < DefaultSymbolType > treeAlphabet ( alphabet.begin ( ), alphabet.end ( ) ); - DefaultSymbolType subtreeWildcard = alphabet::WildcardSymbol::instance < DefaultSymbolType > ( ); - treeAlphabet.insert ( subtreeWildcard ); - DefaultSymbolType subtreeGap = alphabet::GapSymbol::instance < DefaultSymbolType > ( ); - treeAlphabet.insert ( subtreeGap ); - UnrankedPattern < > tree ( std::move ( subtreeWildcard ), std::move ( subtreeGap ), treeAlphabet, root->createUnrankedPatternNode ( ) ); + UnrankedPattern < > tree ( std::move ( subtreeWildcard ), std::move ( subtreeGap ), root->createUnrankedPatternNode ( ) ); delete root; return tree; } @@ -373,27 +366,17 @@ UnrankedExtendedPattern < > RandomUnrankedExtendedPatternFactory::generateUnrank root->generateUnrankedSymbols ( alphabet ); - ext::set < DefaultSymbolType > treeAlphabet ( alphabet.begin ( ), alphabet.end ( ) ); - - DefaultSymbolType subtreeWildcard = alphabet::WildcardSymbol::instance < DefaultSymbolType > ( ); - treeAlphabet.insert ( subtreeWildcard ); - - DefaultSymbolType subtreeGap = alphabet::GapSymbol::instance < DefaultSymbolType > ( ); - treeAlphabet.insert ( subtreeGap ); - - DefaultSymbolType nodeWildcard = alphabet::NodeWildcardSymbol::instance < DefaultSymbolType > ( ); - treeAlphabet.insert ( nodeWildcard ); - ext::set < DefaultSymbolType > selectedNodeWildcards; - for ( const DefaultSymbolType & symbol : treeAlphabet ) { - if ( symbol == subtreeWildcard || symbol == subtreeGap ) - continue; - + for ( const DefaultSymbolType & symbol : alphabet ) { if ( ext::random_devices::semirandom ( ) % 100 < nodeWildcardProbability ) selectedNodeWildcards.insert ( symbol ); } - UnrankedExtendedPattern < > tree ( std::move ( subtreeWildcard ), std::move ( subtreeGap ), std::move ( nodeWildcard ), treeAlphabet, root->createUnrankedExtendedPatternNode ( selectedNodeWildcards ) ); + DefaultSymbolType nodeWildcard = alphabet::NodeWildcardSymbol::instance < DefaultSymbolType > ( ); + DefaultSymbolType subtreeWildcard = alphabet::WildcardSymbol::instance < DefaultSymbolType > ( ); + DefaultSymbolType subtreeGap = alphabet::GapSymbol::instance < DefaultSymbolType > ( ); + + UnrankedExtendedPattern < > tree ( std::move ( subtreeWildcard ), std::move ( subtreeGap ), std::move ( nodeWildcard ), root->createUnrankedExtendedPatternNode ( selectedNodeWildcards ) ); delete root; return tree; } @@ -408,13 +391,7 @@ RankedTree < > RandomRankedTreeFactory::generateRankedTree ( int depth, int node generateRankedAlphabet ( rankedAlphabet, maxAlphabetSize, randomizedAlphabet ); root->generateRankedSymbols ( rankedAlphabet ); - ext::set < common::ranked_symbol < > > treeRankedAlphabet; - - for ( const std::pair < const size_t, ext::vector < DefaultSymbolType > > & it : rankedAlphabet ) - for ( DefaultSymbolType i : it.second ) - treeRankedAlphabet.insert ( common::ranked_symbol < > ( i, it.first ) ); - - RankedTree < > tree ( treeRankedAlphabet, root->createRankedNode ( ) ); + RankedTree < > tree ( root->createRankedNode ( ) ); delete root; return tree; } @@ -429,16 +406,9 @@ RankedPattern < > RandomRankedPatternFactory::generateRankedPattern ( int depth, generateRankedAlphabet ( rankedAlphabet, maxAlphabetSize, randomizedAlphabet ); root->generateRankedSymbols ( rankedAlphabet ); - ext::set < common::ranked_symbol < > > treeRankedAlphabet; - - for ( const std::pair < const size_t, ext::vector < DefaultSymbolType > > & it : rankedAlphabet ) - for ( DefaultSymbolType i : it.second ) - treeRankedAlphabet.insert ( common::ranked_symbol < > ( i, it.first ) ); - common::ranked_symbol < > subtreeWildcard = alphabet::WildcardSymbol::instance < common::ranked_symbol < > > ( ); - treeRankedAlphabet.insert ( subtreeWildcard ); - RankedPattern < > tree ( std::move ( subtreeWildcard ), treeRankedAlphabet, root->createRankedPatternNode ( ) ); + RankedPattern < > tree ( std::move ( subtreeWildcard ), root->createRankedPatternNode ( ) ); delete root; return tree; } @@ -453,32 +423,20 @@ RankedExtendedPattern < > RandomRankedExtendedPatternFactory::generateRankedExte generateRankedAlphabet ( rankedAlphabet, maxAlphabetSize, randomizedAlphabet ); root->generateRankedSymbols ( rankedAlphabet ); - ext::set < common::ranked_symbol < > > treeRankedAlphabet; - + ext::set < common::ranked_symbol < > > selectedNodeWildcards; for ( const std::pair < const size_t, ext::vector < DefaultSymbolType > > & it : rankedAlphabet ) for ( DefaultSymbolType i : it.second ) - treeRankedAlphabet.insert ( common::ranked_symbol < > ( i, it.first ) ); - - common::ranked_symbol < > subtreeWildcard = alphabet::WildcardSymbol::instance < common::ranked_symbol < > > ( ); - treeRankedAlphabet.insert ( subtreeWildcard ); - - ext::set < common::ranked_symbol < > > selectedNodeWildcards; - for ( const common::ranked_symbol < > & symbol : treeRankedAlphabet ) { - if ( symbol == subtreeWildcard ) - continue; - - if ( ext::random_devices::semirandom ( ) % 100 < nodeWildcardProbability ) - selectedNodeWildcards.insert ( symbol ); - } + if ( ext::random_devices::semirandom ( ) % 100 < nodeWildcardProbability ) + selectedNodeWildcards.insert ( common::ranked_symbol < > ( i, it.first ) ); ext::set < common::ranked_symbol < > > nodeWildcards; for ( const common::ranked_symbol < > & symbol : selectedNodeWildcards ) { common::ranked_symbol < > wildcard ( alphabet::NodeWildcardSymbol::template instance < DefaultSymbolType > ( ), symbol.getRank ( ) ); nodeWildcards.insert ( wildcard ); - treeRankedAlphabet.insert ( std::move ( wildcard ) ); } - RankedExtendedPattern < > tree ( std::move ( subtreeWildcard ), std::move ( nodeWildcards ), std::move ( treeRankedAlphabet ), root->createRankedExtendedPatternNode ( selectedNodeWildcards ) ); + common::ranked_symbol < > subtreeWildcard = alphabet::WildcardSymbol::instance < common::ranked_symbol < > > ( ); + RankedExtendedPattern < > tree ( std::move ( subtreeWildcard ), std::move ( nodeWildcards ), root->createRankedExtendedPatternNode ( selectedNodeWildcards ) ); delete root; return tree; } @@ -493,13 +451,8 @@ RankedNonlinearPattern < > RandomRankedNonlinearPatternFactory::generateRankedNo generateRankedAlphabet ( rankedAlphabet, maxAlphabetSize, randomizedAlphabet ); root->generateRankedSymbols ( rankedAlphabet ); - ext::set < common::ranked_symbol < > > treeRankedAlphabet; ext::set < common::ranked_symbol < > > nonlinearVariables; - for ( const std::pair < const size_t, ext::vector < DefaultSymbolType > > & it : rankedAlphabet ) - for ( DefaultSymbolType i : it.second ) - treeRankedAlphabet.insert ( common::ranked_symbol < > ( i, it.first ) ); - if ( singleNonlinearVariable ) nonlinearVariables.insert ( common::ranked_symbol < > ( DefaultSymbolType ( alphabet::NonlinearVariableSymbol < > ( DefaultSymbolType ( "A" ) ) ), 0 ) ); else @@ -507,9 +460,7 @@ RankedNonlinearPattern < > RandomRankedNonlinearPatternFactory::generateRankedNo nonlinearVariables.insert ( common::ranked_symbol < > ( DefaultSymbolType ( alphabet::NonlinearVariableSymbol < > ( DefaultSymbolType ( i ) ) ), 0 ) ); common::ranked_symbol < > subtreeWildcard = alphabet::WildcardSymbol::instance < common::ranked_symbol < > > ( ); - treeRankedAlphabet.insert ( subtreeWildcard ); - treeRankedAlphabet.insert ( nonlinearVariables.begin ( ), nonlinearVariables.end ( ) ); - RankedNonlinearPattern < > tree ( std::move ( subtreeWildcard ), nonlinearVariables, treeRankedAlphabet, root->createRankedNonlinearPatternNode ( singleNonlinearVariable ) ); + RankedNonlinearPattern < > tree ( std::move ( subtreeWildcard ), nonlinearVariables, root->createRankedNonlinearPatternNode ( singleNonlinearVariable ) ); delete root; return tree; } -- GitLab