Go to the documentation of this file.00001 #include "causal_graph.h"
00002 #include "globals.h"
00003 #include "domain_transition_graph.h"
00004
00005 #include <algorithm>
00006 #include <iostream>
00007 #include <cassert>
00008 using namespace std;
00009
00010 CausalGraph::CausalGraph(istream &in)
00011 {
00012 check_magic(in, "begin_CG");
00013 int var_count = g_variable_domain.size();
00014 arcs.resize(var_count);
00015 edges.resize(var_count);
00016 for(int from_node = 0; from_node < var_count; from_node++) {
00017 int num_succ;
00018 in >> num_succ;
00019 arcs[from_node].reserve(num_succ);
00020 for(int j = 0; j < num_succ; j++) {
00021 int to_node;
00022 in >> to_node;
00023 arcs[from_node].push_back(to_node);
00024 edges[from_node].push_back(to_node);
00025 edges[to_node].push_back(from_node);
00026 }
00027 }
00028 check_magic(in, "end_CG");
00029
00030 for(int i = 0; i < var_count; i++) {
00031 sort(edges[i].begin(), edges[i].end());
00032 edges[i].erase(unique(edges[i].begin(), edges[i].end()), edges[i].end());
00033 }
00034 }
00035
00036 const vector<int> &CausalGraph::get_successors(int var) const
00037 {
00038 return arcs[var];
00039 }
00040
00041 const vector<int> &CausalGraph::get_neighbours(int var) const
00042 {
00043 return edges[var];
00044 }
00045
00046 void CausalGraph::get_comp_vars_for_func_var(int var, vector<int>& comp_vars)
00047 {
00048 for(int i = 0; i < arcs[var].size(); ++i) {
00049 if(g_variable_types[arcs[var][i]] == comparison) {
00050 comp_vars.push_back(arcs[var][i]);
00051 } else if(g_variable_types[arcs[var][i]] == subterm_functional) {
00052 get_comp_vars_for_func_var(arcs[var][i], comp_vars);
00053 }
00054 }
00055 }
00056
00057
00058
00059 void CausalGraph::get_functional_vars_in_unrolled_term(int top_var,
00060 set<int>& intermediate_vars)
00061 {
00062 intermediate_vars.insert(top_var);
00063 for(int i = 0; i < edges[top_var].size(); ++i) {
00064 int current_var = edges[top_var][i];
00065 bool current_is_predecessor_of_top_var = false;
00066 for(int j = 0; j < arcs[current_var].size(); ++j) {
00067 if(arcs[current_var][j] == top_var) {
00068 current_is_predecessor_of_top_var = true;
00069 break;
00070 }
00071 }
00072 if(!current_is_predecessor_of_top_var) {
00073 continue;
00074 }
00075 if(g_variable_types[current_var] == primitive_functional) {
00076 intermediate_vars.insert(current_var);
00077 } else {
00078 assert (g_variable_types[current_var] != comparison);
00079 if(g_variable_types[current_var] == subterm_functional) {
00080 get_functional_vars_in_unrolled_term(current_var, intermediate_vars);
00081 }
00082 }
00083 }
00084 }
00085
00086 void CausalGraph::dump() const
00087 {
00088 cout << "Causal graph: " << endl;
00089 for(int i = 0; i < arcs.size(); i++) {
00090 cout << "dependent on var " << g_variable_name[i] << ": " << endl;
00091 for(int j = 0; j < arcs[i].size(); j++)
00092 cout << " " << g_variable_name[arcs[i][j]] << ",";
00093 cout << endl;
00094 }
00095 }
00096