Build Mamdani Systems (GUI)

How to Build Mamdani Systems Using Fuzzy Logic Toolbox Graphical User Interface Tools

This example shows how to build a Fuzzy Inference System (FIS) for the tipping example, described in The Basic Tipping Problem, using the Fuzzy Logic Toolbox™ graphical user interface (GUI) tools.

You use the following graphical tools to build, edit, and view fuzzy inference systems:

  • Fuzzy Logic Designer to handle the high-level issues for the system—How many input and output variables? What are their names?

    Fuzzy Logic Toolbox software does not limit the number of inputs. However, the number of inputs may be limited by the available memory of your machine. If the number of inputs is too large, or the number of membership functions is too big, then it may also be difficult to analyze the FIS using the other tools.

  • Membership Function Editor to define the shapes of all the membership functions associated with each variable

  • Rule Editor to edit the list of rules that defines the behavior of the system.

  • Rule Viewer to view the fuzzy inference diagram. Use this viewer as a diagnostic to see, for example, which rules are active, or how individual membership function shapes influence the results

  • Surface Viewer to view the dependency of one of the outputs on any one or two of the inputs—that is, it generates and plots an output surface map for the system.

These GUIs are dynamically linked, in that changes you make to the FIS using one of them, affect what you see on any of the other open GUIs. For example, if you change the names of the membership functions in the Membership Function Editor, the changes are reflected in the rules shown in the Rule Editor. You can use the GUIs to read and write variables both to the MATLAB® workspace and to a file (the read-only viewers can still exchange plots with the workspace and save them to a file). You can have any or all of them open for any given system or have multiple editors open for any number of FIS systems.

The following figure shows how the main components of a FIS and the three editors fit together. The two viewers examine the behavior of the entire system.

In addition to these five primary GUIs, the toolbox includes the graphical Neuro-Fuzzy Designer, which you use to build and analyze Sugeno-type adaptive neuro-fuzzy inference systems.

The Fuzzy Logic Toolbox GUIs do not support building FIS using data. If you want to use data to build a FIS, use one of the following techniques:

If you wan to use MATLAB workspace variables, use the command-line interface instead of the Fuzzy Logic Designer. For an example, see Building a System from Scratch.

The Basic Tipping Problem

This example uses a two-input, one-output tipping problem based on tipping practices in the U.S.

Given a number between 0 and 10 that represents the quality of service at a restaurant (where 10 is excellent), and another number between 0 and 10 that represents the quality of the food at that restaurant (again, 10 is excellent), what should the tip be?

The starting point is to write down the three golden rules of tipping:

  1. If the service is poor or the food is rancid, then tip is cheap.

  2. If the service is good, then tip is average.

  3. If the service is excellent or the food is delicious, then tip is generous.

Assume that an average tip is 15%, a generous tip is 25%, and a cheap tip is 5%.

Obviously the numbers and the shape of the curve are subject to local traditions, cultural bias, and so on, but the three rules are generally universal.

Now that you know the rules and have an idea of what the output should look like, use the GUI tools to construct a fuzzy inference system for this decision process.

The Fuzzy Logic Designer

The Fuzzy Logic Designer displays information about a fuzzy inference system. To open the Fuzzy Logic Designer, type the following command at the MATLAB prompt:

fuzzyLogicDesigner

The Fuzzy Logic Designer opens and displays a diagram of the fuzzy inference system with the names of each input variable on the left, and those of each output variable on the right, as shown in the next figure. The sample membership functions shown in the boxes are just icons and do not depict the actual shapes of the membership functions.

Below the diagram is the name of the system and the type of inference used.

In this example, you use the default Mamdani-type inference. Another type of inference, called Sugeno-type inference, is also available. See What Is Sugeno-Type Fuzzy Inference?.

In the Fuzzy Logic Designer:

  • The drop-down lists let you modify the fuzzy inference functions.

  • The Current Variable area displays the name of either an input or output variable, its type, and default range.

  • A status line at the bottom displays information about the most recent operation.

To build the Fuzzy Inference System described in The Basic Tipping Problem from scratch, type the following command at the MATLAB prompt:

fuzzyLogicDesigner

