Salta al contenuto
Note per Studenti Esercizio - Tabelle di verità, equivalenze e model checking (slide del corso)

Esercizio - Tabelle di verità, equivalenze e model checking (slide del corso)

In questa pagina 3

Testo (esercizi ricavati dalle slide "Logical agents" del corso di Intelligenza Artificiale, Ing. dell'Informazione UniPD; teoria in Agenti logici e logica proposizionale - inferenza e risoluzioneAgenti basati sulla conoscenza (KB, Tell e Ask), mondo del Wumpus come banco di prova, sintassi e semantica della logica proposizionale, conseguenza logica, equivalenze, inferenza per model checking (7 simboli, 128 modelli, 3 modelli della KB) e per risoluzione (CNF, regola di risoluzione, dimostrazione per assurdo). Limite: poca espressività, da cui la logica del primo ordine.Agenti logici e logica proposizionale - inferenza e risoluzione →).

  1. Verificare con le tavole di verità le leggi di De Morgan, ¬(P∧Q)≡¬P∨¬Q\neg(P \land Q) \equiv \neg P \lor \neg Q e ¬(P∨Q)≡¬P∧¬Q\neg(P \lor Q) \equiv \neg P \land \neg Q, e dire se P⇒QP \Rightarrow Q equivale a Q⇒PQ \Rightarrow P.
  2. Stabilire se sono valide ((P⇒Q)∧(Q⇒R))⇒(P⇒R)((P \Rightarrow Q) \land (Q \Rightarrow R)) \Rightarrow (P \Rightarrow R) e (Q∧(P⇒Q))⇒P(Q \land (P \Rightarrow Q)) \Rightarrow P.
  3. Mondo del Wumpus: la KB è R1∧R2∧R3∧R4∧R5R_1 \land R_2 \land R_3 \land R_4 \land R_5 con R1:¬P1,1R_1: \neg P_{1,1}, R2:B1,1⇔(P1,2∨P2,1)R_2: B_{1,1} \Leftrightarrow (P_{1,2} \lor P_{2,1}), R3:B2,1⇔(P1,1∨P2,2∨P3,1)R_3: B_{2,1} \Leftrightarrow (P_{1,1} \lor P_{2,2} \lor P_{3,1}), R4:¬B1,1R_4: \neg B_{1,1}, R5:B2,1R_5: B_{2,1}. Per model checking, stabilire se KB⊨¬P1,2KB \models \neg P_{1,2} e se KB⊨¬P2,2KB \models \neg P_{2,2}.

Punto 1: De Morgan e implicazione

Le due variabili danno 22=42^2 = 4 righe. Si calcola ogni colonna dalla definizione dei connettivi.

PP QQ P∧QP \land Q ¬(P∧Q)\neg(P \land Q) ¬P∨¬Q\neg P \lor \neg Q P∨QP \lor Q ¬(P∨Q)\neg(P \lor Q) ¬P∧¬Q\neg P \land \neg Q P⇒QP \Rightarrow Q Q⇒PQ \Rightarrow P
V V V F F V F F V V
V F F V V V F F F V
F V F V V V F F V F
F F F V V F V V V V
  • Le colonne ¬(P∧Q)\neg(P \land Q) e ¬P∨¬Q\neg P \lor \neg Q sono uguali riga per riga: equivalenti. Lo stesso per ¬(P∨Q)\neg(P \lor Q) e ¬P∧¬Q\neg P \land \neg Q.
  • P⇒QP \Rightarrow Q e Q⇒PQ \Rightarrow P differiscono nelle righe 22 e 33: non equivalenti. L'implicazione non è simmetrica.

Punto 2: validità

Sillogismo ipotetico ((P⇒Q)∧(Q⇒R))⇒(P⇒R)((P \Rightarrow Q) \land (Q \Rightarrow R)) \Rightarrow (P \Rightarrow R). Una formula A⇒BA \Rightarrow B è falsa solo se AA è vera e BB falsa. Qui P⇒RP \Rightarrow R falsa richiede PP vero e RR falso. Con PP vero, P⇒QP \Rightarrow Q vera richiede QQ vero; ma con QQ vero e RR falso Q⇒RQ \Rightarrow R è falsa, quindi l'antecedente non è vero. Non esiste riga falsa: la formula è valida (controllate tutte le 88 righe).

Affermazione del conseguente (Q∧(P⇒Q))⇒P(Q \land (P \Rightarrow Q)) \Rightarrow P. Con PP falso e QQ vero: QQ vero, P⇒QP \Rightarrow Q vero (antecedente falso), quindi l'antecedente è vero e il conseguente PP è falso. La formula è falsa in quel modello: non valida (è il ragionamento sbagliato "piove implica pavimento bagnato, il pavimento è bagnato, dunque piove").

Punto 3: model checking sulla KB del Wumpus

I simboli sono 77 (B1,1,B2,1,P1,1,P1,2,P2,1,P2,2,P3,1B_{1,1}, B_{2,1}, P_{1,1}, P_{1,2}, P_{2,1}, P_{2,2}, P_{3,1}): 27=1282^7 = 128 modelli. Si scrive la KB come funzione e si enumerano.

python
from itertools import product
syms = ["B11", "B21", "P11", "P12", "P21", "P22", "P31"]

def kb(m):
    return (not m["P11"]                                        # R1
            and m["B11"] == (m["P12"] or m["P21"])              # R2
            and m["B21"] == (m["P11"] or m["P22"] or m["P31"])  # R3
            and not m["B11"] and m["B21"])                      # R4, R5

modelli = [dict(zip(syms, v)) for v in product([False, True], repeat=7)]
buoni = [m for m in modelli if kb(m)]
print(len(modelli), len(buoni))                       # 128 3
print(all(not m["P12"] for m in buoni))               # True
print(all(not m["P22"] for m in buoni))               # False

Output atteso: 128 3, True, False. I 33 modelli della KB si trovano anche a mano (vedi la nota): B1,1B_{1,1}, P1,1P_{1,1}, P1,2P_{1,2}, P2,1P_{2,1} falsi, B2,1B_{2,1} vero, e (P2,2,P3,1)∈{(0,1),(1,0),(1,1)}(P_{2,2}, P_{3,1}) \in \{(0,1), (1,0), (1,1)\}.

  • P1,2P_{1,2} è falso nei 33 modelli: KB⊨¬P1,2KB \models \neg P_{1,2}, sì.
  • P2,2P_{2,2} è vero nel secondo e nel terzo: KB⊨¬P2,2KB \models \neg P_{2,2}, no.

L'agente può quindi andare in [1,2][1,2] senza rischio, ma non può escludere un pozzo in [2,2][2,2] né in [3,1][3,1].

Versione ripasso

  • De Morgan verificate su 44 righe: ¬(P∧Q)\neg(P \land Q) ha colonna F,V,V,V come ¬P∨¬Q\neg P \lor \neg Q; ¬(P∨Q)\neg(P \lor Q) ha F,F,F,V come ¬P∧¬Q\neg P \land \neg Q. P⇒QP \Rightarrow Q (V,F,V,V) non equivale a Q⇒PQ \Rightarrow P (V,V,F,V).
  • Validità: il sillogismo ((P⇒Q)∧(Q⇒R))⇒(P⇒R)((P \Rightarrow Q) \land (Q \Rightarrow R)) \Rightarrow (P \Rightarrow R) è valido (per essere falso servirebbe PP vero, RR falso, ma allora uno tra P⇒QP \Rightarrow Q e Q⇒RQ \Rightarrow R è falso). L'affermazione del conseguente (Q∧(P⇒Q))⇒P(Q \land (P \Rightarrow Q)) \Rightarrow P no: controesempio PP falso, QQ vero.
  • Model checking del Wumpus: 77 simboli, 128128 modelli, KB vera in 33 (con B2,1B_{2,1} vero e (P2,2,P3,1)∈{(0,1),(1,0),(1,1)}(P_{2,2}, P_{3,1}) \in \{(0,1),(1,0),(1,1)\}, il resto falso). KB⊨¬P1,2KB \models \neg P_{1,2} sì; KB⊨¬P2,2KB \models \neg P_{2,2} no. Codice: product([False,True], repeat=7), filtro kb(m), controllo all(not m["P12"] ...).
  • Errore tipico: concludere dal fatto che P⇒QP \Rightarrow Q sia vera che anche Q⇒PQ \Rightarrow P lo sia.

Teoria collegata