Book an info sessionInfo session

Rules and decision trees

AI Explorers · lesson 8 of 24 · 25 min

“If it’s raining, take the bus. If not, and it’s before 8, walk. Otherwise, get a ride.” You just used a decision tree. Computers use them too.

Rules: if this, then that

One of the oldest kinds of AI uses rules. A rule has a condition and an action: if something is true, then do something.

Early AI programs called expert systems were built from hundreds or thousands of rules written by human experts. For example, a plant care system might include rules like:

  • If leaves are yellow and soil is wet, then you may be overwatering.
  • If leaves are droopy and soil is dry, then water the plant.
  • If leaves have white spots, then check for mildew.

Rules are easy to read and easy to explain. If the system says “water the plant,” you can see exactly which rule caused that. The downside: someone has to write every rule, and real life has more situations than anyone can list.

Decision trees

A decision tree organizes rules into a flowchart of yes-or-no questions. You start at the top, answer a question, follow a branch, and keep going until you reach an answer at the end, called a leaf.

Here is a small tree for deciding what to do after school:

  1. Is your homework done? If no: do homework. If yes, go to question 2.
  2. Is it light outside? If yes, go to question 3. If no: read, draw or play a game inside.
  3. Are friends at the court? If yes: go play ball. If no: practice shots on your own.

A good tree asks the most useful questions first. “Is your homework done?” comes first because it matters most.

Trees can be learned, too

Here is the exciting part. Computers can build decision trees by studying examples. Give a program a table of past days with the weather, the day of the week, and whether a park basketball game happened. The program can figure out which questions split the examples best, like “Is it raining?” and “Is it a weekend?” That is a first taste of machine learning, which we cover in Unit 4.

Rule-based systems are still used today wherever being able to explain a decision really matters. A school’s computer might use clear rules to decide which students get a late-bus reminder text. Many games use rules to control characters, like a guard who chases you if you get within ten steps. Rules are simple, fast and predictable, which is exactly what you want in some situations.

Try it

  1. Choose a decision you or your family makes often: what to eat for dinner, what to wear, which game to play, or which chore to do first.
  2. Write five to eight past examples. For each, note two or three facts (like weather, day, time) and what was decided.
  3. Draw a decision tree on paper with at least three questions that would lead to the right decision for most of your examples.
  4. Test it: run each example through your tree. Count how many your tree gets right.
  5. Ask a family member for a brand-new situation and run it through the tree. Did it give a good answer?

Check yourself

What are the two parts of a rule?

A condition (if something is true) and an action (then do something).

What is a leaf in a decision tree?

An end point of the tree, where you reach a final answer or decision.

What is one weakness of rule-based systems?

People must write every rule, and real life has more situations than anyone can list.

Key words

  • Rule: an if-then instruction.
  • Expert system: an AI program built from many rules written by experts.
  • Decision tree: a flowchart of questions that leads to a decision.
  • Leaf: the final answer at the end of a decision tree branch.

Previous lesson: Turning the world into data
Back to AI Explorers
Next lesson: Search: how computers find the best path