解决wmi的基本工具和接口。

pywmi的Python项目详细描述


pywmiBuild Status

安装

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所述。 要使用此引擎,您需要JavaGurobi和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

  1. 下载/克隆wmipa library
  2. 将包含库的目录添加到PYTHONPATH

本机xsdd引擎

本机xsdd引擎(以及用于表示分段函数的分段xsdd类)使用 PySDD库。pysdd软件包的安装方法如下:

pip install git+https://github.com/wannesm/PySDD.git#egg=PySDD

外部XSDD发动机

pywmi还支持使用xsdd推断的wmi。要使用需要安装的xsdd引擎 HAL-ProbLog遵循自述文件中提供的说明。

摘要

  1. 安装dmd compiler v2.078.3
  2. git clone https://github.com/ariovistus/pyd.git
  3. cd pyd
  4. python setup.py install
  5. cd ../
  6. git clone --recursive https://github.com/ML-KULeuven/psipy.git
  7. cd psypi
  8. python psipy/build_psi.py
  9. python setup.py install
  10. 将PSI库添加到您的路径(在上一步中打印的命令)
  11. cd ../
  12. git clone https://bitbucket.org/pedrozudo/hal_problog.git
  13. cd hal_problog
  14. python setup.py install

请注意,您的代码不会与克隆库的代码在同一个目录中运行,因为它们会污染 你的名字空间。

拿铁

latte集成后端和谓词抽象求解器需要 Latte待安装。你可以在他们的 GitHub releases page。你可能想要捆绑:拿铁积分。

摘要

  1. wget "https://github.com/latte-int/latte/releases/download/version_1_7_5/latte-integrale-1.7.5.tar.gz"
  2. tar -xvzf latte-integrale-1.7.5.tar.gz
  3. cd latte-integrale-1.7.5
  4. ./configure
  5. 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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java OnResizeListener或OnDrawListener或类似的东西   java Orika映射嵌套子列表   保存时java Heroku请求超时代码H12   数据库在Java中出现socket读取超时异常的原因是什么?   java如何更改来自Sqlite数据库的特定数据在Listview中的行颜色   java JAXB解组器无法正确处理XML中的列表   java Android日期时区让我抓狂   java不透明属性在Swing中如何工作?   eclipse从JavaEE代码生成流程图   java如何在Hibernate中从相关表中获取计数   java Glassfish部署了项目的依赖项库   java使内容适合JavaFx中的WebView   java不满意的链接错误libcrypto。所以1.0.0   循环中java数组的使用   java找出哪个包调用服务