13#include <unordered_map>
27 std::unordered_map<std::string, std::size_t> token_to_fact;
29 std::unordered_map<std::string, std::size_t> certain_to_fact;
31 for (
const auto &row : rows) {
32 for (
unsigned long e : row.elements)
35 if (row.token.empty()) {
37 std::string key = std::to_string(row.relation_id) +
":";
38 for (
unsigned long e : row.elements)
39 key += std::to_string(e) +
",";
40 if (certain_to_fact.count(key))
42 certain_to_fact[key] = enc.
facts.size();
47 enc.
facts.push_back(std::move(f));
51 auto it = token_to_fact.find(row.token);
52 if (it != token_to_fact.end()) {
53 const Fact &existing = enc.
facts[it->second];
57 "provenance token " + row.token +
58 " gates facts over different element tuples; "
59 "the joint-width data-graph path requires distinct tokens");
63 if (row.prob < 0. || row.prob > 1.)
71 token_to_fact[row.token] = enc.
facts.size();
72 enc.
facts.push_back(std::move(f));
79 unsigned max_degree = 0;
88 std::vector<SliceGate>
slice,
99 for (
const auto &sg : enc.
slice)
110 for (
const auto &f : enc.
facts) {
111 for (std::size_t i = 0; i < f.elements.size(); ++i)
112 for (std::size_t j = i + 1; j < f.elements.size(); ++j)
113 if (f.elements[i] != f.elements[j])
114 data.
add_edge(f.elements[i], f.elements[j]);
115 if (!f.elements.empty())
123 for (std::size_t i = 0; i < enc.
slice.size(); ++i) {
125 std::vector<unsigned long> cl;
129 for (std::size_t a = 0; a < cl.size(); ++a)
130 for (std::size_t b = a + 1; b < cl.size(); ++b)
147 for (
const auto &f :
facts) {
151 std::vector<unsigned long> cl = f.elements;
154 for (std::size_t i = 0; i < cl.size(); ++i)
155 for (std::size_t j = i + 1; j < cl.size(); ++j)
158 if (!cl.empty() && !g.
has_node(cl[0]))
165 for (std::size_t i = 0; i <
slice.size(); ++i) {
171 std::vector<unsigned long> cl;
175 for (std::size_t a = 0; a < cl.size(); ++a)
176 for (std::size_t b = a + 1; b < cl.size(); ++b)
Phase A of the joint-width UCQ compiler: assemble the joint graph of the data and its correlation str...
@ GATE
Present iff its slice gate evaluates true (correlated regime).
@ CERTAIN
Always present: an untracked relation, constant-true token.
@ INDEP
Present iff its independent Bernoulli event is drawn (data-graph regime).
Tree decomposition of a Boolean circuit for knowledge compilation.
Mutable adjacency-list graph over unsigned-long node IDs.
void add_edge(unsigned long src, unsigned long tgt, bool undirected=true)
Add an edge between src and tgt.
bool has_node(unsigned long node) const
Return true if node is present in the graph.
void add_node(unsigned long node)
Add node to the graph (no edges).
Exception thrown when joint-width compilation cannot proceed.
The joint encoding of an instance: facts, world events, and the joint graph the screen and the DP run...
static JointEncoding fromCorrelated(std::vector< Fact > facts, std::vector< SliceGate > slice, unsigned long n_elements)
Construct the correlated-regime encoding from facts and an already-extracted circuit slice.
Graph buildGraph() const
Construct the joint graph for the screen and the DP.
std::vector< Event > events
Independent world variables (data-graph regime).
bool correlated
true when the slice is present (correlated regime).
unsigned circuit_treewidth_lb
Degeneracy lower bound of the slice-only graph (0 in the data-graph regime).
std::vector< Fact > facts
Deduplicated facts.
std::vector< SliceGate > slice
Circuit slice (correlated regime); gate vertex = n_elements + index.
unsigned data_treewidth_lb
Degeneracy lower bound of the data-only graph (diagnostics).
static JointEncoding fromFacts(const std::vector< FactRow > &rows)
Build the data-graph (§3.5 fast path) encoding from raw rows.
unsigned long n_elements
One past the largest domain element id (vertex ids [0,n_elements)).
static unsigned degeneracyLowerBound(const BooleanCircuit &bc, unsigned &max_degree)
Cheap degeneracy lower bound on the treewidth of bc's primal graph.
One independent Bernoulli world variable.
A deduplicated fact participating in the DP.
std::size_t event
Index into events (when INDEP).
std::vector< unsigned long > elements
Dense element ids (the fact's tuple).
FactGateKind kind
How the fact's presence is gated.
unsigned relation_id
Dense id of the relation symbol.
One node of the extracted circuit slice (correlated regime).
std::vector< unsigned > children
Child slice indices (≤ 2; empty for INPUT).