简体   繁体   English

使用 SMTLIB2 查找 z3 中的最大数字

[英]finding max of the numbers in z3 using SMTLIB2

I have 7 cups which contains some water.我有 7 个杯子,里面有一些水。 I need to program these cups to have different amounts of water.我需要对这些杯子进行编程以获得不同量的水。 Once this is done I need to measure the cup which has the highest amount of water and then remove some quantity (say 2 units of water).完成此操作后,我需要测量水量最多的杯子,然后取出一些量(比如 2 个单位的水)。

c implementations: 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; 

This will give an answer as c7 = 3.8这将给出c7 = 3.8的答案

I was trying to implement this in z3 and I assigned the values to c1....c7我试图在 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

and when I get the model values it should give c7_1 as 3.8当我得到 model 值时,它应该将 c7_1 设为 3.8

Is it possible to define this in z3?是否可以在 z3 中定义它? When I am using the and's of different conditions in if condition (in ite) its giving me an error.当我在 if 条件(在 ite 中)中使用不同条件的 and 时,它给了我一个错误。 Can it not be defined like this?不能这样定义吗? Is there someway around this?有什么办法吗?

Thanks in advance提前致谢

[problem description][1] [问题描述][1]

I am experimenting with the Z3 tool, It was easy to get he first part However for the second part is being a bit difficult.我正在尝试使用 Z3 工具,很容易得到他的第一部分但是对于第二部分来说有点困难。

Sure.当然。 Here it is in SMTLib:它在 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 says: 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))

So, looks like it put 2 units in c1 , less than 2 on all the others, so you ended up with a final value of 0 left.因此,看起来它在c1中放置了2个单位,而在其他所有单位中都小于2 ,因此最终的最终值为0

Your question is rather vague in what other constraints might be at play here, but hopefully this will get you started.您的问题在此处可能存在哪些其他限制方面相当含糊,但希望这可以帮助您入门。

Here's an alternative approach for finding max that doesn't use ITE .这是查找不使用ITEmax的另一种方法。

; 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))

(declare-fun max () Real)

(assert (and (<= c1 max) (<= c2 max) (<= c3 max) (<= c4 max) (<= c5 max) (<= c6 max) (<= c7 max)))

(assert (or (<= max c1) (<= max c2) (<= max c3) (<= max c4) (<= max c5) (<= max c6) (<= max c7) ))

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

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

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

And the result:结果:

sat
((c1 2.0)
 (c2 (/ 4.0 3.0))
 (c3 0.0)
 (c4 (/ 1.0 3.0))
 (c5 1.0)
 (c6 (/ 2.0 3.0))
 (c7 (/ 5.0 3.0))
 (max 2.0)
 (finalRes 0.0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM