Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice Problems: Controllers, Rules and Expert Systems

Practice fuzzy controllers, knowledge representation, forward and backward chaining and certainty factors with worked problems, runnable solutions and design questions.

Reading is not the same as being able to do it. This lesson collects problems from the second half of the course. For each one, try it on paper first, then open the runnable solution and compare. Several problems have more than one valid answer: when that is so, the point is to check that your answer behaves sensibly, not that it equals ours.

Problem 1: a tank level controller

A water tank has a level sensor (0 to 100 percent) and an inlet valve (0 to 100 percent open). Design a Mamdani controller with one input and one output:

  • level terms: low, ok, high
  • valve terms: closed, half, open
  • rules: low level -> open, ok level -> half, high level -> closed

Compute the valve opening for levels 10, 40, 50, 70 and 95. Then answer: at level 50, should the valve be exactly 50 percent? Why or why not?

Python Solution: tank level controller

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.

At level 50 only the ok rule fires, and the centroid of the symmetric half set is exactly 50. At 40 and 70 neighboring rules mix in. The table is symmetric here, so the controller treats a surplus and a shortage alike; if filling is cheaper than draining, you would use asymmetric sets.

Problem 2: fuzzy operators and a centroid by hand

Two membership values: A = 0.7 and B = 0.4. Compute with the standard operators:

  1. A AND B, A OR B, NOT A.
  2. Using the product for AND and the probabilistic sum for OR (a + b - ab).
  3. A fuzzy output set has these samples: values 0, 10, 20, 30, 40 with memberships 0, 0.5, 1, 0.5, 0. What is its centroid?
Python Solution: operators and centroid

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 set is symmetric around 20, so its centroid must be 20: a quick sanity check you can do without code.

Problem 3: forward chaining by hand

Working memory: {a, d}. Rules:

Rule IF THEN
R1 a AND b c
R2 d b
R3 c AND d e
R4 e f

List the facts derived and the order in which the rules fire if rules are checked in order and each fires once. Does the order of the list change the final set? Does it change the number of cycles?

Python Solution: forward chaining trace

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.

In the given order only R2 can fire first (it needs only d), then R1, R3, R4. In the reversed list the final facts are identical (forward chaining without negation reaches a unique fixed point), and in this example the rules also fire in the same order, because only one rule is applicable at each step. When several rules match at once, the order would differ but the final set would not.

Problem 4: certainty factors

A rule R1 (CF 0.8) needs x (CF 0.9) and y (CF 0.6). Another rule R2 (CF 0.5) needs z (CF 0.7). Both conclude h. Compute the combined certainty of h. Then add a third rule R3 that gives h a certainty of -0.3 and recompute.

Python Solution: certainty factors

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.

R1 gives 0.48 and R2 gives 0.35. They combine to 0.48 + 0.35 x 0.52 = 0.662. The conflicting evidence of -0.3 lowers the belief to (0.662 - 0.3) / (1 - 0.3) = 0.517.

Problem 5: backward chaining questions

A backward chainer wants to prove diagnosis = overheating, which is concluded by IF fan_failed AND load_high THEN overheating (rule A) and IF dust_clogged AND temp_alarm THEN overheating (rule B). Facts fan_failed, load_high, dust_clogged and temp_alarm are all leaves.

  1. In what order are the questions asked? (Assume rules are tried in the listed order.)
  2. If the user answers no to fan_failed, which questions follow?
  3. How many questions are asked if both rules fail at their first condition?

Answer: the first question is fan_failed. If it is no, rule A is abandoned (the AND cannot succeed), so load_high is not asked; the engine moves to rule B and asks dust_clogged, then temp_alarm if that is yes. If both rules fail at the first condition, only two questions are asked.

Problem 6: choose a representation

For each situation, pick the best scheme (rules, semantic network, frames, logic, fuzzy sets) and justify it in one sentence.

  1. "A bird inherits properties from its species, with exceptions."
  2. "If the oil pressure is low and the engine is hot, stop the engine."
  3. "The room is a bit too warm."
  4. "Every employee manages at most five projects."
  5. "A car has an engine, wheels and seats, and an electric car has a battery too."

Suggested answers: (1) a semantic network or frames with inheritance and defaults; (2) a production rule (an action on conditions); (3) a fuzzy set (vague term); (4) predicate logic (a quantified constraint); (5) frames (structured objects with slots and subclasses).

Problem 7: find the bug

The controller below should make the heater stronger the colder the room is, but the printed power gets lower as the room gets colder. Find the error without running it, then run it to confirm and fix it.

Python Debug the controller

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 labels cold and warm were attached to the wrong membership shapes, so the rule "cold gives 90" was applied to warm temperatures. The fix is to swap the shapes. Lesson: bugs in fuzzy controllers are usually in the sets, not in the algorithm, and the quickest way to catch them is to print outputs for a few sample inputs and check the direction. In your own projects, run the monotonicity checks from the fan project.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A symmetric fuzzy output set over 0 to 40 peaks at 20. Where is its centroid?

    A symmetric set has its center of mass at its axis of symmetry.
  2. 2. Two rules give certainties 0.48 and 0.35 for the same conclusion. What is the combined CF?

    0.48 + 0.35 x (1 - 0.48) = 0.662.
  3. 3. Forward chaining with no negation, run on the same facts and rules in a different order, ends with...

    Without negation or retraction, forward chaining reaches a unique fixed point.
  4. 4. Which representation fits 'the room is a bit too warm' best?

    'A bit too warm' is a vague term with a degree, which fuzzy sets are designed for.

Summary

  • Practice by working the problem on paper first, then check with code.
  • Verify controllers with table outputs, and verify rule bases with monotonicity and regression checks.
  • Forward chaining without negation reaches a unique fixed point; backward chaining asks only what the goal needs.
  • Match the representation to the knowledge: rules for actions, frames and nets for structure, logic for constraints, fuzzy sets for vagueness.

Next: Back to the track overview