Question: - > 0 . 6 The Resolution Principle To prove that a clause is valid using the resolution method, we attempt to show that the

->0.6 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 2 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 z^(^())noty. Then each literal is added to the set of valid clauses (see 4. and 5.). The
resulting set of clauses is:
notpvvq
notzvvy
p
z
noty
Resolution on 2. and 5. gives:
notpvvq
notzvvy
p
z
noty
notz
Finally, we apply the resolution rule on 4. and 6. which produces False. Thus, the original clause
notzvvy is valid.
0.7 The Program
0.7.1 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-1 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{3,1}
y{4,2}
z{5,2}
Contradiction {5,7}
Valid
My current code:
import sys
def parse_kb_file(file_path):
"""Parse the input .kb file to extract the initial KB and clause to test."""
with open(file_path, 'r') as file:
lines = file.readlines()
kb =[set(line.strip().split()) for line in lines[:-1]]
test_clause = set(lines[-1].strip().split())
return kb, test_clause
def negate_clause(clause):
"""Negate the clause to be tested and return its literals as a list."""
return [f'~{literal}' if not literal.startswith('~') else literal[1:] for literal in clause]
def resolve(clause1, clause2):
"""Attempt to resolve two clauses and return the resulting clause or None if not resolvable."""
resolved_clause = set()
found_complementary = False
for literal in clause1:
complement = f'~{literal}' if not literal.startswith('~') else literal[1:]
if complement in clause2:
if found_complementary:
return None # More than one complementary pair, not resolvable
found_complementary = True
else:
resolved_clause.add(literal)
for literal in clause2:
if f'~{literal}' not in clause1:
resolved_clause.add(literal)
return resolved_clause if found_complementary else None
def is_redundant(new_clause, kb):
"""Check if the new_clause is logically equivalent to any clause in kb."""
new_clause_set = set(new_clause)
return any(new_clause_set == existing for existing in kb)
def resolution(kb, test_clause):
"""Perform the resolution algorithm and print the output."""
clause_counter = len(kb)+1
negated_literals = negate_clause(test_clause)
# Add each literal of the negated clause to the KB
for literal in negated_literals:
kb.append({literal})
print(f"{clause_counter}.{{{literal}}}{{}}")
clause_counter +=1
# Main resolution loop
i =0
while i len(kb):
clause1= kb[i]
for j in range(i):
clause2= kb[j]
resolved_clause = resolve(clause1, clause2)
if resolved_clause is not None:
if not resolved_clause: # If resolved to empty, we found a contradiction
print(f"{clause_counter}. Contradiction {{{i +1},{j +1}}}")
print("Valid")
return
if not is_redundant(resolved_clause, kb) and resolved_clause: # Avoid adding redundant or empty clauses
kb.append(resolved_clause)
print(f"{clause_counter}.{''.join(sorted(resolved_clause))}{{{i +1},{j +1}}}")
clause_counter +=1
i +=1
print("Fail")
def main():
if len(sys.argv)!=2:
print("Usage: python main.py ")
return
kb_file = sys.argv[1]
kb, test_clause = parse_kb_file(kb_file)
resolution(kb, test_clause)
if __name__=="__main__":
main()
- > 0 . 6 The Resolution Principle To prove that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!