The generic untitled Fuzzy Logic Designer opens, with one input input1, and one output output1.

    Tip   To open the Fuzzy Logic Designer with the prebuilt fuzzy inference system stored in tipper.fis, type

    fuzzyLogicDesigner tipper
    

    However, if you load the prebuilt system, you will not build rules or construct membership functions.

In this example, you construct a two-input, one output system. The two inputs are service and food. The one output is tip.

To add a second input variable and change the variable names to reflect these designations:

  1. Select Edit > Add variable > Input.

    A second yellow box labeled input2 appears.

  2. Click the yellow box input1. This box is highlighted with a red outline.

  3. Edit the Name field from input1 to service, and press Enter.

  4. Click the yellow box input2. This box is highlighted with a red outline.

  5. Edit the Name field from input2 to food, and press Enter.

  6. Click the blue box output1.

  7. Edit the Name field from output1 to tip, and press Enter.

  8. Select File > Export > To Workspace.

  9. Enter the Workspace variable name tipper, and click OK.

    The diagram is updated to reflect the new names of the input and output variables. There is now a new variable in the workspace called tipper that contains all the information about this system. By saving to the workspace with a new name, you also rename the entire system. Your window looks something like the following diagram.

Leave the inference options in the lower left in their default positions for now. You have entered all the information you need for this particular GUI. Next, define the membership functions associated with each of the variables. To do this, open the Membership Function Editor.

You can open the Membership Function Editor in one of three ways:

  • Within the Fuzzy Logic Designer window, select Edit > Membership Functions..

  • Within the Fuzzy Logic Designer window, double-click the blue icon called tip.

  • At the command line, type mfedit.

The Membership Function Editor

The Membership Function Editor is the tool that lets you display and edit all of the membership functions associated with all of the input and output variables for the entire fuzzy inference system. The Membership Function Editor shares some features with the Fuzzy Logic Designer, as shown in the next figure. In fact, all of the five basic GUI tools have similar menu options, status lines, and Help and Close buttons.

When you open the Membership Function Editor to work on a fuzzy inference system that does not already exist in the workspace, there are no membership functions associated with the variables that you defined with the Fuzzy Logic Designer.

On the upper-left side of the graph area in the Membership Function Editor is a "Variable Palette" that lets you set the membership functions for a given variable.

To set up the membership functions associated with an input or an output variable for the FIS, select a FIS variable in this region by clicking it.

Next select the Edit pull-down menu, and choose Add MFs .. A new window appears, which allows you to select both the membership function type and the number of membership functions associated with the selected variable. In the lower-right corner of the window are the controls that let you change the name, type, and parameters (shape), of the membership function, after it is selected.

The membership functions from the current variable are displayed in the main graph. These membership functions can be manipulated in two ways. You can first use the mouse to select a particular membership function associated with a given variable quality, (such as poor, for the variable, service), and then drag the membership function from side to side. This action affects the mathematical description of the quality associated with that membership function for a given variable. The selected membership function can also be tagged for dilation or contraction by clicking on the small square drag points on the membership function, and then dragging the function with the mouse toward the outside, for dilation, or toward the inside, for contraction. This action changes the parameters associated with that membership function.

Below the Variable Palette is some information about the type and name of the current variable. There is a text field in this region that lets you change the limits of the current variable's range (universe of discourse) and another that lets you set the limits of the current plot (which has no real effect on the system).

