我一直在搜索z3提供的平方根功能。例如,为了添加关于实数x的约束,即x * x = 2,编码它的最佳方法是什么?
我试过了:
(declare-const x Real)
(assert (= 2 (* x x)))
(check-sat)
结果未知。该模型也不可用。
但是,我确信应该有办法满足这一要求。我严格指的是smt-lib 2.0语言的扩展版本而不是python api。
答案 0 :(得分:4)
您需要使用非线性求解器。你的例子没有自动检测和使用它的原因是因为常量2
:这被解析成一个整数而不是一个整数,所以它在技术上是整数和实数的组合理论,而非线性求解器可能只是除非你强迫它,否则涉及实物。要强制使用它,您可以使用check-sat-using
和非线性求解器qfnra-nlsat
(rise4fun link:http://rise4fun.com/Z3/fXDp):
(declare-const x Real)
(push)
(assert (= 2 (* x x)))
(check-sat-using qfnra-nlsat) ; sat
(get-model)
; may have to play with printing options as this is irrational (or this may be a bug)
; (model
; (define-fun x () Real
; (/ 5.0 4.0))
;)
(pop)
; the reason you need to use check-sat-using is because the 2 gets parsed into an integer; to force it to be a real, use a decimal:
(push)
(assert (= 2.0 (* x x)))
(check-sat) ; sat
(get-model)
;(model
; (define-fun x () Real
; (root-obj (+ (^ x 2) (- 2)) 1))
;)
(pop)
对于一般指数(平方根等),您可以使用^进行取幂:
; you can also use ^ for exponentiation
(push)
(assert (= 2.0 (^ x 2.0)))
(check-sat) ; sat
(get-model)
; (model
; (define-fun x () Real
; (root-obj (+ (^ x 2) (- 2)) 1))
;)
(pop)
; to represent square root, etc., you may use fractional or decimal exponents
(push)
(assert (= 25.0 (^ x 0.5))) ; square root: 2 = sqrt(x)
(check-sat) ; sat
(get-model)
; maybe a bug or have to tune the model printer
;(model
; (define-fun x () Real
; (- 1.0))
;)
(pop)
(push)
(assert (= 2.0 (^ x (/ 1.0 3.0))))
(check-sat) ; sat
(get-model)
;(model
; (define-fun x () Real
; 8.0)
;)
(pop)
(push)
(assert (= 10.0 (^ x 0.2)))
(check-sat) ; sat
(get-model)
;(model
; (define-fun x () Real
; 100000.0)
;)
(pop)
这是一篇陈旧但相关的帖子:z3/python reals
某些打印问题,例如,如果您需要十进制近似值,可以使用以下方法修复:
(set-option :pp.decimal true)