Question: - > 0 . 6 The Resolution Principle To prove that a clause is valid using the resolution method, we attempt to show that the
The Resolution Principle
To prove that a clause is valid using the resolution method, we attempt to show that the negation of
the clause is unsatisfiable, meaning it cannot be true under any truth assignment. This is done using
the following algorithm:
Negate the clause and add each literal in the resulting conjunction of literals to the set of clauses
already known to be valid.
Find two clauses for which the resolution rule can be applied. Change the form of the produced
clause to the standard form and add it to the set of valid clauses.
Repeat until False is produced, or until no new clauses can be produced. If no new clauses can
be produced, report failure; the original clause is not valid. If False is produced, report success;
the original clause is valid.
Consider again our example. Assume we now want to prove that notzvvy is valid. First, we negate
the clause and get znoty Then each literal is added to the set of valid clauses see and The
resulting set of clauses is:
notpvvq
notzvvy
p
z
noty
Resolution on and gives:
notpvvq
notzvvy
p
z
noty
notz
Finally, we apply the resolution rule on and which produces False. Thus, the original clause
notzvvy is valid.
The Program
Files and Task Description
Your program should take exactly one argument from the command line:
A kb file that contains the initial KB and the clause whose validity we want to test. The input
file contains n lines organized as follows: the first n lines describe the initial KB while line
n contains the original clause to test. Note that the KB is written in CNF so each clause
represents a disjunction of literals. The literals of each clause are separated by a blank space,
while negated variables are prefixed by
Your program should adhere to the following policy: p
zy
Your program's output should be:
pq
zy
p
z
y
q
y
z
Contradiction
Valid
My current code:
import sys
def parsekbfilefilepath:
Parse the input kb file to extract the initial KB and clause to test."""
with openfilepath, r as file:
lines file.readlines
kb setlinestripsplit for line in lines:
testclause setlinesstripsplit
return kb testclause
def negateclauseclause:
Negate the clause to be tested and return its literals as a list."""
return f~literal if not literal.startswith~ else literal: for literal in clause
def resolveclause clause:
Attempt to resolve two clauses and return the resulting clause or None if not resolvable."""
resolvedclause set
foundcomplementary False
for literal in clause:
complement f~literal if not literal.startswith~ else literal:
if complement in clause:
if foundcomplementary:
return None # More than one complementary pair, not resolvable
foundcomplementary True
else:
resolvedclause.addliteral
for literal in clause:
if f~literal not in clause:
resolvedclause.addliteral
return resolvedclause if foundcomplementary else None
def isredundantnewclause, kb:
Check if the newclause is logically equivalent to any clause in kb
newclauseset setnewclause
return anynewclauseset existing for existing in kb
def resolutionkb testclause:
Perform the resolution algorithm and print the output."""
clausecounter lenkb
negatedliterals negateclausetestclause
# Add each literal of the negated clause to the KB
for literal in negatedliterals:
kbappendliteral
printfclausecounterliteral
clausecounter
# Main resolution loop
i
while i lenkb:
clause kbi
for j in rangei:
clause kbj
resolvedclause resolveclause clause
if resolvedclause is not None:
if not resolvedclause: # If resolved to empty, we found a contradiction
printfclausecounter Contradiction i j
printValid
return
if not isredundantresolvedclause, kb and resolvedclause: # Avoid adding redundant or empty clauses
kbappendresolvedclause
printfclausecounterjoinsortedresolvedclausei j
clausecounter
i
printFail
def main:
if lensysargv:
printUsage: python main.py
return
kbfile sysargv
kb testclause parsekbfilekbfile
resolutionkb testclause
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
