解决wmi的基本工具和接口。
pywmi的Python项目详细描述
pywmi
安装
pip install pywmi
pywmi提供了各种需要额外安装步骤的服务和引擎。
SMT解算器
pywmi依赖pysmt与smt解算器接口。如果您想从依赖于smt解算器的功能中获益 请通过pywmi安装过程中附带的pysmt安装工具安装smt解算器。
pysmt-install --msat # example to install mathsat, more solvers are available
对于较旧版本的pysmt(早于版本0.8),必须确保在使用pywmi时,smt解算器位于您的路径上。 pysmt安装工具可以向您显示必要的命令。
pysmt-install --env
xadd发动机
xadd引擎使用xadds执行wmi,如Kolb et al., 2018所述。 要使用此引擎,您需要Java、Gurobi和xadd库jar文件。 pywmi安装附带的pywmi安装工具可以自动下载并安装jar文件, 但是,您需要手动安装java和gurobi。一旦你这样做了,只要打电话:
pywmi-install xadd
pywmi-install xadd --force # To download a new version
谓词抽象引擎
谓词抽象引擎(short pa engine)使用mathsat和latte使用谓词抽象来解决wmi,如
在Morettin et al., 2017中描述。
为了使用PA引擎,您需要安装Mathsat SMT解算器(请参阅上面的说明)。
拿铁咖啡(见下面的说明)和wmipa library。你可以使用
pysmt-install
用于下载库的实用程序。
pywmi-install pa
pywmi-install pa --force # To download a new version
手动安装
您还可以手动下载或克隆库并将其添加到您的PYTHONPATH
- 下载/克隆wmipa library
- 将包含库的目录添加到
PYTHONPATH
本机xsdd引擎
本机xsdd引擎(以及用于表示分段函数的分段xsdd类)使用 PySDD库。pysdd软件包的安装方法如下:
pip install git+https://github.com/wannesm/PySDD.git#egg=PySDD
外部XSDD发动机
pywmi还支持使用xsdd推断的wmi。要使用需要安装的xsdd引擎 HAL-ProbLog遵循自述文件中提供的说明。
摘要
- 安装dmd compiler v2.078.3
git clone https://github.com/ariovistus/pyd.git
cd pyd
python setup.py install
cd ../
git clone --recursive https://github.com/ML-KULeuven/psipy.git
cd psypi
python psipy/build_psi.py
python setup.py install
- 将PSI库添加到您的路径(在上一步中打印的命令)
cd ../
git clone https://bitbucket.org/pedrozudo/hal_problog.git
cd hal_problog
python setup.py install
请注意,您的代码不会与克隆库的代码在同一个目录中运行,因为它们会污染 你的名字空间。
拿铁
latte集成后端和谓词抽象求解器需要 Latte待安装。你可以在他们的 GitHub releases page。你可能想要捆绑:拿铁积分。
摘要
wget "https://github.com/latte-int/latte/releases/download/version_1_7_5/latte-integrale-1.7.5.tar.gz"
tar -xvzf latte-integrale-1.7.5.tar.gz
cd latte-integrale-1.7.5
./configure
make
用法
调用pywmi
设置密度和查询
import pysmt.shortcuts as smt
# Create a "domain" with boolean variables a and b and real variables x, y (both between 0 and 1)
domain = Domain.make(["a", "b"], ["x", "y"], [(0, 1), (0, 1)])
a, b = domain.get_bool_symbols() # Get PySMT symbols for the boolean variables
x, y = domain.get_real_symbols() # Get PySMT variables for the continuous variables
# Create support
support = (a | b) & (~a | ~b) & (x <= y) & domain.get_bounds()
# Create weight function (PySMT requires constants to be wrapped, e.g., smt.Real(0.2))
weight_function = smt.Ite(a, smt.Real(0.2), smt.Real(0.8)) * (smt.Ite(x <= 0.5, smt.Real(0.2), 0.2 + y) + smt.Real(0.1))
# Create query
query = x <= y / 2
使用引擎执行推断
# Create rejection-sampling based engine (no setup required)
rejection_engine = RejectionEngine(domain, support, weight_function, sample_count=100000)
print("Volume (Rejection): ", rejection_engine.compute_volume()) # Compute the weighted model integral
print("Query probability (Rejection):", rejection_engine.compute_probability(query)) # Compute query probability
使用xadd引擎(确保已安装必备组件)
# Create XADD engine (mode resolve refers to the integration algorithm described in
# Kolb et al. Efficient symbolic integration for probabilistic inference. IJCAI 2018)
# !! Requires XADD solver to be setup (see above) !!
xadd_engine = XaddEngine(domain, support, weight_function, mode="resolve")
print("Volume (XADD): ", xadd_engine.compute_volume()) # Compute the weighted model integral
print("Query probability (XADD): ", xadd_engine.compute_probability(query)) # Compute query probability
生成统一样本及其标签
from pywmi.sample import uniform
# n: Required number of samples
# domain, support: Domain and support defined as above
samples = uniform(domain, n)
labels = evaluate(samples, support, samples)
生成加权正样本
from pywmi.sample import positive
# n: Required number of samples
# domain, support, weight: Defining the density as above
# Optional:
# sample_pool_size: The number of uniformly sampled positive samples to weight and select the samples from
# sample_count: The number of samples to draw initially, from which to build the positive pool
# max_samples: The maximum number of uniformly sampled samples (positive or negative) to generate before failing
# => If max_samples is exceeded a SamplingError will be raised
samples, positive_ratio = positive(n, domain, support, weight)
处理密度并写入文件
# Wrap support and weight function (and optionally queries) in a density object
density = Density(domain, support, weight_function, [query])
# Density object can be saved to and loaded from files
filename = "my_density.json"
density.to_file(filename) # Save to file
density = Density.from_file(filename) # Load from file
从命令行工作
# Compare engines from command line
python -m pywmi my_density.json compare rej:n100000 xadd:mresolve # Compute the weighted model integral
python -m pywmi my_density.json compare rej:n100000 xadd:mresolve -q 0 # Compute query probability (query at index 0)
# Compute volumes and probabilities from command line
# You can provide multiple engines and the result of the first engine not to fail will be returned
python -m pywmi my_density.json volume rej:n100000 # Compute weighted model integral
python -m pywmi my_density.json prob rej:n100000 # Compute all query probabilities
# Plot 2-D support
python -m pywmi my_density.json plot -o my_density.png
在pywmi/tests/running_example.py