Rule-based Systems: The Foundation of AI

4–6 minutes

Tiny Expert System for Lenses → Medical Diagnosis Expert Systems

Introduction to the Applied AI Tutorials Series

Welcome to the first article of the Applied AI Tutorials series. The goal of this series is to make complex AI concepts accessible through practical, hands-on examples. We will begin at the very foundations of Artificial Intelligence, gradually building up toward Machine Learning, Deep Learning, and Generative AI.
Each tutorial will follow a consistent flow:

  1. Sneak Peek: Why this AI concept matters.
  2. Theory Deep Dive: The mathematics and principles behind the concept.
  3. Toy Problem: Applying the concept to a small dataset.
  4. Real-World Application: Scaling the toy example to a real use case.
  5. Strengths & Limitations
  6. Final Notes & Next Steps

This article begins with Rule-based Systems, the earliest and simplest form of AI, often used in expert systems for decision-making.

Why Rule-based Systems?

Rule-based systems were among the first attempts to simulate human intelligence. Before machine learning and deep learning dominated the AI landscape, researchers used if–then rules to encode expert knowledge and automate decision-making.

For example:

  • In medicine, doctors would encode rules like “IF fever AND cough THEN flu”.
  • In finance, analysts used rules such as “IF income < threshold THEN loan denied”.

The appeal of rule-based systems is their simplicity and transparency: the decision-making process is explicitly defined, making it easy to audit and explain.

Rule-based Systems & Expert Systems

1. What is a Rule-based System?

A rule-based system is composed of:

  • Knowledge Base: A collection of rules, usually in the form of IF ; condition ; THEN ; action statements.
  • Inference Engine: The reasoning mechanism that evaluates which rules to apply given the inputs.
  • Working Memory: Stores facts about the current situation (input data).

2. Formal Structure of Rules

A single rule can be expressed as:

IF ; A_1 ; AND ; A_2 ; AND \dots ; AND ; A_n ; THEN ; B

Where:

  • A_i are conditions (premises, facts).
  • B is the conclusion (action, classification, or output).

3. Forward vs. Backward Chaining

Two main reasoning strategies exist:

  • Forward Chaining: Data-driven. Start from known facts, apply rules, and infer new conclusions. Example: diagnosing a disease from symptoms.
  • Backward Chaining: Goal-driven. Start with a hypothesis and check if rules and facts support it. Example: proving whether a patient has glaucoma.

4. Rule-based Systems vs. Machine Learning

  • Rule-based Systems rely on explicit expert-defined rules.
  • Machine Learning Models learn rules implicitly from data.

Despite being overshadowed today, rule-based systems remain relevant for:

  • Domains requiring explainability (e.g., law, compliance, medicine).
  • Situations where expert knowledge is abundant, but data is limited.

Toy Problem – Tiny Expert System for Lenses

We’ll use the UCI Lenses dataset. It’s a classic dataset with only 24 samples, representing conditions for prescribing contact lenses.

Dataset Snapshot (UCI Lenses)

AgePrescriptionAstigmatismTear ProductionLens Type
YoungMyopeNoReducedNone
Pre-presbyopicHypermetropeYesNormalHard
PresbyopicMyopeYesNormalSoft
  • Age: Young / Pre-presbyopic / Presbyopic
  • Prescription: Myope / Hypermetrope
  • Astigmatism: Yes / No
  • Tear Production: Normal / Reduced
  • Lens Type: None / Hard / Soft

Our goal: Build a small expert system that prescribes contact lenses based on these rules.

Step 1: Load the dataset

Loads and displays the dataset for exploration.

import pandas as pd

# Load dataset from UCI (manually downloaded)
columns = ["Age", "Prescription", "Astigmatism", "Tear_Production", "Lens"]
data = pd.read_csv("lenses.csv", names=columns)

print(data.head())

Step 2: Define Rule Base

Encodes the lens-prescription rules as if–then conditions.

def prescribe_lens(age, prescription, astigmatism, tear):
    if tear == "Reduced":
        return "None"
    elif prescription == "Myope" and astigmatism == "Yes" and tear == "Normal":
        return "Hard"
    elif prescription == "Hypermetrope" and astigmatism == "No" and tear == "Normal":
        return "Soft"
    else:
        return "None"

Step 3: Test the System

Evaluates predictions and compares them with ground truth labels.

