Building a Simple Forward-Chaining Expert System
To design and implement a simple rule-based expert system that demonstrates forward-chaining inference mechanism for problem diagnosis.
A rule-based system (or production system) is a type of artificial intelligence system that uses a set of if-then rules to represent knowledge and make decisions. It's one of the earliest forms of AI and is still widely used today.
A reasoning method that starts with known facts and applies rules to derive new facts until a goal is reached. It's data-driven and works from facts to conclusions.
| Step | Question Asked | Condition | Rule / Diagnosis |
|---|---|---|---|
| 1 | Is the switch ON? | no |
Switch is off |
| 2 | Is the bulb OK? | no and switch is yes |
Bulb is fused |
| 3 | Is there power? | no and above are yes |
No electricity |
| 4 | All answers are yes |
Problem with light fixture or wiring |
print("š” Simple Light Diagnosis Expert System š”")
print("Answer the questions to find out why the light is not working.\n")
# Step 1: Ask user if the switch is ON
switch = input("Is the switch ON? (yes/no): ")
if switch == "no":
print("\nš Diagnosis: The switch is OFF. Turn it ON.")
print("Rule used: IF switch == 'no' THEN problem = switch is off")
else:
# Step 2: Ask if the bulb is okay
bulb = input("Is the bulb working (not fused)? (yes/no): ")
if bulb == "no":
print("\nš Diagnosis: The bulb is fused. Replace it.")
print("Rule used: IF switch == 'yes' AND bulb == 'no' THEN problem = bulb is fused")
else:
# Step 3: Ask if there's power
power = input("Is there electricity/power? (yes/no): ")
if power == "no":
print("\nš Diagnosis: No power supply. Check your electric board.")
print("Rule used: IF switch == 'yes' AND bulb == 'yes' AND power == 'no' THEN problem = no power")
else:
# Step 4: All OK but still not working
print("\nš Diagnosis: There may be a wiring or fixture issue. Call an electrician.")
print("Rule used: IF switch == 'yes' AND bulb == 'yes' AND power == 'yes' THEN problem = fixture issue")
print("\nā
Done! You just used a Rule-Based System with IF-THEN logic.")