Skip to content

Commit

Permalink
Optimize algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonChern committed Apr 27, 2023
1 parent f89745d commit 53143f1
Show file tree
Hide file tree
Showing 10 changed files with 6,965,853 additions and 79 deletions.
24 changes: 15 additions & 9 deletions src/algorithms/algo_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ std::unique_ptr<Primitive> CreateNaiveGFDValidationInstance(ParamsMap&& params)
namespace onam = config::names;
auto graph_path = std::filesystem::current_path() / "input_data" /
ExtractOptionValue<std::string>(params, onam::kData);
std::ifstream f(graph_path);
//std::ifstream f(graph_path);
std::ifstream f;
graph_t graph;
GFDtools::Parser::read_graph(f, graph);
f.close();
GFDtools::Parser::read_graph(graph_path / "large_twitch_edges.csv", graph_path / "large_twitch_features.csv", graph);
//GFDtools::Parser::read_graph(f, graph);
//f.close();
std::vector<GFD> gfds = {};
auto gfd_paths = ExtractOptionValue<std::vector<std::string>>(params, onam::kGFDData);
for (const auto& path : gfd_paths) {
Expand All @@ -107,10 +109,12 @@ std::unique_ptr<Primitive> CreateGFDValidationInstance(ParamsMap&& params) {
namespace onam = config::names;
auto graph_path = std::filesystem::current_path() / "input_data" /
ExtractOptionValue<std::string>(params, onam::kData);
std::ifstream f(graph_path);
//std::ifstream f(graph_path);
std::ifstream f;
graph_t graph;
GFDtools::Parser::read_graph(f, graph);
f.close();
GFDtools::Parser::read_graph(graph_path / "large_twitch_edges.csv", graph_path / "large_twitch_features.csv", graph);
//GFDtools::Parser::read_graph(f, graph);
//f.close();
std::vector<GFD> gfds = {};
auto gfd_paths = ExtractOptionValue<std::vector<std::string>>(params, onam::kGFDData);
for (const auto& path : gfd_paths) {
Expand All @@ -129,10 +133,12 @@ std::unique_ptr<Primitive> CreateEGFDValidationInstance(ParamsMap&& params) {
namespace onam = config::names;
auto graph_path = std::filesystem::current_path() / "input_data" /
ExtractOptionValue<std::string>(params, onam::kData);
std::ifstream f(graph_path);
//std::ifstream f(graph_path);
std::ifstream f;
graph_t graph;
GFDtools::Parser::read_graph(f, graph);
f.close();
GFDtools::Parser::read_graph(graph_path / "large_twitch_edges.csv", graph_path / "large_twitch_features.csv", graph);
//GFDtools::Parser::read_graph(f, graph);
//f.close();
std::vector<GFD> gfds = {};
auto gfd_paths = ExtractOptionValue<std::vector<std::string>>(params, onam::kGFDData);
for (const auto& path : gfd_paths) {
Expand Down
41 changes: 26 additions & 15 deletions src/algorithms/gfd/egfd_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,16 @@ bool EGFDValidation::check(CPI& cpi, const graph_t& graph, const GFD& gfd, const
}
else {
vertex_t v;
BGL_FORALL_VERTICES_T(u, query, graph_t) {
if (query[u].node_id == fst_token.first) {
int index = std::find(seq.begin(), seq.end(), u) - seq.begin();
v = *match.at(index).first;
break;
}
}
// BGL_FORALL_VERTICES_T(u, query, graph_t) {
// if (query[u].node_id == fst_token.first) {
// int index = std::find(seq.begin(), seq.end(), u) - seq.begin();
// v = *match.at(index).first;
// break;
// }
// }
vertex_t u = boost::vertex(fst_token.first, query);
int index = std::find(seq.begin(), seq.end(), u) - seq.begin();
v = *match.at(index).first;
auto attrs = graph[v].attributes;
if (attrs.find(fst_token.second) == attrs.end()) {
return false;
Expand All @@ -697,13 +700,16 @@ bool EGFDValidation::check(CPI& cpi, const graph_t& graph, const GFD& gfd, const
}
else {
vertex_t v;
BGL_FORALL_VERTICES_T(u, query, graph_t) {
if (query[u].node_id == snd_token.first) {
int index = std::find(seq.begin(), seq.end(), u) - seq.begin();
v = *match.at(index).first;
break;
}
}
// BGL_FORALL_VERTICES_T(u, query, graph_t) {
// if (query[u].node_id == snd_token.first) {
// int index = std::find(seq.begin(), seq.end(), u) - seq.begin();
// v = *match.at(index).first;
// break;
// }
// }
vertex_t u = boost::vertex(fst_token.first, query);
int index = std::find(seq.begin(), seq.end(), u) - seq.begin();
v = *match.at(index).first;
auto attrs = graph[v].attributes;
if (attrs.find(snd_token.second) == attrs.end()) {
return false;
Expand Down Expand Up @@ -861,6 +867,8 @@ std::vector<std::vector<vertex_t>> EGFDValidation::getPaths(const std::set<verte
}

bool EGFDValidation::validate(const graph_t& graph, const GFD& gfd) {
auto start_time = std::chrono::system_clock::now();

graph_t pat = gfd.getPattern();
std::set<std::string> graph_labels = {};
std::set<std::string> pat_labels = {};
Expand Down Expand Up @@ -892,7 +900,10 @@ bool EGFDValidation::validate(const graph_t& graph, const GFD& gfd) {
CPI cpi;
TopDownConstruct(cpi, graph, pat, levels, parent, candidates, snte);
BottomUpRefinement(cpi, graph, pat, levels, parent, candidates);
std::cout << "CPI constructed. Matching..." << std::endl;
auto elapsed_milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - start_time);
std::cout << "CPI constructed in " << elapsed_milliseconds.count() << ". Matching..." << std::endl;
return check(cpi, graph, gfd, core, forest, parent, nte);
}

Expand Down
85 changes: 48 additions & 37 deletions src/algorithms/gfd/gfd_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ struct CheckCallback {
}
else {
vertex_t v;
BGL_FORALL_VERTICES_T(u, query, graph_t) {
if (query[u].node_id == fst_token.first) {
v = get(f, u);
break;
}
}
// BGL_FORALL_VERTICES_T(u, query, graph_t) {
// if (query[u].node_id == fst_token.first) {
// v = get(f, u);
// break;
// }
// }
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;
Expand All @@ -181,12 +183,14 @@ struct CheckCallback {
}
else {
vertex_t v;
BGL_FORALL_VERTICES_T(u, query, graph_t) {
if (query[u].node_id == snd_token.first) {
v = get(f, u);
break;
}
}
// BGL_FORALL_VERTICES_T(u, query, graph_t) {
// if (query[u].node_id == snd_token.first) {
// v = get(f, u);
// break;
// }
// }
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;
Expand All @@ -211,41 +215,48 @@ struct CheckCallback {
}
};

struct VCompare {
const graph_t& pattern;
const graph_t& graph;
vertex_t pinted_fr;
vertex_t pinted_to;

bool operator()(vertex_t fr, vertex_t to) const {
if (fr == pinted_fr && to == pinted_to) {
return true;
}
if (fr == pinted_fr || to == pinted_to) {
return false;
}
return pattern[fr].attributes.at("label") == graph[to].attributes.at("label");
}
};

struct ECompare {
const graph_t& pattern;
const graph_t& graph;

bool operator()(edge_t fr, edge_t to) const {
return pattern[fr].label == graph[to].label;
}
};

void GFDValidation::calculateUnsatisfied(const graph_t& graph, const std::vector<Message>& messages,
const std::map<int, GFD>& indexed_gfds, std::set<int>& unsatisfied) {
for (auto& message : messages) {
int gfd_index = std::get<0>(message);
if (unsatisfied.find(gfd_index) != unsatisfied.end()) {
continue;
}

vertex_t u = std::get<1>(message);
vertex_t v = std::get<2>(message);

GFD gfd = indexed_gfds.at(gfd_index);
graph_t pattern = gfd.getPattern();

struct VCompare {
const graph_t& pattern;
const graph_t& graph;
vertex_t pinted_fr;
vertex_t pinted_to;

bool operator()(vertex_t fr, vertex_t to) const {
if (fr == pinted_fr && to == pinted_to) {
return true;
}
if (fr == pinted_fr || to == pinted_to) {
return false;
}
return pattern[fr].attributes.at("label") == graph[to].attributes.at("label");
}
} vcompare{ pattern, graph, u, v };

struct ECompare {
const graph_t& pattern;
const graph_t& graph;

bool operator()(edge_t fr, edge_t to) const {
return pattern[fr].label == graph[to].label;
}
} ecompare{ pattern, graph };
VCompare vcompare{ pattern, graph, u, v };
ECompare ecompare{ pattern, graph };

bool satisfied = true;
CheckCallback callback(pattern, graph, gfd.getPremises(), gfd.getConclusion(), satisfied);
Expand Down
28 changes: 16 additions & 12 deletions src/algorithms/gfd/naivegfd_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ struct CheckCallback {
}
else {
vertex_t v;
BGL_FORALL_VERTICES_T(u, query, graph_t) {
if (query[u].node_id == fst_token.first) {
v = get(f, u);
break;
}
}
// BGL_FORALL_VERTICES_T(u, query, graph_t) {
// if (query[u].node_id == fst_token.first) {
// v = get(f, u);
// break;
// }
// }
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;
Expand All @@ -52,12 +54,14 @@ struct CheckCallback {
}
else {
vertex_t v;
BGL_FORALL_VERTICES_T(u, query, graph_t) {
if (query[u].node_id == snd_token.first) {
v = get(f, u);
break;
}
}
// BGL_FORALL_VERTICES_T(u, query, graph_t) {
// if (query[u].node_id == snd_token.first) {
// v = get(f, u);
// break;
// }
// }
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;
Expand Down
52 changes: 52 additions & 0 deletions src/algorithms/gfd/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,56 @@ void Parser::write_gfd(std::string file_name, GFD& result) {
f.close();
};

void Parser::read_graph(std::string edges_name, std::string features_name, graph_t& result) {
std::ifstream features_stream(features_name);
std::map<int, vertex_t> iso;

// int limit = 10000;
int vertices = 0;
std::string line, word;
std::getline(features_stream, line);
while (std::getline(features_stream, line)) {
std::vector<std::string> row = {};

std::stringstream str(line);

while (std::getline(str, word, ','))
row.push_back(word);

auto desc = boost::add_vertex(vertex{ vertices, {{"label", row.at(7)}, {"mature", row.at(1)}, {"dead", row.at(6)}, {"affiliate", row.at(8)}}}, result);
iso.emplace(vertices++, desc);
// if (vertices == limit) {
// break;
// }
}

features_stream.close();

std::ifstream edges_stream(edges_name);

std::getline(edges_stream, line);
while (std::getline(edges_stream, line)) {
std::vector<std::string> row = {};

std::stringstream str(line);

while (std::getline(str, word, ','))
row.push_back(word);

if ((iso.find(stoi(row.at(0))) == iso.end()) ||
(iso.find(stoi(row.at(1))) == iso.end())) {
continue;
}
vertex_t from = iso.at(stoi(row.at(0)));
vertex_t to = iso.at(stoi(row.at(1)));

if (!boost::edge(from, to, result).second) {
boost::add_edge(from, to, edge{ "d" }, result);
}
}
std::cout << "Graph read" << std::endl;

edges_stream.close();
}

} // namespace GFDtools
2 changes: 2 additions & 0 deletions src/algorithms/gfd/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Parser {
static void read_graph_dot(std::istream& stream, graph_t& result);
static void read_graph_dot(std::string file_name, graph_t& result);

static void read_graph(std::string edges_name, std::string features_name, graph_t& result);

static void read_graph(std::istream& stream, graph_t& result);
static void read_graph(std::string file_name, graph_t& result);

Expand Down
15 changes: 15 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ int main(int argc, char const* argv[]) {
std::cout << e.what() << std::endl;
return 1;
}

{
pid_t pid = getpid();
char name[100];
sprintf(name, "/proc/%d/status", pid);
FILE *f = fopen(name, "r");
while (!feof(f)) {
fgets(name, 100, f);
if (!strncmp(name, "VmPeak", 6)) {
printf("%s", name);
break;
}
}
fclose(f);
}

return 0;
}
Loading

0 comments on commit 53143f1

Please sign in to comment.