The process of specifying the membership functions for the two input tipping example, tipper, is as follows:

  1. Double-click the input variable service to open the Membership Function Editor.

  2. In the Membership Function Editor, enter [0 10] in the Range and the Display Range fields.

  3. Create membership functions for the input variable service.

    1. Select Edit > Remove All MFs to remove the default membership functions for the input variable service.

    2. Select Edit > Add MFs. to open the Membership Functions dialog box.

    3. In the Membership Functions dialog box, select gaussmf as the MF Type.

    4. Verify that 3 is selected as the Number of MFs.

    5. Click OK to add three Gaussian curves to the input variable service.

  4. Rename the membership functions for the input variable service, and specify their parameters.

    1. Click on the curve named mf1 to select it, and specify the following fields in the Current Membership Function (click on MF to select) area:

      • In the Name field, enter poor.

      • In the Params field, enter [1.5 0].

        The two inputs of Params represent the standard deviation and center for the Gaussian curve.

          Tip   To adjust the shape of the membership function, type in the desired parameters or use the mouse, as described previously.

    2. Click on the curve named mf2 to select it, and specify the following fields in the Current Membership Function (click on MF to select) area:

      • In the Name field, enter good.

      • In the Params field, enter [1.5 5].

    3. Click on the curve named mf3, and specify the following fields in the Current Membership Function (click on MF to select) area:

      • In the Name field, enter excellent.

      • In the Params field, enter [1.5 10].

      The Membership Function Editor window looks similar to the following figure.

  5. In the FIS Variables area, click the input variable food to select it.

  6. Enter [0 10] in the Range and the Display Range fields.

  7. Create the membership functions for the input variable food.

    1. Select Edit > Remove All MFs to remove the default Membership Functions for the input variable food.

    2. Select Edit > Add MFs to open the Membership Functions dialog box.

    3. In the Membership Functions dialog box, select trapmf as the MF Type.

    4. Select 2 in the Number of MFs drop-down list.

    5. Click OK to add two trapezoidal curves to the input variable food.

  8. Rename the membership functions for the input variable food, and specify their parameters:

    1. In the FIS Variables area, click the input variable food to select it.

    2. Click on the curve named mf1, and specify the following fields in the Current Membership Function (click on MF to select) area:

      • In the Name field, enter rancid.

      • In the Params field, enter [0 0 1 3].

    3. Click on the curve named mf2 to select it, and enter delicious in the Name field.

      Reset the associated parameters if desired.

  9. Click on the output variable tip to select it.

  10. Enter [0 30] in the Range and the Display Range fields to cover the output range.

    The inputs ranges from 0 to 10, but the output is a tip between 5% and 25%.

  11. Rename the default triangular membership functions for the output variable tip, and specify their parameters.

    1. Click the curve named mf1 to select it, and specify the following fields in the Current Membership Function (click on MF to select) area:

      • In the Name field, enter cheap.

      • In the Params field, enter [0 5 10].

    2. Click the curve named mf2 to select it, and specify the following fields in the Current Membership Function (click on MF to select) area:

      • In the Name field, enter average.

      • In the Params field, enter [10 15 20].

    3. Click the curve named mf3 to select it, and specify the following:

      • In the Name field, enter generous.

      • In the Params field, enter [20 25 30].

    The Membership Function Editor looks similar to the following figure.

Now that the variables have been named and the membership functions have appropriate shapes and names, you can enter the rules. To call up the Rule Editor, go to the Edit menu and select Rules, or type ruleedit at the command line.

The Rule Editor

Constructing rules using the graphical Rule Editor interface is fairly self evident. Based on the descriptions of the input and output variables defined with the Fuzzy Logic Designer, the Rule Editor allows you to construct the rule statements automatically, From the GUI, you can:

  • Create rules by selecting an item in each input and output variable box, selecting one Connection item, and clicking Add Rule. You can choose none as one of the variable qualities to exclude that variable from a given rule and choose not under any variable name to negate the associated quality.

  • Delete a rule by selecting the rule and clicking Delete Rule.

  • Edit a rule by changing the selection in the variable box and clicking Change Rule.

  • Specify weight to a rule by typing in a desired number between 0 and 1 in Weight. If you do not specify the weight, it is assumed to be unity (1).

Similar to those in the Fuzzy Logic Designer and the Membership Function Editor, the Rule Editor has the menu bar and the status line. The menu items allow you to open, close, save and edit a fuzzy system using the five basic GUI tools. From the menu, you can also:

  • Set the format for the display by selecting Options > Format.

  • Set the language by selecting Options > Language.

You can access information about the Rule Editor by clicking Help and close the GUI using Close.

To insert the first rule in the Rule Editor, select the following:

  • poor under the variable service

  • rancid under the variable food

  • The or radio button, in the Connection block

  • cheap, under the output variable, tip.

Then, click Add rule.

The resulting rule is

