Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: An Animal Identification Expert System

Build a complete backward-chaining expert system with certainty factors, a scripted consultation, a why-asked log and a how explanation, then extend it with new rules.

This project brings together the whole expert-system chapter. You will build a small but complete expert system shell in Python and give it a knowledge base about animals. The system will pick the most likely species from the user's answers, ask only the questions it needs, handle uncertain answers with certainty factors, and explain its conclusion.

The classic "identify the animal" task is a good teaching domain because the rules are easy to read and you can check the answers yourself.

Design

Piece Choice
Knowledge base List of rules {name, if, then, cf}
Reasoning Backward chaining over each candidate species
Uncertainty Certainty factors from -1 to +1 (as in the CF lesson)
Questions A fact with no rule concluding it is asked once, then remembered
Explanations A log of questions with the rule that needed them, and a trace of rule contributions
flowchart TD
  S["Pick candidate species"] --> P["prove(species)"]
  P --> R["find rules concluding it"]
  R --> C["prove each condition"]
  C -->|"leaf"| A["ask user, remember answer"]
  C -->|"derived"| P
  R --> M["min of conditions x rule CF"]
  M --> K["combine evidence"]
  K --> O["rank species, explain"]

Proof by backward chaining, with certainty factors flowing back up the tree.

The shell and the knowledge base

The shell has two parts: the generic reasoning functions (they know nothing about animals) and the rules. Answers come from a scripted dictionary so the example is repeatable; in a real program ask would call input().

Python A certainty-factor expert system for animals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The penguin gets the highest certainty. Every other species is at zero because a condition failed early. Look at the question log: the system stopped asking about a rule as soon as a condition came back negative (the break), so it never asked about trunks for the elephant after hair and milk had been denied.

Explaining the answer

The trace lists which rules contributed to which conclusion. A how explanation walks backward from the answer and prints the chain of rules.

Python A how explanation for the best answer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output reads like a short argument: "penguin, because it is a bird (from the feathers), flightless and swims." That sentence is the reason people trust expert systems more than opaque scores.

Extending the system

You now have a shell, so changing the domain only means changing RULES, GOALS and ANSWERS. Try these extensions, each of which teaches something:

  1. Add a species. Write a rule for owl (bird, flies, nocturnal). Does the system ask a new question or reuse answers?
  2. Add a negative rule. Real MYCIN rules can also give negative evidence: IF has_trunk THEN NOT mouse with a negative CF. Allow negative rule CFs and check that combine handles them.
  3. Ask for the best question. Instead of proving goals in a fixed order, choose the next question that appears in the most unproven rules.
  4. Interactive mode. Replace the scripted answers with input() in a terminal script. Support "unknown" (CF 0) as an answer.
  5. Test the system. Write ten consultations with the expected species, run them all and report accuracy. Keep them as a regression suite so that adding rules never breaks old answers.

Testing a knowledge base

Knowledge bases need tests just like code. The cheapest useful test is a table of consultations with the expected top answer.

Python A tiny regression suite for the rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the shell stop asking about a rule once a condition's certainty is 0.2 or less?

    A rule's strength is the minimum of its conditions, so one weak condition makes the rule inapplicable.
  2. 2. Which part of the program would change to move the system from animals to car faults?

    The shell is domain independent; only the knowledge base changes.
  3. 3. What does the how explanation show?

    How explains a conclusion by walking back through the rules that produced it.
  4. 4. Why keep a regression suite of consultations?

    Rule bases change over time; tests protect behavior that was already right.

Summary

  • A shell separates generic reasoning from domain rules; swapping the rules changes the domain.
  • Backward chaining with certainty factors asks only needed questions and ranks candidate answers.
  • Explanations come from a trace of rule contributions.
  • Treat the knowledge base like code: extend it step by step and keep consultations as regression tests.

Next: Project: A Fuzzy Loan Risk Scorer