AI Rule-Based System

Building a Simple Forward-Chaining Expert System

šŸŽÆ Aim

To design and implement a simple rule-based expert system that demonstrates forward-chaining inference mechanism for problem diagnosis.

Specific Objectives:

  • Understand the fundamental concepts of production systems and rule-based reasoning
  • Learn to represent domain knowledge using IF-THEN rules
  • Implement forward-chaining inference for automated decision making
  • Develop a working diagnostic system for real-world problem solving
  • Gain hands-on experience with knowledge representation and inference engines

šŸ“š Theory: Rule-Based Systems

What is a Rule-Based System?

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.

Key Components:

  • Knowledge Base: Collection of rules (IF-THEN statements)
  • Working Memory: Current facts and data
  • Inference Engine: Applies rules to derive new facts
  • User Interface: Interaction with the system

Forward Chaining:

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.

Example Rules for Light Diagnosis:

  • IF light_switch_on = False THEN cause = "Switch is off"
  • IF light_switch_on = True AND bulb_working = False THEN cause = "Bulb is fused"
  • IF light_switch_on = True AND bulb_working = True AND power_available = False THEN cause = "No power supply"
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

šŸ”¬ Virtual Lab: Light Diagnosis System

Click 'Start Diagnosis' to begin the expert system!

šŸ” Diagnosis Complete!

šŸ’» Code

Rule-Based System Implementation

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.")