1. If (service is poor) or (food is rancid) then (tip is cheap) (1)

The numbers in the parentheses represent weights.

Follow a similar procedure to insert the second and third rules in the Rule Editor to get

  1. If (service is poor) or (food is rancid) then (tip is cheap) (1)

  2. If (service is good) then (tip is average) (1)

  3. If (service is excellent) or (food is delicious) then (tip is generous) (1)

    Tip   To change a rule, first click on the rule to be changed. Next make the desired changes to that rule, and then click Change rule. For example, to change the first rule to

    1. If (service not poor) or (food not rancid) then (tip is not cheap) (1)

    Select the not check box under each variable, and then click Change rule.

The Format pop-up menu from the Options menu indicates that you are looking at the verbose form of the rules. Try changing it to symbolic. You will see

1. (service==poor) | (food==rancid) => (tip=cheap) (1)
2. (service==good) => (tip=average) (1)
3. (service==excellent) | (food==delicious) => (tip=generous) (1)

There is not much difference in the display really, but it is slightly more language neutral, because it does not depend on terms like if and then. If you change the format to indexed, you see an extremely compressed version of the rules.

1 1, 1 (1) : 2
2 0, 2 (1) : 1
3 2, 3 (1) : 2

This is the version of the rules that the machine deals with.

  • The first column in this structure corresponds to the input variables.

  • The second column corresponds to the output variable.

  • The third column displays the weight applied to each rule.

  • The fourth column is shorthand that indicates whether this is an OR (2) rule or an AND (1) rule.

  • The numbers in the first two columns refer to the index number of the membership function.

A literal interpretation of rule 1 is "If input 1 is MF1 (the first membership function associated with input 1) or if input 2 is MF1, then output 1 should be MF1 (the first membership function associated with output 1) with the weight 1.

The symbolic format does not consider the terms, if, then, and so on. The indexed format doesn't even bother with the names of your variables. Obviously the functionality of your system doesn't depend on how well you have named your variables and membership functions. The whole point of naming variables descriptively is, as always, making the system easier for you to interpret. Thus, unless you have some special purpose in mind, it is probably be easier for you to continue with the verbose format.

At this point, the fuzzy inference system has been completely defined, in that the variables, membership functions, and the rules necessary to calculate tips are in place. Now, look at the fuzzy inference diagram presented at the end of the previous section and verify that everything is behaving the way you think it should. You can use the Rule Viewer, the next of the GUI tools we'll look at. From the View menu, select Rules.

The Rule Viewer

The Rule Viewer displays a roadmap of the whole fuzzy inference process. It is based on the fuzzy inference diagram described in the previous section. You see a single figure window with 10 plots nested in it. The three plots across the top of the figure represent the antecedent and consequent of the first rule. Each rule is a row of plots, and each column is a variable. The rule numbers are displayed on the left of each row. You can click on a rule number to view the rule in the status line.

  • The first two columns of plots (the six yellow plots) show the membership functions referenced by the antecedent, or the if-part of each rule.

  • The third column of plots (the three blue plots) shows the membership functions referenced by the consequent, or the then-part of each rule.

    Notice that under food, there is a plot which is blank. This corresponds to the characterization of none for the variable food in the second rule.

  • The fourth plot in the third column of plots represents the aggregate weighted decision for the given inference system.

    This decision will depend on the input values for the system. The defuzzified output is displayed as a bold vertical line on this plot.

The variables and their current values are displayed on top of the columns. In the lower left, there is a text field Input in which you can enter specific input values. For the two-input system, you will enter an input vector, [9 8], for example, and then press Enter. You can also adjust these input values by clicking on any of the three plots for each input. This will move the red index line horizontally, to the point where you have clicked. Alternatively, you can also click and drag this line in order to change the input values. When you release the line, (or after manually specifying the input), a new calculation is performed, and you can see the whole fuzzy inference process take place:

  • Where the index line representing service crosses the membership function line "service is poor" in the upper-left plot determines the degree to which rule one is activated.

  • A yellow patch of color under the actual membership function curve is used to make the fuzzy membership value visually apparent.

