2013-03-13 46 views
2

我是一個與Z3合作的新人。集z3中的最大值

想知道我該如何計算一個集合和兩個不同集合的最大值。

例如: [1, 6, 5] - 越大6 [1, 6, 5]Ë[10, 7, 2] - 越大6

我用下面的代碼來設置:

(declare-sort Set 0) 

(declare-fun contains (Set Int) bool) 

(declare-const set Set) 
(declare-const distinct_set Set) 

(declare-const A Int) 
(declare-const B Int) 
(declare-const C Int) 

(assert (= A 0)) 
(assert (= B 1)) 
(assert (= C 2)) 

(assert (distinct A C)) 
(assert (distinct set distinct_set)) 

(assert 
(forall ((x Int)) 
     (= (contains set x) (or (= x A) (= x C))))) 

,現在想知道如何能我計算了set(set)中的最大值和set(set和distinct_set)中的最大值。

如果是爲所有整數只是因爲它很容易做到:

(define-fun max ((x Int) (y Int)) Int 
    (ite (< x y) y x)) 

但我不能用自己的整數,即帶套離開,讓那些設置的值。

你能幫我嗎?

謝謝

+0

集合是有限的嗎?如果是的話,當你編碼問題時,你知道每個集合的大小嗎? – 2013-03-13 23:30:09

+0

是的套​​是finites。是的大小可以介於4和9之間 – Robert 2013-03-13 23:45:59

回答

4

以下編碼是否適合您的目的?它也可以在線獲得here

; We Enconde each set S of integers as a function S : Int -> Bool 
(declare-fun S1 (Int) Bool) 
; To assert that A and C are elements of S1, we just assert (S1 A) and (S1 C) 
(declare-const A Int) 
(declare-const C Int) 
(assert (S1 A)) 
(assert (S1 C)) 
; To say that B is not an element of S1, we just assert (not (S1 B)) 
(declare-const B Int) 
(assert (not (S1 B))) 

; Now, let max_S1 be the max value in S1 
(declare-const max_S1 Int) 
; Then, we now that max_S1 is an element of S1, that is 
(assert (S1 max_S1)) 
; All elements in S1 are smaller than or equal to max_S1 
(assert (forall ((x Int)) (=> (S1 x) (not (>= x (+ max_S1 1)))))) 

; Now, let us define a set S2 and S3 
(declare-fun S2 (Int) Bool) 
(declare-fun S3 (Int) Bool) 
; To assert that S3 is equal to the union of S1 and S2, we just assert 
(assert (forall ((x Int)) (= (S3 x) (or (S1 x) (S2 x))))) 

; To assert that S3 is not equal to S1 we assert 
(assert (exists ((x Int)) (not (= (S3 x) (S1 x))))) 

(check-sat) 

; Now let max_S3 be the maximal value of S3 
(declare-const max_S3 Int) 
(assert (S3 max_S3)) 
(assert (forall ((x Int)) (=> (S3 x) (not (>= x (+ max_S3 1)))))) 

; the set of constraints is still satisfiable 
(check-sat) 

; Now, let us assert that max_S3 < max_S1. 
; It should be unsat, since S3 is a super set of S1 
(assert (< max_S3 max_S1)) 
(check-sat) 
+0

謝謝,這是即使打算:)非常感謝。對不起,不知道像stackoverflow – Robert 2013-03-14 16:58:50

+0

沒有問題。順便說一句,如果答案解決了您的問題中提出的問題/問題,您應該接受它。通過將其標記爲已接受,您將通知其他用戶該問題已經解決。 – 2013-03-14 17:54:59