Skip to content
Snippets Groups Projects
Commit ef99c24e authored by Maxence Naud's avatar Maxence Naud
Browse files

Reduce graph regex matching time with the help of @vincentlorrain

parent 6379a891
No related branches found
No related tags found
No related merge requests found
#include "aidge/graphRegex/GraphRegex.hpp" #include "aidge/graphRegex/GraphRegex.hpp"
using namespace Aidge; using namespace Aidge;
void GraphRegex::setKeyFromGraph(std::shared_ptr<GraphView> ref){ void GraphRegex::setKeyFromGraph(std::shared_ptr<GraphView> ref){
...@@ -27,7 +27,7 @@ void GraphRegex::setKeyFromGraph(std::shared_ptr<GraphView> ref){ ...@@ -27,7 +27,7 @@ void GraphRegex::setKeyFromGraph(std::shared_ptr<GraphView> ref){
// void GraphRegex::addQuery(const std::string query){ // void GraphRegex::addQuery(const std::string query){
// //TODO one query only but the same string is a same query but // //TODO one query only but the same string is a same query but
// //2 different string it's maybe the same query , we need to check the AST // //2 different string it's maybe the same query , we need to check the AST
// mQueryRecipe[query] = nullptr; // mQueryRecipe[query] = nullptr;
// } // }
...@@ -52,7 +52,7 @@ void GraphRegex::_generateCombinationsStart(const std::set<NodePtr>& elements, s ...@@ -52,7 +52,7 @@ void GraphRegex::_generateCombinationsStart(const std::set<NodePtr>& elements, s
} }
} }
// factorial(n) tree searched optimized with a stopping condition
void GraphRegex::_findLargestCompatibleSet( void GraphRegex::_findLargestCompatibleSet(
const std::vector<std::shared_ptr<MatchSolution>>& solutions, const std::vector<std::shared_ptr<MatchSolution>>& solutions,
std::set<std::shared_ptr<MatchSolution>>& currentSet, std::set<std::shared_ptr<MatchSolution>>& currentSet,
...@@ -75,6 +75,10 @@ void GraphRegex::_findLargestCompatibleSet( ...@@ -75,6 +75,10 @@ void GraphRegex::_findLargestCompatibleSet(
currentSet.insert(solutions[i]); currentSet.insert(solutions[i]);
_findLargestCompatibleSet(solutions, currentSet, largestSet, i + 1); _findLargestCompatibleSet(solutions, currentSet, largestSet, i + 1);
currentSet.erase(solutions[i]); currentSet.erase(solutions[i]);
// cut the size of the graph of possibilities
if ((currentSet.size() + solutions.size() - currentIndex) <= largestSet.size()) {
return;
}
} }
} }
} }
...@@ -101,14 +105,14 @@ std::set<std::shared_ptr<MatchSolution>> GraphRegex::match(std::shared_ptr<Graph ...@@ -101,14 +105,14 @@ std::set<std::shared_ptr<MatchSolution>> GraphRegex::match(std::shared_ptr<Graph
std::shared_ptr<GraphFsmInterpreter> fsmGenerator = std::make_shared<GraphFsmInterpreter>(query,mAllTest); std::shared_ptr<GraphFsmInterpreter> fsmGenerator = std::make_shared<GraphFsmInterpreter>(query,mAllTest);
std::shared_ptr<FsmGraph> fsm = fsmGenerator->interpret(); std::shared_ptr<FsmGraph> fsm = fsmGenerator->interpret();
// generate all the start possibility // generate all the start possibility
std::size_t nb_startSt = fsm->getNbStart(); std::size_t nb_startSt = fsm->getNbStart();
std::set<std::vector<NodePtr>> combinations; std::set<std::vector<NodePtr>> combinations;
std::vector<NodePtr> current; std::vector<NodePtr> current;
_generateCombinationsStart(ref->getNodes(), nb_startSt, 0, current, combinations); _generateCombinationsStart(ref->getNodes(), nb_startSt, 0, current, combinations);
// all start
// all start
for (const auto& combination : combinations) { for (const auto& combination : combinations) {
std::vector<std::shared_ptr<MatchSolution>> solution = fsm->test(combination); std::vector<std::shared_ptr<MatchSolution>> solution = fsm->test(combination);
solutions.insert(solutions.end(), solution.begin(), solution.end()); solutions.insert(solutions.end(), solution.begin(), solution.end());
...@@ -133,7 +137,7 @@ void GraphRegex::setNodeKey(const std::string key, const std::string conditional ...@@ -133,7 +137,7 @@ void GraphRegex::setNodeKey(const std::string key, const std::string conditional
void GraphRegex::setNodeKey(const std::string key,std::function<bool(NodePtr)> f){ void GraphRegex::setNodeKey(const std::string key,std::function<bool(NodePtr)> f){
//we can applied to all key but it's not efficient //we can applied to all key but it's not efficient
if(mAllLambda.find(key) != mAllLambda.end()){ if(mAllLambda.find(key) != mAllLambda.end()){
throw std::runtime_error(key + " is define"); throw std::runtime_error(key + " is define");
} }
...@@ -142,7 +146,7 @@ void GraphRegex::setNodeKey(const std::string key,std::function<bool(NodePtr)> f ...@@ -142,7 +146,7 @@ void GraphRegex::setNodeKey(const std::string key,std::function<bool(NodePtr)> f
} }
void GraphRegex::_majConditionalInterpreterLambda(){ void GraphRegex::_majConditionalInterpreterLambda(){
for (const auto& test : mAllTest) { for (const auto& test : mAllTest) {
for (const auto& pair : mAllLambda) { for (const auto& pair : mAllLambda) {
const std::string& key = pair.first; const std::string& key = pair.first;
...@@ -151,7 +155,7 @@ void GraphRegex::_majConditionalInterpreterLambda(){ ...@@ -151,7 +155,7 @@ void GraphRegex::_majConditionalInterpreterLambda(){
if(!test->isLambdaRegister(key)){ if(!test->isLambdaRegister(key)){
test->insertLambda(key,lambda); test->insertLambda(key,lambda);
} }
} }
} }
} }
......
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