Each of the characterizations of each of the variables is specified with respect to the input index line in this manner. If you follow rule 1 across the top of the diagram, you can see the consequent "tip is cheap" has been truncated to exactly the same degree as the (composite) antecedent—this is the implication process in action. The aggregation occurs down the third column, and the resultant aggregate plot is shown in the single plot appearing in the lower right corner of the plot field. The defuzzified output value is shown by the thick line passing through the aggregate fuzzy set.

You can shift the plots using left, right, down, and up. The menu items allow you to save, open, or edit a fuzzy system using any of the five basic GUI tools.

The Rule Viewer allows you to interpret the entire fuzzy inference process at once. The Rule Viewer also shows how the shape of certain membership functions influences the overall result. Because it plots every part of every rule, it can become unwieldy for particularly large systems, but, for a relatively small number of inputs and outputs, it performs well (depending on how much screen space you devote to it) with up to 30 rules and as many as 6 or 7 variables.

The Rule Viewer shows one calculation at a time and in great detail. In this sense, it presents a sort of micro view of the fuzzy inference system. If you want to see the entire output surface of your system—the entire span of the output set based on the entire span of the input set—you need to open up the Surface Viewer. This viewer is the last of the five basic Fuzzy Logic Toolbox GUI tools. To open the Surface Viewer, select Surface from the View menu.

The Surface Viewer

Upon opening the Surface Viewer, you see a three-dimensional curve that represents the mapping from food and service quality to tip amount. Because this curve represents a two-input one-output case, you can see the entire mapping in one plot. When we move beyond three dimensions overall, we start to encounter trouble displaying the results.

Accordingly, the Surface Viewer is equipped with drop-down menus X (input):, Y (input): and Z (output): that let you select any two inputs and any one output for plotting. Below these menus are two input fields X grids: and Y grids: that let you specify how many x-axis and y-axis grid lines you want to include. This capability allows you to keep the calculation time reasonable for complex problems.

If you want to create a smoother plot, use the Plot points field to specify the number of points on which the membership functions are evaluated in the input or output range. By default, the value of this field is 101.

Clicking Evaluate initiates the calculation, and the plot is generated after the calculation is complete. To change the x-axis or y-axis grid after the surface is in view, change the appropriate input field, and press Enter. The surface plot is updated to reflect the new grid settings.

The Surface Viewer has a special capability that is very helpful in cases with two (or more) inputs and one output: you can grab the axes, using the mouse and reposition them to get a different three-dimensional view on the data.

The Ref. Input field is used in situations when there are more inputs required by the system than the surface is mapping. You can edit this field to explicitly set inputs not specified in the surface plot.

Suppose you have a four-input one-output system and would like to see the output surface. The Surface Viewer can generate a three-dimensional output surface where any two of the inputs vary, but two of the inputs must be held constant because computer monitors cannot display a five-dimensional shape. In such a case, the input is a four-dimensional vector with NaNs holding the place of the varying inputs while numerical values indicates those values that remain fixed. A NaN is the IEEE® symbol for Not a Number.

The menu items allow you to open, close, save and edit a fuzzy system using the five basic GUI tools. You can access information about the Surface Viewer by clicking Help and close the GUI using Close.

This concludes the quick walk-through of each of the main GUI tools. For the tipping problem, the output of the fuzzy system matches your original idea of the shape of the fuzzy mapping from service to tip fairly well. In hindsight, you might say, "Why bother? I could have just drawn a quick lookup table and been done an hour ago!" However, if you are interested in solving an entire class of similar decision-making problems, fuzzy logic may provide an appropriate tool for the solution, given its ease with which a system can be quickly modified.

Importing and Exporting from the GUI Tools

When you save a fuzzy system to a file, you are saving an ASCII text FIS file representation of that system with the file suffix .fis. This text file can be edited and modified and is simple to understand. When you save your fuzzy system to the MATLAB workspace, you are creating a variable (whose name you choose) that acts as a MATLAB structure for the FIS system. FIS files and FIS structures represent the same system.

    Note   If you do not save your FIS to a file, but only save it to the MATLAB workspace, you cannot recover it for use in a new MATLAB session.

Was this topic helpful?