27#include "catalog/pg_type.h"
28#include "utils/tuplestore.h"
29#include "utils/uuid.h"
30#include "executor/spi.h"
43#include <unordered_map>
62 (
pg_uuid_t token,
pg_uuid_t variable,
const std::string &method,
const std::string &args,
bool banzhaf)
72 provsql_error(
"shapley/banzhaf: conditional Shapley / Banzhaf values are "
73 "not supported -- a conditioned token (X | C) cannot be "
74 "passed to shapley() / banzhaf(). Compute the index on the "
75 "unconditioned token, or use probability_evaluate for the "
76 "conditional probability P(X|C)");
78 std::unordered_map<gate_t, gate_t> gc_to_bc;
82 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
91 dDNNF dd = (method.empty() || method ==
"default" || method ==
"auto")
93 : c.
makeDD(root, method, args);
116 if(PG_ARGISNULL(0) || PG_ARGISNULL(1))
119 Datum token = PG_GETARG_DATUM(0);
120 Datum variable = PG_GETARG_DATUM(1);
123 if(!PG_ARGISNULL(2)) {
124 text *t = PG_GETARG_TEXT_P(2);
125 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
129 if(!PG_ARGISNULL(3)) {
130 text *t = PG_GETARG_TEXT_P(3);
131 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
134 bool banzhaf =
false;
135 if(!PG_ARGISNULL(4)) {
136 banzhaf = PG_GETARG_BOOL(4);
139 PG_RETURN_FLOAT8(
shapley_internal(*DatumGetUUIDP(token), *DatumGetUUIDP(variable), method, args, banzhaf));
140 }
catch(
const std::exception &e) {
152 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
154 MemoryContext per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
155 MemoryContext oldcontext = MemoryContextSwitchTo(per_query_ctx);
157 TupleDesc tupdesc = rsinfo->expectedDesc;
158 Tuplestorestate *tupstore = tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
160 rsinfo->returnMode = SFRM_Materialize;
161 rsinfo->setResult = tupstore;
163 if(!PG_ARGISNULL(0)) {
164 pg_uuid_t token = *DatumGetUUIDP(PG_GETARG_DATUM(0));
167 if(!PG_ARGISNULL(1)) {
168 text *t = PG_GETARG_TEXT_P(1);
169 method = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
173 if(!PG_ARGISNULL(2)) {
174 text *t = PG_GETARG_TEXT_P(2);
175 args = string(VARDATA(t),VARSIZE(t)-VARHDRSZ);
178 bool banzhaf =
false;
179 if(!PG_ARGISNULL(3)) {
180 banzhaf = PG_GETARG_BOOL(3);
186 provsql_error(
"shapley/banzhaf: conditional Shapley / Banzhaf values are "
187 "not supported -- a conditioned token (X | C) cannot be "
188 "passed to shapley() / banzhaf(). Compute the index on the "
189 "unconditioned token, or use probability_evaluate for the "
190 "conditional probability P(X|C)");
192 std::unordered_map<gate_t, gate_t> gc_to_bc;
196 provsql_error(
"Computing Shapley/Banzhaf values is ill-defined for circuits with multivalued (mulinput) gates");
198 dDNNF dd = (method.empty() || method ==
"default" || method ==
"auto")
200 : c.
makeDD(root, method, args);
205 for(
auto &v_circuit_gate: c.
getInputs()) {
206 auto var_uuid_string = c.
getUUID(v_circuit_gate);
207 auto var_gate=dd.
getGate(var_uuid_string);
219 UUIDPGetDatum(uuidp), Float8GetDatum(result)
221 bool nulls[
sizeof(values)] = {0, 0};
223 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
227 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.