27#include "catalog/pg_type.h"
28#include "utils/uuid.h"
29#include "executor/spi.h"
42#include <unordered_map>
61 (
pg_uuid_t token,
pg_uuid_t variable,
const std::string &method,
const std::string &args,
bool banzhaf)
71 provsql_error(
"shapley/banzhaf: conditional Shapley / Banzhaf values are "
72 "not supported -- a conditioned token (X | C) cannot be "
73 "passed to shapley() / banzhaf(). Compute the index on the "
74 "unconditioned token, or use probability_evaluate for the "
75 "conditional probability P(X|C)");
77 std::unordered_map<gate_t, gate_t> gc_to_bc;
81 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
90 dDNNF dd = (method.empty() || method ==
"default" || method ==
"auto")
92 : c.
makeDD(root, method, args);
115 if(PG_ARGISNULL(0) || PG_ARGISNULL(1))
118 Datum token = PG_GETARG_DATUM(0);
119 Datum variable = PG_GETARG_DATUM(1);
122 if(!PG_ARGISNULL(2)) {
123 text *t = PG_GETARG_TEXT_P(2);
124 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
128 if(!PG_ARGISNULL(3)) {
129 text *t = PG_GETARG_TEXT_P(3);
130 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
133 bool banzhaf =
false;
134 if(!PG_ARGISNULL(4)) {
135 banzhaf = PG_GETARG_BOOL(4);
138 PG_RETURN_FLOAT8(
shapley_internal(*DatumGetUUIDP(token), *DatumGetUUIDP(variable), method, args, banzhaf));
139 }
catch(
const std::exception &e) {
151 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
153 MemoryContext per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
154 MemoryContext oldcontext = MemoryContextSwitchTo(per_query_ctx);
156 TupleDesc tupdesc = rsinfo->expectedDesc;
157 Tuplestorestate *tupstore = tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
159 rsinfo->returnMode = SFRM_Materialize;
160 rsinfo->setResult = tupstore;
162 if(!PG_ARGISNULL(0)) {
163 pg_uuid_t token = *DatumGetUUIDP(PG_GETARG_DATUM(0));
166 if(!PG_ARGISNULL(1)) {
167 text *t = PG_GETARG_TEXT_P(1);
168 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
172 if(!PG_ARGISNULL(2)) {
173 text *t = PG_GETARG_TEXT_P(2);
174 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
177 bool banzhaf =
false;
178 if(!PG_ARGISNULL(3)) {
179 banzhaf = PG_GETARG_BOOL(3);
185 provsql_error(
"shapley/banzhaf: conditional Shapley / Banzhaf values are "
186 "not supported -- a conditioned token (X | C) cannot be "
187 "passed to shapley() / banzhaf(). Compute the index on the "
188 "unconditioned token, or use probability_evaluate for the "
189 "conditional probability P(X|C)");
191 std::unordered_map<gate_t, gate_t> gc_to_bc;
195 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
197 dDNNF dd = (method.empty() || method ==
"default" || method ==
"auto")
199 : c.
makeDD(root, method, args);
204 for(
auto &v_circuit_gate: c.
getInputs()) {
205 auto var_uuid_string = c.
getUUID(v_circuit_gate);
206 auto var_gate=dd.
getGate(var_uuid_string);
218 UUIDPGetDatum(uuidp), Float8GetDatum(result)
220 bool nulls[
sizeof(values)] = {0, 0};
222 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
226 MemoryContextSwitchTo(oldcontext);
Boolean provenance circuit with support for knowledge compilation.
@ AND
Logical conjunction of child gates.
@ IN
Input (variable) gate representing a base tuple.
BooleanCircuit getBooleanCircuit(GenericCircuit &gc, pg_uuid_t token, gate_t &gate, std::unordered_map< gate_t, gate_t > &gc_to_bc)
Build a BooleanCircuit from an already-loaded GenericCircuit.
GenericCircuit getGenericCircuit(pg_uuid_t token)
Build a GenericCircuit from the mmap store rooted at token.
Build in-memory circuits from the mmap-backed persistent store.
gate_t
Strongly-typed gate identifier.
Out-of-line template method implementations for Circuit<gateType>.
Semiring-agnostic in-memory provenance circuit.
Catalog of probability-evaluation methods (Strategy + registry).
Fix macro conflicts between PostgreSQL headers and the C++ STL/Boost.
Boolean circuit for provenance formula evaluation.
const std::set< gate_t > & getInputs() const
Return the set of input (IN) gate IDs.
dDNNF makeDD(gate_t g, const std::string &method, const std::string &args) const
Dispatch to the appropriate d-DNNF construction method.
bool hasMultivaluedGates() const
Return true if the circuit contains any MULIN gates.
gateType getGateType(gate_t g) const
Return the type of gate g.
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
In-memory provenance circuit with semiring-generic evaluation.
A d-DNNF circuit supporting exact probabilistic and game-theoretic evaluation.
void makeSmooth()
Make the d-DNNF smooth.
void makeGatesBinary(BooleanGate type)
Rewrite all n-ary AND/OR gates into binary trees.
double shapley(gate_t var) const
Compute the Shapley value of input gate var.
double banzhaf(gate_t var) const
Compute the Banzhaf power index of input gate var.
Constructs a d-DNNF from a Boolean circuit and its tree decomposition.
dDNNF makeDDAuto(BooleanCircuit &c, gate_t g)
Cost-select a d-DNNF construction route for gate g of Boolean circuit c and build it – the default ma...
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Shared-memory segment and inter-process pipe management.
Core types, constants, and utilities shared across ProvSQL.
@ gate_conditioned
Conditioning marker with two children [target, evidence]: measure-only, probability_evaluate returns ...
#define UUID_LEN
Number of bytes in a UUID.
pg_uuid_t string2uuid(const string &source)
Parse a UUID string into a pg_uuid_t.
string uuid2string(pg_uuid_t uuid)
Format a pg_uuid_t as a std::string.
C++ utility functions for UUID manipulation.
static double shapley_internal(pg_uuid_t token, pg_uuid_t variable, const std::string &method, const std::string &args, bool banzhaf)
Core implementation for Shapley and Banzhaf index computation.
Datum shapley(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for shapley() and banzhaf().
Datum shapley_all_vars(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for shapley_all_vars() set-returning function.