ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
TreeDecompositionKnowledgeCompiler.cpp
Go to the documentation of this file.
1/**
2 * @file TreeDecompositionKnowledgeCompiler.cpp
3 * @brief Standalone @c tdkc tool: tree-decomposition-based knowledge compiler.
4 *
5 * This file contains the @c main() entry point for the standalone
6 * @c tdkc (Tree-Decomposition Knowledge Compiler) binary. It is built
7 * separately from the PostgreSQL extension (see @c Makefile target @c tdkc).
8 *
9 * Usage:
10 * @code
11 * tdkc <circuit_file>
12 * @endcode
13 *
14 * The circuit file is a text file produced by @c BooleanCircuit::exportCircuit()
15 * listing the number of gates followed by one line per gate describing its
16 * type, probability, and children.
17 *
18 * The tool:
19 * 1. Reads the circuit from the file.
20 * 2. Rewrites multivalued gates.
21 * 3. Computes a tree decomposition.
22 * 4. Builds a d-DNNF via @c dDNNFTreeDecompositionBuilder.
23 * 5. Evaluates the probability and prints it to @c stdout.
24 *
25 * Timing information is printed to @c stderr.
26 */
27#include <iostream>
28#include <fstream>
29#include <iomanip>
30
31extern "C" {
32#include <sys/time.h>
33}
34
36#include "Circuit.hpp"
37#include "kcmcp_server.h"
38
39/**
40 * @brief Return the current time as a floating-point number of seconds.
41 * @return Current time (seconds since the epoch, with microsecond precision).
42 */
43static double get_timestamp ()
44{
45 struct timeval now;
46 gettimeofday (&now, NULL);
47 return now.tv_usec * 1e-6 + now.tv_sec;
48}
49
50/**
51 * @brief Entry point for the standalone tree-decomposition knowledge compiler.
52 * @param argc Argument count; must be 2.
53 * @param argv Argument vector; @c argv[1] is the path to the circuit file.
54 * @return 0 on success, non-zero on error.
55 */
56int main(int argc, char **argv) {
57 // KCMCP reference-server mode: speak the Knowledge Compiler / Model Counter
58 // Protocol on a Unix or TCP endpoint (see kcmcp_server.h).
59 if(argc == 3 && std::string(argv[1]) == "--kcmcp")
60 return kcmcp_serve(argv[2]);
61
62 if(argc != 2) {
63 std::cerr << "Usage: " << argv[0] << " circuit\n"
64 << " " << argv[0]
65 << " --kcmcp unix:/path|host:port (KCMCP reference server)"
66 << std::endl;
67 exit(1);
68 }
69
70 std::ifstream g(argv[1]);
72 unsigned nbGates;
73
74 g >> nbGates;
75 std::string line;
76 std::getline(g,line);
77
78 for(unsigned i=0; i<nbGates; ++i) {
79 std::getline(g, line);
80 if(line=="IN")
81 c.setGate(std::to_string(i), BooleanGate::IN, 0.001);
82 else if(line=="OR")
83 c.setGate(std::to_string(i), BooleanGate::OR);
84 else if(line=="AND")
85 c.setGate(std::to_string(i), BooleanGate::AND);
86 else if(line=="NOT")
87 c.setGate(std::to_string(i), BooleanGate::NOT);
88 else {
89 std::cerr << "Wrong line type: " << line << std::endl;
90 exit(1);
91 }
92 }
93
94 gate_t u,v;
95 while(g >> u >> v)
96 c.addWire(u,v);
97 g.close();
98
99 try {
100 double t0, t1;
101 t0 = get_timestamp();
102
103 TreeDecomposition td(c);
104 std::cerr << "Treewidth: " << td.getTreewidth() << std::endl;
105 t1 = get_timestamp();
106 std::cerr << "Computing tree decomposition took " << (t1-t0) << "s" << std::endl;
107 t0 = t1;
108
109 auto dnnf{dDNNFTreeDecompositionBuilder{c, c.getGate("0"), td}.build()};
110 t1 = get_timestamp();
111 std::cerr << "Computing dDNNF took " << (t1-t0) << "s" << std::endl;
112 t0 = t1;
113
114 std::cerr << "Probability: " << std::setprecision (15) << dnnf.probabilityEvaluation() << std::endl;
115 t1 = get_timestamp();
116 std::cerr << "Evaluating dDNNF took " << (t1-t0) << "s" << std::endl;
118 std::cerr << "Could not build tree decomposition of width <= " << TreeDecomposition::MAX_TREEWIDTH;
119 exit(1);
120 }
121
122 return 0;
123}
@ NOT
Logical negation of a single child gate.
@ OR
Logical disjunction of child gates.
@ AND
Logical conjunction of child gates.
@ IN
Input (variable) gate representing a base tuple.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:49
Out-of-line template method implementations for Circuit<gateType>.
int main(int argc, char **argv)
Entry point for the standalone tree-decomposition knowledge compiler.
static double get_timestamp()
Return the current time as a floating-point number of seconds.
Boolean circuit for provenance formula evaluation.
gate_t setGate(BooleanGate type) override
Allocate a new gate with type type and no UUID.
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
Definition Circuit.hpp:81
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
Definition Circuit.hpp:33
Exception thrown when a tree decomposition cannot be constructed.
Tree decomposition of a Boolean circuit's primal graph.
unsigned getTreewidth() const
Return the treewidth of this decomposition.
static constexpr int MAX_TREEWIDTH
Maximum supported treewidth.
Builds a d-DNNF from a Boolean circuit using a tree decomposition.
dDNNF && build() &&
Execute the compilation and return the resulting d-DNNF.
Constructs a d-DNNF from a Boolean circuit and its tree decomposition.
int kcmcp_serve(const std::string &endpoint)
Serve KCMCP on endpoint until terminated.
The tdkc KCMCP reference server.