0% found this document useful (0 votes)
5 views

Ai 1

Resolution strategies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Ai 1

Resolution strategies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

AIM:

To implement a resolu on algorithm that can determine if a given goal (proposi on) can be logically

derived from a set of clauses using proposi onal logic.

ALGORITHM:

1. The code Starts by crea ng a `Resolu on` class and define the method `resolve`.

2. The func on takes in a list of clauses as an argument, which represents the known facts and the
negated goal.

3. Negate the goal and add it to the list of clauses to create a contradic on scenario.

4. Ini alize an empty list called `new_clauses` to store any new clauses generated during resolu on.

5. Iterate through all pairs of clauses in the list:

- For each pair of clauses ( C[i] ) and ( C[j] ):

- Resolve the two clauses to generate a new clause, called `resolvent`.

- Check if the resolvent is empty:

- If it is empty, return `True` (indica ng the goal is proved).

- Check if the resolvent is not already in the original list:

- If it is not, add the `resolvent` to `new_clauses`.

6. Repeat the process un l no new clauses can be generated:

- If `new_clauses` is empty, return `False` (indica ng the goal cannot be proved).

- Add all clauses in `new_clauses` back to the original list of clauses.

7. A er exhaus ng all op ons, if an empty clause was derived at any point, return `True` (the goal is
proved).
8. If no proof is found, return `False` (the goal cannot be proved).

PROGRAM:

from typing import List, Set

# Helper func on to check if two literals are complements

def is_complement(literal1: str, literal2: str) -> bool:

return literal1 == f"{literal2}" or literal2 == f"{literal1}"

# Resolve two clauses and return the resul ng clause

def resolve(clause1: Set[str], clause2: Set[str]) -> Set[str]:

resolvent = set()

for literal in clause1:

for other_literal in clause2:

if is_complement(literal, other_literal):

# Resolve: take all literals except the one being resolved

new_clause = (clause1 - {literal}) | (clause2 - {other_literal})

if len(new_clause) == 0:

return new_clause # return empty clause if resolved to empty

resolvent.update(new_clause)

return resolvent

# Check if the set of clauses contains an empty clause

def contains_empty_clause(clauses: List[Set[str]]) -> bool:

return any(len(clause) == 0 for clause in clauses)


# Resolu on algorithm implementa on

def resolu on(clauses: List[Set[str]], goal: Set[str]) -> bool:

# Negate the goal and add to clauses

negated_goal = {f"{literal}" if literal[0] != '' else literal[1:] for literal in goal}

clauses.append(negated_goal)

# Set of new clauses to be added

new_clauses = []

while True:

n = len(clauses)

new_clauses.clear() # Clear new clauses for the current itera on

# Try to resolve each pair of clauses

for i in range(n):

for j in range(i + 1, n):

resolvent = resolve(clauses[i], clauses[j])

# If we derive an empty clause, goal is proved

if resolvent is not None and len(resolvent) == 0:

return True

if resolvent is not None and resolvent not in clauses:

new_clauses.append(resolvent)

# If no new clauses were added, return false

if not new_clauses:

return False
# Add new clauses to exis ng set

clauses.extend(new_clauses)

# Example usage

if __name__ == "__main__":

# Define clauses (in CNF)

# Example: P ∨ Q, ~Q, ~P ∨ R

clauses = [

{"P", "~Q"},

{"Q"},

{"~P", "R"}

# Define the goal to prove

goal = {"R"}

# Run resolu on algorithm

result = resolu on(clauses, goal)

# Output result

if result:

print("The goal is proved.")

else:

print("The goal cannot be proved.")

OUTPUT:

The goal is proved.


RESULT:

Thus the resolu on algorithm was implemented and executed successfully.

You might also like