我正试图在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文件中定义它,则需要显式语法和适当的导入。