A Python tool to generate and encode Bayesian networks into CNF formulae.
Supported formats for Bayesian networks: BIF (Bayesian Interchange Format).
Language: Python 3.8+ (64-bit)
Supported OS: Linux, macOS (Intel and Apple Silicon), Windows
Important
The encoder is not optimised for speed.
- pgmpy 0.1.25 (Linux / macOS / Windows)
Install the required package as follows:
pip install --user pgmpy==0.1.25./Generate.py -h./Generate.py output_file [ -tls positive_integer (min: 2, default: 5) ]
[ -bls positive_integer (min: 2, default: 5) ]
[ -ds positive_integer (min: 2, default: 2) ]
[ -d positive_integer (min: 1, max: 100, default: 100) ]
[ -s positive_integer | None (default: None) ]
[ -gp ]Files:
output_file - name of the output file where the generated BN will be saved (must end with .bif)
-tls - top layer size (min: 2, default: 5)
-bls - bottom layer size (min: 2, default: 5)
-ds - domain size (min: 2, default: 2)
-d - density (min: 1, max: 100, default: 100)
-s - seed (ignored for fully dense BNs): if set to None, a new seed will be randomly generated (default: None)
-gp - if set, generates nontrivial random probabilities summing to exactly 1.0 for CPT blocks; otherwise, deterministic (1.0/0.0) probabilities are used (default: False)
./Encode.py -h./Encode.py input_file output_file [ -ct {nwDNNF, dDNNF, sdDNNF} (default: nwDNNF) ]
[ -mc ] [ -e ] [ -s positive_integer | None (default: None) ]Files:
input_file - name of the input file (must end with .bif)
output_file - name of the output file where the CNF formula will be saved
-ct - circuit type {nwDNNF, dDNNF, sdDNNF} (default: nwDNNF)
nwDNNF – negative weak DNNF (nwDNNF) circuit
dDNNF – deterministic DNNF (d-DNNF) circuit
sdDNNF – smooth deterministic DNNF (sd-DNNF) circuit
-mc - if set, outputs CNF formatted for the Model Counting Competition (includes reproducibility metadata c r, unified track descriptor c t pwmc, and explicit weight declarations for every literal) (default: False)
-e - if set, randomly selects a parentless/root variable and assigns it a random state as evidence (formatted as a unit clause at the bottom of the CNF) (default: False)
-s - seed used to deterministically select the evidence variable and state. If set to None, a secure random seed is generated and printed (default: None)
Bels supports generating benchmarks strictly compliant with the official input specifications for the Model Counting Competition.
To compile a weighted CNF formula for competition tracks:
- Generate a Bayesian network with non-trivial, random probabilities using the -gp flag.
- Encode using the -mc flag to inject the required header comments and literal weight specifications.
- Provide seed-replicable evidence using -e and -s. (optional)
./Generate.py sample.bif -tls 4 -bls 5 -ds 2 -d 100 -gp./Encode.py sample.bif sample.cnf -ct sdDNNF -mc -e -s 12345The resulting sample.cnf will have the mandatory structures formatted as follows:
c 4_5_2_100_e_12345
c
c r https://github.com/Illner/Bels https://github.com/Illner/Bels
c
c Parameters:
...
p cnf 66 264
c t pwmc
c
c Indicator variable weights:
c p weight 1 1 0
c p weight -1 1 0
...
c p weight 13 0.891 0
c p weight -13 1 0
...
c evidence
4 0
Bels was used in the following paper:
@article{Illner_2025,
author = {Illner, Petr},
title = {New Compilation Languages Based on Restricted Weak Decomposability},
volume = {39},
url = {https://ojs.aaai.org/index.php/AAAI/article/view/33643},
DOI = {10.1609/aaai.v39i14.33643},
number = {14},
journal = {Proceedings of the AAAI Conference on Artificial Intelligence},
year = {2025},
month = {Apr.},
pages = {14987-14996}
}
Template: tls_ds_100__...
Example: 4_2_100__n__1
./Generate.py output_file -tls 4 -bls 4 -ds 2 -d 100Template: tls_ds_d_s__...
Example: 5_2_80_178863288008571802098406570058470324423__d4__1
./Generate.py output_file -tls 5 -bls 5 -ds 2 -d 80 -s 178863288008571802098406570058470324423Template: tls_bls_ds_100__...
Example: 5_10_2_100__c2d__1
./Generate.py output_file -tls 5 -bls 10 -ds 2 -d 100Template: tls_bls_ds_d_s__...
Example: 5_10_2_80_215044699595513658902283950224290651799__c__1
./Generate.py output_file -tls 5 -bls 10 -ds 2 -d 80 -s 215044699595513658902283950224290651799