AI Exp-8,9,10
AI Exp-8,9,10
08
1. Forward Chaining Implementation (Python)
# Forward Chaining Algorithm in Python
# Knowledge Base: Facts and Rules
knowledge_base = {
'facts': ['Human(Socrates)'], # Known fact
'rules': ['Human(x) -> Mortal(x)'] # General rule}
# Function to check if a fact is true
def is_fact_true(fact, kb):
return fact in kb['facts']
# Function to apply forward chaining
def forward_chaining(goal, kb):
# Iterate over rules to see if they can be applied
for rule in kb['rules']:
if '->' in rule:
# Extract the condition and conclusion
condition, conclusion = rule.split(' -> ')
# Replace variable with specific instance in the knowledge base
condition = condition.replace('x', 'Socrates')
conclusion = conclusion.replace('x', 'Socrates')
OUTPUT:
Goal 'Mortal(Socrates)' is proven by forward chaining.
2.Backward Chaining in Python
OUTPUT:
Goal 'Mortal(Socrates)' is proven by backward chaining.
3.Resolution in Prolog
Prolog code:
% Facts
human(socrates).
% Rules
mortal(X) :- human(X).
OUTPUT:
true.
EXPERIMENT NO:09
Construct the Bayesian Network in a Tool
Using Python with pgmpy
Here is how you can model and infer using Python and the pgmpy library:
Program:
OUTPUT:
+------------+------------+
| Mortal | phi(M) |
+------------+------------+
| Mortal(0) | 0.0100 |
| Mortal(1) | 0.9900 |
+------------+------------+
EXPERIMENT NO:10
class PlanningAgent:
def __init__(self):
self.actions = {
'Move(A, B)': {'preconditions': ['At(Room A)'], 'effects': ['At(Room B)']},
'Move(B, A)': {'preconditions': ['At(Room B)'], 'effects': ['At(Room A)']}
}
def is_goal_state(self, state, goal_state):
return state == goal_state
while queue:
current_state, plan = queue.popleft()
OUTPUT:
Plan: ['Move(A, B)']