简体   繁体   English

用于受限随机化的 Gecode 与 Z3

[英]Gecode vs. Z3 for Constrained Randomization

I'm looking for a C++-based alternative to the SystemVerilog language.我正在寻找 SystemVerilog 语言的基于 C++ 的替代方案。 While I doubt anything out there can match the simplicity and flexibility of the SystemVerilog constraint language, I have settled on using either Z3 or Gecode for what I'm working on, primarily because they're both under the MIT license.虽然我怀疑任何东西都可以与 SystemVerilog 约束语言的简单性和灵活性相媲美,但我已经决定使用 Z3 或 Gecode 来完成我的工作,主要是因为它们都在 MIT 许可下。

What I'm looking for is:我正在寻找的是:

  1. Support for variable-sized bit vectors AND bit vector arithmetic logic operations.支持可变大小的位向量和位向量算术逻辑运算。 For example:例如:
 bit_vector a<30>; bit_vector b<30>; constraint { a == (b << 2); a == (b * 2); b < a; }

The problem with Gecode, as far as I can tell, is that it does not provide bit vectors right out of the box.据我所知,Gecode 的问题在于它没有提供开箱即用的位向量。 However, its programming model seems a bit simpler, and it does provide a means for one to create their own types of variables.然而,它的编程模型似乎更简单一些,它确实为人们提供了一种创建自己类型变量的方法。 So I could perhaps creates some kind of wrapper around the C++ bitset, similar to how IntVar wraps around 32-bit integers.所以我也许可以围绕 C++ 位集创建某种包装,类似于IntVar包装 32 位整数的方式。 However, that would lack the ability to perform multiplication-based constrains, since C++ bitsets don't support such operations.但是,这将缺乏执行基于乘法的约束的能力,因为 C++ 位集不支持此类操作。

Z3 does provide bit vectors right out of the box, but I'm not sure how it would handle trying to set constraints on, for example, 128-bit vectors. Z3 确实提供了开箱即用的位向量,但我不确定它会如何处理对例如 128 位向量设置约束的尝试。 I'm also unsure how I can specify that I want to produce a variety of randomized variables that satisfy a constraint when possible.我也不确定如何指定我想要生成各种满足约束条件的随机变量。 With Gecode, it's much clearer given how thorough its documentation is.使用 Gecode,鉴于其文档的详尽程度,它会更加清晰。

  1. A simplistic constraint programming model, close or similar to SystemVerilog.一个简单的约束编程模型,接近或类似于 SystemVerilog。 For example, a language where I only need to type (x == y + z) instead of something like EQ(x, y + z).例如,我只需要输入 (x == y + z) 而不是 EQ(x, y + z) 之类的语言。 As far as I can tell, both APIs provide such a simple programming model.据我所知,这两个 API 都提供了如此简单的编程模型。

  2. A means of performing constrained randomization, for the sake of producing random stimulus.为了产生随机刺激而执行约束随机化的一种方法。 As in, I can provide some random seed that, depending on the constraints, result in an answer that may differ from the previous answer.如在,我可以提供一些随机种子,根据约束,产生可能与之前的答案不同的答案。 Similar to how SystemVerilog randomize calls may produce new random results.类似于 SystemVerilog 随机化调用可能会产生新的随机结果。 Gecode seems to support the use of random seeds. Gecode 似乎支持使用随机种子。 Z3, it's much less clear. Z3,就不太清楚了。

  3. Support for weighted distribution.支持加权分布。 Gecode appears to support this via weighted sets. Gecode 似乎通过加权集支持这一点。 I imagine I can establish a relationship between certain conditions and boolean variables, and then add weights to those variables.我想我可以在某些条件和布尔变量之间建立关系,然后为这些变量添加权重。 Z3 appears to be more flexible in that you can assign weights to expressions, via the optimize class. Z3 似乎更灵活,因为您可以通过优化类为表达式分配权重。

At the moment, I'm undecided, because Z3 lacks in documentation what Gecode lacks in out-of-the-box variable types.目前,我还没有决定,因为 Z3 缺少 Gecode 在开箱即用的变量类型中缺少的文档。 I'm wondering if anyone has any prior experience using either tool to achieve what SystemVerilog could.我想知道是否有人有使用这两种工具来实现 SystemVerilog 功能的任何先前经验。 I'd like to hear any suggestions for any other API under a flexible license as well.我也想听听对灵活许可下的任何其他 API 的任何建议。

While z3 (or any SMT solver) can handle all of these, getting a nice sampling of satisfying assignments would be rather difficult to control.虽然 z3(或任何 SMT 求解器)可以处理所有这些问题,但获得令人满意的分配的良好采样将相当难以控制。 SMT solvers are optimized for just giving you a model, and they don't have much in terms of how you want to sample the solution space. SMT 求解器经过优化,只是为您提供了一个模型,而在您希望如何对解空间进行采样方面,它们并没有太多内容。

Incidentally, this is an active research area in SMT solving.顺便说一下,这是 SMT 求解中一个活跃的研究领域。 Here's a paper that appeared only 6 weeks ago on this very topic: https://ieeexplore.ieee.org/document/8894251这是一篇仅在 6 周前就该主题发表的论文: https : //ieeexplore.ieee.org/document/8894251

So, I'd say if support for "good sampling" is your primary motivation, using an SMT solver is probably not the best choice.因此,我想说,如果支持“良好采样”是您的主要动机,那么使用 SMT 求解器可能不是最佳选择。 If your goal is to find satisfying assumptions for bit-vectors expressed conveniently (there are high level APIs in any language you can imagine these days), then z3 would be an extremely fine choice.如果您的目标是为方便表达的位向量找到令人满意的假设(如今,您可以想象任何语言的高级 API),那么 z3 将是一个非常好的选择。

From your description, good sampling sounds like the primary motivation though, and for that SMT solvers are probably not that great.根据您的描述,良好的采样听起来像是主要动机,因此 SMT 求解器可能不是那么好。 At least not for the time being.至少暂时不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM