1
Fork 0
mirror of https://github.com/Jaxan/hybrid-ads.git synced 2025-04-27 15:07:45 +02:00

Rewrites pre_gephi_tool to handle more hypotheses

This commit is contained in:
Joshua Moerman 2015-03-31 11:40:56 +02:00
parent c2be20b2f0
commit 77695e8f22

View file

@ -1,64 +1,95 @@
#include <read_mealy_from_dot.hpp>
#include <mealy.hpp>
#include <read_mealy_from_dot.hpp>
#include <transfer_sequences.hpp>
#include <cassert>
#include <string>
#include <boost/optional.hpp>
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using namespace boost;
int main(int argc, char *argv[]){
if(argc != 3) return 1;
if(argc != 5) return argc;
const string hypo_filename = argv[1];
const string mach_filename = argv[2];
// Program options
const string machince_filename = argv[1];
const string machince_positions_filename = argv[2];
const string hypo_filename_pattern = argv[3];
const size_t maximal_hypothesis = stoul(argv[4]);
// Read all the hypothesis
translation t;
vector<mealy> hypotheses;
for(size_t i = 0; i <= maximal_hypothesis; ++i){
clog << "Reading hypo " << i << endl;
string hypothesis_filename = hypo_filename_pattern;
auto it = hypothesis_filename.find('%');
hypothesis_filename.replace(it, 1, to_string(i));
hypotheses.push_back(read_mealy_from_dot(hypothesis_filename, t));
}
const auto hypothesis = read_mealy_from_dot(hypo_filename, t);
const auto machine = read_mealy_from_dot(mach_filename, t);
const auto machine = read_mealy_from_dot(machince_filename, t);
auto input_to_string = create_reverse_map(t.input_indices);
auto output_to_string = create_reverse_map(t.output_indices);
vector<bool> visited(machine.graph_size, false);
{
const auto state_cover = create_transfer_sequences(hypothesis, 0);
// Read the positions by gephi, indexed by state
// (export to .net file and then `tail +2 Untitled.net | awk '{print $3, $4}' > positions.txt`)
vector<pair<double, double>> positions(machine.graph_size);
ifstream position_file(machince_positions_filename);
for(auto & p : positions){
position_file >> p.first >> p.second;
}
// Visit all states and record in which hypo it is reached
vector<optional<size_t>> visited(machine.graph_size);
for(size_t i = 0; i <= maximal_hypothesis; ++i){
clog << "Visiting hypo " << i << endl;
const auto state_cover = create_transfer_sequences(hypotheses[i], 0);
for(auto && p : state_cover){
state s = 0;
for(auto && i : p){
visited[s.base()] = true;
s = apply(machine, s, i).to;
for(auto && inp : p){
if(!visited[s.base()]) visited[s.base()] = i;
s = apply(machine, s, inp).to;
}
visited[s.base()] = true;
if(!visited[s.base()]) visited[s.base()] = i;
}
}
{
ofstream out("gephi.dot");
out << "digraph g {\n";
// Output a dot per hypo, making a movie
for(size_t i = 0; i < hypotheses.size(); ++i){
clog << "Saving frame " << i << endl;
string hypothesis_filename = hypo_filename_pattern + ".movie";
auto it = hypothesis_filename.find('%');
hypothesis_filename.replace(it, 1, to_string(i));
ofstream out(hypothesis_filename);
out << "digraph {\n";
for(state s = 0; s < machine.graph_size; ++s){
bool is_visited = visited[s.base()] ? (visited[s.base()].value() <= i) : false;
out << "\t" << "s" << s << " [";
out << "visited=\"" << (visited[s.base()] ? "1" : "0") << "\"";
out << "color=\"" << (is_visited ? "green" : "red") << "\"" << ", ";
out << "pos=\"" << positions[s.base()].first << "," << positions[s.base()].second << "\"";
out << "]\n";
}
for(state s = 0; s < machine.graph_size; ++s){
vector<bool> visited(machine.graph_size, false);
visited[s.base()] = true;
for(input i = 0; i < machine.input_size; ++i){
const auto ret = apply(machine, s, i);
out << "\t" << "s" << s << " -> " << "s" << ret.to << " [";
out << "input=\"" << input_to_string[i.base()] << "\", ";
out << "output=\"" << output_to_string[ret.output.base()] << "\"";
out << "]\n";
const auto t = apply(machine, s, i).to;
if(visited[t.base()]) continue;
out << "\t" << "s" << s << " -> " << "s" << t << "\n";
visited[t.base()] = true;
}
}
out << "}" << endl;
}
}