简体   繁体   English

简化布尔表达式算法

[英]Simplify boolean expression algorithm

Anybody knows of an algorithm to simplify boolean expressions? 有人知道一个简化布尔表达式的算法吗?

I remember the boolean algebra and Karnaught maps, but this is meant for digital hardware where EVERITHING is boolean. 我记得布尔代数和Karnaught地图,但这适用于EVERITHING为布尔值的数字硬件。 I would like something that takes into account that some sub-expressions are not boolean. 我想要考虑一些子表达式不是布尔值的东西。

For example: 例如:

a == 1 && a == 3

this could be translated to a pure boolean expression: 这可以转换为纯布尔表达式:

a1 && a3 

but this is expression is irreducible, while with a little bit of knowledge of arithmetics everibody can determine that the expression is just: 但这是表达是不可简化的,而对于算术的一点点知识,everibody可以确定表达式只是:

false

Some body knows some links? 有些人知道一些链接?

You might be interested in K-maps and the Quine–McCluskey algorithm . 您可能对K-mapsQuine-McCluskey算法感兴趣。

I think SymPy is able to solve and simplify boolean expressions, looking at the source might be useful. 我认为SymPy能够解决和简化布尔表达式,查看源代码可能很有用。

There are two parts to this problem, logical simplification and representation simplification. 这个问题有两个部分,逻辑简化和表示简化。

For logical simplification, Quine-McCluskey. 为了简化逻辑,Quine-McCluskey。 For simplification of the representation, recursively extract terms using the distribution identity (0&1|0&2) == 0&(1|2). 为了简化表示,使用分布标识(0&1 | 0&2)== 0&(1 | 2)递归地提取术语。

I detailed the process here . 我在这里详述了这个过程。 That gives the explanation using only & and |, but it can be modified to include all boolean operators. 这给出了仅使用&和|的解释,但可以修改它以包括所有布尔运算符。

Your particular example would be solved by an SMT solver . 您的特定示例将由SMT解算器解决 (It'd determine that no setting of the variables could make the expression true; therefore it's always false. More-general simplification is out of scope for such solvers.) Showing that an expression is equivalent to true or false is of course NP-hard even without bringing arithmetic into the deal, so it's pretty cool that there's practical software for even this much. (它确定变量的设置不能使表达式成为真;因此它总是错误的。更一般的简化超出了此类求解器的范围。)显示表达式等效于truefalse当然是NP-即使没有将算术带入交易中也很难,所以即使这样也有很多实用的软件。 Depending on how much arithmetic knowledge is in scope, the problem may be undecidable . 根据范围内的算术知识量,问题可能是不可判定的

First shot using Google found this paper: 使用谷歌的第一枪发现了这篇论文:

http://hopper.unco.edu/KARNAUGH/Algorithm.html http://hopper.unco.edu/KARNAUGH/Algorithm.html

Of course, that does not deal with non-boolean subexpressions. 当然,这不涉及非布尔子表达式。 But this latter part in its general form is really hard, since there is definitely no algorithm to check if an arbitrary arithmetic expression is true, false or whatever. 但是后一部分的一般形式确实很难,因为肯定没有算法来检查任意算术表达式是真,假或其他。 What you are asking for goes deeply into the field of compiler optimization . 您要求的内容深入到编译器优化领域。

Is the number of possible distinct values finite and known? 可能的不同值的数量是否有限且已知? If so you could convert each expression into a boolean expression. 如果是这样,您可以将每个表达式转换为布尔表达式。 For instance if a has 3 distinct values then you could have variables a1 , a2 , and a3 where a1 being true means that a == 1 , etc. Once you did that you could rely on the Quine-McCluskey algorithm (which is probably better for larger examples than Karnaugh maps). 例如,如果a有3个不同的值,那么你可以有变量a1a2a3 ,其中a1为真意味着a == 1 ,等等。一旦你这样做,你可以依赖Quine-McCluskey算法(这可能更好比卡诺图更大的例子)。 Here is some Java code for Quine-McCluskey. 以下是Quine-McCluskey的一些Java代码

I can't speak to whether this design would actually simplify things or make them more complicated, but you might want to consider it at least. 我不能说这个设计是否会真正简化事情或使它们更复杂,但你可能至少要考虑它。

This is hard man. 这很难。 The algorithm in the simplest way that I found was match every output combination with each input each combination. 我找到的算法以最简单的方式匹配每个输出组合,每个输入组合。 But that's the basic algorithm, didn't solve every expression. 但这是基本的算法,没有解决每一个表达。

If all output (q1,q2,q3,q4) is same with for ie input A combination then the result of simplification will be A. 如果所有输出(q1,q2,q3,q4)与输入A组合相同,那么简化的结果将是A.

If not, you will try another variabel / input dependency. 如果没有,您将尝试另一个变量/输入依赖项。

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

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