Sage:如何进行 CNFEncode?

Sage: How to CNFEncode?

我正在尝试使用 sagemath 中的 CNFEncode 模块。在遇到一些奇怪的错误后,我尝试简单地复制并执行 Sage Cloud 中随文档提供的代码片段。但是我遇到了一些错误

sage: B.<a,b,c> = BooleanPolynomialRing
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-ce036e32ef87> in <module>()
----> 1 B = BooleanPolynomialRing; (a, b, c,) = B._first_ngens(3)

AttributeError: 'function' object has no attribute '_first_ngens'

我设法通过了:

B = BooleanPolynomialRing(3,["a","b","c"])

但是在几行之后我又卡住了,这里:

sage: B = BooleanPolynomialRing(3,["a","b","c"])
sage: from sage.sat.converters.polybori import CNFEncoder
sage: from sage.sat.solvers.dimacs import DIMACS
sage: fn = tmp_filename()
sage: solver = DIMACS(filename=fn)
sage: e = CNFEncoder(solver, B)
sage: e.clauses_sparse(a*b + a + 1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-0dac365c6fab> in <module>()
----> 1 e.clauses_sparse(a*b + a + Integer(1))
NameError: name 'a' is not defined

我试图更改最后一行,但我只是遇到了另一个错误:

sage: e.clauses_sparse("a*b + a + 1")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-081f32dddbcf> in <module>()
----> 1 e.clauses_sparse("a*b + a + 1")

/projects/sage/sage-6.7/local/lib/python2.7/site-packages/sage/sat/converters/polybori.py in clauses_sparse(self, f)
    284         # any zero block of f+1
    285
--> 286         blocks = self.zero_blocks(f+1)
    287         C = [dict([(variable, 1-value) for (variable, value) in b.iteritems()]) for b in blocks ]
    288

TypeError: cannot concatenate 'str' and 'int' objects

我对此非常困惑,在文档中找不到任何帮助(除了相同的代码,我无法开始工作)

你忘了括号。而不是

sage: B.<a,b,c> = BooleanPolynomialRing

使用压缩语法编写,

sage: B.<a,b,c> = BooleanPolynomialRing()

或使用更明确的语法

sage: B = BooleanPolynomialRing(3,['a','b','c'])
sage: a, b, c = B.gens()

其中第一行设置显示名称 多项式变量的 a, b, c, 第二行创建 Python 个变量 这些多项式变量的值。

如果您想在 .py 文件中定义它, 您需要明确的语法和适当的导入。