我有7个杯子,里面装有一些水。我需要对这些杯子进行编程,以容纳不同量的水。完成此操作后,我需要测量杯中水量最高的杯子,然后取出一些杯子(例如2个单位)。

c实现:

float c1=2.0, c2= 2.6, c3 = 2.8, c4=4.4 , c5 = 2.4, c6 = 2.1, c7 = 5.8;

if((c1 > c2) && (c1 > c3) && (c1 > c4) && (c1 > c5) && (c1 > c6) && (c1 > c7)); c1=c1-2;

if((c2 > c1) && (c2 > c3) && (c2 > c4) && (c2 > c5) && (c2 > c6) && (c2 > c7)); c2=c2-2;

if((c3 > c2) && (c3 > c1) && (c3 > c4) && (c3 > c5) && (c3 > c6) && (c3 > c7)); c3=c3-2;

if((c4 > c2) && (c4 > c3) && (c4 > c1) && (c4 > c5) && (c4 > c6) && (c4 > c7)); c4=c4-2;

if((c5 > c2) && (c5 > c3) && (c5 > c4) && (c5 > c1) && (c5 > c6) && (c5 > c7)); c5=c5-2;

if((c6 > c2) && (c6 > c3) && (c6 > c4) && (c6 > c5) && (c6 > c1) && (c6 > c7)); c6=c6-2;

if((c7 > c2) && (c7 > c3) && (c7 > c4) && (c7 > c5) && (c7 > c6) && (c7 > c1)); c7=c7-2;


这将以c7 = 3.8给出答案

我试图在z3中实现这一点,并将值分配给c1 .... c7

ite( (and((> c1  c2) (> c1  c3) (> c1  c4) (> c1  c5) (> c1  c6) (> c1  c7)))  (= c1_1 (- c1 2)  (= c1_1 c1))
.
.
.repeated till c7_1


当我得到模型值时,应将c7_1设为3.8

是否可以在z3中定义它?当我在if条件(ite)中使用不同条件的ands时,给我一个错误。不能这样定义吗?周围有什么办法吗?

提前致谢

[问题描述] [1]

我正在使用Z3工具进行试验,很容易就能获得他的第一部分,但是对于第二部分却有些困难。

最佳答案

当然。在SMTLib中:

; declare the cups
(declare-const c1 Real)
(declare-const c2 Real)
(declare-const c3 Real)
(declare-const c4 Real)
(declare-const c5 Real)
(declare-const c6 Real)
(declare-const c7 Real)

; each cup has a non-negative units of water
(assert (>= c1 0))
(assert (>= c2 0))
(assert (>= c3 0))
(assert (>= c4 0))
(assert (>= c5 0))
(assert (>= c6 0))
(assert (>= c7 0))

; each amount is different
(assert (distinct c1 c2 c3 c4 c5 c6 c7))

; find maximum, helper function
(define-fun max ((a Real) (b Real)) Real (ite (> a b) a b))

; find the cup with maximum water in it
(define-fun maxC () Real (max c1 (max c2 (max c3 (max c4 (max c5 (max c6 c7)))))))

; make sure there's at least 2 units in the max, per the problem
(assert (>= maxC 2))

; final value
(define-fun finalRes () Real (- maxC 2))

; solve
(check-sat)
(get-value (c1 c2 c3 c4 c5 c6 c7 maxC finalRes))


z3说:

sat
((c1 2.0)
 (c2 (/ 11.0 6.0))
 (c3 (/ 19.0 12.0))
 (c4 (/ 7.0 4.0))
 (c5 (/ 3.0 2.0))
 (c6 (/ 23.0 12.0))
 (c7 (/ 5.0 3.0))
 (maxC 2.0)
 (finalRes 0.0))


因此,看起来将2单位放入c1,小于所有其他单位上的2,所以最终得到的最终值是0

您的问题在这里还有什么其他的限制方面比较模糊,但是希望这可以帮助您入门。

关于python - 使用SMTLIB2查找z3中的最大数量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58422778/

10-11 22:56
查看更多