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™ UI tools. These tools support only type-1 fuzzy systems.
You use the following 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 UIs 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 UIs. 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 UIs 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 fuzzy 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 UIs, 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 UIs do not support building a FIS using data. If you want to use data to build a FIS, use one of the following techniques:
genfis
to generate a Sugeno-type FIS. Then, select File > Import in Fuzzy Logic Designer to import the FIS and perform fuzzy
inference, as described in Fuzzy Logic Designer.
Neuro-adaptive learning techniques to model the FIS, as described in Neuro-Adaptive Learning and ANFIS.
If you want to use MATLAB workspace variables, use the command-line interface instead of Fuzzy Logic Designer. For an example, see Build Fuzzy Systems at the Command Line.
This example creates a Mamdani fuzzy inference system using on a two-input, one-output tipping problem based on tipping practices in the U.S. While the example creates a Mamdani FIS, the methods used apply to creating Sugeno systems as well.
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:
If the service is poor or the food is rancid, then tip is cheap.
If the service is good, then tip is average.
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%.
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 UI tools to construct a fuzzy inference system for this decision process.
The Fuzzy Logic Designer app displays information about a fuzzy inference system. To open Fuzzy Logic Designer, type the following command at the MATLAB prompt:
fuzzyLogicDesigner
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. For more information, see Mamdani and Sugeno Fuzzy Inference Systems.
In 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 Fuzzy Logic Designer with the prebuilt fuzzy inference system
stored in tipper.fis
, enter
fuzzyLogicDesigner('tipper.fis')
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:
Select Edit > Add variable > Input.
A second yellow box labeled input2 appears.
Click the yellow box input1. This box is highlighted with a red outline.
Edit the Name field from
input1
to service
, and press
Enter.
Click the yellow box input2. This box is highlighted with a red outline.
Edit the Name field from input2
to
food
, and press Enter.
Click the blue box output1.
Edit the Name field from
output1
to tip
, and press
Enter.
Select File > Export > To Workspace.
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 UI. 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 app, select Edit > Membership Functions.
Within the Fuzzy Logic Designer app, double-click the blue icon called tip.
At the command line, type mfedit
.
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 Fuzzy Logic Designer, as shown in the next figure. In fact, all of the five basic UI 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 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:
Double-click the input variable service
to open
the Membership Function Editor.
In the Membership Function Editor, enter [0 10]
in the Range and the Display Range
fields.
Create membership functions for the input variable service
.
Select Edit > Remove All MFs to remove the default membership functions for the input variable
service
.
Select Edit > Add MFs to open the Membership Functions dialog box.
In the Membership Functions dialog box, select
gaussmf
as the MF Type.
Verify that 3
is selected as the Number
of MFs.
Click OK to add three Gaussian curves to the input
variable service
.
Rename the membership functions for the input variable
service
, and specify their parameters.
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.
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]
.
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.
In the FIS Variables area, click the input variable
food
to select it.
Enter [0 10]
in the Range and
the Display Range fields.
Create the membership functions for the input variable food
.
Select Edit > Remove All MFs to remove the default Membership Functions for the input variable
food
.
Select Edit > Add MFs to open the Membership Functions dialog box.
In the Membership Functions dialog box, select
trapmf
as the MF Type.
Select 2
in the Number of MFs
drop-down list.
Click OK to add two trapezoidal curves to the input
variable food
.
Rename the membership functions for the input variable food
, and
specify their parameters:
In the FIS Variables area, click the input variable
food
to select it.
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]
.
Click on the curve named mf2
to select it, and enter
delicious
in the Name field.
Reset the associated parameters if desired.
Click on the output variable tip
to select it.
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%.
Rename the default triangular membership functions for the output variable tip
, and specify their parameters.
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]
.
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]
.
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.
Constructing rules using the graphical Rule Editor interface is fairly self evident. Based on the descriptions of the input and output variables defined with Fuzzy Logic Designer, the Rule Editor allows you to construct the rule statements automatically. 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 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 UI 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 UI 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
If (service is poor) or (food is rancid) then (tip is cheap) (1)
If (service is good) then (tip is average) (1)
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 UI tools we'll look at. From the View menu, select Rules.
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 UI 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 UI tools. To open the Surface Viewer, select Surface from the View menu.
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.
By default, the surface plot updates automatically when you change the input or output variable selections or the number of grid points. To disable automatic plot updates, in the Options menu, clear the Always evaluate option. When this option is disabled, to update the plot, click Evaluate.
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. This field defaults to the minimum number of plot plots,
101
. If you specify fewer plot points, the field value automatically
resets to 101
. When you specify the number of plot points, the surface
plot automatically updates.
By clicking on the plot axes and dragging the mouse, you can manipulate the surface so that you can view it from different angles.
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 NaN
s holding the place of the varying inputs while numerical
values indicates those values that remain fixed.
The menu items allow you to open, close, save and edit a fuzzy system using the five basic UI tools. You can access information about the Surface Viewer by clicking Help and close the UI using Close.
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
. Do not manually
edit the contents of a .fis
file. Doing so can produce unexpected results
when loading the file. When you save your fuzzy system to the MATLAB workspace, you are creating a variable that acts as a MATLAB object for the fuzzy 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.