ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
dDNNF.h
Go to the documentation of this file.
1/**
2 * @file dDNNF.h
3 * @brief Decomposable Deterministic Negation Normal Form circuit.
4 *
5 * A **d-DNNF** (decomposable deterministic Negation Normal Form) is a
6 * Boolean circuit with two structural properties:
7 * - **Determinism**: for every OR gate, the two sub-formulae are
8 * mutually exclusive (their models are disjoint).
9 * - **Decomposability**: for every AND gate, the two sub-formulae share
10 * no variable.
11 *
12 * These properties enable:
13 * - Linear-time computation of the probability of satisfying assignments.
14 * - Polynomial computation of Shapley and Banzhaf power indices.
15 *
16 * @c dDNNF extends @c BooleanCircuit with:
17 * - A designated @c root gate.
18 * - A memoisation cache for probability evaluation.
19 * - Normalisation methods (@c makeSmooth(), @c makeGatesBinary(),
20 * @c simplify()) that transform the circuit into a canonical form
21 * required by the evaluation algorithms.
22 * - Conditioning methods (@c condition(), @c conditionAndSimplify())
23 * used during Shapley/Banzhaf computation.
24 *
25 * ### @c hash_gate_t
26 * A standard-library compatible hash functor for @c gate_t, required
27 * because @c gate_t is a scoped enum and has no built-in @c std::hash
28 * specialisation.
29 */
30#ifndef DDNNF_H
31#define DDNNF_H
32
33#include <string>
34#include <unordered_set>
35
36#include "BooleanCircuit.h"
37
38// Forward declaration for friend
40
41/**
42 * @brief @c std::hash functor for @c gate_t.
43 *
44 * Allows @c gate_t to be used as a key in @c std::unordered_map and
45 * @c std::unordered_set by delegating to @c std::hash on the underlying
46 * integer type.
47 */
49{
50 /**
51 * @brief Compute the hash of a gate identifier.
52 * @param g Gate to hash.
53 * @return Hash value suitable for use in unordered containers.
54 */
55 size_t operator()(gate_t g) const
56 {
57 return std::hash<typename std::underlying_type<gate_t>::type>()(
58 static_cast<typename std::underlying_type<gate_t>::type>(g));
59 }
60};
61
62/**
63 * @brief A d-DNNF circuit supporting exact probabilistic and game-theoretic evaluation.
64 *
65 * Inherits the full @c BooleanCircuit structure and adds a root gate and
66 * algorithms that exploit the d-DNNF structural properties for efficient
67 * computation.
68 */
69class dDNNF : public BooleanCircuit {
70private:
71/** @brief Memoisation cache mapping gates to their probability values. */
72mutable std::unordered_map<gate_t, double, hash_gate_t> probability_cache;
73
74/**
75 * @brief Compute the δ table used in the Shapley algorithm.
76 *
77 * Returns a map from each input gate to a vector of δ values (one per
78 * possible "defection count"), as described in the algorithm of
79 * Choicenet / Livshits et al.
80 * @return Map from each input gate to its vector of δ values.
81 */
82std::unordered_map<gate_t, std::vector<double> > shapley_delta() const;
83
84/**
85 * @brief Compute the α table used in the Shapley algorithm.
86 *
87 * Returns a 2-D vector of α coefficients that weight the δ values when
88 * computing the Shapley value of each input gate.
89 * @return 2-D vector of α coefficients (indexed by defection count).
90 */
91std::vector<std::vector<double> > shapley_alpha() const;
92
93/**
94 * @brief Compute the unnormalised Banzhaf value for the whole circuit.
95 *
96 * Used internally by @c banzhaf() to compute the Banzhaf power index
97 * for a specific variable after conditioning.
98 *
99 * @return Unnormalised Banzhaf value.
100 */
101double banzhaf_internal() const;
102
103/**
104 * @brief Compute a topological ordering of the circuit.
105 *
106 * @param reversedWires Adjacency list of the reversed wires.
107 * @return Gates in topological order (leaves first).
108 */
109std::vector<gate_t> topological_order(const std::vector<std::vector<gate_t> > &reversedWires) const;
110
111gate_t root{0}; ///< The root gate of the d-DNNF
112
113public:
114/**
115 * @brief Return the root gate of this d-DNNF.
116 * @return Gate identifier of the root.
117 */
119 return root;
120}
121/**
122 * @brief Set the root gate.
123 * @param g New root gate.
124 */
126 root=g;
127}
128
129/**
130 * @brief Return the set of all variable (IN) gates reachable from @p root.
131 * @param root Root gate of the sub-circuit.
132 * @return Set of reachable IN gate identifiers.
133 */
134std::unordered_set<gate_t> vars(gate_t root) const;
135
136/**
137 * @brief Make the d-DNNF smooth.
138 *
139 * A d-DNNF is smooth if every OR gate's children mention exactly the
140 * same set of variables. Smoothing is required before probability
141 * evaluation to ensure correctness.
142 */
143void makeSmooth();
144
145/**
146 * @brief Rewrite all n-ary AND/OR gates into binary trees.
147 *
148 * Some evaluation algorithms assume binary gates. This method replaces
149 * every AND (or OR, depending on @p type) gate with more than two
150 * children by a balanced binary tree of the same type.
151 *
152 * @param type The gate type (@c BooleanGate::AND or @c BooleanGate::OR)
153 * to binarise.
154 */
156
157/**
158 * @brief Simplify the d-DNNF by removing redundant constants.
159 *
160 * Propagates constant @c true / @c false values upward and removes
161 * gates that have become trivially constant after conditioning.
162 */
163void simplify();
164
165/**
166 * @brief Condition on variable @p var having value @p value and simplify.
167 *
168 * Sets input gate @p var to @p value and propagates the simplification
169 * through the circuit, returning a new (simplified) @c dDNNF.
170 *
171 * @param var Input gate to condition on.
172 * @param value Truth value to assign to @p var.
173 * @return A simplified copy of the conditioned circuit.
174 */
175dDNNF conditionAndSimplify(gate_t var, bool value) const;
176
177/**
178 * @brief Condition on variable @p var having value @p value (no simplification).
179 *
180 * @param var Input gate to condition on.
181 * @param value Truth value to assign to @p var.
182 * @return A copy of the circuit with @p var fixed to @p value.
183 */
184dDNNF condition(gate_t var, bool value) const;
185
186/**
187 * @brief Compute the exact probability of the d-DNNF being @c true.
188 *
189 * Requires the circuit to be smooth. Uses the structural properties of
190 * the d-DNNF to evaluate in time linear in the circuit size.
191 *
192 * @return Probability in [0, 1].
193 */
194double probabilityEvaluation() const;
195
196/**
197 * @brief Compute the Shapley value of input gate @p var.
198 *
199 * Implements the polynomial-time Shapley-value algorithm for d-DNNFs
200 * described in Livshits et al. (PODS 2021).
201 *
202 * @param var Input gate whose Shapley value is requested.
203 * @return Shapley value.
204 */
205double shapley(gate_t var) const;
206
207/**
208 * @brief Compute the Banzhaf power index of input gate @p var.
209 *
210 * Uses repeated conditioning and probability evaluation.
211 *
212 * @param var Input gate whose Banzhaf index is requested.
213 * @return Banzhaf power index.
214 */
215double banzhaf(gate_t var) const;
216
217friend dDNNFTreeDecompositionBuilder; ///< Allowed to construct and populate this d-DNNF
218friend dDNNF BooleanCircuit::compilation(gate_t g, std::string compiler) const; ///< Allowed to access internal d-DNNF state
219};
220
221#endif /* DDNNF_H */
Boolean provenance circuit with support for knowledge compilation.
BooleanGate
Gate types for a Boolean provenance circuit.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:48
Boolean circuit for provenance formula evaluation.
dDNNF compilation(gate_t g, std::string compiler) const
Compile the sub-circuit rooted at g to a dDNNF via an external tool.
Builds a d-DNNF from a Boolean circuit using a tree decomposition.
A d-DNNF circuit supporting exact probabilistic and game-theoretic evaluation.
Definition dDNNF.h:69
dDNNF conditionAndSimplify(gate_t var, bool value) const
Condition on variable var having value value and simplify.
gate_t root
The root gate of the d-DNNF.
Definition dDNNF.h:111
dDNNF condition(gate_t var, bool value) const
Condition on variable var having value value (no simplification).
Definition dDNNF.cpp:553
double banzhaf_internal() const
Compute the unnormalised Banzhaf value for the whole circuit.
Definition dDNNF.cpp:217
double probabilityEvaluation() const
Compute the exact probability of the d-DNNF being true.
Definition dDNNF.cpp:137
void setRoot(gate_t g)
Set the root gate.
Definition dDNNF.h:125
void simplify()
Simplify the d-DNNF by removing redundant constants.
Definition dDNNF.cpp:593
void makeSmooth()
Make the d-DNNF smooth.
Definition dDNNF.cpp:57
void makeGatesBinary(BooleanGate type)
Rewrite all n-ary AND/OR gates into binary trees.
Definition dDNNF.cpp:104
std::vector< std::vector< double > > shapley_alpha() const
Compute the α table used in the Shapley algorithm.
Definition dDNNF.cpp:411
double shapley(gate_t var) const
Compute the Shapley value of input gate var.
Definition dDNNF.cpp:515
double banzhaf(gate_t var) const
Compute the Banzhaf power index of input gate var.
Definition dDNNF.cpp:543
std::vector< gate_t > topological_order(const std::vector< std::vector< gate_t > > &reversedWires) const
Compute a topological ordering of the circuit.
Definition dDNNF.cpp:570
friend dDNNFTreeDecompositionBuilder
Allowed to construct and populate this d-DNNF.
Definition dDNNF.h:217
std::unordered_set< gate_t > vars(gate_t root) const
Return the set of all variable (IN) gates reachable from root.
Definition dDNNF.cpp:29
std::unordered_map< gate_t, double, hash_gate_t > probability_cache
Memoisation cache mapping gates to their probability values.
Definition dDNNF.h:72
gate_t getRoot() const
Return the root gate of this d-DNNF.
Definition dDNNF.h:118
std::unordered_map< gate_t, std::vector< double > > shapley_delta() const
Compute the δ table used in the Shapley algorithm.
Definition dDNNF.cpp:309
std::hash functor for gate_t.
Definition dDNNF.h:49
size_t operator()(gate_t g) const
Compute the hash of a gate identifier.
Definition dDNNF.h:55