Utilizzare Z3 e SMTLIB per calcolare la configurazione/modello con valori misti

StackOverflow https://stackoverflow.com/questions/8387807

  •  28-10-2019
  •  | 
  •  

Domanda

Come si calcola i valori attribuiti? Ecco un esempio:

(declare-fun x () bool)
(declare-fun y () bool)
(declare-fun z () bool)
(assert (AND x (OR y z)))

Con questo otterrei 2 modelli:

x=true and y=true
x=true and z=true

Ora, quello che voglio è qualcosa del genere:

(declare-fun x () bool)
(declare-fun y () bool)
(declare-fun z () bool)
(declare-fun x.val () Int)
(declare-fun y.val () Int)
(declare-fun z.val () Int)
(assert (= x.val 2))
(assert (= y.val 3))
(assert (= z.val 5))
(assert (AND x (OR y z)))
(assert (> sum 6))

Quindi, vorrei ottenere il modello in cui la somma degli attributi è maggiore di 6:

x=true and z=true

Forse lavorare con array è un modo per raggiungere questo obiettivo ...

È stato utile?

Soluzione

Non sono sicuro di aver compreso correttamente la tua domanda. Sembra che tu voglia associare un attributo (intero) a ciascuna variabile booleana. Quindi, ogni variabile è una coppia: un valore booleano e un attributo intero. Lo presumo da sum , intendevi la somma degli attributi delle variabili assegnate a true. In tal caso, puoi modellarlo in Z3 nel modo seguente:

;; Enable model construction
(set-option :produce-models true)

;; Declare a new type (sort) that is a pair (Bool, Int).
;; Given a variable x of type/sort WBool, we can write
;;  - (value x) for getting its Boolean value
;;  - (attr x)  for getting the integer "attribute" value
(declare-datatypes () ((WBool (mk-wbool (value Bool) (attr Int)))))

;; Now, we declare a macro int-value that returns (attr x) if
;; (value x) is true, and 0 otherwise
(define-fun int-value ((x WBool)) Int
  (ite (value x) (attr x) 0))

(declare-fun x () WBool)
(declare-fun y () WBool)
(declare-fun z () WBool)

;; Set the attribute values for x, y and z
(assert (= (attr x) 2))
(assert (= (attr y) 3))
(assert (= (attr z) 5))

;; Assert Boolean constraint on x, y and z.
(assert (and (value x) (or (value y) (value z))))

;; Assert that the sum of the attributes of the variables assigned to true is greater than 6.
(assert (> (+ (int-value x) (int-value y) (int-value z)) 6))
(check-sat)
(get-model)

(assert (not (value z)))
(check-sat)

Altri suggerimenti

Con tre variabili, immagino che sarebbe qualcosa di simile:

(define-fun cond_add ((cond Bool) (x Int) (sum Int)) Int
  (ite cond (+ sum x) sum))
(declare-fun sum () Int)
(assert (= sum (cond_add x x.val (cond_add y y.val (cond_add z z.val 0)))))
(assert (> sum 6))

Qui definisco una macro cond_add Per aggiungere una variabile a un accumulatore quando vale una condizione corrispondente. E sum è definito per tenere conto della somma condizionale di x.val, y.val e z.val Basato sui valori di verità di x, y e z.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top