Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Fuzzy Inference

Mamdani Inference

Learn Mamdani fuzzy inference step by step: fuzzify, fire rules with AND/OR, clip consequents, aggregate with max and defuzzify, then build a two-input tipping system in Python.

Mamdani inference is the classic, most widely taught fuzzy method, proposed by Ebrahim Mamdani in 1975 for controlling a steam engine. Its output is a fuzzy set, assembled from the clipped consequents of all active rules, which then has to be defuzzified into a number. In this lesson you will go through the five steps one by one and then write a small, reusable Mamdani engine that you can point at any rule base.

You will need the ideas of Fuzzification and Fuzzy IF-THEN Rules.

The five steps

flowchart LR
    A["1. Fuzzify<br/>inputs"] --> B["2. Fire rules<br/>AND = min, OR = max"]
    B --> C["3. Implication<br/>clip each consequent"]
    C --> D["4. Aggregate<br/>max over all rules"]
    D --> E["5. Defuzzify<br/>centroid -> number"]

Steps 1 to 4 build a fuzzy output set; step 5 turns it into a number.

  1. Fuzzify the crisp inputs into degrees of their terms.
  2. Evaluate each rule's antecedent to get its firing strength w.
  3. Apply implication: clip the rule's consequent set at height w (min(w, mu_out(y))).
  4. Aggregate all clipped sets into one fuzzy output set, using the maximum.
  5. Defuzzify (see Defuzzification Methods); here we use the centroid.

An example: tipping with two inputs

Inputs are service quality and food quality, both from 0 to 10. The output is a tip from 0 to 25 percent. The rules:

TEXT
R1: IF service is poor      OR  food is rancid     THEN tip is low
R2: IF service is good                              THEN tip is medium
R3: IF service is excellent OR  food is delicious  THEN tip is high

The code below goes through the five steps with prints in between, so you can see what each stage produces.

Python Mamdani inference, one step at a time

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.

With service 6.5 (good 0.7, excellent 0.3) and food 8 (delicious 0.5), the rules fire at about 0, 0.7 and 0.5. The "medium" set is clipped at 0.7, the "high" set at 0.5, and "low" disappears completely. The centre of gravity of their union gives a tip of about 13.9 percent. Change the two inputs at the top and watch each number change.

A reusable engine

Writing the five steps out for every system is tedious. A small engine separates the data (variables, rules) from the procedure. The version below takes a dictionary of input membership functions, a list of rules, and the output terms, and returns the crisp answer. It is the same logic as above, just generalised.

Python A tiny Mamdani engine

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 table shows the control surface at a few points: good food lifts the tip at every service level, and the tip grows smoothly with service. A rule can be any function of the degree table, so AND, OR, NOT and hedges are all just Python expressions.

Min versus product implication

Mamdani's original method clips (min); using the product (np.multiply) instead is often called the Larsen variant. The engine above supports both through one argument.

Python Clipping versus scaling

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 two answers differ slightly, because the scaled sets keep their pointed peaks and shift the centre of mass. Neither is "right": pick one and keep it.

Strengths and weaknesses

  • Intuitive: the output set is a picture you can plot and discuss with an expert, and rules read like language.
  • Smooth: overlapping terms give gradual changes.
  • Costly: aggregating and defuzzifying a whole set takes more computation than a weighted average, so on small hardware people often prefer Sugeno Inference.
  • Sensitive to defuzzification: the method turns the final set into a number, and different methods give different answers, as the next lessons show.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the steps of Mamdani inference in order?

    Mamdani inference fuzzifies, evaluates antecedents, clips consequents, aggregates with max and finally defuzzifies.
  2. 2. How are the clipped output sets of different rules combined?

    The standard aggregation is the point-wise maximum (union) of the clipped sets.
  3. 3. A rule fires at strength 0.7 and its consequent is the set medium. What does the implication step produce with min?

    Min implication clips: min(0.7, mu_medium(y)).
  4. 4. Which is a typical drawback of Mamdani inference compared with Sugeno?

    Computing and defuzzifying a full output set costs more than a weighted average of crisp values.

Summary

  • Mamdani inference: fuzzify, fire rules, clip consequents, aggregate with max, defuzzify.
  • Antecedents use AND = min, OR = max (or other norms); implication clips the consequent.
  • A small engine can hold variables and rules as data, so only the rule base changes between systems.
  • Min implication clips, product implication scales; both are valid.
  • The method is readable and smooth, but heavier to compute than Sugeno.

Next: Sugeno Inference