CUDD 积和布尔表达式
CUDD sum of products Boolean expression
我想为以下布尔函数创建 BDD:
F = (A'B'C'D') OR (A'B C) OR (C' D') OR (A)
我使用以下代码仅成功创建了 F = (A'B'C'D')
,但如何将其他产品条款添加到现有 BDD?
int main (int argc, char *argv[])
{
char filename[30];
DdManager *gbm; /* Global BDD manager. */
gbm = Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); /* Initialize a new BDD manager. */
DdNode *bdd, *var, *tmp_neg, *tmp;
int i;
bdd = Cudd_ReadOne(gbm); /*Returns the logic one constant of the manager*/
Cudd_Ref(bdd); /*Increases the reference count of a node*/
for (i = 3; i >= 0; i--) {
var = Cudd_bddIthVar(gbm,i); /*Create a new BDD variable*/
tmp_neg = Cudd_Not(var); /*Perform NOT boolean operation*/
tmp = Cudd_bddAnd(gbm, tmp_neg, bdd); /*Perform AND boolean operation*/
Cudd_Ref(tmp);
Cudd_RecursiveDeref(gbm,bdd);
bdd = tmp;
}
bdd = Cudd_BddToAdd(gbm, bdd);
print_dd (gbm, bdd, 2,4);
sprintf(filename, "./bdd/graph.dot");
write_dd(gbm, bdd, filename);
Cudd_Quit(gbm);
return 0;
}
独立构建每个连词,以便您得到 conj0
到 conj3
确保只取反正确的文字。我不是特别精通 C,现在没有开发环境设置,因此您需要进行一些更正。
我将使用以下映射
A <=> BDD(0)
B <=> BDD(1)
C <=> BDD(2)
D <=> BDD(3)
按照您现在在 for 循环中的方式构建 conj0
。之后确保conj0 = bdd
。
对于将编码 (A' B C)
的 conj1
使用
bdd = Cudd_IthVar(gbm, 0);
bdd = Cudd_Not(bdd);
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 1));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 2));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
conj1 = bdd;
对 conj2
和 conj3
执行相同的操作。
获得所有连词后,使用 Cudd_bddOr()
构建顶级析取。
还要确保 Cudd_Ref()
和 Cudd_Deref()
正确,否则会泄漏内存。
如果您只对那个特定功能感兴趣,可以通过以下方式构建和检查它:
#include <stdio.h>
#include <stdlib.h>
#include "cudd.h"
int main(void) {
/* Get set. */
DdManager * mgr = Cudd_Init(4,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
DdNode *a = Cudd_bddIthVar(mgr, 0);
DdNode *c = Cudd_bddIthVar(mgr, 1);
DdNode *b = Cudd_bddIthVar(mgr, 2);
DdNode *d = Cudd_bddIthVar(mgr, 3);
char const * const inames[] = {"a", "c", "b", "d"};
/* Build BDD. */
DdNode * tmp = Cudd_bddIte(mgr, c, b, Cudd_Not(d));
Cudd_Ref(tmp);
DdNode * f = Cudd_bddOr(mgr, a, tmp);
Cudd_Ref(f);
Cudd_RecursiveDeref(mgr, tmp);
/* Inspect it. */
printf("f");
Cudd_PrintSummary(mgr, f, 4, 0);
Cudd_bddPrintCover(mgr, f, f);
char * fform = Cudd_FactoredFormString(mgr, f, inames);
printf("%s\n", fform);
/* Break up camp and go home. */
free(fform);
Cudd_RecursiveDeref(mgr, f);
int err = Cudd_CheckZeroRef(mgr);
Cudd_Quit(mgr);
return err;
}
注意(最佳)变量顺序的选择。你应该看到这个输出:
f: 5 nodes 1 leaves 12 minterms
1--- 1
-11- 1
-0-0 1
a | (c & b | !c & !d)
我想为以下布尔函数创建 BDD:
F = (A'B'C'D') OR (A'B C) OR (C' D') OR (A)
我使用以下代码仅成功创建了 F = (A'B'C'D')
,但如何将其他产品条款添加到现有 BDD?
int main (int argc, char *argv[])
{
char filename[30];
DdManager *gbm; /* Global BDD manager. */
gbm = Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); /* Initialize a new BDD manager. */
DdNode *bdd, *var, *tmp_neg, *tmp;
int i;
bdd = Cudd_ReadOne(gbm); /*Returns the logic one constant of the manager*/
Cudd_Ref(bdd); /*Increases the reference count of a node*/
for (i = 3; i >= 0; i--) {
var = Cudd_bddIthVar(gbm,i); /*Create a new BDD variable*/
tmp_neg = Cudd_Not(var); /*Perform NOT boolean operation*/
tmp = Cudd_bddAnd(gbm, tmp_neg, bdd); /*Perform AND boolean operation*/
Cudd_Ref(tmp);
Cudd_RecursiveDeref(gbm,bdd);
bdd = tmp;
}
bdd = Cudd_BddToAdd(gbm, bdd);
print_dd (gbm, bdd, 2,4);
sprintf(filename, "./bdd/graph.dot");
write_dd(gbm, bdd, filename);
Cudd_Quit(gbm);
return 0;
}
独立构建每个连词,以便您得到 conj0
到 conj3
确保只取反正确的文字。我不是特别精通 C,现在没有开发环境设置,因此您需要进行一些更正。
我将使用以下映射
A <=> BDD(0)
B <=> BDD(1)
C <=> BDD(2)
D <=> BDD(3)
按照您现在在 for 循环中的方式构建 conj0
。之后确保conj0 = bdd
。
对于将编码 (A' B C)
的 conj1
使用
bdd = Cudd_IthVar(gbm, 0);
bdd = Cudd_Not(bdd);
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 1));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 2));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
conj1 = bdd;
对 conj2
和 conj3
执行相同的操作。
获得所有连词后,使用 Cudd_bddOr()
构建顶级析取。
还要确保 Cudd_Ref()
和 Cudd_Deref()
正确,否则会泄漏内存。
如果您只对那个特定功能感兴趣,可以通过以下方式构建和检查它:
#include <stdio.h>
#include <stdlib.h>
#include "cudd.h"
int main(void) {
/* Get set. */
DdManager * mgr = Cudd_Init(4,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
DdNode *a = Cudd_bddIthVar(mgr, 0);
DdNode *c = Cudd_bddIthVar(mgr, 1);
DdNode *b = Cudd_bddIthVar(mgr, 2);
DdNode *d = Cudd_bddIthVar(mgr, 3);
char const * const inames[] = {"a", "c", "b", "d"};
/* Build BDD. */
DdNode * tmp = Cudd_bddIte(mgr, c, b, Cudd_Not(d));
Cudd_Ref(tmp);
DdNode * f = Cudd_bddOr(mgr, a, tmp);
Cudd_Ref(f);
Cudd_RecursiveDeref(mgr, tmp);
/* Inspect it. */
printf("f");
Cudd_PrintSummary(mgr, f, 4, 0);
Cudd_bddPrintCover(mgr, f, f);
char * fform = Cudd_FactoredFormString(mgr, f, inames);
printf("%s\n", fform);
/* Break up camp and go home. */
free(fform);
Cudd_RecursiveDeref(mgr, f);
int err = Cudd_CheckZeroRef(mgr);
Cudd_Quit(mgr);
return err;
}
注意(最佳)变量顺序的选择。你应该看到这个输出:
f: 5 nodes 1 leaves 12 minterms
1--- 1
-11- 1
-0-0 1
a | (c & b | !c & !d)