首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用SMTLIB2求z3中的最大数

用SMTLIB2求z3中的最大数
EN

Stack Overflow用户
提问于 2019-10-17 07:07:26
回答 2查看 299关注 0票数 2

我有7个杯子,里面装了一些水。我需要对这些杯子进行编程,使其具有不同的水量。一旦这样做,我需要测量的杯子,其中有最多的水,然后删除一些数量(例如2个单位的水)。

C实现:

代码语言:javascript
复制
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

代码语言:javascript
复制
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中)中使用不同条件的and时,它会给我一个错误。它不能这样定义吗?有什么办法可以解决这个问题吗?

提前感谢

问题描述

我正在用Z3工具做实验,第一部分很容易,但是第二部分有点难。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-17 11:43:28

好的。下面是SMTLib中的代码:

代码语言:javascript
复制
; 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说:

代码语言:javascript
复制
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 left的最终值。

你的问题在这里可能还有哪些其他限制因素方面相当模糊,但希望这能让你开始。

票数 3
EN

Stack Overflow用户

发布于 2019-10-17 19:36:39

这里有一种查找不使用ITEmax的替代方法。

代码语言:javascript
复制
; 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))

结果是:

代码语言:javascript
复制
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))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58422778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档