检测图中是否存在循环:Z3的SMTLIB格式

时间:2018-08-10 10:20:04

标签: z3 smt

这个问题与:Cyclic relation in Datalog using SMTLib for z3

我想扭转以上链接中描述的问题。我的意思是我想检测图中不存在循环。

建议的解决方案是:

(set-option :fixedpoint.engine datalog) 
(define-sort s () Int) 

(declare-rel edge (s s)) 
(declare-rel path (s s)) 

(declare-var a s) 
(declare-var b s) 
(declare-var c s) 

(rule (=> (edge a b) (path a b)) P-1)
(rule (=> (and (path a b) (path b c)) (path a c)) P-2)

(rule (edge 1 2) E-1)
(rule (edge 2 3) E-2)
(rule (edge 3 1) E-3)

(declare-rel cycle (s))
(rule (=> (path a a) (cycle a)))
(query cycle :print-answer true)

但是我的问题是,当图中没有周期时如何获取SAT,而当存在周期时如何获得UNSAT。

一个建议(但这不能满足我的需要)是

(set-option :fixedpoint.engine datalog) 
(define-sort s () Int) 

(declare-rel edge (s s)) 
(declare-rel path (s s)) 

(declare-var a s) 
(declare-var b s) 
(declare-var c s) 

(rule (=> (edge a b) (path a b)))
(rule (=> (and (path a b) (path b c)) (path a c)))

(rule (edge 1 2) r-1)
(rule (edge 2 3) r-2)
(rule (edge 3 1) r-3)


(assert (not (path a a)))

(check-sat)
(get-model)

返回结果:

> z3 test.txt
sat
(model
  (define-fun a () Int
    0)
  (define-fun path ((x!0 Int) (x!1 Int)) Bool
    (ite (and (= x!0 0) (= x!1 0)) false
      false))
)

我不明白为什么z3将变量0赋给变量,而我只有1、2和3作为顶点?

另一个建议是:

(set-option :fixedpoint.engine datalog) 
(define-sort s () Int) 

(declare-rel edge (s s)) 
(declare-rel path (s s)) 

(declare-var a s) 
(declare-var b s) 
(declare-var c s) 

(rule (=> (edge a b) (path a b)))
(rule (=> (and (path a b) (path b c)) (path a c)))

(rule (edge 1 2) r-1)
(rule (edge 2 3) r-2)
(rule (edge 3 1) r-3)


(assert
         (=> (path a a)
            false
            )  

 )


(check-sat)
(get-model)

哪个返回结果:

> z3 test2.txt
sat
(model
  (define-fun a () Int
    0)
  (define-fun path ((x!0 Int) (x!1 Int)) Bool
    (ite (and (= x!0 0) (= x!1 0)) false
      false))
)

有解决这个问题的主意吗?

(使用量词会增加问题的复杂性,这就是为什么我在寻找一些替代解决方案的原因。)

1 个答案:

答案 0 :(得分:0)

我对z3的这一部分并不熟悉,但是总的来说,结果对我来说似乎是合理的。 Z3正在尝试为您的问题找到模型。 您从来没有说过1 2 3是所有顶点,而只是说它们是一些顶点。您可能可以通过引入顶点谓词并说其他任何东西都不是顶点来解决此问题。

在最坏的情况下,您可能需要量词