for i, row in data.iterrows():
    result = prescribe_lens(row["Age"], row["Prescription"], row["Astigmatism"], row["Tear_Production"])
    print(f"Input: {row.values[:-1]} → Predicted: {result}, Actual: {row['Lens']}")

Quick Reference: Full Code

import pandas as pd

columns = ["Age", "Prescription", "Astigmatism", "Tear_Production", "Lens"]
data = pd.read_csv("lenses.csv", names=columns)

def prescribe_lens(age, prescription, astigmatism, tear):
    if tear == "Reduced":
        return "None"
    elif prescription == "Myope" and astigmatism == "Yes" and tear == "Normal":
        return "Hard"
    elif prescription == "Hypermetrope" and astigmatism == "No" and tear == "Normal":
        return "Soft"
    else:
        return "None"

for i, row in data.iterrows():
    result = prescribe_lens(row["Age"], row["Prescription"], row["Astigmatism"], row["Tear_Production"])
    print(f"Input: {row.values[:-1]} → Predicted: {result}, Actual: {row['Lens']}")

Real‑World Application — Medical Diagnosis Expert Systems

Expert systems in medicine are a direct extension of rule-based systems. One classic example is MYCIN (1970s), which diagnosed bacterial infections using hundreds of if–then rules.

Here, let’s simulate a simplified diagnosis system for eye diseases:

Step 1: Define Rule Base for Medical Diagnosis

Encodes medical knowledge into rules.

def diagnose(symptoms):
    if "blurred_vision" in symptoms and "eye_pain" in symptoms:
        return "Possible Glaucoma"
    elif "redness" in symptoms and "itching" in symptoms:
        return "Possible Conjunctivitis"
    elif "night_blindness" in symptoms:
        return "Possible Vitamin A Deficiency"
    else:
        return "Unknown – further tests required"

Step 2: Test Cases

Applies the rule-based expert system to patient cases.

patients = [
    {"id": 1, "symptoms": ["blurred_vision", "eye_pain"]},
    {"id": 2, "symptoms": ["redness", "itching"]},
    {"id": 3, "symptoms": ["night_blindness"]},
    {"id": 4, "symptoms": ["headache"]}
]

for p in patients:
    print(f"Patient {p['id']} → Diagnosis: {diagnose(p['symptoms'])}")

Quick Reference: Full Code

def diagnose(symptoms):
    if "blurred_vision" in symptoms and "eye_pain" in symptoms:
        return "Possible Glaucoma"
    elif "redness" in symptoms and "itching" in symptoms:
        return "Possible Conjunctivitis"
    elif "night_blindness" in symptoms:
        return "Possible Vitamin A Deficiency"
    else:
        return "Unknown – further tests required"

patients = [
    {"id": 1, "symptoms": ["blurred_vision", "eye_pain"]},
    {"id": 2, "symptoms": ["redness", "itching"]},
    {"id": 3, "symptoms": ["night_blindness"]},
    {"id": 4, "symptoms": ["headache"]}
]

for p in patients:
    print(f"Patient {p['id']} → Diagnosis: {diagnose(p['symptoms'])}")

Strengths & Limitations

Strengths

  • Transparent and explainable decisions.
  • Effective in domains with strong expert knowledge.
  • Easy to implement for small, well-defined problems.

Limitations

  • Hard to scale (rule explosion).
  • Inflexible—cannot handle new/unexpected cases.
  • Maintenance burden as domains evolve.

Final Notes

In this tutorial, we learned that rule-based systems form the roots of AI.

  • We built a tiny expert system for lenses using the UCI dataset.
  • We extended the concept to a medical diagnosis system.
  • We explored where rule-based systems shine and where they fall short.

This is the foundational stone for understanding more advanced AI methods.

Next Steps for You:

Explore Decision Trees: See how they automate rule generation from data.

Study Fuzzy Logic: Learn how to handle uncertainty in rule-based systems.

References

[1] UCI Machine Learning Repository, “Lenses Data Set.” [Online]. Available: https://archive.ics.uci.edu/dataset/56/lenses

[2] E. H. Shortliffe, Computer-Based Medical Consultations: MYCIN. Elsevier, 1976.

[3] N. J. Nilsson, Principles of Artificial Intelligence. Palo Alto: Tioga, 1980.

Leave a comment