-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement naive GFD validation algorithm
GFD validation algorithm that uses VF2 algorithm to find a subgraph.
- Loading branch information
1 parent
d411276
commit 1c969c9
Showing
4 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include "naivegfd_validation.h" | ||
|
||
#include <iostream> | ||
#include <set> | ||
|
||
#include <boost/graph/vf2_sub_graph_iso.hpp> | ||
|
||
#include "gfd.h" | ||
|
||
namespace { | ||
|
||
struct CheckCallback { | ||
private: | ||
graph_t const& query; | ||
graph_t const& graph; | ||
const std::vector<Literal> premises; | ||
const std::vector<Literal> conclusion; | ||
bool& res; | ||
int& amount; | ||
|
||
public: | ||
CheckCallback(graph_t const& query_, graph_t const& graph_, | ||
std::vector<Literal> const& premises_, std::vector<Literal> const& conclusion_, | ||
bool& res_, int& amount_) | ||
: query(query_), | ||
graph(graph_), | ||
premises(premises_), | ||
conclusion(conclusion_), | ||
res(res_), | ||
amount(amount_) {} | ||
|
||
template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1> | ||
bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1) const { | ||
amount++; | ||
auto satisfied = [this, &f](std::vector<Literal> const& literals) { | ||
for (const Literal& l : literals) { | ||
auto fst_token = l.first; | ||
auto snd_token = l.second; | ||
std::string fst; | ||
std::string snd; | ||
if (fst_token.first == -1) { | ||
fst = fst_token.second; | ||
} else { | ||
vertex_t v; | ||
vertex_t u = boost::vertex(fst_token.first, query); | ||
v = get(f, u); | ||
auto attrs = graph[v].attributes; | ||
if (attrs.find(fst_token.second) == attrs.end()) { | ||
return false; | ||
} | ||
fst = attrs.at(fst_token.second); | ||
} | ||
if (snd_token.first == -1) { | ||
snd = snd_token.second; | ||
} else { | ||
vertex_t v; | ||
vertex_t u = boost::vertex(fst_token.first, query); | ||
v = get(f, u); | ||
auto attrs = graph[v].attributes; | ||
if (attrs.find(snd_token.second) == attrs.end()) { | ||
return false; | ||
} | ||
fst = attrs.at(snd_token.second); | ||
} | ||
if (fst != snd) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
if (!satisfied(premises)) { | ||
return true; | ||
} | ||
if (!satisfied(conclusion)) { | ||
res = false; | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; | ||
|
||
bool Validate(graph_t const& graph, Gfd const& gfd) { | ||
graph_t pattern = gfd.GetPattern(); | ||
|
||
struct VCompare { | ||
graph_t const& pattern; | ||
graph_t const& graph; | ||
|
||
bool operator()(vertex_t fr, vertex_t to) const { | ||
return pattern[fr].attributes.at("label") == graph[to].attributes.at("label"); | ||
} | ||
} vcompare{pattern, graph}; | ||
|
||
struct ECompare { | ||
graph_t const& pattern; | ||
graph_t const& graph; | ||
|
||
bool operator()(edge_t fr, edge_t to) const { | ||
return pattern[fr].label == graph[to].label; | ||
} | ||
} ecompare{pattern, graph}; | ||
|
||
bool res = true; | ||
int amount = 0; | ||
CheckCallback callback(pattern, graph, gfd.GetPremises(), gfd.GetConclusion(), res, amount); | ||
|
||
bool found = boost::vf2_subgraph_iso( | ||
pattern, graph, callback, get(boost::vertex_index, pattern), | ||
get(boost::vertex_index, graph), vertex_order_by_mult(pattern), ecompare, vcompare); | ||
std::cout << "Checked embeddings: " << amount << std::endl; | ||
if (!found) { | ||
return true; | ||
} | ||
return res; | ||
} | ||
|
||
} // namespace | ||
|
||
namespace algos { | ||
|
||
std::vector<Gfd> NaiveGfdValidation::GenerateSatisfiedGfds(graph_t const& graph, | ||
std::vector<Gfd> const& gfds) { | ||
for (auto& gfd : gfds) { | ||
if (Validate(graph, gfd)) { | ||
result_.push_back(gfd); | ||
} | ||
} | ||
return result_; | ||
} | ||
|
||
} // namespace algos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
#include <vector> | ||
|
||
#include "algorithms/algorithm.h" | ||
#include "algorithms/gfd/gfd_handler.h" | ||
#include "gfd.h" | ||
|
||
namespace algos { | ||
|
||
class NaiveGfdValidation : public GfdHandler { | ||
public: | ||
std::vector<Gfd> GenerateSatisfiedGfds(graph_t const& graph, std::vector<Gfd> const& gfds); | ||
|
||
NaiveGfdValidation() : GfdHandler(){}; | ||
|
||
NaiveGfdValidation(graph_t graph_, std::vector<Gfd> gfds_) : GfdHandler(graph_, gfds_) {} | ||
}; | ||
|
||
} // namespace algos |